Skip to content

Instantly share code, notes, and snippets.

@metaory
Last active June 12, 2024 23:01
Show Gist options
  • Save metaory/9a7980d7a51cb76a711339db29721cd9 to your computer and use it in GitHub Desktop.
Save metaory/9a7980d7a51cb76a711339db29721cd9 to your computer and use it in GitHub Desktop.
Pure Web API URL Query Strings
const { log, assert } = console
// with only primitives: string number bigint boolean undefined symbol null
const data = {
cipher: '1eV6syzW',
engine: 'V8',
lexer: 'doctrine',
relay: null,
seed: 1710396609.2115135,
space: 'lab-d65',
tag: true,
token: 'Identifier',
}
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
const qs = new URLSearchParams(data).toString()
log('qs', '::', qs)
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
const parsed = Object.fromEntries(new URLSearchParams(qs).entries())
log('parsed.space', '::', parsed.space) // lab-d65
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
assert(parsed.space === 'lab-d65', '[FAILED]')
const { log, assert } = console
const data = {
cipher: '1eV6syzW',
engine: 'V8',
total: true,
resources: ['base.js'],
jobs: [
{
name: 'Proxies',
path: ['Proxies'],
main: 'run.js',
engine: 'V8',
tests: [{ name: 'GetStringWithoutTrap' }, { name: 'GetStrin' }],
},
{
vec: 991383901,
importance: 8.7,
cycle: {
distance: [true, -1259037374, { standard: false, or: 'purpose' }, 'wind'],
phrase: -274093901,
discover: 736843873.0780926,
token: 'Identifier',
},
},
],
count: 358638932.34953165,
void: ['realize', true, 268857211.96555376],
}
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
const qs = new URLSearchParams({ data: JSON.stringify(data) }).toString()
log('qs', '::', qs)
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
const res = Object.fromEntries(new URLSearchParams(qs).entries())
log('res', '::', res)
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
const parsed = JSON.parse(res.data)
log('parsed', '::', parsed)
// · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
assert(parsed.jobs[0].name === 'Proxies', '[FAILED]')
import querystring from 'node:querystring'
/* TODO: ?
querystring.decode()
querystring.encode()
querystring.escape(str)
querystring.parse(str[, sep[, eq[, options]]])
querystring.stringify(obj[, sep[, eq[, options]]])
querystring.unescape(str)
*/
// WebAPI > Node ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment