Skip to content

Instantly share code, notes, and snippets.

@crapthings
Created October 20, 2019 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crapthings/7bf1e569fedb4745e418dbf2ed36798d to your computer and use it in GitHub Desktop.
Save crapthings/7bf1e569fedb4745e418dbf2ed36798d to your computer and use it in GitHub Desktop.
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)
@crapthings
Copy link
Author

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