Skip to content

Commit 8bdaf5c

Browse files
committed
feat: rename package to nested-querystring
1 parent 6a5c491 commit 8bdaf5c

File tree

6 files changed

+83
-36
lines changed

6 files changed

+83
-36
lines changed

LICENSE.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# [MIT License](https://spdx.org/licenses/MIT)
22

3+
Copyright (c) 2023 James Garbutt
34
Copyright (c) 2017 Renée Kooi <[email protected]>
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

README.md

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
# qs-stringify
1+
# nested-querystring
22

3-
Simple query stringify with nesting.
3+
A small utility to create nested query strings with `deep[property]=value`
4+
syntax.
45

5-
[![npm][npm-image]][npm-url]
6-
[![github][github-image]][github-url]
7-
[![standard][standard-image]][standard-url]
8-
9-
[npm-image]: https://img.shields.io/npm/v/qs-stringify.svg?style=flat-square
10-
[npm-url]: https://www.npmjs.com/package/qs-stringify
11-
[github-image]: https://github.com/goto-bus-stop/qs-stringify/workflows/Node%20CI/badge.svg
12-
[github-url]: https://github.com/goto-bus-stop/qs-stringify/actions
13-
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
14-
[standard-url]: http://npm.im/standard
6+
Originally a fork of
7+
[qs-stringify](https://github.com/goto-bus-stop/qs-stringify).
158

169
## Install
1710

1811
```
19-
npm install qs-stringify
12+
npm install nested-querystring
2013
```
2114

2215
## Usage
2316

2417
```js
25-
var stringify = require('qs-stringify')
18+
import {stringify, create} from 'nested-querystring';
2619

2720
stringify({
2821
page: {
@@ -32,6 +25,16 @@ stringify({
3225
filter: 'hello world'
3326
})
3427
// → "page[offset]=50&page[limit]=25&filter=hello%20world"
28+
29+
create({
30+
page: {
31+
offset: 50,
32+
limit: 25
33+
},
34+
filter: 'hello world'
35+
});
36+
// URLSearchParams { .... }
37+
3538
```
3639

3740
## License

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "qs-stringify",
3-
"description": "Simple query stringify with nesting.",
4-
"version": "1.2.1",
5-
"author": "Renée Kooi <[email protected]>",
2+
"name": "nested-querystring",
3+
"description": "A small utility to create nested query strings",
4+
"version": "0.0.1",
5+
"author": "James Garbutt (https://github.com/43081j)",
66
"files": [
77
"lib/**/*.js",
88
"!lib/test"
99
],
1010
"bugs": {
11-
"url": "https://github.com/goto-bus-stop/qs-stringify/issues"
11+
"url": "https://github.com/43081j/nested-querystring/issues"
1212
},
1313
"devDependencies": {
1414
"@types/node": "^20.11.8",
@@ -18,7 +18,7 @@
1818
"prettier": "^3.2.4",
1919
"typescript": "^5.3.3"
2020
},
21-
"homepage": "https://github.com/goto-bus-stop/qs-stringify",
21+
"homepage": "https://github.com/43081j/nested-querystring",
2222
"keywords": [
2323
"format",
2424
"qs",
@@ -29,7 +29,7 @@
2929
"main": "lib/main.js",
3030
"repository": {
3131
"type": "git",
32-
"url": "https://github.com/goto-bus-stop/qs-stringify.git"
32+
"url": "https://github.com/43081j/nested-querystring.git"
3333
},
3434
"scripts": {
3535
"lint": "npm run lint:js && npm run format:check",

src/main.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ function appendValuesToQuery(
3535
* Stringify an object for use in a query string.
3636
*
3737
* @param {object} obj Object to stringify
38-
* @param {string} prefix When nesting, the parent key. keys in `obj` will be
39-
* stringified as `prefix[key]`.
4038
* @return {string}
4139
*/
42-
export function queryStringify(obj: object, prefix?: string): string {
40+
export function stringify(obj: object): string {
41+
return create(obj).toString();
42+
}
43+
44+
/**
45+
* Creates a URLSearchParams for a given object, taking nested keys
46+
* into account
47+
*
48+
* @param {object} obj Object to retrieve keys from
49+
* @return {URLSearchParams}
50+
*/
51+
export function create(obj: object): URLSearchParams {
4352
const params = new URLSearchParams();
44-
appendValuesToQuery(params, obj, prefix);
45-
return params.toString();
53+
appendValuesToQuery(params, obj);
54+
return params;
4655
}

src/test/main_test.ts

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
import * as assert from 'node:assert/strict';
22
import {test} from 'node:test';
3-
import {queryStringify} from '../main.js';
3+
import {stringify, create} from '../main.js';
44

5-
test('queryStringify', async (t) => {
5+
test('create', async (t) => {
6+
await t.test('simple objects', () => {
7+
assert.deepEqual(
8+
[
9+
...create({
10+
a: 1,
11+
b: 2
12+
})
13+
],
14+
[
15+
['a', '1'],
16+
['b', '2']
17+
]
18+
);
19+
});
20+
21+
await t.test('nested objects', () => {
22+
assert.deepEqual(
23+
[
24+
...create({
25+
nested: {
26+
a: {
27+
b: {
28+
c: 'd'
29+
}
30+
}
31+
}
32+
})
33+
],
34+
[['nested[a][b][c]', 'd']]
35+
);
36+
});
37+
});
38+
39+
test('stringify', async (t) => {
640
await t.test('stringify simple objects', () => {
741
assert.equal(
8-
queryStringify({
42+
stringify({
943
a: 1,
1044
b: 2
1145
}),
@@ -15,7 +49,7 @@ test('queryStringify', async (t) => {
1549

1650
await t.test('nested objects using [xyz] syntax', () => {
1751
assert.equal(
18-
queryStringify({
52+
stringify({
1953
nested: {
2054
a: {
2155
b: {
@@ -30,7 +64,7 @@ test('queryStringify', async (t) => {
3064

3165
await t.test('URL encoding', () => {
3266
assert.equal(
33-
queryStringify({
67+
stringify({
3468
'key&value': 'key=value'
3569
}),
3670
'key%26value=key%3Dvalue'
@@ -39,7 +73,7 @@ test('queryStringify', async (t) => {
3973

4074
await t.test('encoded arrays', () => {
4175
assert.equal(
42-
queryStringify({
76+
stringify({
4377
object: {
4478
xyz: 'hello'
4579
},

0 commit comments

Comments
 (0)