@formvk/shared 0.0.0-stage → 0.1.0
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/dist/index.d.ts +181 -0
- package/dist/index.js +1347 -0
- package/dist/index.js.map +1 -0
- package/package.json +21 -3
- package/src/array.ts +200 -0
- package/src/checkers.ts +47 -0
- package/src/clone.ts +66 -0
- package/src/compare.ts +136 -0
- package/src/defaults.ts +44 -0
- package/src/deprecate.ts +21 -0
- package/src/global.ts +23 -0
- package/src/index.ts +15 -0
- package/src/instanceof.ts +14 -0
- package/src/isEmpty.ts +89 -0
- package/src/merge.ts +226 -0
- package/src/middleware.ts +18 -0
- package/src/path.ts +5 -0
- package/src/string.ts +38 -0
- package/src/subscribable.ts +53 -0
- package/src/uid.ts +10 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +21 -0
- package/README.md +0 -3
package/src/compare.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { isArr } from './checkers'
|
|
2
|
+
import { instOf } from './instanceof'
|
|
3
|
+
const isArray = isArr
|
|
4
|
+
const keyList = Object.keys
|
|
5
|
+
const hasProp = Object.prototype.hasOwnProperty
|
|
6
|
+
|
|
7
|
+
/* eslint-disable */
|
|
8
|
+
function equal(a: any, b: any) {
|
|
9
|
+
// fast-deep-equal index.js 2.0.1
|
|
10
|
+
if (a === b) {
|
|
11
|
+
return true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
15
|
+
const arrA = isArray(a)
|
|
16
|
+
const arrB = isArray(b)
|
|
17
|
+
let i: number
|
|
18
|
+
let length: number
|
|
19
|
+
let key: string | number
|
|
20
|
+
|
|
21
|
+
if (arrA && arrB) {
|
|
22
|
+
length = a.length
|
|
23
|
+
if (length !== b.length) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
for (i = length; i-- !== 0;) {
|
|
27
|
+
if (!equal(a[i], b[i])) {
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (arrA !== arrB) {
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
const momentA = a && a._isAMomentObject
|
|
38
|
+
const momentB = b && b._isAMomentObject
|
|
39
|
+
if (momentA !== momentB) return false
|
|
40
|
+
if (momentA && momentB) return a.isSame(b)
|
|
41
|
+
const immutableA = a && a.toJS
|
|
42
|
+
const immutableB = b && b.toJS
|
|
43
|
+
if (immutableA !== immutableB) return false
|
|
44
|
+
if (immutableA) return a.is ? a.is(b) : a === b
|
|
45
|
+
const dateA = instOf(a, 'Date')
|
|
46
|
+
const dateB = instOf(b, 'Date')
|
|
47
|
+
if (dateA !== dateB) {
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
if (dateA && dateB) {
|
|
51
|
+
return a.getTime() === b.getTime()
|
|
52
|
+
}
|
|
53
|
+
const regexpA = instOf(a, 'RegExp')
|
|
54
|
+
const regexpB = instOf(b, 'RegExp')
|
|
55
|
+
if (regexpA !== regexpB) {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
if (regexpA && regexpB) {
|
|
59
|
+
return a.toString() === b.toString()
|
|
60
|
+
}
|
|
61
|
+
const urlA = instOf(a, 'URL')
|
|
62
|
+
const urlB = instOf(b, 'URL')
|
|
63
|
+
|
|
64
|
+
if (urlA !== urlB) {
|
|
65
|
+
return false
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (urlA && urlB) {
|
|
69
|
+
return a.href === b.href
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const schemaA = a && a.toJSON
|
|
73
|
+
const schemaB = b && b.toJSON
|
|
74
|
+
if (schemaA !== schemaB) return false
|
|
75
|
+
if (schemaA && schemaB) return equal(a.toJSON(), b.toJSON())
|
|
76
|
+
|
|
77
|
+
const keys = keyList(a)
|
|
78
|
+
length = keys.length
|
|
79
|
+
|
|
80
|
+
if (length !== keyList(b).length) {
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (i = length; i-- !== 0;) {
|
|
85
|
+
if (!hasProp.call(b, keys[i])) {
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// end fast-deep-equal
|
|
90
|
+
|
|
91
|
+
// Custom handling for React
|
|
92
|
+
for (i = length; i-- !== 0;) {
|
|
93
|
+
key = keys[i]
|
|
94
|
+
|
|
95
|
+
if (key === '_owner' && a.$$typeof) {
|
|
96
|
+
// React-specific: avoid traversing React elements' _owner.
|
|
97
|
+
// _owner contains circular references
|
|
98
|
+
// and is not needed when comparing the actual elements (and not their owners)
|
|
99
|
+
// .$$typeof and ._store on just reasonable markers of a react element
|
|
100
|
+
continue
|
|
101
|
+
} else {
|
|
102
|
+
// all other properties should be traversed as usual
|
|
103
|
+
if (!equal(a[key], b[key])) {
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// fast-deep-equal index.js 2.0.1
|
|
110
|
+
return true
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return a !== a && b !== b
|
|
114
|
+
}
|
|
115
|
+
// end fast-deep-equal
|
|
116
|
+
|
|
117
|
+
export const isEqual = function exportedEqual(a: any, b: any) {
|
|
118
|
+
try {
|
|
119
|
+
return equal(a, b)
|
|
120
|
+
} catch (error) {
|
|
121
|
+
const err = error as { message?: string; number?: number; name?: string }
|
|
122
|
+
/* istanbul ignore next */
|
|
123
|
+
if ((err.message && err.message.match(/stack|recursion/i)) || err.number === -2146828260) {
|
|
124
|
+
// warn on circular references, don't crash
|
|
125
|
+
// browsers give this different errors name and messages:
|
|
126
|
+
// chrome/safari: "RangeError", "Maximum call stack size exceeded"
|
|
127
|
+
// firefox: "InternalError", too much recursion"
|
|
128
|
+
// edge: "Error", "Out of stack space"
|
|
129
|
+
console.warn('Warning: react-fast-compare does not handle circular references.', err.name, err.message)
|
|
130
|
+
return false
|
|
131
|
+
}
|
|
132
|
+
// some other error. we should definitely know about these
|
|
133
|
+
/* istanbul ignore next */
|
|
134
|
+
throw error
|
|
135
|
+
}
|
|
136
|
+
}
|
package/src/defaults.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { each } from './array'
|
|
2
|
+
import { getType, isArr, isPlainObj } from './checkers'
|
|
3
|
+
import { isEmpty, isValid } from './isEmpty'
|
|
4
|
+
|
|
5
|
+
function isUnNormalObject(value: any) {
|
|
6
|
+
if (value?._owner && value?.$$typeof) {
|
|
7
|
+
return true
|
|
8
|
+
}
|
|
9
|
+
if (value?._isAMomentObject || value?._isJSONSchemaObject) {
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
if (value?.toJS || value?.toJSON) {
|
|
13
|
+
return true
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isEnumerableObject(val: any) {
|
|
18
|
+
if (isUnNormalObject(val)) {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
return typeof val === 'object'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @param defaults
|
|
27
|
+
* @param targets
|
|
28
|
+
*/
|
|
29
|
+
export function defaults(defaults_: any, targets: any) {
|
|
30
|
+
if (getType(defaults_) !== getType(targets) || !isEnumerableObject(defaults_) || !isEnumerableObject(targets)) {
|
|
31
|
+
return !isEmpty(targets) ? targets : defaults_
|
|
32
|
+
} else {
|
|
33
|
+
const results = isArr(defaults_) ? [] : isPlainObj(defaults_) ? {} : defaults_
|
|
34
|
+
each(targets, (value, key) => {
|
|
35
|
+
results[key] = defaults(defaults_[key], value)
|
|
36
|
+
})
|
|
37
|
+
each(defaults_, (value, key) => {
|
|
38
|
+
if (!isValid(results[key])) {
|
|
39
|
+
results[key] = value
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
return results
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/deprecate.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isFn, isStr } from './checkers'
|
|
2
|
+
|
|
3
|
+
const caches: Record<string, boolean> = {}
|
|
4
|
+
|
|
5
|
+
export function deprecate<P1 = any, P2 = any, P3 = any, P4 = any, P5 = any>(
|
|
6
|
+
method: any,
|
|
7
|
+
message?: string,
|
|
8
|
+
help?: string
|
|
9
|
+
) {
|
|
10
|
+
if (isFn(method)) {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
12
|
+
return function (this: any, p1?: P1, p2?: P2, p3?: P3, p4?: P4, p5?: P5) {
|
|
13
|
+
deprecate(message, help)
|
|
14
|
+
return method.apply(this, arguments)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (isStr(method) && !caches[method]) {
|
|
18
|
+
caches[method] = true
|
|
19
|
+
console.warn(new Error(`${method} has been deprecated. Do not continue to use this api.${message || ''}`))
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/global.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable no-empty */
|
|
2
|
+
/* istanbul ignore next */
|
|
3
|
+
function globalSelf() {
|
|
4
|
+
try {
|
|
5
|
+
if (typeof self !== 'undefined') {
|
|
6
|
+
return self
|
|
7
|
+
}
|
|
8
|
+
} catch (e) {}
|
|
9
|
+
try {
|
|
10
|
+
if (typeof window !== 'undefined') {
|
|
11
|
+
return window
|
|
12
|
+
}
|
|
13
|
+
} catch (e) {}
|
|
14
|
+
try {
|
|
15
|
+
// @ts-expect-error
|
|
16
|
+
if (typeof global !== 'undefined') {
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
return global
|
|
19
|
+
}
|
|
20
|
+
} catch (e) {}
|
|
21
|
+
return Function('return this')()
|
|
22
|
+
}
|
|
23
|
+
export const globalThisPolyfill: Window = globalSelf()
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './array'
|
|
2
|
+
export * from './compare'
|
|
3
|
+
export * from './checkers'
|
|
4
|
+
export * from './clone'
|
|
5
|
+
export * from './isEmpty'
|
|
6
|
+
export * from './string'
|
|
7
|
+
export * from './global'
|
|
8
|
+
export * from './path'
|
|
9
|
+
export * from './deprecate'
|
|
10
|
+
export * from './subscribable'
|
|
11
|
+
export * from './middleware'
|
|
12
|
+
export * from './merge'
|
|
13
|
+
export * from './instanceof'
|
|
14
|
+
export * from './defaults'
|
|
15
|
+
export * from './uid'
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isStr, isFn } from './checkers'
|
|
2
|
+
import { globalThisPolyfill } from './global'
|
|
3
|
+
|
|
4
|
+
export function instOf<
|
|
5
|
+
K extends keyof typeof window & string & {},
|
|
6
|
+
R = K extends keyof typeof window ? InstanceType<(typeof window)[K]> : unknown,
|
|
7
|
+
>(value: any, cls: K): value is R {
|
|
8
|
+
if (isFn(cls)) return value instanceof cls
|
|
9
|
+
if (isStr(cls)) {
|
|
10
|
+
const ctor = (globalThisPolyfill as Record<string, any>)[cls]
|
|
11
|
+
return ctor ? value instanceof ctor : false
|
|
12
|
+
}
|
|
13
|
+
return false
|
|
14
|
+
}
|
package/src/isEmpty.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { instOf } from './instanceof'
|
|
2
|
+
const has = Object.prototype.hasOwnProperty
|
|
3
|
+
|
|
4
|
+
const toString = Object.prototype.toString
|
|
5
|
+
|
|
6
|
+
export function isUndef<T>(val: T): val is T extends undefined ? T : never {
|
|
7
|
+
return val === undefined
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function isValid<T>(val: T): val is T extends undefined ? never : T extends null ? never : T {
|
|
11
|
+
return val !== undefined && val !== null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function isEmpty<T>(val: T, strict = false): boolean {
|
|
15
|
+
// Null and Undefined...
|
|
16
|
+
if (val == null) {
|
|
17
|
+
return true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Booleans...
|
|
21
|
+
if (typeof val === 'boolean') {
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Numbers...
|
|
26
|
+
if (typeof val === 'number') {
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Strings...
|
|
31
|
+
if (typeof val === 'string') {
|
|
32
|
+
return val.length === 0
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Functions...
|
|
36
|
+
if (typeof val === 'function') {
|
|
37
|
+
return val.length === 0
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Arrays...
|
|
41
|
+
if (Array.isArray(val)) {
|
|
42
|
+
if (val.length === 0) {
|
|
43
|
+
return true
|
|
44
|
+
}
|
|
45
|
+
for (let i = 0; i < val.length; i++) {
|
|
46
|
+
if (strict) {
|
|
47
|
+
if (val[i] !== undefined && val[i] !== null) {
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
if (val[i] !== undefined && val[i] !== null && val[i] !== '' && val[i] !== 0) {
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return true
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Errors...
|
|
60
|
+
if (instOf(val, 'Error')) {
|
|
61
|
+
return val.message === ''
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Objects...
|
|
65
|
+
if (val.toString === toString) {
|
|
66
|
+
switch (val.toString()) {
|
|
67
|
+
// Maps, Sets, Files and Errors...
|
|
68
|
+
case '[object File]':
|
|
69
|
+
case '[object Map]':
|
|
70
|
+
case '[object Set]': {
|
|
71
|
+
return (val as any).size === 0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Plain objects...
|
|
75
|
+
case '[object Object]': {
|
|
76
|
+
for (const key in val) {
|
|
77
|
+
if (has.call(val, key)) {
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return true
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Anything else...
|
|
88
|
+
return false
|
|
89
|
+
}
|
package/src/merge.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { isFn, isPlainObj } from './checkers'
|
|
2
|
+
import { isEmpty, isValid } from './isEmpty'
|
|
3
|
+
|
|
4
|
+
function defaultIsMergeableObject(value: any) {
|
|
5
|
+
return isNonNullObject(value) && !isSpecial(value)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function isNonNullObject(value: any) {
|
|
9
|
+
// TODO: value !== null && typeof value === 'object'
|
|
10
|
+
return Boolean(value) && typeof value === 'object'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isSpecial(value: any) {
|
|
14
|
+
// TODO: use isComplexObject()
|
|
15
|
+
if ('$$typeof' in value && '_owner' in value) {
|
|
16
|
+
return true
|
|
17
|
+
}
|
|
18
|
+
if (value._isAMomentObject) {
|
|
19
|
+
return true
|
|
20
|
+
}
|
|
21
|
+
if (value._isJSONSchemaObject) {
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
if (isFn(value.toJS)) {
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
if (isFn(value.toJSON)) {
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
30
|
+
return !isPlainObj(value)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function emptyTarget(val: any) {
|
|
34
|
+
return Array.isArray(val) ? [] : {}
|
|
35
|
+
}
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
function cloneUnlessOtherwiseSpecified(value: any, options: Options) {
|
|
38
|
+
if (options.clone !== false && options.isMergeableObject?.(value)) {
|
|
39
|
+
return deepmerge(emptyTarget(value), value, options)
|
|
40
|
+
}
|
|
41
|
+
return value
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function defaultArrayMerge(target: any, source: any, options: Options) {
|
|
45
|
+
return target.concat(source).map(function (element: any) {
|
|
46
|
+
return cloneUnlessOtherwiseSpecified(element, options)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getMergeFunction(key: string, options: Options) {
|
|
51
|
+
if (!options.customMerge) {
|
|
52
|
+
return deepmerge
|
|
53
|
+
}
|
|
54
|
+
const customMerge = options.customMerge(key)
|
|
55
|
+
return typeof customMerge === 'function' ? customMerge : deepmerge
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getEnumerableOwnPropertySymbols(target: any): any {
|
|
59
|
+
return Object.getOwnPropertySymbols
|
|
60
|
+
? Object.getOwnPropertySymbols(target).filter(function (symbol) {
|
|
61
|
+
return target.propertyIsEnumerable(symbol)
|
|
62
|
+
})
|
|
63
|
+
: []
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getKeys(target: any) {
|
|
67
|
+
if (!isValid(target)) return []
|
|
68
|
+
return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function propertyIsOnObject(object: any, property: any) {
|
|
72
|
+
/* istanbul ignore next */
|
|
73
|
+
try {
|
|
74
|
+
return property in object
|
|
75
|
+
} catch (_) {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Protects from prototype poisoning and unexpected merging up the prototype chain.
|
|
81
|
+
function propertyIsUnsafe(target: any, key: PropertyKey) {
|
|
82
|
+
return (
|
|
83
|
+
propertyIsOnObject(target, key) && // Properties are safe to merge if they don't exist in the target yet,
|
|
84
|
+
!(
|
|
85
|
+
Object.hasOwnProperty.call(target, key) && // unsafe if they exist up the prototype chain,
|
|
86
|
+
Object.propertyIsEnumerable.call(target, key)
|
|
87
|
+
)
|
|
88
|
+
) // and also unsafe if they're nonenumerable.
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function mergeObject(target: any, source: any, options: Options) {
|
|
92
|
+
const destination = options.assign ? target || {} : {}
|
|
93
|
+
if (!options.isMergeableObject?.(target)) return target
|
|
94
|
+
if (!options.assign) {
|
|
95
|
+
getKeys(target).forEach(function (key) {
|
|
96
|
+
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options)
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
getKeys(source).forEach(function (key) {
|
|
100
|
+
/* istanbul ignore next */
|
|
101
|
+
if (propertyIsUnsafe(target, key)) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
if (isEmpty(target[key])) {
|
|
105
|
+
destination[key] = source[key]
|
|
106
|
+
} else if (
|
|
107
|
+
propertyIsOnObject(target, key) &&
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
options.isMergeableObject(source[key])
|
|
110
|
+
) {
|
|
111
|
+
destination[key] = getMergeFunction(key, options)(target[key], source[key], options)
|
|
112
|
+
} else {
|
|
113
|
+
destination[key] = cloneUnlessOtherwiseSpecified(source[key], options)
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
return destination
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface Options {
|
|
120
|
+
arrayMerge?(target: any[], source: any[], options?: Options): any[]
|
|
121
|
+
clone?: boolean
|
|
122
|
+
assign?: boolean
|
|
123
|
+
customMerge?: (key: string, options?: Options) => ((x: any, y: any) => any) | undefined
|
|
124
|
+
isMergeableObject?(value: object): boolean
|
|
125
|
+
cloneUnlessOtherwiseSpecified?: (value: any, options: Options) => any
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
function deepmerge(target: any, source: any, options?: Options) {
|
|
130
|
+
options = options || {}
|
|
131
|
+
options.arrayMerge = options.arrayMerge || defaultArrayMerge
|
|
132
|
+
options.isMergeableObject = options.isMergeableObject || defaultIsMergeableObject
|
|
133
|
+
// cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge()
|
|
134
|
+
// implementations can use it. The caller may not replace it.
|
|
135
|
+
options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified
|
|
136
|
+
|
|
137
|
+
const sourceIsArray = Array.isArray(source)
|
|
138
|
+
const targetIsArray = Array.isArray(target)
|
|
139
|
+
const sourceAndTargetTypesMatch = sourceIsArray === targetIsArray
|
|
140
|
+
|
|
141
|
+
if (!sourceAndTargetTypesMatch) {
|
|
142
|
+
return cloneUnlessOtherwiseSpecified(source, options)
|
|
143
|
+
} else if (sourceIsArray) {
|
|
144
|
+
return options.arrayMerge(target, source, options)
|
|
145
|
+
} else {
|
|
146
|
+
return mergeObject(target, source, options)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function lazyMerge<T extends object | Function>(target: T, ...args: T[]): any {
|
|
151
|
+
function _lazyMerge<T extends object | Function>(target: T, source: T): {} {
|
|
152
|
+
if (!isValid(source)) return target
|
|
153
|
+
if (!isValid(target)) return source
|
|
154
|
+
const isTargetObject = typeof target === 'object'
|
|
155
|
+
const isSourceObject = typeof source === 'object'
|
|
156
|
+
const isTargetFn = typeof target === 'function'
|
|
157
|
+
const isSourceFn = typeof source === 'function'
|
|
158
|
+
if (!isTargetObject && !isTargetFn) return source
|
|
159
|
+
if (!isSourceObject && !isSourceFn) return target
|
|
160
|
+
function getTarget() {
|
|
161
|
+
return typeof target === 'function' ? (target as Function)() : target
|
|
162
|
+
}
|
|
163
|
+
function getSource() {
|
|
164
|
+
return typeof source === 'function' ? (source as Function)() : source
|
|
165
|
+
}
|
|
166
|
+
function set(_: object, key: PropertyKey, value: any) {
|
|
167
|
+
const source = getSource()
|
|
168
|
+
const target = getTarget()
|
|
169
|
+
if (key in source) {
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
source[key] = value
|
|
172
|
+
} else if (key in target) {
|
|
173
|
+
// @ts-ignore
|
|
174
|
+
target[key] = value
|
|
175
|
+
} else {
|
|
176
|
+
source[key] = value
|
|
177
|
+
}
|
|
178
|
+
return true
|
|
179
|
+
}
|
|
180
|
+
function get(_: object, key: PropertyKey) {
|
|
181
|
+
const source = getSource()
|
|
182
|
+
// @ts-ignore
|
|
183
|
+
if (key in source) {
|
|
184
|
+
return source[key]
|
|
185
|
+
}
|
|
186
|
+
// @ts-ignore
|
|
187
|
+
return getTarget()[key]
|
|
188
|
+
}
|
|
189
|
+
function ownKeys() {
|
|
190
|
+
const source = getSource()
|
|
191
|
+
const target = getTarget()
|
|
192
|
+
const keys = Object.keys(target)
|
|
193
|
+
for (const key in source) {
|
|
194
|
+
if (!(key in target)) {
|
|
195
|
+
keys.push(key)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return keys
|
|
199
|
+
}
|
|
200
|
+
function getOwnPropertyDescriptor(_: object, key: PropertyKey) {
|
|
201
|
+
return {
|
|
202
|
+
value: get(_, key),
|
|
203
|
+
enumerable: true,
|
|
204
|
+
configurable: true,
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function has(_: object, key: PropertyKey) {
|
|
208
|
+
if (key in getSource() || key in getTarget()) return true
|
|
209
|
+
return false
|
|
210
|
+
}
|
|
211
|
+
function getPrototypeOf() {
|
|
212
|
+
return Object.getPrototypeOf({})
|
|
213
|
+
}
|
|
214
|
+
return new Proxy(Object.create(null), {
|
|
215
|
+
set,
|
|
216
|
+
get,
|
|
217
|
+
ownKeys,
|
|
218
|
+
getPrototypeOf,
|
|
219
|
+
getOwnPropertyDescriptor,
|
|
220
|
+
has,
|
|
221
|
+
}) as any
|
|
222
|
+
}
|
|
223
|
+
return args.reduce<{}>((buf, arg) => _lazyMerge(buf, arg), target)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export const merge = deepmerge
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface IMiddleware<Payload = any, Result = any> {
|
|
2
|
+
(payload: Payload, next: (payload?: Payload) => Result): Result
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function applyMiddleware(payload: any, fns: IMiddleware[] = []) {
|
|
6
|
+
function compose(payload: any, fns: IMiddleware[]): Promise<any> {
|
|
7
|
+
const prevPayload = payload
|
|
8
|
+
return Promise.resolve(fns[0](payload, payload => compose(payload ?? prevPayload, fns.slice(1))))
|
|
9
|
+
}
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
compose(
|
|
12
|
+
payload,
|
|
13
|
+
fns.concat(payload => {
|
|
14
|
+
resolve(payload)
|
|
15
|
+
})
|
|
16
|
+
).catch(reject)
|
|
17
|
+
})
|
|
18
|
+
}
|
package/src/path.ts
ADDED
package/src/string.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// ansiRegex
|
|
2
|
+
function ansiRegex() {
|
|
3
|
+
const pattern = [
|
|
4
|
+
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
|
|
5
|
+
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))',
|
|
6
|
+
].join('|')
|
|
7
|
+
|
|
8
|
+
return new RegExp(pattern, 'g')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// astralRegex
|
|
12
|
+
const regex = '[\uD800-\uDBFF][\uDC00-\uDFFF]'
|
|
13
|
+
|
|
14
|
+
function astralRegex(opts?: { exact: boolean }) {
|
|
15
|
+
return opts && opts.exact ? new RegExp(`^${regex}$`) : new RegExp(regex, 'g')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// stripAnsi
|
|
19
|
+
function stripAnsi(input: any) {
|
|
20
|
+
return typeof input === 'string' ? input.replace(ansiRegex(), '') : input
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function stringLength(input: string) {
|
|
24
|
+
return stripAnsi(input).replace(astralRegex(), ' ').length
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function lowerCase(input: string) {
|
|
28
|
+
return input.toLowerCase()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 'validate-error' -> 'ValidateError' */
|
|
32
|
+
export function pascalCase(input: string) {
|
|
33
|
+
return input
|
|
34
|
+
.split(/[-_\s]+/)
|
|
35
|
+
.filter(Boolean)
|
|
36
|
+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
37
|
+
.join('')
|
|
38
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { each } from './array'
|
|
2
|
+
import type { Subscriber, Subscription } from './checkers'
|
|
3
|
+
import { isFn } from './checkers'
|
|
4
|
+
|
|
5
|
+
export class Subscribable<Payload = any> {
|
|
6
|
+
subscribers: {
|
|
7
|
+
index: number
|
|
8
|
+
[key: number]: Subscriber<Payload>
|
|
9
|
+
} = {
|
|
10
|
+
index: 0,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
subscription!: Subscription<Payload>
|
|
14
|
+
|
|
15
|
+
subscribe = (callback?: Subscriber<Payload>): number | undefined => {
|
|
16
|
+
if (isFn(callback)) {
|
|
17
|
+
const index: number = this.subscribers.index + 1
|
|
18
|
+
this.subscribers[index] = callback
|
|
19
|
+
this.subscribers.index++
|
|
20
|
+
return index
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
unsubscribe = (index?: number) => {
|
|
25
|
+
if (!index && typeof index !== 'number') {
|
|
26
|
+
this.subscribers = {
|
|
27
|
+
index: 0,
|
|
28
|
+
}
|
|
29
|
+
} else if (this.subscribers[index]) {
|
|
30
|
+
delete this.subscribers[index]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
notify = (payload: Payload, silent?: boolean) => {
|
|
35
|
+
if (this.subscription) {
|
|
36
|
+
if (this.subscription && isFn(this.subscription.notify)) {
|
|
37
|
+
if (this.subscription.notify.call(this, payload) === false) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (silent) return
|
|
43
|
+
const filter = (payload: Payload) => {
|
|
44
|
+
if (this.subscription && isFn(this.subscription.filter)) {
|
|
45
|
+
return this.subscription.filter.call(this, payload)
|
|
46
|
+
}
|
|
47
|
+
return payload
|
|
48
|
+
}
|
|
49
|
+
each(this.subscribers, (callback: any) => {
|
|
50
|
+
if (isFn(callback)) callback(filter(payload))
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
}
|