Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow strings to be split into arrays if desired #233

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ assert.deepEqual(detectedAsIso8859_1, { a: '☺' });
It also works when the charset has been detected in `charsetSentinel`
mode.

### Overriding how values are parsed

**qs** can take an optional `valueParser` function as an option - this will parse the value before it is assigned

signature: Function that takes key and val, and returns the desired parsed value. in this example, Key would be `a[]` and value would be `b,c`. the return value is an array of `['b', 'c']`

```javascript
var parseCommaDelimitedString = function (key, val) {
var brackets = /(\[[^[\]]*])/;
var returnVal = val;

if (val !== null
&& brackets.test(key)
&& typeof val === 'string'
&& val.indexOf(',') !== -1)
{
returnVal = val.split(',');
}
return returnVal;
};
var customParsedObject = qs.parse('a[]=b,c', { valueParser: parseCommaDelimitedString })
assert.deepEqual(customParsedObject, { a: ['b', 'c'] })
```

### Parsing Arrays

**qs** can also parse arrays using a similar `[]` notation:
Expand Down
11 changes: 9 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ var parseValues = function parseQueryStringValues(str, options) {
val = interpretNumericEntities(val);
}

if (val && options.comma && val.indexOf(',') > -1) {
if (options.valueParser !== null) {
val = options.valueParser(key, val, options);
} else if (val && options.comma && val.indexOf(',') > -1) {
val = val.split(',');
}

Expand Down Expand Up @@ -200,6 +202,10 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
}
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;

if (opts.valueParser !== null && opts.valueParser !== undefined && typeof opts.valueParser !== 'function') {
throw new TypeError('valueParser has to be a function.');
}

return {
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
Expand All @@ -216,7 +222,8 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
parseArrays: opts.parseArrays !== false,
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,
valueParser: valueParser
};
};

Expand Down
25 changes: 25 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ test('parse()', function (t) {
st.end();
});

t.test('parses a value with the supplied function', function (st) {
st.deepEqual(qs.parse('a[]=b,c', { valueParser: function (key, val) {
var brackets = /(\[[^[\]]*])/;
var returnVal = val;

if (
val !== null
&& brackets.test(key)
&& typeof val === 'string'
&& val.indexOf(',') !== -1
) {
returnVal = val.split(',');
}
return returnVal;
} }), { a: ['b', 'c'] });
st.end();
});

t.test('throws error with badly crafted valueParser', function (st) {
st['throws'](function () {
qs.parse({}, { valueParser: 'notAFunction' });
}, new TypeError('valueParser has to be a function.'));
st.end();
});

t.test('parses a mix of simple and explicit arrays', function (st) {
st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
Expand Down