hyperscript-rxjs 1.3.14 → 1.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -22
- package/src/array/advance.d.ts +9 -0
- package/src/array/advance.js +13 -0
- package/src/array/advance.test.js +12 -0
- package/src/array/arrayInsert.d.ts +8 -0
- package/src/array/arrayInsert.js +13 -0
- package/src/array/arrayInsert.test.js +13 -0
- package/src/array/arrayRemove.d.ts +7 -0
- package/src/array/arrayRemove.js +15 -0
- package/src/array/arrayRemove.test.js +13 -0
- package/src/array/findLastIndex.d.ts +14 -0
- package/src/array/findLastIndex.js +20 -0
- package/src/array/findLastIndex.test.js +41 -0
- package/src/array/index.d.ts +9 -0
- package/src/array/index.js +9 -0
- package/src/array/isRange.d.ts +7 -0
- package/src/array/isRange.js +15 -0
- package/src/array/isRange.test.js +6 -0
- package/src/array/rangeArray.d.ts +7 -0
- package/src/array/rangeArray.js +10 -0
- package/src/array/rangeArray.test.js +11 -0
- package/src/array/unwrapArgs.d.ts +10 -0
- package/src/array/unwrapArgs.js +15 -0
- package/src/array/unwrapArgs.test.js +33 -0
- package/src/array/zipArray.d.ts +11 -0
- package/src/array/zipArray.js +24 -0
- package/src/array/zipArray.test.js +16 -0
- package/src/comparers/Comparer.d.ts +101 -0
- package/src/comparers/Comparer.js +149 -0
- package/src/comparers/comparers.d.ts +21 -0
- package/src/comparers/comparers.js +10 -0
- package/src/comparers/differenceSet.d.ts +20 -0
- package/src/comparers/differenceSet.js +35 -0
- package/src/comparers/differenceSet.test.js +11 -0
- package/src/comparers/distinctArray.d.ts +13 -0
- package/src/comparers/distinctArray.js +30 -0
- package/src/comparers/distinctArray.test.js +10 -0
- package/src/comparers/findIndexInSet.d.ts +20 -0
- package/src/comparers/findIndexInSet.js +27 -0
- package/src/comparers/findIndexInSet.test.js +8 -0
- package/src/comparers/groupArrayBy.d.ts +19 -0
- package/src/comparers/groupArrayBy.js +29 -0
- package/src/comparers/groupArrayBy.test.js +38 -0
- package/src/comparers/groupSortedEntries.d.ts +17 -0
- package/src/comparers/groupSortedEntries.js +38 -0
- package/src/comparers/groupSortedEntries.test.js +46 -0
- package/src/comparers/index.d.ts +14 -0
- package/src/comparers/index.js +14 -0
- package/src/comparers/intersectSet.d.ts +19 -0
- package/src/comparers/intersectSet.js +35 -0
- package/src/comparers/intersectSet.test.js +14 -0
- package/src/comparers/isEqualset.d.ts +22 -0
- package/src/comparers/isEqualset.js +33 -0
- package/src/comparers/isEqualset.test.js +22 -0
- package/src/comparers/isSubset.d.ts +21 -0
- package/src/comparers/isSubset.js +33 -0
- package/src/comparers/isSubset.test.js +21 -0
- package/src/comparers/isSuperset.d.ts +21 -0
- package/src/comparers/isSuperset.js +13 -0
- package/src/comparers/isSuperset.test.js +21 -0
- package/src/comparers/sortedArrayToSet.d.ts +20 -0
- package/src/comparers/sortedArrayToSet.js +35 -0
- package/src/comparers/sortedArrayToSet.test.js +11 -0
- package/src/comparers/unionSet.d.ts +21 -0
- package/src/comparers/unionSet.js +34 -0
- package/src/comparers/unionSet.test.js +11 -0
- package/src/comparison/compareDate.d.ts +8 -0
- package/src/comparison/compareDate.js +11 -0
- package/src/comparison/compareEntries.d.ts +13 -0
- package/src/comparison/compareEntries.js +13 -0
- package/src/comparison/compareKey.d.ts +11 -0
- package/src/comparison/compareKey.js +25 -0
- package/src/comparison/compareKey.test.js +21 -0
- package/src/comparison/compareKeyPath.d.ts +15 -0
- package/src/comparison/compareKeyPath.js +33 -0
- package/src/comparison/compareKeyPath.test.js +28 -0
- package/src/comparison/compareNumber.d.ts +11 -0
- package/src/comparison/compareNumber.js +27 -0
- package/src/comparison/compareNumber.test.js +21 -0
- package/src/comparison/defaultCompare.d.ts +8 -0
- package/src/comparison/defaultCompare.js +12 -0
- package/src/comparison/defaultCompare.test.js +24 -0
- package/src/comparison/index.d.ts +7 -0
- package/src/comparison/index.js +7 -0
- package/src/comparison/infinity.test.js +122 -0
- package/src/comparison/typeof.test.js +64 -0
- package/src/comparison/types.d.ts +5 -0
- package/src/comparison/types.js +11 -0
- package/src/deep/Deep.d.ts +58 -0
- package/src/deep/Deep.js +267 -0
- package/src/deep/Deep.test.js +130 -0
- package/src/deep/deepCombineLatest.test.js +36 -0
- package/src/deep/deepMerge.test.js +34 -0
- package/src/deep/differenceDeep.test.js +31 -0
- package/src/deep/freshValueDeep.test.js +17 -0
- package/src/deep/index.d.ts +1 -0
- package/src/deep/index.js +2 -0
- package/src/deep/intersectDeep.test.js +25 -0
- package/src/deep/intersectEntries.d.ts +13 -0
- package/src/deep/intersectEntries.js +37 -0
- package/src/deep/intersectEntries.test.js +20 -0
- package/src/deep/objectToDeep.test.js +31 -0
- package/src/deep/replaceValueDeep.test.js +21 -0
- package/src/deep/unionDeep.test.js +30 -0
- package/src/deep/zipValueDeep.test.js +21 -0
- package/src/deep-rxjs/ObservableArray.d.ts +55 -0
- package/src/deep-rxjs/ObservableArray.js +94 -0
- package/src/deep-rxjs/ObservableArray.test.js +117 -0
- package/src/deep-rxjs/index.d.ts +2 -0
- package/src/deep-rxjs/index.js +2 -0
- package/src/deep-rxjs/isRxType.d.ts +9 -0
- package/src/deep-rxjs/isRxType.js +15 -0
- package/src/deep-rxjs/isRxType.test.js +43 -0
- package/src/hyperscript-rxjs/HyperscriptExtensions.d.ts +20 -0
- package/src/hyperscript-rxjs/checkbox.d.ts +13 -0
- package/src/hyperscript-rxjs/checkbox.js +47 -0
- package/src/hyperscript-rxjs/checkbox.test.js +68 -0
- package/src/hyperscript-rxjs/choice.d.ts +13 -0
- package/src/hyperscript-rxjs/choice.js +24 -0
- package/src/hyperscript-rxjs/choice.test.js +108 -0
- package/src/hyperscript-rxjs/collapse.d.ts +14 -0
- package/src/hyperscript-rxjs/collapse.js +32 -0
- package/src/hyperscript-rxjs/collapse.test.js +67 -0
- package/src/hyperscript-rxjs/displays/blockLevelFamily.d.ts +5 -0
- package/src/hyperscript-rxjs/displays/blockLevelFamily.js +51 -0
- package/src/hyperscript-rxjs/displays/getDisplay.d.ts +7 -0
- package/src/hyperscript-rxjs/displays/getDisplay.js +51 -0
- package/src/hyperscript-rxjs/displays/getDisplay.test.js +56 -0
- package/src/hyperscript-rxjs/displays/index.d.ts +3 -0
- package/src/hyperscript-rxjs/displays/index.js +3 -0
- package/src/hyperscript-rxjs/displays/inlineFamily.d.ts +5 -0
- package/src/hyperscript-rxjs/displays/inlineFamily.js +73 -0
- package/src/hyperscript-rxjs/flip.d.ts +15 -0
- package/src/hyperscript-rxjs/flip.js +29 -0
- package/src/hyperscript-rxjs/flip.test.js +85 -0
- package/src/hyperscript-rxjs/fragment.d.ts +10 -0
- package/src/hyperscript-rxjs/fragment.js +22 -0
- package/src/hyperscript-rxjs/fragment.test.js +70 -0
- package/src/hyperscript-rxjs/hyperscript.d.ts +15 -0
- package/src/hyperscript-rxjs/hyperscript.js +170 -0
- package/src/hyperscript-rxjs/hyperscript.test.js +75 -0
- package/src/hyperscript-rxjs/index.d.ts +19 -0
- package/src/hyperscript-rxjs/index.js +19 -0
- package/src/hyperscript-rxjs/multiselect.d.ts +18 -0
- package/src/hyperscript-rxjs/multiselect.js +41 -0
- package/src/hyperscript-rxjs/multiselect.test.js +121 -0
- package/src/hyperscript-rxjs/numberbox.d.ts +14 -0
- package/src/hyperscript-rxjs/numberbox.js +73 -0
- package/src/hyperscript-rxjs/numberbox.test.js +84 -0
- package/src/hyperscript-rxjs/radio.d.ts +15 -0
- package/src/hyperscript-rxjs/radio.js +53 -0
- package/src/hyperscript-rxjs/radio.test.js +59 -0
- package/src/hyperscript-rxjs/select.d.ts +28 -0
- package/src/hyperscript-rxjs/select.js +88 -0
- package/src/hyperscript-rxjs/select.test.js +101 -0
- package/src/hyperscript-rxjs/tabControls/bindTabIndex.d.ts +12 -0
- package/src/hyperscript-rxjs/tabControls/bindTabIndex.js +59 -0
- package/src/hyperscript-rxjs/tabControls/index.d.ts +8 -0
- package/src/hyperscript-rxjs/tabControls/index.js +10 -0
- package/src/hyperscript-rxjs/tabControls/tabControl.d.ts +19 -0
- package/src/hyperscript-rxjs/tabControls/tabControl.js +40 -0
- package/src/hyperscript-rxjs/tabControls/tabControl.test.js +98 -0
- package/src/hyperscript-rxjs/tabControls/tabNavItem.d.ts +9 -0
- package/src/hyperscript-rxjs/tabControls/tabNavItem.js +30 -0
- package/src/hyperscript-rxjs/tabControls/tabPanel.d.ts +9 -0
- package/src/hyperscript-rxjs/tabControls/tabPanel.js +21 -0
- package/src/hyperscript-rxjs/tabControls/tabRoot.d.ts +7 -0
- package/src/hyperscript-rxjs/tabControls/tabRoot.js +26 -0
- package/src/hyperscript-rxjs/tags.d.ts +193 -0
- package/src/hyperscript-rxjs/tags.js +751 -0
- package/src/hyperscript-rxjs/tags.test.js +75 -0
- package/src/hyperscript-rxjs/textNode.d.ts +11 -0
- package/src/hyperscript-rxjs/textNode.js +51 -0
- package/src/hyperscript-rxjs/textNode.test.js +56 -0
- package/src/hyperscript-rxjs/textarea.d.ts +17 -0
- package/src/hyperscript-rxjs/textarea.js +45 -0
- package/src/hyperscript-rxjs/textarea.test.js +52 -0
- package/src/hyperscript-rxjs/textbox.d.ts +15 -0
- package/src/hyperscript-rxjs/textbox.js +42 -0
- package/src/hyperscript-rxjs/textbox.test.js +52 -0
- package/src/index.d.ts +19 -0
- package/src/index.js +19 -0
- package/src/nodes/attachSubscriptionToNode.d.ts +13 -0
- package/src/nodes/attachSubscriptionToNode.js +25 -0
- package/src/nodes/attachSubscriptionToNode.test.js +73 -0
- package/src/nodes/index.d.ts +6 -0
- package/src/nodes/index.js +6 -0
- package/src/nodes/normalizeChildNodes.d.ts +9 -0
- package/src/nodes/normalizeChildNodes.js +15 -0
- package/src/nodes/normalizeChildNodes.test.js +55 -0
- package/src/nodes/parseHyperscriptArgs.d.ts +10 -0
- package/src/nodes/parseHyperscriptArgs.js +57 -0
- package/src/nodes/parseHyperscriptArgs.test.js +85 -0
- package/src/nodes/pipeEvent.d.ts +15 -0
- package/src/nodes/pipeEvent.js +49 -0
- package/src/nodes/pipeEvent.test.js +97 -0
- package/src/nodes/subscribeEvent.d.ts +15 -0
- package/src/nodes/subscribeEvent.js +56 -0
- package/src/nodes/subscribeEvent.test.js +88 -0
- package/src/object/index.d.ts +10 -0
- package/src/object/index.js +11 -0
- package/src/object/intersectObject.d.ts +12 -0
- package/src/object/intersectObject.js +23 -0
- package/src/object/intersectObject.test.js +69 -0
- package/src/object/isEmptyObject.d.ts +7 -0
- package/src/object/isEmptyObject.js +13 -0
- package/src/object/isEmptyObject.test.js +33 -0
- package/src/object/isPlainObject.d.ts +11 -0
- package/src/object/isPlainObject.js +18 -0
- package/src/object/nestedCombineLatest.d.ts +11 -0
- package/src/object/nestedCombineLatest.js +18 -0
- package/src/object/nestedCombineLatest.test.js +25 -0
- package/src/object/nestedMerge.d.ts +11 -0
- package/src/object/nestedMerge.js +11 -0
- package/src/object/nestedMerge.test.js +61 -0
- package/src/object/pickBehaviorSubject.d.ts +13 -0
- package/src/object/pickBehaviorSubject.js +81 -0
- package/src/object/pickBehaviorSubject.test.js +88 -0
- package/src/object/pluckProperty.d.ts +13 -0
- package/src/object/pluckProperty.js +24 -0
- package/src/object/pluckProperty.test.js +37 -0
- package/src/object/restore.d.ts +12 -0
- package/src/object/restore.js +69 -0
- package/src/object/restore.test.js +124 -0
- package/src/object/splitObjectByObservable.d.ts +12 -0
- package/src/object/splitObjectByObservable.js +41 -0
- package/src/object/splitObjectByObservable.test.js +78 -0
- package/src/props/getNestedProperty.d.ts +12 -0
- package/src/props/getNestedProperty.js +31 -0
- package/src/props/getNestedProperty.test.js +72 -0
- package/src/props/index.d.ts +7 -0
- package/src/props/index.js +7 -0
- package/src/props/parsePropName.d.ts +13 -0
- package/src/props/parsePropName.js +45 -0
- package/src/props/parsePropName.test.js +67 -0
- package/src/props/setProp.d.ts +16 -0
- package/src/props/setProp.js +42 -0
- package/src/props/setProp.test.js +59 -0
- package/src/props/setProps.d.ts +14 -0
- package/src/props/setProps.js +47 -0
- package/src/props/setProps.test.js +97 -0
- package/src/props/subscribeProp.d.ts +16 -0
- package/src/props/subscribeProp.js +47 -0
- package/src/props/subscribeProp.test.js +81 -0
- package/src/ramda/compose.d.ts +10 -0
- package/src/ramda/compose.js +36 -0
- package/src/ramda/compose.test.js +73 -0
- package/src/ramda/cond.d.ts +12 -0
- package/src/ramda/cond.js +29 -0
- package/src/ramda/cond.test.js +88 -0
- package/src/ramda/fold.d.ts +13 -0
- package/src/ramda/fold.js +20 -0
- package/src/ramda/fold.test.js +51 -0
- package/src/ramda/index.d.ts +6 -0
- package/src/ramda/index.js +6 -0
- package/src/ramda/pipe.d.ts +13 -0
- package/src/ramda/pipe.js +27 -0
- package/src/ramda/pipe.test.js +77 -0
- package/src/ramda/unfold.d.ts +11 -0
- package/src/ramda/unfold.js +20 -0
- package/src/ramda/unfold.test.js +29 -0
- package/src/unquoted-json/ajax.test.js +1074 -0
- package/src/unquoted-json/index.d.ts +13 -0
- package/src/unquoted-json/index.js +12 -0
- package/src/unquoted-json/queryStringify.d.ts +8 -0
- package/src/unquoted-json/queryStringify.js +70 -0
- package/src/unquoted-json/queryStringify.test.js +110 -0
- package/src/unquoted-json/stringifyKey.d.ts +7 -0
- package/src/unquoted-json/stringifyKey.js +16 -0
- package/src/unquoted-json/stringifyKey.test.js +51 -0
- package/src/unquoted-json/stringifyStringValue.d.ts +7 -0
- package/src/unquoted-json/stringifyStringValue.js +17 -0
- package/src/unquoted-json/stringifyStringValue.test.js +52 -0
- package/src/unquoted-json/unquotedJsonStringify.d.ts +7 -0
- package/src/unquoted-json/unquotedJsonStringify.js +39 -0
- package/src/unquoted-json/unquotedJsonStringify.test.js +52 -0
- package/dist/hyperscript-rxjs.d.ts +0 -1412
- package/dist/hyperscript-rxjs.js +0 -1
- package/dist/tsdoc-metadata.json +0 -11
@@ -0,0 +1,13 @@
|
|
1
|
+
// // 工具函数
|
2
|
+
// export * from './peercent-encode/pctEncodeChar'
|
3
|
+
// export * from './peercent-encode/queryPctEncode'
|
4
|
+
// export * from './peercent-encode/toUtf8'
|
5
|
+
|
6
|
+
// 主函数
|
7
|
+
export * from "./queryStringify"
|
8
|
+
|
9
|
+
// export * from "./stringifyKey"
|
10
|
+
// export * from "./stringifyStringValue"
|
11
|
+
|
12
|
+
|
13
|
+
export * from './unquotedJsonStringify'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
// // 工具函数
|
2
|
+
// export * from './peercent-encode/pctEncodeChar'
|
3
|
+
// export * from './peercent-encode/queryPctEncode'
|
4
|
+
// export * from './peercent-encode/toUtf8'
|
5
|
+
// export * from "./stringifyKey"
|
6
|
+
// export * from "./stringifyStringValue"
|
7
|
+
|
8
|
+
|
9
|
+
// 主函数
|
10
|
+
export * from "./queryStringify"
|
11
|
+
export * from './unquotedJsonStringify'
|
12
|
+
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import { unquotedJsonStringify } from './unquotedJsonStringify'
|
2
|
+
import { isPlainObject } from '../object'
|
3
|
+
/**
|
4
|
+
* 将对象序列化为 URL 查询字符串(支持嵌套对象和多类型字段)。
|
5
|
+
*
|
6
|
+
* @public
|
7
|
+
* @param {Object} data - 要序列化的对象
|
8
|
+
* @returns {string} 查询字符串(如 "?a=1&b=2"),无有效字段时返回空字符串
|
9
|
+
*/
|
10
|
+
export function queryStringify(data) {
|
11
|
+
if (!isPlainObject(data)) {
|
12
|
+
throw new Error("input should be a plain object.")
|
13
|
+
}
|
14
|
+
let pairs =
|
15
|
+
Object.entries(data)
|
16
|
+
.filter(([k, v]) =>
|
17
|
+
supportedDataTypes.has(typeof v) && v !== ''
|
18
|
+
)
|
19
|
+
|
20
|
+
if (pairs.length === 0) {
|
21
|
+
return ''
|
22
|
+
}
|
23
|
+
|
24
|
+
// 如果字段是对象,包括数组,则序列化字段值,否则不变。
|
25
|
+
return pairs
|
26
|
+
.map(([k, v]) => {
|
27
|
+
let vv = typeof v === 'object' ? unquotedJsonStringify(v) : String(v)
|
28
|
+
return escape(k) + '=' + escape(vv)
|
29
|
+
})
|
30
|
+
.join('&')
|
31
|
+
|
32
|
+
}
|
33
|
+
|
34
|
+
const supportedDataTypes = new Set([
|
35
|
+
'string',
|
36
|
+
'number',
|
37
|
+
'boolean',
|
38
|
+
'bigint',
|
39
|
+
// 'function',
|
40
|
+
// 'symbol',
|
41
|
+
// 'undefined',
|
42
|
+
'object',
|
43
|
+
])
|
44
|
+
|
45
|
+
/**
|
46
|
+
* 需要编码的ASCII字符及其百分比编码(键为字符本身,用\x或可打印形式)
|
47
|
+
* @type {Map<string, string>}
|
48
|
+
*/
|
49
|
+
const mp = new Map([
|
50
|
+
['\x00', '%00'], ['\x01', '%01'], ['\x02', '%02'], ['\x03', '%03'], ['\x04', '%04'],
|
51
|
+
['\x05', '%05'], ['\x06', '%06'], ['\x07', '%07'], ['\x08', '%08'], ['\t', '%09'],
|
52
|
+
['\n', '%0A'], ['\x0B', '%0B'], ['\x0C', '%0C'], ['\r', '%0D'],
|
53
|
+
['\x0E', '%0E'], ['\x0F', '%0F'], ['\x10', '%10'], ['\x11', '%11'], ['\x12', '%12'],
|
54
|
+
['\x13', '%13'], ['\x14', '%14'], ['\x15', '%15'], ['\x16', '%16'], ['\x17', '%17'],
|
55
|
+
['\x18', '%18'], ['\x19', '%19'], ['\x1A', '%1A'], ['\x1B', '%1B'], ['\x1C', '%1C'],
|
56
|
+
['\x1D', '%1D'], ['\x1E', '%1E'], ['\x1F', '%1F'], ['\x20', '%20'],
|
57
|
+
['#', '%23'],
|
58
|
+
['%', '%25'], //浏览器能识别不是%dd格式的百分号为原义,这里冗余转义为了保险起见。
|
59
|
+
['&', '%26'],
|
60
|
+
['+', '%2B'], // 原义+有时候会被解析为空格
|
61
|
+
['=', '%3D'],
|
62
|
+
['\x7F', '%7F'], // DEL
|
63
|
+
]);
|
64
|
+
|
65
|
+
/**
|
66
|
+
* 这是最小的转义函数,其他一概保持原义。
|
67
|
+
* @param {string} str
|
68
|
+
* @returns {string}
|
69
|
+
*/
|
70
|
+
const escape = str => str.replace(/[\x00-\x20#%&+=\x7F]/g, c => mp.get(c) || c)
|
@@ -0,0 +1,110 @@
|
|
1
|
+
import { queryStringify } from './queryStringify'
|
2
|
+
|
3
|
+
test('queryStringify', () => {
|
4
|
+
let obj = { foo: 'bar', baz: ['qux', 'quux'], corge: '' }
|
5
|
+
let y = queryStringify(obj)
|
6
|
+
expect(y).toEqual("foo=bar&baz=[qux,quux]")
|
7
|
+
})
|
8
|
+
|
9
|
+
describe('queryStringify ignore', () => {
|
10
|
+
test('undefined', () => {
|
11
|
+
let obj = { foo: undefined }
|
12
|
+
let y = queryStringify(obj)
|
13
|
+
expect(y).toEqual('')
|
14
|
+
})
|
15
|
+
|
16
|
+
test('function', () => {
|
17
|
+
let obj = { foo: x => x }
|
18
|
+
let y = queryStringify(obj)
|
19
|
+
expect(y).toEqual('')
|
20
|
+
})
|
21
|
+
|
22
|
+
test('Symbol', () => {
|
23
|
+
let obj = { foo: Symbol() }
|
24
|
+
let y = queryStringify(obj)
|
25
|
+
expect(y).toEqual('')
|
26
|
+
})
|
27
|
+
|
28
|
+
test('empty string', () => {
|
29
|
+
let obj = { foo: '' }
|
30
|
+
let y = queryStringify(obj)
|
31
|
+
expect(y).toEqual('')
|
32
|
+
})
|
33
|
+
})
|
34
|
+
|
35
|
+
describe('queryStringify real', () => {
|
36
|
+
test('null', () => {
|
37
|
+
let obj = { foo: null }
|
38
|
+
let y = queryStringify(obj)
|
39
|
+
expect(y).toEqual("foo=null")
|
40
|
+
})
|
41
|
+
|
42
|
+
test('NaN', () => {
|
43
|
+
let obj = { foo: NaN }
|
44
|
+
let y = queryStringify(obj)
|
45
|
+
expect(y).toEqual("foo=NaN")
|
46
|
+
})
|
47
|
+
|
48
|
+
test('Infinity', () => {
|
49
|
+
let obj = { foo: Infinity, bar: -Infinity }
|
50
|
+
let y = queryStringify(obj)
|
51
|
+
expect(y).toEqual("foo=Infinity&bar=-Infinity")
|
52
|
+
})
|
53
|
+
|
54
|
+
test('boolean', () => {
|
55
|
+
let obj = { foo: true, bar: false }
|
56
|
+
let y = queryStringify(obj)
|
57
|
+
expect(y).toEqual('foo=true&bar=false')
|
58
|
+
})
|
59
|
+
|
60
|
+
// test('bigint', () => {
|
61
|
+
// let obj = { foo: 0n, bar: 1n }
|
62
|
+
// let y = queryStringify(obj)
|
63
|
+
// expect(y).toEqual('?foo=0&bar=1')
|
64
|
+
// })
|
65
|
+
|
66
|
+
test('number 0', () => {
|
67
|
+
let obj = { foo: 1 / Infinity, bar: -1 / Infinity }
|
68
|
+
let y = queryStringify(obj)
|
69
|
+
expect(y).toEqual('foo=0&bar=0')
|
70
|
+
})
|
71
|
+
|
72
|
+
test('string', () => {
|
73
|
+
let obj = { foo: 'test' }
|
74
|
+
let y = queryStringify(obj)
|
75
|
+
expect(y).toEqual('foo=test')
|
76
|
+
})
|
77
|
+
|
78
|
+
test('object', () => {
|
79
|
+
let obj = { foo: {} }
|
80
|
+
let y = queryStringify(obj)
|
81
|
+
expect(y).toEqual('foo={}')
|
82
|
+
})
|
83
|
+
|
84
|
+
test('array', () => {
|
85
|
+
let obj = { foo: [] }
|
86
|
+
let y = queryStringify(obj)
|
87
|
+
expect(y).toEqual('foo=[]')
|
88
|
+
})
|
89
|
+
|
90
|
+
test('encode data', () => {
|
91
|
+
const data = {
|
92
|
+
name: 'this is a test',
|
93
|
+
inlet: {
|
94
|
+
SO2: 4273.11,
|
95
|
+
SO3: 45.35924,
|
96
|
+
'CaSO4*(1/2)H2O': 49.79,
|
97
|
+
HF: 38.48,
|
98
|
+
ash: 'NO',
|
99
|
+
},
|
100
|
+
effect: [96, 30, 95, 95, 85],
|
101
|
+
'CaSO4*(1/2)H2O': -1,
|
102
|
+
cleanLeakage: -1,
|
103
|
+
}
|
104
|
+
|
105
|
+
let res = queryStringify(data)
|
106
|
+
expect(res).toBe("name=this%20is%20a%20test&inlet={SO2:4273.11,SO3:45.35924,CaSO4*(1/2)H2O:49.79,HF:38.48,ash:NO}&effect=[96,30,95,95,85]&CaSO4*(1/2)H2O=-1&cleanLeakage=-1")
|
107
|
+
})
|
108
|
+
|
109
|
+
})
|
110
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/**
|
2
|
+
* 将键名字符串化,必要时加引号(如包含特殊字符或空字符串)。
|
3
|
+
*
|
4
|
+
* @public
|
5
|
+
* @param {string} x - 要处理的键名
|
6
|
+
* @returns {string} 处理后的键名
|
7
|
+
*/
|
8
|
+
export const stringifyKey = x => {
|
9
|
+
if (
|
10
|
+
x === "" ||
|
11
|
+
/(^\u0020)|[,:{}[\]"\u0000-\u001F\u007F]|(\u0020$)/.test(x)
|
12
|
+
)
|
13
|
+
return JSON.stringify(x)
|
14
|
+
else
|
15
|
+
return x
|
16
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import { stringifyKey } from './stringifyKey'
|
2
|
+
|
3
|
+
test("stringifyKey empty", () => {
|
4
|
+
let x = ""
|
5
|
+
let y = stringifyKey(x)
|
6
|
+
let e = '""'
|
7
|
+
expect(y).toEqual(e)
|
8
|
+
})
|
9
|
+
|
10
|
+
test("stringifyKey control char", () => {
|
11
|
+
let x = "\u0000"
|
12
|
+
let y = stringifyKey(x)
|
13
|
+
let e = '"\\u0000"'
|
14
|
+
expect(y).toEqual(e)
|
15
|
+
})
|
16
|
+
|
17
|
+
test("stringifyKey space", () => {
|
18
|
+
let x = " x"
|
19
|
+
let y = stringifyKey(x)
|
20
|
+
let e = '" x"'
|
21
|
+
expect(y).toEqual(e)
|
22
|
+
})
|
23
|
+
|
24
|
+
test("stringifyKey keyword", () => {
|
25
|
+
let x = "]"
|
26
|
+
let y = stringifyKey(x)
|
27
|
+
let e = '"]"'
|
28
|
+
expect(y).toEqual(e)
|
29
|
+
})
|
30
|
+
|
31
|
+
test("stringifyKey normal", () => {
|
32
|
+
let x = "abc"
|
33
|
+
let y = stringifyKey(x)
|
34
|
+
let e = x
|
35
|
+
expect(y).toEqual(e)
|
36
|
+
})
|
37
|
+
|
38
|
+
test("stringifyKey null", () => {
|
39
|
+
let x = "null"
|
40
|
+
let y = stringifyKey(x)
|
41
|
+
let e = x
|
42
|
+
expect(y).toEqual(e)
|
43
|
+
})
|
44
|
+
|
45
|
+
test("stringifyKey 123", () => {
|
46
|
+
let x = "-123.45"
|
47
|
+
let y = stringifyKey(x)
|
48
|
+
let e = x
|
49
|
+
expect(y).toEqual(e)
|
50
|
+
})
|
51
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { stringifyKey } from './stringifyKey'
|
2
|
+
|
3
|
+
/**
|
4
|
+
* 将字符串值序列化为 JSON 字符串,必要时加引号(如布尔/null/数字字面量或特殊字符)。
|
5
|
+
*
|
6
|
+
* @public
|
7
|
+
* @param {string} x - 要处理的字符串值
|
8
|
+
* @returns {string} 处理后的字符串
|
9
|
+
*/
|
10
|
+
export const stringifyStringValue = x => {
|
11
|
+
if (x === "true" || x === "false" || x === "null")
|
12
|
+
return JSON.stringify(x)
|
13
|
+
else if (/^[-+]?\d+(\.\d+)?([eE][-+]?\d+)?$/.test(x))
|
14
|
+
return JSON.stringify(x)
|
15
|
+
else
|
16
|
+
return stringifyKey(x)
|
17
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { stringifyStringValue } from './stringifyStringValue'
|
2
|
+
|
3
|
+
test("stringifyStringValue empty", () => {
|
4
|
+
let x = ""
|
5
|
+
let y = stringifyStringValue(x)
|
6
|
+
let e = '""'
|
7
|
+
expect(y).toEqual(e)
|
8
|
+
})
|
9
|
+
|
10
|
+
test("stringifyStringValue control char", () => {
|
11
|
+
let x = "\u0000"
|
12
|
+
let y = stringifyStringValue(x)
|
13
|
+
let e = '"\\u0000"'
|
14
|
+
expect(y).toEqual(e)
|
15
|
+
})
|
16
|
+
|
17
|
+
test("stringifyStringValue space", () => {
|
18
|
+
let x = " x"
|
19
|
+
let y = stringifyStringValue(x)
|
20
|
+
let e = '" x"'
|
21
|
+
expect(y).toEqual(e)
|
22
|
+
})
|
23
|
+
|
24
|
+
test("stringifyStringValue keyword", () => {
|
25
|
+
let x = "]"
|
26
|
+
let y = stringifyStringValue(x)
|
27
|
+
let e = '"]"'
|
28
|
+
expect(y).toEqual(e)
|
29
|
+
})
|
30
|
+
|
31
|
+
test("stringifyStringValue normal", () => {
|
32
|
+
let x = "abc"
|
33
|
+
let y = stringifyStringValue(x)
|
34
|
+
let e = x
|
35
|
+
expect(y).toEqual(e)
|
36
|
+
})
|
37
|
+
|
38
|
+
test("stringifyStringValue null", () => {
|
39
|
+
let x = "null"
|
40
|
+
let y = stringifyStringValue(x)
|
41
|
+
let e = '"null"'
|
42
|
+
expect(y).toEqual(e)
|
43
|
+
})
|
44
|
+
|
45
|
+
test("stringifyStringValue 123", () => {
|
46
|
+
let x = "-123.45"
|
47
|
+
let y = stringifyStringValue(x)
|
48
|
+
let e = '"-123.45"'
|
49
|
+
expect(y).toEqual(e)
|
50
|
+
})
|
51
|
+
|
52
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { stringifyKey } from './stringifyKey'
|
2
|
+
import { stringifyStringValue } from './stringifyStringValue'
|
3
|
+
|
4
|
+
/**
|
5
|
+
* 将任意 JS 值序列化为“无引号 JSON”字符串(仅对键名和部分字符串值去引号)。
|
6
|
+
*
|
7
|
+
* @public
|
8
|
+
* @param {*} value - 要序列化的值
|
9
|
+
* @returns {string} 序列化后的字符串
|
10
|
+
*/
|
11
|
+
export function unquotedJsonStringify(value) {
|
12
|
+
if (value === null) {
|
13
|
+
return 'null'
|
14
|
+
} else if (typeof value === 'string') {
|
15
|
+
return stringifyStringValue(value)
|
16
|
+
} else if (typeof value === 'number') {
|
17
|
+
return isFinite(value) ? value.toString() : 'null'
|
18
|
+
} else if (typeof value === 'boolean') {
|
19
|
+
return value ? 'true' : 'false'
|
20
|
+
} else if (typeof value === 'bigint') {
|
21
|
+
return value.toString()
|
22
|
+
} else if (Array.isArray(value)) {
|
23
|
+
let elems = value.map(e => unquotedJsonStringify(e)).join(',')
|
24
|
+
return '[' + elems + ']'
|
25
|
+
} else if (typeof value === 'object') {
|
26
|
+
let fields = Object.entries(value)
|
27
|
+
.map(([k, v]) => stringifyKey(k) + ':' + unquotedJsonStringify(v))
|
28
|
+
.join(',')
|
29
|
+
return '{' + fields + '}'
|
30
|
+
} else if (typeof value === 'undefined') {
|
31
|
+
return 'null'
|
32
|
+
} else if (typeof value === 'function') {
|
33
|
+
return 'null'
|
34
|
+
} else if (typeof value === 'symbol') {
|
35
|
+
return 'null'
|
36
|
+
} else {
|
37
|
+
return 'null'
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { unquotedJsonStringify } from './unquotedJsonStringify'
|
2
|
+
|
3
|
+
test("null", () => {
|
4
|
+
let x = null
|
5
|
+
let y = unquotedJsonStringify(x)
|
6
|
+
let e = 'null'
|
7
|
+
expect(y).toEqual(e)
|
8
|
+
})
|
9
|
+
|
10
|
+
test("string unquoted", () => {
|
11
|
+
let x = "string"
|
12
|
+
let y = unquotedJsonStringify(x)
|
13
|
+
let e = x
|
14
|
+
expect(y).toEqual(e)
|
15
|
+
})
|
16
|
+
|
17
|
+
test("string quoted", () => {
|
18
|
+
let x = "true"
|
19
|
+
let y = unquotedJsonStringify(x)
|
20
|
+
let e = '"true"'
|
21
|
+
expect(y).toEqual(e)
|
22
|
+
})
|
23
|
+
|
24
|
+
|
25
|
+
test("number", () => {
|
26
|
+
let x = -123
|
27
|
+
let y = unquotedJsonStringify(x)
|
28
|
+
let e = '-123'
|
29
|
+
expect(y).toEqual(e)
|
30
|
+
})
|
31
|
+
|
32
|
+
test("boolean", () => {
|
33
|
+
let x = true
|
34
|
+
let y = unquotedJsonStringify(x)
|
35
|
+
let e = 'true'
|
36
|
+
expect(y).toEqual(e)
|
37
|
+
})
|
38
|
+
|
39
|
+
test("array", () => {
|
40
|
+
let x = []
|
41
|
+
let y = unquotedJsonStringify(x)
|
42
|
+
let e = '[]'
|
43
|
+
expect(y).toEqual(e)
|
44
|
+
})
|
45
|
+
|
46
|
+
test("obj", () => {
|
47
|
+
let x = {}
|
48
|
+
let y = unquotedJsonStringify(x)
|
49
|
+
let e = '{}'
|
50
|
+
expect(y).toEqual(e)
|
51
|
+
})
|
52
|
+
|