Last active
June 3, 2024 19:42
-
-
Save Cold06/d655774a510dfd14ca2d9f8f6b88bfe0 to your computer and use it in GitHub Desktop.
Stupid custom loosely defined query language parser
This file contains 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 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