Skip to content

Instantly share code, notes, and snippets.

@Cold06
Last active June 3, 2024 19:42
Show Gist options
  • Save Cold06/d655774a510dfd14ca2d9f8f6b88bfe0 to your computer and use it in GitHub Desktop.
Save Cold06/d655774a510dfd14ca2d9f8f6b88bfe0 to your computer and use it in GitHub Desktop.
Stupid custom loosely defined query language parser
const parse = input =>
input.split(',') // params separated by comma
.map(part => part.split(/[:=]/g) // param/value pair separated by : or =
.map(part => part.map(term => term.trim()))) // trim params and values
.filter(([k, v]) => k && v) // filter out empty entries
.reduce((p, [k, v]) => ({ ...p, [k]: v }), {}) // join everything on one object
// parse('a : 1, b=32423, c: true, d e f g : example AND test, test = false ')
// outputs
// { a: '1', b: '32423', c: 'true', 'd e f g': 'example AND test', test: 'false' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment