sift.js
sift.js copied to clipboard
Support asynchronous (promised-based) custom matchers
For our use case, we'd like to implement custom expressions that make database calls. An example of what we'd like to do (that doesn't work today):
sift.use({
$existsInDb: async function(expectToExist, name) {
const existsInDb = !!(await db.users.findOne({ name }));
return existsInDb === expectToExist;
}
});
const result = await sift({ $existsInDb: true }, ['bob', 'ben', 'brad']);
FYI: we forked sift.js and implemented async support in https://github.com/mixmaxhq/sift.js/pull/1.
Just starting on this functionality in #157. The API will likely be:
const asyncFilter = sift({ $existsInDb: true }, {
operations: {
async $existsInDb(expectToExist, name) {
const existsInDb = Boolean(await db.users.findOne({ name }));
return existsInDb === expectToExist;
}
}
});
const results = await Promise.all(['bob', 'ben', 'brad'].filter(asyncFilter));