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

Improved checkJS #285

Open
wants to merge 3 commits into
base: ts
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions lib/formats.d.ts

This file was deleted.

19 changes: 15 additions & 4 deletions lib/formats.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
'use strict';

/** @type {function(string | RegExp, string): string} */
var replace = String.prototype.replace;
var percentTwenties = /%20/g;

/** @type {import('./qs').FormatsExport} */
var formatsExport = {
/** @typedef {'RFC1738' | 'RFC3986'} Format */
/** @callback Formatter
*
* @param {string | Buffer} value
* @returns {string}
*/

/** @type {{
* default: Format,
* formatters: { [s: string]: Formatter },
* RFC1738: 'RFC1738',
* RFC3986: 'RFC3986',
* }} */
module.exports = {
'default': 'RFC3986',

formatters: {
Expand All @@ -19,5 +32,3 @@ var formatsExport = {
RFC1738: 'RFC1738',
RFC3986: 'RFC3986'
};

module.exports = formatsExport;
9 changes: 0 additions & 9 deletions lib/index.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions lib/parse.d.ts

This file was deleted.

52 changes: 40 additions & 12 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@ var utils = require('./utils');

var has = Object.prototype.hasOwnProperty;

/** @typedef {import('./qs').ParseOptionsInternal} ParseOptionsInternal */
/** @typedef {import('./qs').ParseOptions} ParseOptions */
/** @typedef {import('./utils').UtilOptions} UtilOptions */
/** @typedef {import('./utils').Decoder} Decoder */

/** @callback InterpretNumericEntities
*
* @param {string} str
* @returns str
*/
/** @typedef ParseOptionsInternalType
*
* @property {boolean} allowPrototypes
* @property {number} arrayLimit
* @property {string | RegExp} delimiter
* @property {boolean} ignoreQueryPrefix
* @property {boolean | InterpretNumericEntities} interpretNumericEntities
* @property {number} parameterLimit
* @property {boolean} strictNullHandling
* @property {boolean} charsetSentinel
* @property {Decoder} decoder
* @property {number} depth
* @property {boolean} parseArrays
*/
/** @typedef {UtilOptions & ParseOptionsInternalType} ParseOptionsInternal */
/** @typedef {Partial<ParseOptionsInternal> & { decoder?: Decoder }} ParseOptions */

/** @type {ParseOptions & Pick<ParseOptionsInternal, "decoder">} */
var defaults = {
Expand All @@ -25,7 +47,7 @@ var defaults = {
strictNullHandling: false
};

/** @type {import('./qs').InterpretNumericEntities} */
/** @type {InterpretNumericEntities} */
var interpretNumericEntities = function (str) {
return str.replace(
/&#(\d+);/g,
Expand Down Expand Up @@ -53,7 +75,7 @@ var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓')
/**
* @param {string} str
* @param {ParseOptionsInternal} options
* @returns {object | Array<*>}
* @returns {NonPrimitive | Array<*>}
*/
var parseValues = function parseQueryStringValues(str, options) {
var obj = {};
Expand Down Expand Up @@ -112,7 +134,7 @@ var parseValues = function parseQueryStringValues(str, options) {

/**
* @param {string[]} chain
* @param {object=} val
* @param {NonPrimitive=} val
* @param {ParseOptionsInternal} options
* @returns
*/
Expand All @@ -124,7 +146,7 @@ var parseObject = function (chain, val, options) {
var root = chain[i];

if (root === '[]' && options.parseArrays) {
obj = [].concat(leaf);
obj = /** @type {*} */([]).concat(leaf);
} else {
obj = options.plainObjects ? Object.create(null) : {};
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
Expand Down Expand Up @@ -153,9 +175,9 @@ var parseObject = function (chain, val, options) {

/**
* @param {string=} givenKey
* @param {object=} val
* @param {NonPrimitive=} val
* @param {ParseOptionsInternal} options
* @returns {object}
* @returns {NonPrimitive | undefined}
*/
var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
if (!givenKey) {
Expand Down Expand Up @@ -211,8 +233,13 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
return parseObject(keys, val, options);
};

/** @type {import('./qs').Parse} */
/**
* @param {NonPrimitive | string | Nullish} str
* @param {ParseOptions} [opts]
* @returns {NonPrimitive | Array<Value>}
*/
module.exports = function (str, opts) {
/** @type {ParseOptions} */
var options = opts ? utils.assign({}, opts) : {};

if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
Expand Down Expand Up @@ -242,16 +269,17 @@ module.exports = function (str, opts) {
return options.plainObjects ? Object.create(null) : {};
}

var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
var internalOptions = /** @type {ParseOptionsInternal} */(options);
var tempObj = typeof str === 'string' ? parseValues(str, internalOptions) : str;
var obj = options.plainObjects ? Object.create(null) : {};

// Iterate over the keys and setup the new object

var keys = Object.keys(tempObj);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var newObj = parseKeys(key, tempObj[key], options);
obj = utils.merge(obj, newObj, options);
var newObj = parseKeys(key, tempObj[key], internalOptions);
obj = utils.merge(obj, newObj, internalOptions);
}

return utils.compact(obj);
Expand Down
153 changes: 2 additions & 151 deletions lib/qs.d.ts
Original file line number Diff line number Diff line change
@@ -1,157 +1,8 @@
export type Format = 'RFC1738' | 'RFC3986';

export type Formatter = (value: string | Buffer) => string;

type Formats = {
[format in Format]: format
};

type Formatters = {
[format in Format]: Formatter
};

export type FormatsExport = {
default: Format,
formatters: Formatters,
} & Formats;

export type ArrayFormat = 'brackets' | 'indices' | 'repeat';

export type arrayPrefixGenerator = (prefix: string, key?: string) => string;

export type arrayPrefixGenerators = {
[format in ArrayFormat]: arrayPrefixGenerator
}

export type Filter<T> = (prefix: string, obj: T) => T;

export type Comparator = (a: Value, b: Value) => number;

export type Charset = 'iso-8859-1' | 'utf-8';

export type Decoder = (
str: string,
defaultDecoder: Decoder,
charset: Charset
) => string;

export type Encoder = (
str: string | Buffer,
defaultEncoder: Encoder,
charset?: Charset
) => string | Buffer;

export type UtilOptions = {
allowDots: boolean,
charset: Charset,
plainObjects: boolean,
};

export type UtilOptionsInternal = UtilOptions & {
allowPrototypes: boolean,
arrayLimit: number,
};

type Nullish = null | undefined;
type NonNullishPrimitive = boolean | number | string | symbol;
type ObjectCoercible = {} | NonNullishPrimitive;
type ObjectIndexable = { [key: string]: Value } | NonNullishPrimitive;
type ObjectCoercible = object | NonNullishPrimitive;
type Primitive = Nullish | NonNullishPrimitive;
type Concatable<T> = T | T[];
type Value = Concatable<object | Primitive>;

export type arrayToObject = (source: Value[], options?: UtilOptionsInternal) => object;
export type assign = (target: object, source: object) => /* target */ object;
type queueObject = {
[key: string]: Concatable<object>,
};
type queueItem = {
obj: queueObject,
prop: keyof queueObject,
};
export type compactQueue = (queue: queueItem[]) => void;
export type compact = (value: ObjectCoercible) => ObjectCoercible;
export type isBuffer = (obj: (Buffer | Value) & ({ constructor?: typeof Buffer })) => boolean;
export type isRegExp = (obj: RegExp | Value) => boolean;
export type merge = (
target: ObjectIndexable[] | Object,
source?: ObjectCoercible,
options?: UtilOptionsInternal,
) => /* target | */ object;

export type utils = {
arrayToObject: arrayToObject,
assign: assign,
compact: compact,
decode: Decoder,
encode: Encoder,
isBuffer: isBuffer,
isRegExp: isRegExp,
merge: merge,
}

export type DateSerializer = (date: Date) => string | number;

export type StringifyOptionsInternal = UtilOptions & {
arrayFormat: ArrayFormat,
charsetSentinel: boolean,
delimiter: string | RegExp,
encode: boolean,
encoder: Encoder,
encodeValuesOnly: boolean,
filter: Filter<object & ObjectIndexable> | Array<string | number>
format: Format,
serializeDate: DateSerializer,
skipNulls: boolean,
sort: Comparator,
strictNullHandling: boolean,
};

export type StringifyOptions = Partial<StringifyOptionsInternal> & {
encoder?: Encoder | null,
addQueryPrefix?: boolean,
indices?: boolean,
};

export type Stringify = (object: ObjectIndexable | Nullish, opts?: StringifyOptions) => string;

export type StringifyInternal = (
object: Value,
prefix: string,
generateArrayPrefix: arrayPrefixGenerator,
strictNullHandling: boolean,
skipNulls: boolean,
encoder: Encoder,
filter: Filter<Value>,
sort: Comparator,
allowDots: boolean,
serializeDate: DateSerializer,
formatter: Formatter,
encodeValuesOnly: boolean,
charset: Charset,
) => Concatable<string | Buffer>;

export type InterpretNumericEntities = (str: string) => string;

export type ParseOptionsInternal = UtilOptions & {
allowPrototypes: boolean,
arrayLimit: number,
charsetSentinel: boolean,
decoder: Decoder,
delimiter: string | RegExp,
depth: number,
ignoreQueryPrefix: boolean,
interpretNumericEntities: InterpretNumericEntities | undefined,
parameterLimit: number,
parseArrays: boolean,
strictNullHandling: boolean,
};

export type ParseOptions = Partial<ParseOptionsInternal> & {
decoder?: Decoder,
};

export type Parse = (
str: object | string | Nullish,
opts?: ParseOptions,
) => object | Array<Value>;
type NonPrimitive = object;
3 changes: 0 additions & 3 deletions lib/stringify.d.ts

This file was deleted.

Loading