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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
want to make function works too moment(new Date('2018')).endOf('year').toDate()