Commit e8613c14 authored by Leon Arnott's avatar Leon Arnott
Browse files

Fixed the 'matches' operator not working correctly on datasets.

This is probably a slow solution, but I feel like datasets won't be used that often with it anyway.
parent 39dc7cdb8cfc
......@@ -78,6 +78,32 @@ define(['jquery', 'markup', 'utils/selectors', 'utils/polyfills'],
return obj;
},
/*
Returns an array of every permutation of the given sequence.
*/
permutations(...list) {
const {length} = list,
result = [[...list]],
c = Array(length).fill(0);
let i = 1, k, p;
while (i < length) {
if (c[i] < i) {
k = i % 2 && c[i];
p = list[i];
list[i] = list[k];
list[k] = p;
++c[i];
i = 1;
result.push([...list]);
} else {
c[i] = 0;
++i;
}
}
return result;
},
/*
String utilities
*/
......
"use strict";
define(['jquery', 'utils', 'datatypes/hookset', 'internaltypes/twineerror'], ($, {impossible, toJSLiteral}, HookSet, TwineError) => {
define(['jquery', 'utils', 'datatypes/hookset', 'internaltypes/twineerror'], ($, {impossible, permutations, toJSLiteral}, HookSet, TwineError) => {
/*
First, a quick shortcut to determine whether the
......@@ -439,7 +439,12 @@ define(['jquery', 'utils', 'datatypes/hookset', 'internaltypes/twineerror'], ($,
);
}
if (l instanceof Set && r instanceof Set) {
return matches([...l], [...r]);
/*
This is a little trickier... we need to see if the right side has any permutation that
matches the left side.
*/
l = [...l];
return permutations(...r).some(r => matches(l,r));
}
/*
From here, all the data structures are covered, so we can just invoke is() directly.
......
......@@ -38,7 +38,7 @@ describe("identifiers", function () {
expect("(set:$red to 'egg')(set:$blue to 'g')(print: $blue is in $red and is in 'go')").markupToPrint("true");
});
it("isn't recognised inside text", function (){
expect("(set:$red to window.xit)(set:$red to window.itx)(set:$red to window.xitx)").not.markupToError();
expect("(set:$red to window.sito)(set:$red to window.itx)(set:$red to window.sito)").not.markupToError();
});
});
describe("the 'its' property access syntax", function () {
......@@ -55,7 +55,7 @@ describe("identifiers", function () {
expect("(set:$red to (a:'Bee'))(set:$red to its 1st's 1st)$red").markupToPrint("B");
});
it("isn't recognised inside text", function (){
expect("(set:$red to window.xits)(set:$red to window.itsx)(set:$red to window.xitsx)").not.markupToError();
expect("(set:$red to window.sits)(set:$red to window.itsx)(set:$red to window.sitss)").not.markupToError();
});
});
describe("the computed 'its' property access syntax", function () {
......
......@@ -114,8 +114,8 @@ describe("patterns", function() {
expect("(print: (dm: " + ['"A"',type1,'"B"',type2,'"C"',"(a:" + type1 + ")"] +
") matches (dm:" + ['"A"',value,'"B"',value,'"C"',"(a:" + value + ")"] + "))").markupToPrint("true");
});
xit("matches the " + type1 + " datatype inside datasets to a " + type1 + " regardless of position", function() {
expect("(print: (ds:" + [type1,'"X"',false] + ") matches (ds:" + ['"X"',false,value] + "))").markupToPrint("true");
it("matches the " + type1 + " datatype inside datasets to a " + type1 + " regardless of position", function() {
expect("(print: (ds:" + [type1,'"Y"',false] + ") matches (ds:" + ['"Y"',false,value] + "))").markupToPrint("true");
});
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment