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

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)

@crapthings
Copy link
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' } ]
[]

@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