Last active
September 24, 2017 20:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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