Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Last active September 24, 2017 20:48
Show Gist options
  • Save Calvin-Huang/1e272d2fa42dbf7bd390e0c14179076b to your computer and use it in GitHub Desktop.
Save Calvin-Huang/1e272d2fa42dbf7bd390e0c14179076b to your computer and use it in GitHub Desktop.
Parse tag string to query map, check user is exists at same time.
const User = require('./models/user');
description('Tag parser', () => {
it('should parse tag stirng to query map, and remove query when user is not exists', async () => {
// bob, id: 1
// foo, id: 2
// system is no such user
const tagString = 'fav:bob fav:foo fav:system threshold:1';
const queryMap = await tagString
.split(/\s+/)
.reduce(async (accuPromise, tag) => {
const [, name, value] = tag.match(/^(.+):(.+)$/);
if (/^fav/.test(name)) {
const user = await User.find({ where: { name } });
if (user) {
const accumulator = await accuPromise;
return Promise.resolve({ ...accumulator, [name]: user.id });
}
return accuPromise;
}
const accumulator = await accuPromise;
return Promise.resolve({ ...accumulator, [name]: value });
}, Promise.resolve({}));
expect(queryMap).toEqual({ fav: [1, 2], threshold: ['1'] });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment