const data = {
num: 3
}
const siftChecker = sift(data => data.num === 2)
const ucastChecker = filter(data => data.num === 2)
console.log(siftChecker(data));
//false
console.log(ucastChecker(data));
//true
const topic = [
{
name: "craig",
},
{
name: "tim",
},
];
const query = {
name: "tim",
};
const siftChecker = sift(query);
const ucastChecker = filter(query);
console.log(topic.filter(siftChecker).length);
// 1
console.log(topic.filter(ucastChecker).length);
// TypeError: n is not a function
// at p (/home/jeremy/Projects/playground/node_modules/@ucast/js/dist/es6c/index.js:1:440)
// at interpret (/home/jeremy/Projects/playground/node_modules/@ucast/core/dist/es6c/index.js:1:760)
// at Array.filter (<anonymous>)
// at Object.<anonymous> (/home/jeremy/Projects/playground/sift.js:86:19)
// at Module._compile (internal/modules/cjs/loader.js:1138:30)
// at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
// at Module.load (internal/modules/cjs/loader.js:986:32)
// at Function.Module._load (internal/modules/cjs/loader.js:879:14)
// at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
// at internal/main/run_main_module.js:17:47
const topic = [
{
name: "craig",
},
{
name: "tim",
},
];
const query = {
name: "tim",
};
const siftChecker = sift(query);
const ucastChecker = filter(query);
const ucastCheckerFixed = (data) => ucastChecker(data);
console.log(topic.filter(siftChecker).length);
// 1
console.log(topic.filter(ucastCheckerFixed).length);
// 1
const query = {
num: val => val > 2
}
This usecase also won't be supported because it's not MongoDB compliant. MongoDB equivalent to this isconst query = {
$where() {
return this.num > 2
}
}
In context of CASL functions are not very useful, because they are not serializable. If you won't to use functions on fields, why do you use sift at all? As it's much easier to write a function with all the required conditions.
You can use functions as conditions in CASL - https://casl.js.org/v4/en/advanced/customize-ability#custom-conditions-matcher-implementation
Array.prototype.filter
passes additional arguments to function which is then passed down parser, which excepts an object and that's why it crashes
new Function
. You also cannot use new Function
when Content-Secure-Policy doesn't allow to eval - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
getValueByPath
right now
it('returns deeply nested property values from array', () => {
const object = { items: [{ specs: [{ price: 12 }] }, { specs: [{ price: 14 }] }] }
expect(getObjectField(object, 'items.specs.price')).to.deep.equal([12, 14])
})
AssertionError: expected [ undefined, undefined ] to deeply equal [ 12, 14 ] + expected - actual [ - [undefined] - [undefined] + 12 + 14 ]
Array.map
we can use Array.reduce
+ concat