Created
October 20, 2019 11:31
-
-
Save crapthings/7bf1e569fedb4745e418dbf2ed36798d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 parser = str => { | |
let ops = [] | |
let method, arg | |
let isMethod = true | |
let open = [] | |
for (const char of str) { | |
// skip whitespace | |
if (char === ' ') continue | |
// append method or arg string | |
if (char !== '(' && char !== ')') { | |
if (isMethod) { | |
(method ? (method += char) : (method = char)) | |
} else { | |
(arg ? (arg += char) : (arg = char)) | |
} | |
} | |
if (char === '(') { | |
// nested parenthesis should be a part of arg | |
if (!isMethod) arg += char | |
isMethod = false | |
open.push(char) | |
} else if (char === ')') { | |
open.pop() | |
// check end of arg | |
if (open.length < 1) { | |
isMethod = true | |
ops.push({ method, arg }) | |
method = arg = undefined | |
} else { | |
arg += char | |
} | |
} | |
} | |
return ops | |
} | |
const test = parser(`$$(groups) filter({ type: 'ORGANIZATION', isDisabled: { $ne: true } }) pickBy(_id, type) map(test()) as(groups)`) | |
// const test = parser(`push(number) map(test(a(a()))) bass(wow, abc)`) | |
console.log(test) |
Author
crapthings
commented
Oct 21, 2019
`
first line will be title
$$(groups) pickBy(parentGroupId, shortName, name) as(groups)
$$(users) pickBy(profile.name) as(users)
$$(issues) pickBy(title, groupId, createdById, createdAt) as(issues)
ref(groups) keyBy(_id) as(groups)
ref(users) keyBy(_id) as(users)
ref(issues) map(groupId = groups.shortname || groups.name, createdById = users.profile.name, createdAt = @YYYY) as(issues)
pivot rows(createdAt, createdById, groupId)
dict(groups, users)
output(issues)
`.split('\n').forEach(line => {
// console.log(line)
console.log(parser(line))
})
[]
[]
[]
[ { method: '$$', arg: 'groups' },
{ method: 'pickBy', arg: 'parentGroupId,shortName,name' },
{ method: 'as', arg: 'groups' } ]
[ { method: '$$', arg: 'users' },
{ method: 'pickBy', arg: 'profile.name' },
{ method: 'as', arg: 'users' } ]
[ { method: '$$', arg: 'issues' },
{ method: 'pickBy', arg: 'title,groupId,createdById,createdAt' },
{ method: 'as', arg: 'issues' } ]
[]
[ { method: 'ref', arg: 'groups' },
{ method: 'keyBy', arg: '_id' },
{ method: 'as', arg: 'groups' } ]
[ { method: 'ref', arg: 'users' },
{ method: 'keyBy', arg: '_id' },
{ method: 'as', arg: 'users' } ]
[ { method: 'ref', arg: 'issues' },
{ method: 'map',
arg:
'groupId=groups.shortname||groups.name,createdById=users.profile.name,createdAt=@YYYY' },
{ method: 'as', arg: 'issues' } ]
[]
[ { method: 'pivotrows', arg: 'createdAt,createdById,groupId' } ]
[]
[ { method: 'dict', arg: 'groups,users' } ]
[ { method: 'output', arg: 'issues' } ]
[]
test
$$(issues) filter({ createdAt: { $gte: moment(new Date('2018')).endOf('year').toDate() } }) pickBy(createdAt) as(issues)
output(issues)
want to make function works too moment(new Date('2018')).endOf('year').toDate()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment