You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add optional replacer function in parse and stringify functions (#16)
* feat: add optional replacer function in stringify function
* feat: add optional replacer function in parse function
Signed-off-by: Rong Sen Ng (motss) <wes.ngrongsen@gmail.com>
| ✅ |[parse]| Decodes URL search params into an object. |`parse('a=a&b=1')` returns `{ a: 'a', b: '1' }`. |
59
+
| ✅ |[stringify]| Encodes an object into URL search params. |`stringify({ a: 'a', b: 1 })` gives `a=a&b=1`. |
60
+
| ✅ | Parse multiple values | Parses comma-separated param into an array of values. |`parse('a=a,b')` returns `{ a: ['a', 'b'] }`. |
61
+
| ✅ | Parse single value | Parses single-value param into a string. |`parse('a=a')` returns `{ a: 'a' }`. |
62
+
| ✅ | Parse multiple params of the same name | Parses multiple params of the same name into an array of values. |`parse('a=a,b&a=c')` returns `{ a: ['a', 'b', 'c'] }`. |
| ✅ | Optional replacer function for parsing | Optionally alters final parsed value. | See [Optional replacer function][optional-replacer-function-url]. |
66
+
| ✅ | Optional replacer function for stringify | Optionally alters final stringified value. | See [Optional replacer function][optional-replacer-function-url]. |
67
+
| ✅ | Omit nullish value in stringify | By default, all nullish values are omitted when stringify-ing an object. |`stringify({ a: 'a', b: undefined, c: null })` gives `a=a`. |
68
+
| ❌ | Parse `a[0]=a&a[1]=b` into array | Not supported but it should work. For arrays, use comma-separated value. |`parse('a[0]=a&a[1]=b')` returns `{ a: { 0: 'a', 1: 'b' } }`. |
69
+
| 🚧 | Stringify non-JavaScript primitives | Stringifies all non-JavaScript primitives with its best effort. |`stringify({ a() {return;} })` gives `a=a%28%29+%7Breturn%3B%7D`. |
-`singles` <?[Array][array-mdn-url]<[string][string-mdn-url]>> A list of keys that need to be decoded as single string value instead of an array of values.
75
-
-`smart` <?[boolean][boolean-mdn-url]> Defaults to true. The decoder will assume all URL search params to be an array of values. With smart mode enabled, it will not force a single-value search param into an array.
76
-
- returns: <[object][object-mdn-url]> An object of decoded URL search params from a given string.
181
+
-`replacer` <?[Function][function-mdn-url]> Optional replacer function that allows you to alter the final parsed value.
182
+
-`firstRawValue` <[Array][array-mdn-url]<[string][string-mdn-url]>> This returns an array of values of the first key-value pair of a given key, e.g. *`a=a&a=b` will return `{ a: ['a'] }`*.
183
+
-`key` <[string][string-mdn-url]> Parameter name.
184
+
-`rawValue` <[Array][array-mdn-url]<[string][string-mdn-url]>> This returns an array of values from all key-value pairs of the same key, e.g. *`a=a&a=b` will return `{ a: ['a', 'b'] }`*.
185
+
-`value` <[string][string-mdn-url] | [Array][array-mdn-url]<[string][string-mdn-url]>> This returns the best value of a given parameter key which is heuristically determined by the library, e.g. *`a=a,b&b=a&a=c` will return `{ a: ['a', 'b', 'c'] }` (an array of values) and `b='a'` (single value)*.
186
+
- returns: <[Object][object-mdn-url]> An object of decoded URL search params from a given string.
77
187
78
-
This method decodes/ parses a string value into an object.
188
+
This method decodes/ parses a string value into an object. By default, [URLSearchParams.prototype.getAll] is used to retrieve the values from all key-value pairs of the same name, e.g. *`a=a&a=b` will return `{ a: ['a', 'b'] }`*. As you can see, this approach will be able to get all param values when you define multiple pairs of the same key. However, there is a downside which is when you have just **1** key-value pair and you expect it to be a single-value param, say `a=a&b=b`, will give `{ a: ['a'], b: ['b'] }`. To avoid any confusion, the library automatically parses such single-value param into a single value instead, e.g. *`a=a&b=b` will always give `{ a: 'a', b: 'b' }`*.
79
189
80
-
### stringify(value)
190
+
Under some circumstances, you might want it to behave differently. For that you can alter the outcome with an [optional `replacer` function][optional-replacer-function-url].
191
+
192
+
### stringify(input[, replacer])
81
193
82
194
-`value` <`unknown`> Any value of unknown type. It accepts any JavaScript primitives and objects.
83
-
- returns: <[string][string-mdn-url]> A string of encoded URL search params from a given input.
195
+
-`replacer` <?[Function][function-mdn-url]> Optional replacer function that allows you to alter the final stringified value.
196
+
-`flattenedKey` <[string][string-mdn-url]> Flattened key, e.g. *`{ a: { b: { c: 'a' } } }`'s key will be flattened to `a.b.c`*.
- returns: <[string][string-mdn-url]> A string of encoded URL search params from a given object.
201
+
202
+
This method encodes/ stringifies an object into a string. When a raw value is nullish, it will be omitted in the stringified output, e.g. *`{ a: 'a', b: null, c: undefined }` will return `a=a` as `null` and `undefined` are nullish values*.
84
203
85
-
This method encodes/ stringifies an input into a string.
204
+
If you want to include nullish values in the stringified output, you can override that with an [optional `replacer` function][optional-replacer-function-url].
86
205
87
206
## Contributing
88
207
@@ -98,17 +217,22 @@ Please note that this project is released with a [Contributor Code of Conduct][c
0 commit comments