Skip to content

Commit

Permalink
feat: rename nested to nesting (#13)
Browse files Browse the repository at this point in the history
To be consistent with `arrayRepeat` vs `arrayRepeatSyntax` etc.
  • Loading branch information
43081j authored Jun 21, 2024
1 parent 819c444 commit 98b8e2d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 42 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,41 @@ The default options are as follows:

```ts
{
nested: true,
nesting: true,
nestingSyntax: 'dot',
arrayRepeat: false,
arrayRepeatSyntax: 'repeat',
delimiter: '&'
}
```

### `nested`
### `nesting`

When true, nested objects are supported.

For example, when parsing:

```ts
parse('foo.bar=baz', {nested: true});
parse('foo.bar=baz', {nesting: true});

// {foo: {bar: 'baz'}}
```

When stringifying:

```ts
stringify({foo: {bar: 'baz'}}, {nested: true});
stringify({foo: {bar: 'baz'}}, {nesting: true});

// foo.bar=baz
```

This also results in arrays being supported:

```ts
parse('foo.0=bar', {nested: true});
parse('foo.0=bar', {nesting: true});
// {foo: ['bar']}

stringify({foo: ['bar']}, {nested: true});
stringify({foo: ['bar']}, {nesting: true});
// foo.0=bar
```

Expand Down
8 changes: 4 additions & 4 deletions bench/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ const suites = [
inputs: ['foo=a&bar=b&baz=c'],
options: {
qs: {allowDots: false},
pico: {nested: false}
pico: {nesting: false}
}
},
{
name: 'Dot-syntax nesting',
inputs: ['foo.bar.x=303&foo.bar.y=808'],
options: {
qs: {allowDots: true},
pico: {nested: true}
pico: {nesting: true}
}
},
{
name: 'Index-syntax nesting',
inputs: ['foo[bar][x]=303&foo[bar][y]=808'],
options: {
qs: {allowDots: false},
pico: {nested: true, nestingSyntax: 'index'}
pico: {nesting: true, nestingSyntax: 'index'}
}
},
{
name: 'Custom delimiter',
inputs: ['foo=a;bar=b;baz=c'],
options: {
qs: {delimiter: ';'},
pico: {nested: false, delimiter: ';'}
pico: {nesting: false, delimiter: ';'}
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions bench/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const suites = [
inputs: [{foo: 'x', bar: 'y', baz: 'z'}],
options: {
qs: {allowDots: false},
pico: {nested: false}
pico: {nesting: false}
}
},
{
Expand All @@ -27,7 +27,7 @@ const suites = [
],
options: {
qs: {allowDots: true},
pico: {nested: true}
pico: {nesting: true}
}
},
{
Expand All @@ -45,15 +45,15 @@ const suites = [
],
options: {
qs: {allowDots: false},
pico: {nested: true, nestingSyntax: 'index'}
pico: {nesting: true, nestingSyntax: 'index'}
}
},
{
name: 'Custom delimiter',
inputs: [{foo: 'a', bar: 'b', baz: 'c'}],
options: {
qs: {delimiter: ';'},
pico: {nested: false, delimiter: ';'}
pico: {nesting: false, delimiter: ';'}
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/object-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function walkNestedValues(
nestingSyntax = defaultOptions.nestingSyntax,
arrayRepeat = defaultOptions.arrayRepeat,
arrayRepeatSyntax = defaultOptions.arrayRepeatSyntax,
nested = defaultOptions.nested
nesting = defaultOptions.nesting
} = options;

if (depth > MAX_DEPTH) {
Expand Down Expand Up @@ -64,7 +64,7 @@ function walkNestedValues(
if (
typeof value === 'object' &&
value !== null &&
(nested || (arrayRepeat && probableArray))
(nesting || (arrayRepeat && probableArray))
) {
walkNestedValues(
value as Record<PropertyKey, unknown>,
Expand Down
10 changes: 5 additions & 5 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function parse(input: string, options?: ParseOptions): ParsedQuery {
valueDeserializer = defaultOptions.valueDeserializer,
keyDeserializer = defaultOptions.keyDeserializer,
arrayRepeatSyntax = defaultOptions.arrayRepeatSyntax,
nested = defaultOptions.nested,
nesting = defaultOptions.nesting,
arrayRepeat = defaultOptions.arrayRepeat,
nestingSyntax = defaultOptions.nestingSyntax,
delimiter = defaultOptions.delimiter
Expand Down Expand Up @@ -118,7 +118,7 @@ export function parse(input: string, options?: ParseOptions): ParsedQuery {
const newKey = keyDeserializer(key);
let dlvKey;

if (typeof newKey === 'string' && nested) {
if (typeof newKey === 'string' && nesting) {
if (nestingSyntax === 'index') {
dlvKey = splitByIndexPattern(newKey);
} else {
Expand All @@ -127,10 +127,10 @@ export function parse(input: string, options?: ParseOptions): ParsedQuery {
}

const currentValue =
nested && dlvKey ? getDeepValue(result, dlvKey) : result[newKey];
nesting && dlvKey ? getDeepValue(result, dlvKey) : result[newKey];

if (currentValue === undefined || !arrayRepeat) {
if (nested && dlvKey) {
if (nesting && dlvKey) {
dset(result, dlvKey, newValue);
} else {
result[newKey] = newValue;
Expand All @@ -140,7 +140,7 @@ export function parse(input: string, options?: ParseOptions): ParsedQuery {
if ((currentValue as unknown[]).pop) {
(currentValue as unknown[]).push(newValue);
} else {
if (nested && dlvKey) {
if (nesting && dlvKey) {
dset(result, dlvKey, [currentValue, newValue]);
} else {
result[newKey] = [currentValue, newValue];
Expand Down
13 changes: 4 additions & 9 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,14 @@ export type NestingSyntax =
// `foo[bar]`
| 'index';

export type DeserializeValueFunction = (
value: string,
key: string
) => unknown;
export type DeserializeValueFunction = (value: string, key: string) => unknown;

export type DeserializeKeyFunction = (
key: string
) => PropertyKey;
export type DeserializeKeyFunction = (key: string) => PropertyKey;

export interface Options {
// Enable parsing nested objects and arrays
// default: true
nested: boolean;
nesting: boolean;

// Nesting syntax
// default: "dot"
Expand All @@ -49,7 +44,7 @@ export interface Options {
const identityFunc = <T>(v: T): T => v;

export const defaultOptions: Options = {
nested: true,
nesting: true,
nestingSyntax: 'dot',
arrayRepeat: false,
arrayRepeatSyntax: 'repeat',
Expand Down
24 changes: 12 additions & 12 deletions src/test/test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const testCases: TestCase[] = [
{
input: 'foo=x&foo=y&foo=z',
output: {foo: ['x', 'y', 'z']},
options: {nested: false, arrayRepeat: true, arrayRepeatSyntax: 'repeat'}
options: {nesting: false, arrayRepeat: true, arrayRepeatSyntax: 'repeat'}
},
{
input: 'foo.bar=x&foo.bar=y',
Expand All @@ -151,61 +151,61 @@ export const testCases: TestCase[] = [
input: 'foo[0]=x&foo[1]=y',
stringifyOutput: 'foo%5B0%5D=x&foo%5B1%5D=y',
output: {foo: ['x', 'y']},
options: {nested: true, nestingSyntax: 'index'}
options: {nesting: true, nestingSyntax: 'index'}
},
{
input: 'foo[0]=x&foo[1]=y',
stringifyOutput: 'foo%5B0%5D=x&foo%5B1%5D=y',
output: {'foo[0]': 'x', 'foo[1]': 'y'},
options: {nested: true, nestingSyntax: 'dot'}
options: {nesting: true, nestingSyntax: 'dot'}
},
{
input: 'foo[bar]=x&foo[baz]=y',
stringifyOutput: 'foo%5Bbar%5D=x&foo%5Bbaz%5D=y',
output: {foo: {bar: 'x', baz: 'y'}},
options: {nested: true, nestingSyntax: 'index'}
options: {nesting: true, nestingSyntax: 'index'}
},
{
input: 'foo[bar]=x&foo[baz]=y',
stringifyOutput: 'foo%5Bbar%5D=x&foo%5Bbaz%5D=y',
output: {'foo[bar]': 'x', 'foo[baz]': 'y'},
options: {nested: true, nestingSyntax: 'dot'}
options: {nesting: true, nestingSyntax: 'dot'}
},
{
input: 'foo[bar][baz]=dwa',
stringifyOutput: 'foo%5Bbar%5D%5Bbaz%5D=dwa',
output: {foo: {bar: {baz: 'dwa'}}},
options: {nested: true, nestingSyntax: 'index'}
options: {nesting: true, nestingSyntax: 'index'}
},

// Nesting syntax: dot
{
input: 'foo.0=x&foo.1=y',
output: {foo: ['x', 'y']},
options: {nested: true, nestingSyntax: 'dot'}
options: {nesting: true, nestingSyntax: 'dot'}
},
{
input: 'foo.0=x&foo.1=y',
output: {'foo.0': 'x', 'foo.1': 'y'},
options: {nested: true, nestingSyntax: 'index'}
options: {nesting: true, nestingSyntax: 'index'}
},
{
input: 'foo.bar=x&foo.baz=y',
output: {foo: {bar: 'x', baz: 'y'}},
options: {nested: true, nestingSyntax: 'dot'}
options: {nesting: true, nestingSyntax: 'dot'}
},
{
input: 'foo.bar=x&foo.baz=y',
output: {'foo.bar': 'x', 'foo.baz': 'y'},
options: {nested: true, nestingSyntax: 'index'}
options: {nesting: true, nestingSyntax: 'index'}
},

// Sparse array with nestinh
{
input: 'foo[0]=x&foo[2]=y',
stringifyOutput: 'foo%5B0%5D=x&foo%5B2%5D=y',
output: {foo: createSparseArray(['x', undefined, 'y'])},
options: {nested: true, nestingSyntax: 'index'}
options: {nesting: true, nestingSyntax: 'index'}
},

// Delimiter: ;
Expand All @@ -220,7 +220,7 @@ export const testCases: TestCase[] = [
input: 'foo[bar]=x',
stringifyOutput: 'foo%5Bbar%5D=x',
output: {'foo[bar]': 'x'},
options: {nested: false}
options: {nesting: false}
},

// With a key deserializer
Expand Down

0 comments on commit 98b8e2d

Please sign in to comment.