Skip to content

Commit

Permalink
feat: rename package to nested-querystring
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Jan 28, 2024
1 parent 6a5c491 commit 09dc03f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 35 deletions.
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# [MIT License](https://spdx.org/licenses/MIT)

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

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:
Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
# qs-stringify
# nested-querystring

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

[![npm][npm-image]][npm-url]
[![github][github-image]][github-url]
[![standard][standard-image]][standard-url]

[npm-image]: https://img.shields.io/npm/v/qs-stringify.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/qs-stringify
[github-image]: https://github.com/goto-bus-stop/qs-stringify/workflows/Node%20CI/badge.svg
[github-url]: https://github.com/goto-bus-stop/qs-stringify/actions
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/standard
Originally a fork of
[qs-stringify](https://github.com/goto-bus-stop/qs-stringify).

## Install

```
npm install qs-stringify
npm install nested-querystring
```

## Usage

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

stringify({
page: {
Expand All @@ -32,6 +25,16 @@ stringify({
filter: 'hello world'
})
// → "page[offset]=50&page[limit]=25&filter=hello%20world"

create({
page: {
offset: 50,
limit: 25
},
filter: 'hello world'
});
// URLSearchParams { .... }

```

## License
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "qs-stringify",
"description": "Simple query stringify with nesting.",
"version": "1.2.1",
"name": "nested-querystring",
"description": "A small utility to create nested query strings",
"version": "1.0.0",
"author": "Renée Kooi <[email protected]>",
"files": [
"lib/**/*.js",
"!lib/test"
],
"bugs": {
"url": "https://github.com/goto-bus-stop/qs-stringify/issues"
"url": "https://github.com/43081j/nested-querystring/issues"
},
"devDependencies": {
"@types/node": "^20.11.8",
Expand All @@ -18,7 +18,7 @@
"prettier": "^3.2.4",
"typescript": "^5.3.3"
},
"homepage": "https://github.com/goto-bus-stop/qs-stringify",
"homepage": "https://github.com/43081j/nested-querystring",
"keywords": [
"format",
"qs",
Expand All @@ -29,7 +29,7 @@
"main": "lib/main.js",
"repository": {
"type": "git",
"url": "https://github.com/goto-bus-stop/qs-stringify.git"
"url": "https://github.com/43081j/nested-querystring.git"
},
"scripts": {
"lint": "npm run lint:js && npm run format:check",
Expand Down
19 changes: 14 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ function appendValuesToQuery(
* Stringify an object for use in a query string.
*
* @param {object} obj Object to stringify
* @param {string} prefix When nesting, the parent key. keys in `obj` will be
* stringified as `prefix[key]`.
* @return {string}
*/
export function queryStringify(obj: object, prefix?: string): string {
export function stringify(obj: object): string {
return create(obj).toString();
}

/**
* Creates a URLSearchParams for a given object, taking nested keys
* into account
*
* @param {object} obj Object to retrieve keys from
* @return {URLSearchParams}
*/
export function create(obj: object): URLSearchParams {
const params = new URLSearchParams();
appendValuesToQuery(params, obj, prefix);
return params.toString();
appendValuesToQuery(params, obj);
return params;
}
46 changes: 40 additions & 6 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
import * as assert from 'node:assert/strict';
import {test} from 'node:test';
import {queryStringify} from '../main.js';
import {stringify, create} from '../main.js';

test('queryStringify', async (t) => {
test('create', async (t) => {
await t.test('simple objects', () => {
assert.deepEqual(
[
...create({
a: 1,
b: 2
})
],
[
['a', '1'],
['b', '2']
]
);
});

await t.test('nested objects', () => {
assert.deepEqual(
[
...create({
nested: {
a: {
b: {
c: 'd'
}
}
}
})
],
[['nested[a][b][c]', 'd']]
);
});
});

test('stringify', async (t) => {
await t.test('stringify simple objects', () => {
assert.equal(
queryStringify({
stringify({
a: 1,
b: 2
}),
Expand All @@ -15,7 +49,7 @@ test('queryStringify', async (t) => {

await t.test('nested objects using [xyz] syntax', () => {
assert.equal(
queryStringify({
stringify({
nested: {
a: {
b: {
Expand All @@ -30,7 +64,7 @@ test('queryStringify', async (t) => {

await t.test('URL encoding', () => {
assert.equal(
queryStringify({
stringify({
'key&value': 'key=value'
}),
'key%26value=key%3Dvalue'
Expand All @@ -39,7 +73,7 @@ test('queryStringify', async (t) => {

await t.test('encoded arrays', () => {
assert.equal(
queryStringify({
stringify({
object: {
xyz: 'hello'
},
Expand Down

0 comments on commit 09dc03f

Please sign in to comment.