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

fix issue 210: custom values formatting schema #249

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type CustomValueParser = (value: string) => any;
goodwin64 marked this conversation as resolved.
Show resolved Hide resolved

export interface ParseOptions {
/**
Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
Expand Down Expand Up @@ -121,6 +123,23 @@ export interface ParseOptions {
```
*/
readonly parseBooleans?: boolean;

/**
Parse the value as a boolean type instead of string type if it's a boolean.

@default false

@example
```
import queryString = require('query-string');

queryString.parse('foo=true', {parseBooleans: true});
//=> {foo: true}
```
*/
readonly types?: {
[key: string]: 'string' | 'number' | CustomValueParser;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[key: string]: 'string' | 'number' | CustomValueParser;
[type: string]: 'string' | 'number' | CustomValueParser;

};
}

export interface ParsedQuery<T = string> {
Expand Down
51 changes: 41 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,90 @@ function encoderForArrayFormat(options) {
}
}

function createCustomValueParser(typedSchemaFormatter) {
if (typedSchemaFormatter === 'string') {
return String;
}

if (typedSchemaFormatter === 'number') {
return Number;
}

if (typeof typedSchemaFormatter === 'function') {
return typedSchemaFormatter;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should throw if the typedSchemaFormatter type is not a supported type.

return val => val;
}

function getPreparedValue(typesSchema, key, value) {
const customValueParser = createCustomValueParser(typesSchema[key]);
return customValueParser(value);
}

function parserForArrayFormat(options) {
let result;

switch (options.arrayFormat) {
case 'index':
return (key, value, accumulator) => {
const preparedValue = getPreparedValue(options.types, key, value);

result = /\[(\d*)\]$/.exec(key);

key = key.replace(/\[\d*\]$/, '');

if (!result) {
accumulator[key] = value;
accumulator[key] = preparedValue;
return;
}

if (accumulator[key] === undefined) {
accumulator[key] = {};
}

accumulator[key][result[1]] = value;
accumulator[key][result[1]] = preparedValue;
};

case 'bracket':
return (key, value, accumulator) => {
const preparedValue = getPreparedValue(options.types, key, value);

result = /(\[\])$/.exec(key);
key = key.replace(/\[\]$/, '');

if (!result) {
accumulator[key] = value;
accumulator[key] = preparedValue;
return;
}

if (accumulator[key] === undefined) {
accumulator[key] = [value];
accumulator[key] = [preparedValue];
return;
}

accumulator[key] = [].concat(accumulator[key], value);
accumulator[key] = [].concat(accumulator[key], preparedValue);
};

case 'comma':
case 'separator':
return (key, value, accumulator) => {
const isArray = typeof value === 'string' && value.split('').indexOf(options.arrayFormatSeparator) > -1;
const newValue = isArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
accumulator[key] = newValue;
const preparedValue = getPreparedValue(options.types, key, newValue);
accumulator[key] = preparedValue;
};

default:
return (key, value, accumulator) => {
const preparedValue = getPreparedValue(options.types, key, value);

if (accumulator[key] === undefined) {
accumulator[key] = value;
accumulator[key] = preparedValue;
return;
}

accumulator[key] = [].concat(accumulator[key], value);
accumulator[key] = [].concat(accumulator[key], preparedValue);
};
}
}
Expand Down Expand Up @@ -206,7 +234,8 @@ function parse(input, options) {
arrayFormat: 'none',
arrayFormatSeparator: ',',
parseNumbers: false,
parseBooleans: false
parseBooleans: false,
types: Object.create(null)
}, options);

validateArrayFormatSeparator(options.arrayFormatSeparator);
Expand Down Expand Up @@ -250,7 +279,9 @@ function parse(input, options) {
return ret;
}

return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much logic in the return statement makes it hard to understand what's actually returned from the function.

const retKeys = options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort);

return retKeys.reduce((result, key) => {
const value = ret[key];
if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
// Sort object keys, not values
Expand Down