hyperscript-rxjs 1.3.13 → 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 +16 -27
- 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,149 @@
|
|
1
|
+
import { distinctArray } from "./distinctArray.js";
|
2
|
+
import { groupArrayBy } from "./groupArrayBy";
|
3
|
+
import { groupSortedEntries } from "./groupSortedEntries";
|
4
|
+
import { differenceSet } from "./differenceSet.js";
|
5
|
+
import { findIndexInSet } from "./findIndexInSet.js";
|
6
|
+
import { intersectSet } from "./intersectSet.js";
|
7
|
+
import { isEqualset } from "./isEqualset.js";
|
8
|
+
import { isSubset } from "./isSubset.js";
|
9
|
+
import { isSuperset } from "./isSuperset.js";
|
10
|
+
import { unionSet } from "./unionSet.js";
|
11
|
+
import { sortedArrayToSet } from "./sortedArrayToSet.js";
|
12
|
+
|
13
|
+
/**
|
14
|
+
* 用于比较和操作数组及集合的工具类。
|
15
|
+
*
|
16
|
+
* @public
|
17
|
+
* @template T
|
18
|
+
*/
|
19
|
+
export class Comparer {
|
20
|
+
/**
|
21
|
+
* @param {(a: T, b: T) => number} compare - 比较函数
|
22
|
+
*/
|
23
|
+
constructor(compare) {
|
24
|
+
this.compare = compare;
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* 使用实例的比较函数对数组进行排序。
|
29
|
+
* @param {T[]} array - 要排序的数组
|
30
|
+
* @returns {T[]} 排序后的数组
|
31
|
+
*/
|
32
|
+
sort(array) {
|
33
|
+
return array.sort(this.compare);
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* 移除数组中的重复值。
|
38
|
+
* @param {T[]} array - 要处理的数组
|
39
|
+
* @returns {T[]} 去重后的新数组
|
40
|
+
*/
|
41
|
+
distinct(array) {
|
42
|
+
return distinctArray(array, (a, b) => this.compare(a, b) === 0);
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* 将已排序的数组转为集合(去重)。
|
47
|
+
* @param {T[]} array - 已排序的数组
|
48
|
+
* @returns {T[]} 唯一元素数组
|
49
|
+
*/
|
50
|
+
sortedArrayToSet(array) {
|
51
|
+
return sortedArrayToSet(array, this.compare);
|
52
|
+
}
|
53
|
+
|
54
|
+
|
55
|
+
/**
|
56
|
+
* 根据 getKey 生成的 key 对数组元素分组。
|
57
|
+
* @template U
|
58
|
+
* @param {U[]} array - 要分组的数组
|
59
|
+
* @param {(item:U) => T} getKey - 生成分组 key 的函数
|
60
|
+
* @returns {[T, U[]][]} 分组后的对象或数组
|
61
|
+
*/
|
62
|
+
groupBy(array, getKey) {
|
63
|
+
return groupArrayBy(array, getKey, this.compare);
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* 对已排序的数组按 key 分组。
|
68
|
+
* @template V
|
69
|
+
* @param {[T,V][]} array - 已排序的数组
|
70
|
+
* @returns {[T, V[]][]} 分组结果
|
71
|
+
*/
|
72
|
+
groupSortedEntries(array) {
|
73
|
+
return groupSortedEntries(array, this.compare);
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* 计算两个数组的差集。数组元素是升序排列
|
78
|
+
* @param {T[]} array1 - 第一个数组
|
79
|
+
* @param {T[]} array2 - 第二个数组
|
80
|
+
* @returns {T[]} array1 中有而 array2 中没有的元素
|
81
|
+
*/
|
82
|
+
differenceSet(array1, array2) {
|
83
|
+
return differenceSet(array1, array2, this.compare);
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* 计算两个数组的交集。
|
88
|
+
* @param {T[]} array1 - 第一个数组
|
89
|
+
* @param {T[]} array2 - 第二个数组
|
90
|
+
* @returns {T[]} 同时存在于 array1 和 array2 的元素
|
91
|
+
*/
|
92
|
+
intersectSet(array1, array2) {
|
93
|
+
return intersectSet(array1, array2, this.compare);
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* 计算两个数组的并集(去重)。
|
98
|
+
* @param {T[]} array1 - 第一个数组
|
99
|
+
* @param {T[]} array2 - 第二个数组
|
100
|
+
* @returns {T[]} 两个数组的唯一元素合集
|
101
|
+
*/
|
102
|
+
unionSet(array1, array2) {
|
103
|
+
return unionSet(array1, array2, this.compare);
|
104
|
+
}
|
105
|
+
|
106
|
+
/**
|
107
|
+
* 在集合(数组)中查找元素的索引。
|
108
|
+
* @param {T[]} set - 要查找的数组
|
109
|
+
* @param {T} element - 要查找的元素
|
110
|
+
* @returns {number} 元素在集合中的索引,未找到返回 -1
|
111
|
+
*/
|
112
|
+
findIndexInSet(set, element) {
|
113
|
+
return findIndexInSet(set, element, this.compare);
|
114
|
+
}
|
115
|
+
|
116
|
+
/**
|
117
|
+
* 判断两个数组作为集合是否相等。
|
118
|
+
* @param {T[]} array1 - 第一个数组
|
119
|
+
* @param {T[]} array2 - 第二个数组
|
120
|
+
* @returns {boolean} 如果集合相等返回 true,否则返回 false
|
121
|
+
*/
|
122
|
+
isEqualset(array1, array2) {
|
123
|
+
return isEqualset(array1, array2, this.compare);
|
124
|
+
}
|
125
|
+
|
126
|
+
/**
|
127
|
+
* 判断第一个数组是否为第二个数组的子集。
|
128
|
+
* @param {T[]} array1 - 第一个数组
|
129
|
+
* @param {T[]} array2 - 第二个数组
|
130
|
+
* @returns {boolean} 如果 array1 是 array2 的子集返回 true,否则返回 false
|
131
|
+
*/
|
132
|
+
isSubset(array1, array2) {
|
133
|
+
return isSubset(array1, array2, this.compare);
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* 判断第一个数组是否为第二个数组的超集。
|
138
|
+
* @param {T[]} array1 - 第一个数组
|
139
|
+
* @param {T[]} array2 - 第二个数组
|
140
|
+
* @returns {boolean} 如果 array1 是 array2 的超集返回 true,否则返回 false
|
141
|
+
*/
|
142
|
+
isSuperset(array1, array2) {
|
143
|
+
return isSuperset(array1, array2, this.compare);
|
144
|
+
}
|
145
|
+
|
146
|
+
}
|
147
|
+
|
148
|
+
|
149
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { Comparer } from './Comparer';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* A comparer instance using the default comparison logic.
|
5
|
+
*/
|
6
|
+
export const defaultComparer: Comparer<any>;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* A comparer instance for comparing keys.
|
10
|
+
*/
|
11
|
+
export const keyComparer: Comparer<number | string>;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* A comparer instance for comparing key paths.
|
15
|
+
*/
|
16
|
+
export const keyPathComparer: Comparer<(number | string)[]>;
|
17
|
+
|
18
|
+
/**
|
19
|
+
* A comparer instance for comparing entries.
|
20
|
+
*/
|
21
|
+
export const entryComparer: Comparer<[(number | string)[], any]>;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { defaultCompare } from "../comparison/defaultCompare";
|
2
|
+
import { compareKey } from "../comparison/compareKey";
|
3
|
+
import { compareKeyPath } from "../comparison/compareKeyPath";
|
4
|
+
import { compareEntries } from "../comparison/compareEntries";
|
5
|
+
import { Comparer } from "./Comparer"
|
6
|
+
|
7
|
+
export const defaultComparer = new Comparer(defaultCompare)
|
8
|
+
export const keyComparer = new Comparer(compareKey)
|
9
|
+
export const keyPathComparer = new Comparer(compareKeyPath)
|
10
|
+
export const entryComparer = new Comparer(compareEntries)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* Computes the difference between two sorted sets (set1 - set2).
|
3
|
+
*
|
4
|
+
* - Assumes both `set1` and `set2` are sorted.
|
5
|
+
* - Uses the provided `compare` function to determine the order and equality of elements.
|
6
|
+
*
|
7
|
+
* @param set1 - The first sorted set.
|
8
|
+
* @param set2 - The second sorted set.
|
9
|
+
* @param compare - A comparison function that takes two elements and returns:
|
10
|
+
* - A negative number if the first element is less than the second.
|
11
|
+
* - 0 if the elements are equal.
|
12
|
+
* - A positive number if the first element is greater than the second.
|
13
|
+
* Default is `defaultCompare`.
|
14
|
+
* @returns A new array containing elements in `set1` that are not in `set2`.
|
15
|
+
*/
|
16
|
+
export function differenceSet<T>(
|
17
|
+
set1: T[],
|
18
|
+
set2: T[],
|
19
|
+
compare: (a: T, b: T) => number
|
20
|
+
): T[];
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/**
|
2
|
+
* return set1 - set2
|
3
|
+
* @template T
|
4
|
+
* @param {T[]} set1
|
5
|
+
* @param {T[]} set2
|
6
|
+
* @param {(a: T, b: T) => number} compare
|
7
|
+
* @returns {T[]}
|
8
|
+
*/
|
9
|
+
export function differenceSet(set1, set2, compare) {
|
10
|
+
/**
|
11
|
+
* @param {T[]} acc
|
12
|
+
* @param {T[]} a
|
13
|
+
* @param {T[]} b
|
14
|
+
* @returns {T[]}
|
15
|
+
*/
|
16
|
+
function loop(acc, a, b) {
|
17
|
+
if (a.length === 0) {
|
18
|
+
return acc
|
19
|
+
} else if (b.length === 0) {
|
20
|
+
return [...acc, ...a]
|
21
|
+
} else {
|
22
|
+
let n = compare(a[0], b[0])
|
23
|
+
if (n === 0) {
|
24
|
+
return loop(acc, a.slice(1), b.slice(1))
|
25
|
+
} else if (n < 0) {
|
26
|
+
return loop([...acc, a[0]], a.slice(1), b)
|
27
|
+
}
|
28
|
+
else { // if (n > 0)
|
29
|
+
return loop(acc, a, b.slice(1))
|
30
|
+
}
|
31
|
+
|
32
|
+
}
|
33
|
+
}
|
34
|
+
return loop([], set1, set2)
|
35
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { differenceSet } from './differenceSet'
|
2
|
+
import { defaultCompare } from '../comparison'
|
3
|
+
|
4
|
+
|
5
|
+
test('differenceSet test', () => {
|
6
|
+
let a = ['A', 'B', 'C'].sort()
|
7
|
+
let b = ['C', 'D', 'E'].sort()
|
8
|
+
|
9
|
+
const s = differenceSet(a, b, defaultCompare)
|
10
|
+
expect(s).toEqual(['A', 'B'])
|
11
|
+
})
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/**
|
2
|
+
* Removes duplicate elements from an array while maintaining the relative order of elements.
|
3
|
+
*
|
4
|
+
* @param array - The input array to process.
|
5
|
+
* @param equals - A function to compare two elements for equality.
|
6
|
+
* It should return `true` if the elements are equal, otherwise `false`.
|
7
|
+
* Default is `(a, b) => a === b`.
|
8
|
+
* @returns A new array with duplicates removed.
|
9
|
+
*/
|
10
|
+
export function distinctArray<T>(
|
11
|
+
array: T[],
|
12
|
+
equals: (a: T, b: T) => boolean
|
13
|
+
): T[];
|
@@ -0,0 +1,30 @@
|
|
1
|
+
/**
|
2
|
+
* 数组去重,并保持元素的相对顺序。
|
3
|
+
* @template T
|
4
|
+
* @param {T[]} array
|
5
|
+
* @param {(a:T,b:T)=>number} equals
|
6
|
+
* @returns {T[]}
|
7
|
+
*/
|
8
|
+
export function distinctArray(array, equals) {
|
9
|
+
/**
|
10
|
+
* 递归处理去重
|
11
|
+
* @param {T[]} resultArray - 已去重的结果数组
|
12
|
+
* @param {T[]} rest - 剩余待处理的数组
|
13
|
+
* @returns {T[]} - 去重后的数组
|
14
|
+
*/
|
15
|
+
function loop(resultArray, rest) {
|
16
|
+
if (rest.length === 0) {
|
17
|
+
return resultArray
|
18
|
+
} else {
|
19
|
+
let [head, ...tail] = rest
|
20
|
+
if (resultArray.findIndex(e => equals(head, e)) < 0) {
|
21
|
+
return loop([...resultArray, head], tail)
|
22
|
+
} else {
|
23
|
+
return loop(resultArray, tail)
|
24
|
+
}
|
25
|
+
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
return loop([], array)
|
30
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
/**
|
3
|
+
* Similar to `Array.findIndex`, returns the index of an element in a sorted set.
|
4
|
+
*
|
5
|
+
* - Assumes the input set is sorted using the provided `compare` function.
|
6
|
+
*
|
7
|
+
* @param set - The sorted array to search.
|
8
|
+
* @param e - The element to find.
|
9
|
+
* @param compare - A comparison function that takes two elements and returns:
|
10
|
+
* - A negative number if the first element is less than the second.
|
11
|
+
* - 0 if the elements are equal.
|
12
|
+
* - A positive number if the first element is greater than the second.
|
13
|
+
* Default is `defaultCompare`.
|
14
|
+
* @returns The index of the element in the set, or `-1` if not found.
|
15
|
+
*/
|
16
|
+
export function findIndexInSet<T>(
|
17
|
+
set: T[],
|
18
|
+
e: T,
|
19
|
+
compare: (a: T, b: T) => number
|
20
|
+
): number;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
* 同Array.findIndex,返回元素在集合中的索引位置
|
3
|
+
*
|
4
|
+
* @template T
|
5
|
+
* @param {T[]} set Array.sort(compare)后的Array
|
6
|
+
* @param {T} e 是要查找的元素
|
7
|
+
* @param {(a:T,b:T)=>number} compare 元素的比较函数
|
8
|
+
*/
|
9
|
+
export function findIndexInSet(set, e, compare) {
|
10
|
+
/** @param {number} i */
|
11
|
+
function loop(i) {
|
12
|
+
if (i === set.length) {
|
13
|
+
return -1
|
14
|
+
} else {
|
15
|
+
const n = compare(set[i], e)
|
16
|
+
if (n === 0) {
|
17
|
+
return i
|
18
|
+
} else if (n < 0) {
|
19
|
+
return loop(i + 1)
|
20
|
+
} else {
|
21
|
+
return -1
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
return loop(0)
|
27
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { findIndexInSet } from './findIndexInSet'
|
2
|
+
import { defaultCompare } from '../comparison'
|
3
|
+
|
4
|
+
test('find index', () => {
|
5
|
+
let st = ['A', 'B', 'C'].sort()
|
6
|
+
expect(findIndexInSet(st, 'B',defaultCompare)).toEqual(1)
|
7
|
+
expect(findIndexInSet(st, 'D',defaultCompare)).toEqual(-1)
|
8
|
+
})
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
/**
|
3
|
+
* Groups an array of elements by a key generated by the provided `getKey` function.
|
4
|
+
*
|
5
|
+
* - The array is first transformed into key-value pairs, sorted by the keys, and then grouped.
|
6
|
+
*
|
7
|
+
* @param array - The input array to group.
|
8
|
+
* @param getKey - A function that takes an element and returns the key to group by.
|
9
|
+
* @param compare - A comparison function that takes two keys and returns:
|
10
|
+
* - A negative number if the first key is less than the second.
|
11
|
+
* - 0 if the keys are equal.
|
12
|
+
* - A positive number if the first key is greater than the second.
|
13
|
+
* @returns An array of grouped elements, where each group is represented as a tuple containing the key and an array of elements.
|
14
|
+
*/
|
15
|
+
export function groupArrayBy<T, K>(
|
16
|
+
array: T[],
|
17
|
+
getKey: (item: T) => K,
|
18
|
+
compare: (a: K, b: K) => number
|
19
|
+
): [K, T[]][];
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { groupSortedEntries } from './groupSortedEntries'
|
2
|
+
|
3
|
+
/**
|
4
|
+
* 按指定的 key 函数对数组元素分组。
|
5
|
+
*
|
6
|
+
* 先将数组元素映射为 [key, value] 对,按 key 排序后,再分组。
|
7
|
+
*
|
8
|
+
* @public
|
9
|
+
* @template T, K
|
10
|
+
* @param {T[]} array - 要分组的数组
|
11
|
+
* @param {(item: T) => K} getKey - 生成分组 key 的函数
|
12
|
+
* @param {(a: K, b: K) => number} compare - key 的比较函数
|
13
|
+
* @returns {[K, T[]][]} 分组后的结果,每组为 [key, 元素数组]
|
14
|
+
*/
|
15
|
+
export function groupArrayBy(array, getKey, compare) {
|
16
|
+
if (array.length === 0) {
|
17
|
+
return []
|
18
|
+
}
|
19
|
+
|
20
|
+
let sortedEntries =
|
21
|
+
array.map(
|
22
|
+
/**
|
23
|
+
* @returns {[K, T]}
|
24
|
+
*/
|
25
|
+
e => [getKey(e), e]
|
26
|
+
)
|
27
|
+
.sort(([ka], [kb]) => compare(ka, kb))
|
28
|
+
return groupSortedEntries(sortedEntries, compare)
|
29
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import { groupArrayBy } from './groupArrayBy'
|
2
|
+
import { defaultCompare } from '../comparison'
|
3
|
+
|
4
|
+
describe('groupArrayBy', function () {
|
5
|
+
test('group array by', function () {
|
6
|
+
const students = [
|
7
|
+
{ name: 'Abby', score: 84 },
|
8
|
+
{ name: 'Brad', score: 73 },
|
9
|
+
{ name: 'Chris', score: 89 },
|
10
|
+
{ name: 'Eddy', score: 58 },
|
11
|
+
{ name: 'Gillian', score: 91 },
|
12
|
+
]
|
13
|
+
let getKey = student => (student.score > 80 ? '录取' : '淘汰')
|
14
|
+
const x = groupArrayBy(students, getKey, defaultCompare)
|
15
|
+
|
16
|
+
///返回的group key保留输入顺序。
|
17
|
+
let y = [
|
18
|
+
[
|
19
|
+
'录取',
|
20
|
+
[
|
21
|
+
{ name: 'Abby', score: 84 },
|
22
|
+
{ name: 'Chris', score: 89 },
|
23
|
+
{ name: 'Gillian', score: 91 },
|
24
|
+
],
|
25
|
+
],
|
26
|
+
[
|
27
|
+
'淘汰',
|
28
|
+
[
|
29
|
+
{ name: 'Brad', score: 73 },
|
30
|
+
{ name: 'Eddy', score: 58 },
|
31
|
+
],
|
32
|
+
],
|
33
|
+
]
|
34
|
+
|
35
|
+
expect(x).toEqual(y)
|
36
|
+
})
|
37
|
+
|
38
|
+
})
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/**
|
2
|
+
* Groups an array of sorted key-value entries into an array of groups,
|
3
|
+
* where each group contains entries with the same key.
|
4
|
+
*
|
5
|
+
* - Assumes the input `sortedEntries` is already sorted by keys in ascending order.
|
6
|
+
*
|
7
|
+
* @param sortedEntries - An array of key-value pairs, where the keys are already sorted in ascending order.
|
8
|
+
* @param compare - A comparison function that takes two keys and returns:
|
9
|
+
* - 0 if the keys are equal.
|
10
|
+
* - A negative number if the first key is less than the second.
|
11
|
+
* - A positive number if the first key is greater than the second.
|
12
|
+
* @returns An array of groups, where each group is a tuple containing a key and an array of values associated with that key.
|
13
|
+
*/
|
14
|
+
export function groupSortedEntries<K, V>(
|
15
|
+
sortedEntries: [K, V][],
|
16
|
+
compare: (a: K, b: K) => number
|
17
|
+
): [K, V[]][];
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
/**
|
3
|
+
* Groups an array of sorted key-value entries into an array of groups,
|
4
|
+
* where each group contains entries with the same key.
|
5
|
+
*
|
6
|
+
* @template K, V
|
7
|
+
* @param {[K, V][]} sortedEntries - An array of key-value pairs,
|
8
|
+
* where the keys are already sorted in ascending order.
|
9
|
+
* @param {(a:K,b:K)=>number} compare - A comparison function that takes two keys
|
10
|
+
* and returns a number: 0 if the keys are equal, a negative number if the first key
|
11
|
+
* is less than the second, and a positive number otherwise.
|
12
|
+
* @returns {[K, V[]][]} An array of groups, where each group is a tuple
|
13
|
+
* containing a key and an array of values associated with that key.
|
14
|
+
*/
|
15
|
+
export function groupSortedEntries(sortedEntries, compare) {
|
16
|
+
if (sortedEntries.length === 0) {
|
17
|
+
return []
|
18
|
+
}
|
19
|
+
|
20
|
+
const [head,...tail] = sortedEntries
|
21
|
+
/** @type {[K, V[]]} */
|
22
|
+
const firstGroup = [head[0], [head[1]]]
|
23
|
+
|
24
|
+
return tail.reduce(
|
25
|
+
(groups, [k, v]) => {
|
26
|
+
let [k0, group] = groups[groups.length - 1]
|
27
|
+
if (compare(k0, k) === 0) {
|
28
|
+
group.push(v)
|
29
|
+
} else {
|
30
|
+
groups.push([k, [v]])
|
31
|
+
}
|
32
|
+
return groups
|
33
|
+
},
|
34
|
+
[firstGroup]
|
35
|
+
)
|
36
|
+
|
37
|
+
}
|
38
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { defaultCompare } from '../comparison'
|
2
|
+
import { groupSortedEntries } from './groupSortedEntries'
|
3
|
+
|
4
|
+
describe('groupSortedEntries', function () {
|
5
|
+
test('sorted key value pairs', () => {
|
6
|
+
//console.log(x)
|
7
|
+
let x = [
|
8
|
+
['录取', { name: 'Abby', score: 84 }],
|
9
|
+
['录取', { name: 'Chris', score: 89 }],
|
10
|
+
['录取', { name: 'Dianne', score: 99 }],
|
11
|
+
['录取', { name: 'Gillian', score: 91 }],
|
12
|
+
['录取', { name: 'Irene', score: 85 }],
|
13
|
+
['淘汰', { name: 'Brad', score: 73 }],
|
14
|
+
['淘汰', { name: 'Eddy', score: 58 }],
|
15
|
+
['淘汰', { name: 'Fred', score: 67 }],
|
16
|
+
['淘汰', { name: 'Hannah', score: 78 }],
|
17
|
+
['淘汰', { name: 'Jack', score: 69 }],
|
18
|
+
]
|
19
|
+
|
20
|
+
let e = groupSortedEntries(x, defaultCompare)
|
21
|
+
let y = [
|
22
|
+
[
|
23
|
+
'录取',
|
24
|
+
[
|
25
|
+
{ name: 'Abby', score: 84 },
|
26
|
+
{ name: 'Chris', score: 89 },
|
27
|
+
{ name: 'Dianne', score: 99 },
|
28
|
+
{ name: 'Gillian', score: 91 },
|
29
|
+
{ name: 'Irene', score: 85 },
|
30
|
+
],
|
31
|
+
],
|
32
|
+
[
|
33
|
+
'淘汰',
|
34
|
+
[
|
35
|
+
{ name: 'Brad', score: 73 },
|
36
|
+
{ name: 'Eddy', score: 58 },
|
37
|
+
{ name: 'Fred', score: 67 },
|
38
|
+
{ name: 'Hannah', score: 78 },
|
39
|
+
{ name: 'Jack', score: 69 },
|
40
|
+
],
|
41
|
+
],
|
42
|
+
]
|
43
|
+
|
44
|
+
expect(y).toEqual(e)
|
45
|
+
})
|
46
|
+
})
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export * from "./Comparer"
|
2
|
+
export * from "./comparers"
|
3
|
+
// export * from "./differenceSet"
|
4
|
+
// export * from "./distinctArray"
|
5
|
+
// export * from "./findIndexInSet"
|
6
|
+
// export * from "./groupArrayBy"
|
7
|
+
// export * from "./groupSortedEntries"
|
8
|
+
// export * from "./intersectSet"
|
9
|
+
// export * from "./isEqualset"
|
10
|
+
// export * from "./isSubset"
|
11
|
+
// export * from "./isSuperset"
|
12
|
+
// export * from "./sortedArrayToSet"
|
13
|
+
// export * from "./unionSet"
|
14
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export * from "./Comparer"
|
2
|
+
export * from "./comparers"
|
3
|
+
// export * from "./differenceSet"
|
4
|
+
// export * from "./distinctArray"
|
5
|
+
// export * from "./findIndexInSet"
|
6
|
+
// export * from "./groupArrayBy"
|
7
|
+
// export * from "./groupSortedEntries"
|
8
|
+
// export * from "./intersectSet"
|
9
|
+
// export * from "./isEqualset"
|
10
|
+
// export * from "./isSubset"
|
11
|
+
// export * from "./isSuperset"
|
12
|
+
// export * from "./sortedArrayToSet"
|
13
|
+
// export * from "./unionSet"
|
14
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* Computes the intersection of two sorted sets.
|
3
|
+
*
|
4
|
+
* - Assumes both `set1` and `set2` are sorted in ascending order.
|
5
|
+
* - Uses the provided `compare` function to determine the order and equality of elements.
|
6
|
+
*
|
7
|
+
* @param set1 - The first sorted set.
|
8
|
+
* @param set2 - The second sorted set.
|
9
|
+
* @param compare - A comparison function that takes two elements and returns:
|
10
|
+
* - A negative number if the first element is less than the second.
|
11
|
+
* - 0 if the elements are equal.
|
12
|
+
* - A positive number if the first element is greater than the second.
|
13
|
+
* @returns A new array containing elements that are present in both `set1` and `set2`.
|
14
|
+
*/
|
15
|
+
export function intersectSet<T>(
|
16
|
+
set1: T[],
|
17
|
+
set2: T[],
|
18
|
+
compare: (a: T, b: T) => number
|
19
|
+
): T[];
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/**
|
2
|
+
* 计算两个有序数组的交集。
|
3
|
+
* @template T
|
4
|
+
* @param {T[]} set1 - 第一个已排序的数组
|
5
|
+
* @param {T[]} set2 - 第二个已排序的数组
|
6
|
+
* @param {(a: T, b: T) => number} compare - 比较函数
|
7
|
+
* @returns {T[]} 两数组的交集
|
8
|
+
*/
|
9
|
+
export function intersectSet(set1, set2, compare) {
|
10
|
+
/**
|
11
|
+
* 递归计算交集
|
12
|
+
* @param {T[]} acc - 当前交集结果
|
13
|
+
* @param {T[]} a - 剩余的 set1
|
14
|
+
* @param {T[]} b - 剩余的 set2
|
15
|
+
* @returns {T[]} 交集
|
16
|
+
*/
|
17
|
+
function loop(acc, a, b) {
|
18
|
+
if (a.length === 0 || b.length === 0) {
|
19
|
+
return acc
|
20
|
+
} else {
|
21
|
+
//每次去掉最大的元素
|
22
|
+
switch (Math.sign(compare(a[0], b[0]))) {
|
23
|
+
case -1:
|
24
|
+
return loop(acc, a.slice(1), b)
|
25
|
+
case 0:
|
26
|
+
return loop([...acc, a[0]], a.slice(1), b.slice(1))
|
27
|
+
case 1:
|
28
|
+
default:
|
29
|
+
return loop(acc, a, b.slice(1))
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
return loop([], set1, set2)
|
35
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { intersectSet } from './intersectSet'
|
2
|
+
import { defaultCompare } from '../comparison'
|
3
|
+
|
4
|
+
|
5
|
+
test('intersectSet test', () => {
|
6
|
+
//输入数组应该排序sort()
|
7
|
+
let a = [1, 2]
|
8
|
+
let b = [1, 3]
|
9
|
+
let c = [1, 2, 3]
|
10
|
+
|
11
|
+
expect(intersectSet(a, b, defaultCompare)).toEqual([1])
|
12
|
+
expect(intersectSet(a, c, defaultCompare)).toEqual(a)
|
13
|
+
expect(intersectSet(b, c, defaultCompare)).toEqual(b)
|
14
|
+
})
|