{"name":"picoquery","version":"2.4.0","keywords":["qs","querystring","query","query-string","url"],"author":{"url":"https://github.com/43081j","name":"James Garbutt"},"license":"MIT","_id":"picoquery@2.4.0","maintainers":[{"name":"43081j","email":"43081james@gmail.com"}],"homepage":"https://github.com/43081j/picoquery#readme","bugs":{"url":"https://github.com/43081j/picoquery/issues"},"dist":{"shasum":"f1b2eaf772e7d630e71b6863e3a4dae828bd3245","tarball":"http://registry-npm.ywwl.com/picoquery/download/picoquery-2.4.0.tgz","fileCount":15,"integrity":"sha512-FTiUit3mPWtFs7wh4qeYr9Gypj8cca7Tl2gZQsdPi3dCIx+jE4W5fprzFTerH6pvDGEMwxTuGumhqK8gC1foYQ==","signatures":[{"sig":"MEUCIQC/jjUJMayi5z6vAFCOMbajw/jGzW5HxaadxYUeuLcCQQIgX87Rw4f08BueLcW4DGBmwceL1zQ3mp8SO7chdE7PxR0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/picoquery@2.4.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":33089,"size":8541,"noattachment":false,"key":"/picoquery/-/picoquery-2.4.0.tgz"},"main":"lib/main.js","type":"module","types":"./lib/main.d.ts","gitHead":"d663bd18b95c4cfec4053755627d49b154598f10","scripts":{"lint":"npm run lint:format && eslint src","test":"npm run build && c8 node --test","build":"npm run build:clean && tsc","format":"prettier --write src bench","prepare":"npm run build","bench:parse":"node ./bench/parse.js","build:clean":"rimraf lib","lint:format":"prettier --check src bench","bench:stringify":"node ./bench/stringify.js"},"_npmUser":{"name":"43081j","email":"43081james@gmail.com"},"repository":{"url":"git+https://github.com/43081j/picoquery.git","type":"git"},"_npmVersion":"10.8.2","description":"A small library for parsing and serialisation query strings","directories":{},"_nodeVersion":"22.5.1","dependencies":{"fast-decode-uri-component":"^1.0.1"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","qs":"^6.12.1","rimraf":"^5.0.7","prettier":"^3.2.5","@types/qs":"^6.9.15","tinybench":"^2.8.0","@eslint/js":"^9.1.1","typescript":"^5.4.5","@types/node":"^20.12.7","fast-querystring":"^1.1.2","typescript-eslint":"^7.7.1"},"_npmOperationalInternal":{"tmp":"tmp/picoquery_2.4.0_1722073092880_0.5946722463603953","host":"s3://npm-registry-packages"},"readme":"# picoquery\n\n[![CI](https://github.com/43081j/picoquery/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/43081j/picoquery/actions/workflows/main.yml)\n[![NPM version](https://img.shields.io/npm/v/picoquery.svg?style=flat)](https://www.npmjs.com/package/picoquery)\n\nA lightweight query string parser/stringifier with support for nesting and some\nconfigurability.\n\nBuilt on top of [fast-querystring](https://github.com/anonrig/fast-querystring).\n\n## Install\n\n```sh\nnpm i -S picoquery\n```\n\n### CommonJS vs ESM\n\n`2.x` and above are _ESM only_ (i.e. if you're running Node.js older than\n`v20.17.0`/`v22.0.0`, your project will need `type: \"module\"` in the `package.json`).\n\nIf you cannot update Node.js or move to ESM yet, you may continue to use `1.x` which will still\nreceive non-breaking changes, backported from `main`.\n\n## Usage\n\nParsing a query string:\n\n```ts\nimport {parse} from 'picoquery';\n\nparse('foo.bar=abc&baz=def');\n\n/*\n  {\n    foo: {\n      bar: 'abc'\n    },\n    baz: 'def'\n  }\n*/\n```\n\nStringifying an object:\n\n```ts\nimport {stringify} from 'picoquery';\n\nstringify({\n  foo: {\n    bar: 123\n  }\n});\n\n/*\nfoo.bar=123\n*/\n```\n\n## API\n\n### `stringify(object[, options])`\n\nConverts the given object into a query string, optionally with configured\noptions.\n\n### `parse(str[, options])`\n\nParses the given query string into an object, optionally with configured\noptions.\n\n## Options\n\n### Default options\n\nThe default options are as follows:\n\n```ts\n{\n  nesting: true,\n  nestingSyntax: 'dot',\n  arrayRepeat: false,\n  arrayRepeatSyntax: 'repeat',\n  delimiter: '&',\n  allowSparseArrays: false\n}\n```\n\n### `nesting`\n\nWhen true, nested objects are supported.\n\nFor example, when parsing:\n\n```ts\nparse('foo.bar=baz', {nesting: true});\n\n// {foo: {bar: 'baz'}}\n```\n\nWhen stringifying:\n\n```ts\nstringify({foo: {bar: 'baz'}}, {nesting: true});\n\n// foo.bar=baz\n```\n\nThis also results in arrays being supported:\n\n```ts\nparse('foo.0=bar', {nesting: true});\n// {foo: ['bar']}\n\nstringify({foo: ['bar']}, {nesting: true});\n// foo.0=bar\n```\n\n### `nestingSyntax`\n\nSets which style of nesting syntax should be used. The choices are:\n\n- `dot` (e.g. `foo.bar=baz`)\n- `index` (e.g. `foo[bar]=baz`)\n- `js` (e.g. `foo.bar[0]=baz`, i.e. arrays are indexed and properties are\ndotted or indexed)\n\n### `arrayRepeat`\n\nIf `true`, this will treat repeated keys as arrays.\n\nFor example:\n\n```ts\nparse('foo=x&foo=y', {arrayRepeat: true});\n// {foo: ['x', 'y']}\n\nstringify({foo: ['x', 'y']}, {arrayRepeat: true});\n// foo=x&foo=y\n```\n\n### `arrayRepeatSyntax`\n\nSets which style of array repetition syntax should be used. The choices are:\n\n- `bracket` (e.g. `foo[]=x&foo[]=y`)\n- `repeat` (e.g. `foo=x&foo=y`)\n\n### `delimiter`\n\nSets the delimiter to be used instead of `&`.\n\nFor example:\n\n```ts\nparse('foo=x;bar=y', {delimiter: ';'});\n// {foo: 'x', bar: 'y'}\n\nstringify({foo: 'x', bar: 'y'}, {delimiter: ';'});\n// foo=x;bar=y\n```\n\n### `allowSparseArrays`\n\nIf `true`, arrays parsed with gaps in their indices will be kept as sparse\narrays.\n\nBy default (`false`), parsed arrays are truncated at the first missing index.\n\nFor example:\n\n```ts\nparse('foo[0]=x&foo[2]=y', {nestingSyntax: 'index'});\n// {foo: ['x']}\n\nparse('foo[0]=x&foo[2]=y', {nestingSyntax: 'index', allowSparseArrays: true});\n// {foo: ['x', <empty>, 'y']}\n```\n\nThis option only applies when parsing.\n\n### `valueDeserializer`\n\nCan be set to a function which will be used to deserialize each value during\nparsing.\n\nIt will be called with the `value` and the already deserialized\n`key` (i.e. `(value: string, key: PropertyKey) => *`).\n\nFor example:\n\n```ts\nparse('foo=300', {\n  valueDeserializer: (value) => {\n    const asNum = Number(value);\n    return Number.isNaN(asNum) ? value : asNum;\n  }\n});\n\n// {foo: 300}\n```\n\n### `keyDeserializer`\n\nCan be set to a function which will be used to deserialize each key during\nparsing.\n\nIt will be called with the `key` from the query string\n(i.e. `(key) => PropertyKey`).\n\nFor example:\n\n```ts\nparse('300=foo', {\n  keyDeserializer: (key) => {\n    const asNum = Number(key);\n    return Number.isNaN(asNum) ? key : asNum;\n  }\n});\n\n// {300: 'foo'}\n```\n\n### `shouldSerializeObject`\n\nCan be set to a function which determines if an _object-like_ value should be\nserialized instead of being treated as a nested object.\n\n**All non-object primitives will always be serialized.**\n\nFor example:\n\n```\n// Assuming `StringifableObject` returns its constructor value when `toString`\n// is called.\nstringify({\n  foo: new StringifiableObject('test')\n}, {\n  shouldSerializeObject(val) {\n    return val instanceof StringifableObject;\n  },\n  valueSerializer: (value) => {\n    return String(value);\n  }\n});\n\n// foo=test\n```\n\nIf you want to fall back to the default logic, you can import the default\nfunction:\n\n```\nimport {defaultShouldSerializeObject, stringify} from 'picoquery';\n\nstringify({\n  foo: new StringifiableObject('test')\n}, {\n  shouldSerializeObject(val) {\n    if (val instanceof StringifableObject) {\n      return true;\n    }\n    return defaultShouldSerializeObject(val);\n  }\n});\n```\n\n### `valueSerializer`\n\nCan be set to a function which will be used to serialize each value during\nstringifying.\n\nIt will be called with the `value` and the `key`\n(i.e. `(value: unknown, key: PropertyKey) => string`).\n\nFor example:\n\n```ts\nstringify({foo: 'bar'}, {\n  valueSerializer: (val) => String(val) + String(val)\n});\n\n// foo=barbar\n```\n\n**Note** that you can import the default serializer if you only want to handle\nsome cases.\n\nFor example:\n\n```ts\nimport {defaultValueSerializer, stringify} from 'picoquery';\n\nstringify({foo: 'bar'}, {\n  valueSerializer: (val, key) => {\n    if (val instanceof Date) {\n      return val.toISOString();\n    }\n\n    // Call the original serializer otherwise\n    return defaultValueSerializer(val, key);\n  }\n});\n```\n\n## Benchmarks\n\n**IMPORTANT**: there are a few things to take into account with these\nbenchmarks:\n\n- `fast-querystring` is not capable of parsing or stringifying nested objects,\nso the results are incomparible (but here as reference)\n- all libraries have their own level of configurability. It will be possible\nto increase perf in each of them by disabling various features, but these are\njust the 'happy path'\n\n### Parse\n\n```\nBenchmark: Basic (no nesting)\n┌─────────┬─────────────────────────────────┬─────────────┬────────────────────┬──────────┬─────────┐\n│ (index) │ Task Name                       │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples │\n├─────────┼─────────────────────────────────┼─────────────┼────────────────────┼──────────┼─────────┤\n│ 0       │ 'picoquery'                     │ '2,393,539' │ 417.79130492907393 │ '±0.42%' │ 1196770 │\n│ 1       │ 'qs'                            │ '382,933'   │ 2611.4212945313157 │ '±1.04%' │ 191467  │\n│ 2       │ 'fast-querystring (no nesting)' │ '2,633,148' │ 379.77342802356833 │ '±1.58%' │ 1316575 │\n└─────────┴─────────────────────────────────┴─────────────┴────────────────────┴──────────┴─────────┘\nBenchmark: Dot-syntax nesting\n┌─────────┬─────────────────────────────────┬─────────────┬───────────────────┬──────────┬─────────┐\n│ (index) │ Task Name                       │ ops/sec     │ Average Time (ns) │ Margin   │ Samples │\n├─────────┼─────────────────────────────────┼─────────────┼───────────────────┼──────────┼─────────┤\n│ 0       │ 'picoquery'                     │ '1,406,039' │ 711.2176054736389 │ '±0.56%' │ 703020  │\n│ 1       │ 'qs'                            │ '205,611'   │ 4863.536155478235 │ '±0.98%' │ 102806  │\n│ 2       │ 'fast-querystring (no nesting)' │ '2,588,560' │ 386.3151031345139 │ '±0.66%' │ 1294281 │\n└─────────┴─────────────────────────────────┴─────────────┴───────────────────┴──────────┴─────────┘\n```\n\n### Stringify\n\n```\nBenchmark: Basic (no nesting)\n┌─────────┬─────────────────────────────────┬─────────────┬────────────────────┬──────────┬─────────┐\n│ (index) │ Task Name                       │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples │\n├─────────┼─────────────────────────────────┼─────────────┼────────────────────┼──────────┼─────────┤\n│ 0       │ 'picoquery'                     │ '4,163,092' │ 240.20605684139227 │ '±0.16%' │ 2081547 │\n│ 1       │ 'qs'                            │ '843,630'   │ 1185.352608720127  │ '±0.76%' │ 421816  │\n│ 2       │ 'fast-querystring (no nesting)' │ '3,795,565' │ 263.46536774751036 │ '±0.24%' │ 1897783 │\n└─────────┴─────────────────────────────────┴─────────────┴────────────────────┴──────────┴─────────┘\nBenchmark: Dot-syntax nesting\n┌─────────┬─────────────────────────────────┬─────────────┬────────────────────┬──────────┬─────────┐\n│ (index) │ Task Name                       │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples │\n├─────────┼─────────────────────────────────┼─────────────┼────────────────────┼──────────┼─────────┤\n│ 0       │ 'picoquery'                     │ '1,768,770' │ 565.3644818387182  │ '±1.08%' │ 884387  │\n│ 1       │ 'qs'                            │ '332,254'   │ 3009.743667534287  │ '±1.38%' │ 166128  │\n│ 2       │ 'fast-querystring (no nesting)' │ '7,227,031' │ 138.36940410118828 │ '±1.15%' │ 3613517 │\n└─────────┴─────────────────────────────────┴─────────────┴────────────────────┴──────────┴─────────┘\n```\n\n## License\n\nMIT\n","publish_time":1722073093047,"_cnpm_publish_time":1722073093047,"dist-tags":{"latest":"3.0.1","next":"1.1.0-beta.0","v1-next":"1.1.0-beta.1","v1-stable":"1.5.0"}}