Comparing version 1.1.4 to 1.1.5
@@ -233,2 +233,54 @@ 'use strict'; | ||
const throttle = (delay, fn, _isDebounce) => { | ||
let lastExec = 0; | ||
let cancelled = false; | ||
let timer = null; | ||
const clear = () => (timer = null); | ||
function wrapper(...args) { | ||
if (cancelled) return; | ||
const cur = Date.now(); | ||
const elapsed = cur - lastExec; | ||
const exec = (cur) => { | ||
lastExec = cur || Date.now(); | ||
fn.apply(this, args); | ||
}; | ||
// Default begin call | ||
if (_isDebounce && !timer) { | ||
exec(cur); | ||
} | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
if (!_isDebounce && elapsed > delay) { | ||
exec(cur); | ||
} else { | ||
timer = setTimeout( | ||
_isDebounce ? clear : exec, | ||
_isDebounce ? delay : delay - elapsed, | ||
); | ||
} | ||
} | ||
wrapper.cancel = () => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
clear(); | ||
cancelled = true; | ||
}; | ||
return wrapper; | ||
}; | ||
const debounce = (delay, fn) => throttle(delay, fn, true); | ||
const defaultStringifyOptions = { | ||
encode: true, | ||
addQueryPrefix: true, | ||
commaRoundTrip: true, | ||
arrayFormat: 'indices', | ||
}; | ||
const defaultParseOptions = { | ||
depth: 5, | ||
arrayLimit: 20, | ||
comma: true, | ||
allowSparse: true, | ||
}; | ||
const decode = (s) => { | ||
@@ -242,2 +294,139 @@ s = s.replace(/\+/g, ' '); | ||
}; | ||
let hexTable; | ||
const encode = (str) => { | ||
// This code was originally written by Brian White (mscdex) for the io.js core querystring library. | ||
// It has been adapted here for stricter adherence to RFC 3986 | ||
if (str.length === 0) return str; | ||
let s = str; | ||
const limit = 1024; | ||
if (typeof str === 'symbol') { | ||
s = Symbol.prototype.toString.call(str); | ||
} else if (typeof str !== 'string') { | ||
s = String(str); | ||
} | ||
if (!hexTable) { | ||
hexTable = []; | ||
for (let i = 0; i < 256; i++) { | ||
hexTable.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase()); | ||
} | ||
} | ||
let out = ''; | ||
for (let j = 0; j < s.length; j += limit) { | ||
const arr = []; | ||
const segment = s.length >= limit ? s.slice(j, j + limit) : s; | ||
for (let i = 0; i < segment.length; i++) { | ||
let c = segment.charCodeAt(i); | ||
if ( | ||
c === 0x2d || // - | ||
c === 0x2e || // . | ||
c === 0x5f || // _ | ||
c === 0x7e || // ~ | ||
(c >= 0x30 && c <= 0x39) || // 0-9 | ||
(c >= 0x41 && c <= 0x5a) || // a-z | ||
(c >= 0x61 && c <= 0x7a) // A-Z | ||
) { | ||
arr[arr.length] = segment.charAt(i); | ||
continue; | ||
} | ||
if (c < 0x80) { | ||
arr[arr.length] = hexTable[c]; | ||
continue; | ||
} | ||
if (c < 0x800) { | ||
arr[arr.length] = | ||
hexTable[0xc0 | (c >> 6)] + hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
if (c < 0xd800 || c >= 0xe000) { | ||
arr[arr.length] = | ||
hexTable[0xe0 | (c >> 12)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
i += 1; | ||
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff)); | ||
arr[arr.length] = | ||
hexTable[0xf0 | (c >> 18)] + | ||
hexTable[0x80 | ((c >> 12) & 0x3f)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
} | ||
out += arr.join(''); | ||
} | ||
return out; | ||
}; | ||
const pushToArray = (arr, valueOrArray) => { | ||
arr.push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]); | ||
}; | ||
const arrayPrefixFns = { | ||
comma: (p) => p, | ||
repeat: (p) => p, | ||
brackets: (p) => `${p}[]`, | ||
indices: (p, k) => `${p}[${k}]`, | ||
}; | ||
const sentinel = {}; | ||
const stringify = (object, prefix, commaRoundTrip, sideChannel, options) => { | ||
let step = 0; | ||
let obj = object; | ||
let tmpSc = sideChannel; | ||
let findFlag = false; | ||
while ((tmpSc = tmpSc.get(sentinel)) !== void undefined && !findFlag) { | ||
const pos = tmpSc.get(object); | ||
step += 1; | ||
if (typeof pos !== 'undefined') { | ||
if (pos === step) { | ||
throw new RangeError('Cyclic object value'); | ||
} | ||
findFlag = true; // Break while | ||
} | ||
if (typeof tmpSc.get(sentinel) === 'undefined') { | ||
step = 0; | ||
} | ||
} | ||
if (isDate(obj)) { | ||
obj = obj.toISOString(); | ||
} else if (options.arrayFormat === 'comma' && isArray(obj)) { | ||
obj = obj.map((val) => (isDate(val) ? val.toISOString() : val)); | ||
} | ||
if (obj === null) { | ||
obj = ''; | ||
} | ||
if ((!isNil(obj) && isPrimitiveValue(obj)) || isBuffer(obj)) { | ||
return !options.encode | ||
? [`${prefix}=${obj}`] | ||
: [`${encode(prefix)}=${encode(obj)}`]; | ||
} | ||
const values = []; | ||
if (typeof obj === 'undefined') return values; | ||
let objKeys; | ||
if (options.arrayFormat === 'comma' && isArray(obj)) { | ||
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }]; | ||
} else { | ||
objKeys = Object.keys(obj); | ||
} | ||
const adjustedPrefix = | ||
commaRoundTrip && isArray(obj) && obj.length === 1 ? `${prefix}[]` : prefix; | ||
for (let j = 0; j < objKeys.length; j++) { | ||
const key = objKeys[j]; | ||
const value = | ||
typeof key === 'object' && typeof key.value !== 'undefined' | ||
? key.value | ||
: obj[key]; | ||
let keyPrefix; | ||
if (isArray(obj)) { | ||
keyPrefix = arrayPrefixFns[options.arrayFormat](adjustedPrefix, key); | ||
} else { | ||
keyPrefix = `${adjustedPrefix}[${key}]`; | ||
} | ||
sideChannel.set(object, step); | ||
const valueSideChannel = new Map(); | ||
valueSideChannel.set(sentinel, sideChannel); | ||
pushToArray( | ||
values, | ||
stringify(value, keyPrefix, commaRoundTrip, valueSideChannel, options), | ||
); | ||
} | ||
return values; | ||
}; | ||
const compact = (value) => { | ||
@@ -403,12 +592,6 @@ const refs = []; | ||
}; | ||
const defaultOptions = { | ||
depth: 5, | ||
arrayLimit: 20, | ||
comma: true, | ||
allowSparse: true, | ||
}; | ||
// https://github.com/ljharb/qs/blob/main/lib/parse.js | ||
const qsParse = (s, options) => { | ||
if (!s || typeof s !== 'string') return {}; | ||
options = Object.assign({}, defaultOptions, options); | ||
options = Object.assign({}, defaultParseOptions, options); | ||
let obj = {}; | ||
@@ -424,43 +607,22 @@ const tempObj = parse(s, options); | ||
// https://github.com/ljharb/qs/blob/main/lib/stringify.js | ||
const qsStringify = () => {}; | ||
const throttle = (delay, fn, _isDebounce) => { | ||
let lastExec = 0; | ||
let cancelled = false; | ||
let timer = null; | ||
const clear = () => (timer = null); | ||
function wrapper(...args) { | ||
if (cancelled) return; | ||
const cur = Date.now(); | ||
const elapsed = cur - lastExec; | ||
const exec = (cur) => { | ||
lastExec = cur || Date.now(); | ||
fn.apply(this, args); | ||
}; | ||
// Default begin call | ||
if (_isDebounce && !timer) { | ||
exec(cur); | ||
} | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
if (!_isDebounce && elapsed > delay) { | ||
exec(cur); | ||
} else { | ||
timer = setTimeout( | ||
_isDebounce ? clear : exec, | ||
_isDebounce ? delay : delay - elapsed, | ||
); | ||
} | ||
const qsStringify = (obj, options) => { | ||
if (!isObject(obj)) return ''; | ||
options = Object.assign({}, defaultStringifyOptions, options); | ||
const keys = []; | ||
const objKeys = Object.keys(obj); | ||
const sideChannel = new WeakMap(); | ||
const commaRoundTrip = Boolean( | ||
options.arrayFormat === 'comma' && options.commaRoundTrip, | ||
); | ||
for (let i = 0; i < objKeys.length; ++i) { | ||
const key = objKeys[i]; | ||
pushToArray( | ||
keys, | ||
stringify(obj[key], key, commaRoundTrip, sideChannel, options), | ||
); | ||
} | ||
wrapper.cancel = () => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
clear(); | ||
cancelled = true; | ||
}; | ||
return wrapper; | ||
const res = keys.join('&'); | ||
if (res.length === 0) return ''; | ||
return options.addQueryPrefix ? `?${res}` : res; | ||
}; | ||
const debounce = (delay, fn) => throttle(delay, fn, true); | ||
@@ -514,2 +676,8 @@ const noop = () => {}; | ||
const isPromise = (v) => isObject(v) && typeof v.then === 'function'; | ||
const isBuffer = (v) => { | ||
if (!isObject(v)) return false; | ||
return Boolean( | ||
v.constructor && v.constructor.isBuffer && v.constructor.isBuffer(v), | ||
); | ||
}; | ||
const isInBounds = ([a, b], v) => { | ||
@@ -525,3 +693,3 @@ if (v === a || v === b) return true; | ||
}; | ||
const isNativeValue = (v) => { | ||
const isPrimitiveValue = (v) => { | ||
return ( | ||
@@ -746,2 +914,3 @@ typeof v === 'number' || | ||
exports.isBrowser = isBrowser; | ||
exports.isBuffer = isBuffer; | ||
exports.isDate = isDate; | ||
@@ -751,6 +920,6 @@ exports.isEmptyObject = isEmptyObject; | ||
exports.isMap = isMap; | ||
exports.isNativeValue = isNativeValue; | ||
exports.isNil = isNil; | ||
exports.isObject = isObject; | ||
exports.isPlainObject = isPlainObject; | ||
exports.isPrimitiveValue = isPrimitiveValue; | ||
exports.isPromise = isPromise; | ||
@@ -757,0 +926,0 @@ exports.isRegExp = isRegExp; |
@@ -229,2 +229,54 @@ export { Queue } from 'small-queue'; | ||
const throttle = (delay, fn, _isDebounce) => { | ||
let lastExec = 0; | ||
let cancelled = false; | ||
let timer = null; | ||
const clear = () => (timer = null); | ||
function wrapper(...args) { | ||
if (cancelled) return; | ||
const cur = Date.now(); | ||
const elapsed = cur - lastExec; | ||
const exec = (cur) => { | ||
lastExec = cur || Date.now(); | ||
fn.apply(this, args); | ||
}; | ||
// Default begin call | ||
if (_isDebounce && !timer) { | ||
exec(cur); | ||
} | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
if (!_isDebounce && elapsed > delay) { | ||
exec(cur); | ||
} else { | ||
timer = setTimeout( | ||
_isDebounce ? clear : exec, | ||
_isDebounce ? delay : delay - elapsed, | ||
); | ||
} | ||
} | ||
wrapper.cancel = () => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
clear(); | ||
cancelled = true; | ||
}; | ||
return wrapper; | ||
}; | ||
const debounce = (delay, fn) => throttle(delay, fn, true); | ||
const defaultStringifyOptions = { | ||
encode: true, | ||
addQueryPrefix: true, | ||
commaRoundTrip: true, | ||
arrayFormat: 'indices', | ||
}; | ||
const defaultParseOptions = { | ||
depth: 5, | ||
arrayLimit: 20, | ||
comma: true, | ||
allowSparse: true, | ||
}; | ||
const decode = (s) => { | ||
@@ -238,2 +290,139 @@ s = s.replace(/\+/g, ' '); | ||
}; | ||
let hexTable; | ||
const encode = (str) => { | ||
// This code was originally written by Brian White (mscdex) for the io.js core querystring library. | ||
// It has been adapted here for stricter adherence to RFC 3986 | ||
if (str.length === 0) return str; | ||
let s = str; | ||
const limit = 1024; | ||
if (typeof str === 'symbol') { | ||
s = Symbol.prototype.toString.call(str); | ||
} else if (typeof str !== 'string') { | ||
s = String(str); | ||
} | ||
if (!hexTable) { | ||
hexTable = []; | ||
for (let i = 0; i < 256; i++) { | ||
hexTable.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase()); | ||
} | ||
} | ||
let out = ''; | ||
for (let j = 0; j < s.length; j += limit) { | ||
const arr = []; | ||
const segment = s.length >= limit ? s.slice(j, j + limit) : s; | ||
for (let i = 0; i < segment.length; i++) { | ||
let c = segment.charCodeAt(i); | ||
if ( | ||
c === 0x2d || // - | ||
c === 0x2e || // . | ||
c === 0x5f || // _ | ||
c === 0x7e || // ~ | ||
(c >= 0x30 && c <= 0x39) || // 0-9 | ||
(c >= 0x41 && c <= 0x5a) || // a-z | ||
(c >= 0x61 && c <= 0x7a) // A-Z | ||
) { | ||
arr[arr.length] = segment.charAt(i); | ||
continue; | ||
} | ||
if (c < 0x80) { | ||
arr[arr.length] = hexTable[c]; | ||
continue; | ||
} | ||
if (c < 0x800) { | ||
arr[arr.length] = | ||
hexTable[0xc0 | (c >> 6)] + hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
if (c < 0xd800 || c >= 0xe000) { | ||
arr[arr.length] = | ||
hexTable[0xe0 | (c >> 12)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
i += 1; | ||
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff)); | ||
arr[arr.length] = | ||
hexTable[0xf0 | (c >> 18)] + | ||
hexTable[0x80 | ((c >> 12) & 0x3f)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
} | ||
out += arr.join(''); | ||
} | ||
return out; | ||
}; | ||
const pushToArray = (arr, valueOrArray) => { | ||
arr.push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]); | ||
}; | ||
const arrayPrefixFns = { | ||
comma: (p) => p, | ||
repeat: (p) => p, | ||
brackets: (p) => `${p}[]`, | ||
indices: (p, k) => `${p}[${k}]`, | ||
}; | ||
const sentinel = {}; | ||
const stringify = (object, prefix, commaRoundTrip, sideChannel, options) => { | ||
let step = 0; | ||
let obj = object; | ||
let tmpSc = sideChannel; | ||
let findFlag = false; | ||
while ((tmpSc = tmpSc.get(sentinel)) !== void undefined && !findFlag) { | ||
const pos = tmpSc.get(object); | ||
step += 1; | ||
if (typeof pos !== 'undefined') { | ||
if (pos === step) { | ||
throw new RangeError('Cyclic object value'); | ||
} | ||
findFlag = true; // Break while | ||
} | ||
if (typeof tmpSc.get(sentinel) === 'undefined') { | ||
step = 0; | ||
} | ||
} | ||
if (isDate(obj)) { | ||
obj = obj.toISOString(); | ||
} else if (options.arrayFormat === 'comma' && isArray(obj)) { | ||
obj = obj.map((val) => (isDate(val) ? val.toISOString() : val)); | ||
} | ||
if (obj === null) { | ||
obj = ''; | ||
} | ||
if ((!isNil(obj) && isPrimitiveValue(obj)) || isBuffer(obj)) { | ||
return !options.encode | ||
? [`${prefix}=${obj}`] | ||
: [`${encode(prefix)}=${encode(obj)}`]; | ||
} | ||
const values = []; | ||
if (typeof obj === 'undefined') return values; | ||
let objKeys; | ||
if (options.arrayFormat === 'comma' && isArray(obj)) { | ||
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }]; | ||
} else { | ||
objKeys = Object.keys(obj); | ||
} | ||
const adjustedPrefix = | ||
commaRoundTrip && isArray(obj) && obj.length === 1 ? `${prefix}[]` : prefix; | ||
for (let j = 0; j < objKeys.length; j++) { | ||
const key = objKeys[j]; | ||
const value = | ||
typeof key === 'object' && typeof key.value !== 'undefined' | ||
? key.value | ||
: obj[key]; | ||
let keyPrefix; | ||
if (isArray(obj)) { | ||
keyPrefix = arrayPrefixFns[options.arrayFormat](adjustedPrefix, key); | ||
} else { | ||
keyPrefix = `${adjustedPrefix}[${key}]`; | ||
} | ||
sideChannel.set(object, step); | ||
const valueSideChannel = new Map(); | ||
valueSideChannel.set(sentinel, sideChannel); | ||
pushToArray( | ||
values, | ||
stringify(value, keyPrefix, commaRoundTrip, valueSideChannel, options), | ||
); | ||
} | ||
return values; | ||
}; | ||
const compact = (value) => { | ||
@@ -399,12 +588,6 @@ const refs = []; | ||
}; | ||
const defaultOptions = { | ||
depth: 5, | ||
arrayLimit: 20, | ||
comma: true, | ||
allowSparse: true, | ||
}; | ||
// https://github.com/ljharb/qs/blob/main/lib/parse.js | ||
const qsParse = (s, options) => { | ||
if (!s || typeof s !== 'string') return {}; | ||
options = Object.assign({}, defaultOptions, options); | ||
options = Object.assign({}, defaultParseOptions, options); | ||
let obj = {}; | ||
@@ -420,43 +603,22 @@ const tempObj = parse(s, options); | ||
// https://github.com/ljharb/qs/blob/main/lib/stringify.js | ||
const qsStringify = () => {}; | ||
const throttle = (delay, fn, _isDebounce) => { | ||
let lastExec = 0; | ||
let cancelled = false; | ||
let timer = null; | ||
const clear = () => (timer = null); | ||
function wrapper(...args) { | ||
if (cancelled) return; | ||
const cur = Date.now(); | ||
const elapsed = cur - lastExec; | ||
const exec = (cur) => { | ||
lastExec = cur || Date.now(); | ||
fn.apply(this, args); | ||
}; | ||
// Default begin call | ||
if (_isDebounce && !timer) { | ||
exec(cur); | ||
} | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
if (!_isDebounce && elapsed > delay) { | ||
exec(cur); | ||
} else { | ||
timer = setTimeout( | ||
_isDebounce ? clear : exec, | ||
_isDebounce ? delay : delay - elapsed, | ||
); | ||
} | ||
const qsStringify = (obj, options) => { | ||
if (!isObject(obj)) return ''; | ||
options = Object.assign({}, defaultStringifyOptions, options); | ||
const keys = []; | ||
const objKeys = Object.keys(obj); | ||
const sideChannel = new WeakMap(); | ||
const commaRoundTrip = Boolean( | ||
options.arrayFormat === 'comma' && options.commaRoundTrip, | ||
); | ||
for (let i = 0; i < objKeys.length; ++i) { | ||
const key = objKeys[i]; | ||
pushToArray( | ||
keys, | ||
stringify(obj[key], key, commaRoundTrip, sideChannel, options), | ||
); | ||
} | ||
wrapper.cancel = () => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
clear(); | ||
cancelled = true; | ||
}; | ||
return wrapper; | ||
const res = keys.join('&'); | ||
if (res.length === 0) return ''; | ||
return options.addQueryPrefix ? `?${res}` : res; | ||
}; | ||
const debounce = (delay, fn) => throttle(delay, fn, true); | ||
@@ -510,2 +672,8 @@ const noop = () => {}; | ||
const isPromise = (v) => isObject(v) && typeof v.then === 'function'; | ||
const isBuffer = (v) => { | ||
if (!isObject(v)) return false; | ||
return Boolean( | ||
v.constructor && v.constructor.isBuffer && v.constructor.isBuffer(v), | ||
); | ||
}; | ||
const isInBounds = ([a, b], v) => { | ||
@@ -521,3 +689,3 @@ if (v === a || v === b) return true; | ||
}; | ||
const isNativeValue = (v) => { | ||
const isPrimitiveValue = (v) => { | ||
return ( | ||
@@ -742,2 +910,3 @@ typeof v === 'number' || | ||
isBrowser, | ||
isBuffer, | ||
isDate, | ||
@@ -747,6 +916,6 @@ isEmptyObject, | ||
isMap, | ||
isNativeValue, | ||
isNil, | ||
isObject, | ||
isPlainObject, | ||
isPrimitiveValue, | ||
isPromise, | ||
@@ -753,0 +922,0 @@ isRegExp, |
@@ -283,2 +283,54 @@ (function (global, factory) { | ||
const throttle = (delay, fn, _isDebounce) => { | ||
let lastExec = 0; | ||
let cancelled = false; | ||
let timer = null; | ||
const clear = () => (timer = null); | ||
function wrapper(...args) { | ||
if (cancelled) return; | ||
const cur = Date.now(); | ||
const elapsed = cur - lastExec; | ||
const exec = (cur) => { | ||
lastExec = cur || Date.now(); | ||
fn.apply(this, args); | ||
}; | ||
// Default begin call | ||
if (_isDebounce && !timer) { | ||
exec(cur); | ||
} | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
if (!_isDebounce && elapsed > delay) { | ||
exec(cur); | ||
} else { | ||
timer = setTimeout( | ||
_isDebounce ? clear : exec, | ||
_isDebounce ? delay : delay - elapsed, | ||
); | ||
} | ||
} | ||
wrapper.cancel = () => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
clear(); | ||
cancelled = true; | ||
}; | ||
return wrapper; | ||
}; | ||
const debounce = (delay, fn) => throttle(delay, fn, true); | ||
const defaultStringifyOptions = { | ||
encode: true, | ||
addQueryPrefix: true, | ||
commaRoundTrip: true, | ||
arrayFormat: 'indices', | ||
}; | ||
const defaultParseOptions = { | ||
depth: 5, | ||
arrayLimit: 20, | ||
comma: true, | ||
allowSparse: true, | ||
}; | ||
const decode = (s) => { | ||
@@ -292,2 +344,143 @@ s = s.replace(/\+/g, ' '); | ||
}; | ||
let hexTable; | ||
const encode = (str) => { | ||
// This code was originally written by Brian White (mscdex) for the io.js core querystring library. | ||
// It has been adapted here for stricter adherence to RFC 3986 | ||
if (str.length === 0) return str; | ||
let s = str; | ||
const limit = 1024; | ||
if (typeof str === 'symbol') { | ||
s = Symbol.prototype.toString.call(str); | ||
} else if (typeof str !== 'string') { | ||
s = String(str); | ||
} | ||
if (!hexTable) { | ||
hexTable = []; | ||
for (let i = 0; i < 256; i++) { | ||
hexTable.push( | ||
'%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase(), | ||
); | ||
} | ||
} | ||
let out = ''; | ||
for (let j = 0; j < s.length; j += limit) { | ||
const arr = []; | ||
const segment = s.length >= limit ? s.slice(j, j + limit) : s; | ||
for (let i = 0; i < segment.length; i++) { | ||
let c = segment.charCodeAt(i); | ||
if ( | ||
c === 0x2d || // - | ||
c === 0x2e || // . | ||
c === 0x5f || // _ | ||
c === 0x7e || // ~ | ||
(c >= 0x30 && c <= 0x39) || // 0-9 | ||
(c >= 0x41 && c <= 0x5a) || // a-z | ||
(c >= 0x61 && c <= 0x7a) // A-Z | ||
) { | ||
arr[arr.length] = segment.charAt(i); | ||
continue; | ||
} | ||
if (c < 0x80) { | ||
arr[arr.length] = hexTable[c]; | ||
continue; | ||
} | ||
if (c < 0x800) { | ||
arr[arr.length] = | ||
hexTable[0xc0 | (c >> 6)] + hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
if (c < 0xd800 || c >= 0xe000) { | ||
arr[arr.length] = | ||
hexTable[0xe0 | (c >> 12)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
i += 1; | ||
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff)); | ||
arr[arr.length] = | ||
hexTable[0xf0 | (c >> 18)] + | ||
hexTable[0x80 | ((c >> 12) & 0x3f)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
} | ||
out += arr.join(''); | ||
} | ||
return out; | ||
}; | ||
const pushToArray = (arr, valueOrArray) => { | ||
arr.push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]); | ||
}; | ||
const arrayPrefixFns = { | ||
comma: (p) => p, | ||
repeat: (p) => p, | ||
brackets: (p) => `${p}[]`, | ||
indices: (p, k) => `${p}[${k}]`, | ||
}; | ||
const sentinel = {}; | ||
const stringify = (object, prefix, commaRoundTrip, sideChannel, options) => { | ||
let step = 0; | ||
let obj = object; | ||
let tmpSc = sideChannel; | ||
let findFlag = false; | ||
while ((tmpSc = tmpSc.get(sentinel)) !== void undefined && !findFlag) { | ||
const pos = tmpSc.get(object); | ||
step += 1; | ||
if (typeof pos !== 'undefined') { | ||
if (pos === step) { | ||
throw new RangeError('Cyclic object value'); | ||
} | ||
findFlag = true; // Break while | ||
} | ||
if (typeof tmpSc.get(sentinel) === 'undefined') { | ||
step = 0; | ||
} | ||
} | ||
if (isDate(obj)) { | ||
obj = obj.toISOString(); | ||
} else if (options.arrayFormat === 'comma' && isArray(obj)) { | ||
obj = obj.map((val) => (isDate(val) ? val.toISOString() : val)); | ||
} | ||
if (obj === null) { | ||
obj = ''; | ||
} | ||
if ((!isNil(obj) && isPrimitiveValue(obj)) || isBuffer(obj)) { | ||
return !options.encode | ||
? [`${prefix}=${obj}`] | ||
: [`${encode(prefix)}=${encode(obj)}`]; | ||
} | ||
const values = []; | ||
if (typeof obj === 'undefined') return values; | ||
let objKeys; | ||
if (options.arrayFormat === 'comma' && isArray(obj)) { | ||
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }]; | ||
} else { | ||
objKeys = Object.keys(obj); | ||
} | ||
const adjustedPrefix = | ||
commaRoundTrip && isArray(obj) && obj.length === 1 | ||
? `${prefix}[]` | ||
: prefix; | ||
for (let j = 0; j < objKeys.length; j++) { | ||
const key = objKeys[j]; | ||
const value = | ||
typeof key === 'object' && typeof key.value !== 'undefined' | ||
? key.value | ||
: obj[key]; | ||
let keyPrefix; | ||
if (isArray(obj)) { | ||
keyPrefix = arrayPrefixFns[options.arrayFormat](adjustedPrefix, key); | ||
} else { | ||
keyPrefix = `${adjustedPrefix}[${key}]`; | ||
} | ||
sideChannel.set(object, step); | ||
const valueSideChannel = new Map(); | ||
valueSideChannel.set(sentinel, sideChannel); | ||
pushToArray( | ||
values, | ||
stringify(value, keyPrefix, commaRoundTrip, valueSideChannel, options), | ||
); | ||
} | ||
return values; | ||
}; | ||
const compact = (value) => { | ||
@@ -453,12 +646,6 @@ const refs = []; | ||
}; | ||
const defaultOptions = { | ||
depth: 5, | ||
arrayLimit: 20, | ||
comma: true, | ||
allowSparse: true, | ||
}; | ||
// https://github.com/ljharb/qs/blob/main/lib/parse.js | ||
const qsParse = (s, options) => { | ||
if (!s || typeof s !== 'string') return {}; | ||
options = Object.assign({}, defaultOptions, options); | ||
options = Object.assign({}, defaultParseOptions, options); | ||
let obj = {}; | ||
@@ -474,43 +661,22 @@ const tempObj = parse(s, options); | ||
// https://github.com/ljharb/qs/blob/main/lib/stringify.js | ||
const qsStringify = () => {}; | ||
const throttle = (delay, fn, _isDebounce) => { | ||
let lastExec = 0; | ||
let cancelled = false; | ||
let timer = null; | ||
const clear = () => (timer = null); | ||
function wrapper(...args) { | ||
if (cancelled) return; | ||
const cur = Date.now(); | ||
const elapsed = cur - lastExec; | ||
const exec = (cur) => { | ||
lastExec = cur || Date.now(); | ||
fn.apply(this, args); | ||
}; | ||
// Default begin call | ||
if (_isDebounce && !timer) { | ||
exec(cur); | ||
} | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
if (!_isDebounce && elapsed > delay) { | ||
exec(cur); | ||
} else { | ||
timer = setTimeout( | ||
_isDebounce ? clear : exec, | ||
_isDebounce ? delay : delay - elapsed, | ||
); | ||
} | ||
const qsStringify = (obj, options) => { | ||
if (!isObject(obj)) return ''; | ||
options = Object.assign({}, defaultStringifyOptions, options); | ||
const keys = []; | ||
const objKeys = Object.keys(obj); | ||
const sideChannel = new WeakMap(); | ||
const commaRoundTrip = Boolean( | ||
options.arrayFormat === 'comma' && options.commaRoundTrip, | ||
); | ||
for (let i = 0; i < objKeys.length; ++i) { | ||
const key = objKeys[i]; | ||
pushToArray( | ||
keys, | ||
stringify(obj[key], key, commaRoundTrip, sideChannel, options), | ||
); | ||
} | ||
wrapper.cancel = () => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
clear(); | ||
cancelled = true; | ||
}; | ||
return wrapper; | ||
const res = keys.join('&'); | ||
if (res.length === 0) return ''; | ||
return options.addQueryPrefix ? `?${res}` : res; | ||
}; | ||
const debounce = (delay, fn) => throttle(delay, fn, true); | ||
@@ -564,2 +730,8 @@ const noop = () => {}; | ||
const isPromise = (v) => isObject(v) && typeof v.then === 'function'; | ||
const isBuffer = (v) => { | ||
if (!isObject(v)) return false; | ||
return Boolean( | ||
v.constructor && v.constructor.isBuffer && v.constructor.isBuffer(v), | ||
); | ||
}; | ||
const isInBounds = ([a, b], v) => { | ||
@@ -575,3 +747,3 @@ if (v === a || v === b) return true; | ||
}; | ||
const isNativeValue = (v) => { | ||
const isPrimitiveValue = (v) => { | ||
return ( | ||
@@ -796,2 +968,3 @@ typeof v === 'number' || | ||
exports.isBrowser = isBrowser; | ||
exports.isBuffer = isBuffer; | ||
exports.isDate = isDate; | ||
@@ -801,6 +974,6 @@ exports.isEmptyObject = isEmptyObject; | ||
exports.isMap = isMap; | ||
exports.isNativeValue = isNativeValue; | ||
exports.isNil = isNil; | ||
exports.isObject = isObject; | ||
exports.isPlainObject = isPlainObject; | ||
exports.isPrimitiveValue = isPrimitiveValue; | ||
exports.isPromise = isPromise; | ||
@@ -807,0 +980,0 @@ exports.isRegExp = isRegExp; |
@@ -1,2 +0,2 @@ | ||
import type { BaseType, TypedArray } from './types'; | ||
import type { TypedArray, PrimitiveType } from './types'; | ||
export { Queue } from 'small-queue'; | ||
@@ -7,5 +7,15 @@ export { root } from './root'; | ||
export { loopSlice } from './loopSlice'; | ||
export { qsParse, qsStringify } from './qs'; | ||
export { throttle, debounce } from './throttle'; | ||
export type { BaseType, TypedArray, Prettify, DeepPrettify } from './types'; | ||
export { | ||
qsParse, | ||
qsStringify, | ||
type QsParseOptions, | ||
type QsStringifyOptions, | ||
} from './qs'; | ||
export type { | ||
TypedArray, | ||
PrimitiveType, | ||
Prettify, | ||
DeepPrettify, | ||
} from './types'; | ||
export declare const noop: () => void; | ||
@@ -26,3 +36,3 @@ export declare const objectToString: () => string; | ||
export declare const isNil: (v: unknown) => v is null | undefined; | ||
export declare const isObject: (v: unknown) => boolean; | ||
export declare const isObject: (v: unknown) => v is object; | ||
export declare const isPlainObject: <T>( | ||
@@ -48,2 +58,3 @@ v: unknown, | ||
) => v is PromiseLike<T>; | ||
export declare const isBuffer: (v: unknown) => boolean; | ||
export declare const isInBounds: ([a, b]: Array<number>, v: number) => boolean; | ||
@@ -53,3 +64,3 @@ export declare const isEmptyObject: <T extends Record<PropertyKey, any>>( | ||
) => boolean; | ||
export declare const isNativeValue: (v: unknown) => v is BaseType; | ||
export declare const isPrimitiveValue: (v: unknown) => v is PrimitiveType; | ||
export declare const isAbsolute: (p: string) => boolean; | ||
@@ -56,0 +67,0 @@ export declare const last: <T>(arr: T[], i?: number) => T; |
import type { Prettify } from './types'; | ||
export interface QsStringifyOptions { | ||
encode?: boolean; | ||
addQueryPrefix?: boolean; | ||
commaRoundTrip?: boolean; | ||
arrayFormat?: keyof typeof arrayPrefixFns; | ||
} | ||
export interface QsParseOptions { | ||
@@ -8,2 +14,8 @@ comma: boolean; | ||
} | ||
declare const arrayPrefixFns: { | ||
comma: (p: string) => string; | ||
repeat: (p: string) => string; | ||
brackets: (p: string) => string; | ||
indices: (p: string, k: string) => string; | ||
}; | ||
export declare const qsParse: <T = Record<PropertyKey, unknown>>( | ||
@@ -13,2 +25,6 @@ s?: unknown, | ||
) => T; | ||
export declare const qsStringify: () => void; | ||
export declare const qsStringify: ( | ||
obj: unknown, | ||
options?: QsStringifyOptions, | ||
) => string; | ||
export {}; |
@@ -11,3 +11,3 @@ export type Awaitable<T> = T | PromiseLike<T>; | ||
} & {}; | ||
export type BaseType = | ||
export type PrimitiveType = | ||
| number | ||
@@ -14,0 +14,0 @@ | bigint |
{ | ||
"name": "aidly", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Tool library.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/aidly.cjs.js", |
@@ -0,0 +0,0 @@ <div align="center"> |
Sorry, the diff of this file is not supported yet
92020
3095