@formvk/shared 0.1.0 → 1.0.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/src/array.ts DELETED
@@ -1,200 +0,0 @@
1
- import { isArr, isObj, isStr } from './checkers'
2
-
3
- type EachArrayIterator<T> = (currentValue: T, key: number) => void | boolean
4
- type EachStringIterator = (currentValue: string, key: number) => void | boolean
5
- type EachObjectIterator<T = any> = (currentValue: T, key: string) => void | boolean
6
- type MapArrayIterator<TItem, TResult> = (currentValue: TItem, key: number) => TResult
7
- type MapStringIterator<TResult> = (currentValue: string, key: number) => TResult
8
- type MapObjectIterator<TItem, TResult> = (currentValue: TItem, key: string) => TResult
9
- type MemoArrayIterator<T, U> = (previousValue: U, currentValue: T, key: number) => U
10
- type MemoStringIterator<T> = (previousValue: T, currentValue: string, key: number) => T
11
- type MemoObjectIterator<TValue, TResult> = (previousValue: TResult, currentValue: TValue, key: string) => TResult
12
-
13
- export function toArr<T extends any, R = T extends (infer P)[] ? P[] : T extends any[] ? T : any[]>(val: T): R {
14
- return (isArr(val) ? val : val ? [val] : []) as R
15
- }
16
-
17
- export function each(val: string, iterator: EachStringIterator, revert?: boolean): void
18
- export function each<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): void
19
- export function each<T extends object, TValue extends T[keyof T]>(
20
- val: T,
21
- iterator: EachObjectIterator<TValue>,
22
- revert?: boolean
23
- ): void
24
- export function each(val: any, iterator: any, revert?: boolean): void {
25
- if (isArr(val) || isStr(val)) {
26
- if (revert) {
27
- for (let i: number = val.length - 1; i >= 0; i--) {
28
- if (iterator(val[i], i) === false) {
29
- return
30
- }
31
- }
32
- } else {
33
- for (let i = 0; i < val.length; i++) {
34
- if (iterator(val[i], i) === false) {
35
- return
36
- }
37
- }
38
- }
39
- } else if (isObj(val)) {
40
- const obj = val as Record<string, any>
41
- let key: string
42
- for (key in obj) {
43
- if (Object.hasOwnProperty.call(obj, key)) {
44
- if (iterator(obj[key], key) === false) {
45
- return
46
- }
47
- }
48
- }
49
- }
50
- }
51
-
52
- export function map<T>(val: string, iterator: MapStringIterator<T>, revert?: boolean): T[]
53
- export function map<TItem, TResult>(
54
- val: TItem[],
55
- iterator: MapArrayIterator<TItem, TResult>,
56
- revert?: boolean
57
- ): TResult[]
58
- export function map<T extends object, TResult>(
59
- val: T,
60
- iterator: MapObjectIterator<T[keyof T], TResult>,
61
- revert?: boolean
62
- ): Record<keyof T, TResult>
63
- export function map(val: any, iterator: any, revert?: any): any {
64
- const res = isArr(val) || isStr(val) ? [] : {}
65
- each(
66
- val,
67
- (item, key) => {
68
- const value = iterator(item, key)
69
- if (isArr(res)) {
70
- ;(res as any).push(value)
71
- } else {
72
- ;(res as Record<string, any>)[key] = value
73
- }
74
- },
75
- revert
76
- )
77
- return res
78
- }
79
-
80
- export function reduce<T, U>(val: T[], iterator: MemoArrayIterator<T, U>, accumulator?: U, revert?: boolean): U
81
- export function reduce<T>(val: string, iterator: MemoStringIterator<T>, accumulator?: T, revert?: boolean): T
82
- export function reduce<T extends object, TValue extends T[keyof T], TResult = any>(
83
- val: T,
84
- iterator: MemoObjectIterator<TValue, TResult>,
85
- accumulator?: TResult,
86
- revert?: boolean
87
- ): TResult
88
- export function reduce(val: any, iterator: any, accumulator?: any, revert?: boolean): any {
89
- let result = accumulator
90
- each(
91
- val,
92
- (item, key) => {
93
- result = iterator(result, item, key)
94
- },
95
- revert
96
- )
97
- return result
98
- }
99
-
100
- export function every<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): boolean
101
- export function every<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean
102
- export function every<T extends object>(val: T, iterator: EachObjectIterator, revert?: boolean): boolean
103
- export function every(val: any, iterator: any, revert?: boolean): boolean {
104
- let res = true
105
- each(
106
- val,
107
- (item, key) => {
108
- if (!iterator(item, key)) {
109
- res = false
110
- return false
111
- }
112
- },
113
- revert
114
- )
115
- return res
116
- }
117
-
118
- export function some<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): boolean
119
- export function some<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean
120
- export function some<T extends object>(val: T, iterator: EachObjectIterator, revert?: boolean): boolean
121
- export function some(val: any, iterator: any, revert?: boolean): boolean {
122
- let res = false
123
- each(
124
- val,
125
- (item, key) => {
126
- if (iterator(item, key)) {
127
- res = true
128
- return false
129
- }
130
- },
131
- revert
132
- )
133
- return res
134
- }
135
-
136
- export function findIndex<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): number
137
- export function findIndex<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): number
138
- export function findIndex<T extends object>(val: T, iterator: EachObjectIterator, revert?: boolean): keyof T
139
- export function findIndex(val: any, iterator: any, revert?: boolean): string | number {
140
- let res: number | string = -1
141
- each(
142
- val,
143
- (item, key) => {
144
- if (iterator(item, key)) {
145
- res = key
146
- return false
147
- }
148
- },
149
- revert
150
- )
151
- return res
152
- }
153
-
154
- export function find<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): any
155
- export function find<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): T
156
- export function find<T extends object>(val: T, iterator: EachObjectIterator, revert?: boolean): T[keyof T]
157
- export function find(val: any, iterator: any, revert?: boolean): any {
158
- let res: any
159
- each(
160
- val,
161
- (item, key) => {
162
- if (iterator(item, key)) {
163
- res = item
164
- return false
165
- }
166
- },
167
- revert
168
- )
169
- return res
170
- }
171
-
172
- export function includes<T extends string>(val: T, searchElement: string, revert?: boolean): boolean
173
- export function includes<T>(val: T[], searchElement: T, revert?: boolean): boolean
174
- export function includes(val: any, searchElement: any, revert?: boolean) {
175
- if (isStr(val)) return val.includes(searchElement)
176
- return some(val, item => item === searchElement, revert)
177
- }
178
-
179
- export function move<T>(array: T[], fromIndex: number, toIndex: number) {
180
- if (fromIndex === toIndex) return array
181
-
182
- if (toIndex < 0 || fromIndex < 0 || toIndex > array.length - 1 || fromIndex > array.length - 1) {
183
- return array
184
- }
185
-
186
- if (fromIndex < toIndex) {
187
- const fromItem = array[fromIndex]
188
- for (let index = fromIndex; index < toIndex; index++) {
189
- array[index] = array[index + 1]
190
- }
191
- array[toIndex] = fromItem
192
- } else {
193
- const fromItem = array[fromIndex]
194
- for (let index = fromIndex; index > toIndex; index--) {
195
- array[index] = array[index - 1]
196
- }
197
- array[toIndex] = fromItem
198
- }
199
- return array
200
- }
package/src/checkers.ts DELETED
@@ -1,47 +0,0 @@
1
- const toString = Object.prototype.toString
2
- function isType<T>(type: string | string[]) {
3
- return (obj: unknown): obj is T => getType(obj) === `[object ${type}]`
4
- }
5
- export function getType(obj: any) {
6
- return toString.call(obj)
7
- }
8
- export function isFn(val: any): val is Function {
9
- return typeof val === 'function'
10
- }
11
- export const isArr = Array.isArray
12
- export const isPlainObj = isType<object>('Object')
13
- export const isStr = isType<string>('String')
14
- export const isBool = isType<boolean>('Boolean')
15
- export const isNum = isType<number>('Number')
16
- export function isMap(val: any): val is Map<any, any> {
17
- return val && val instanceof Map
18
- }
19
- export function isSet(val: any): val is Set<any> {
20
- return val && val instanceof Set
21
- }
22
- export function isWeakMap(val: any): val is WeakMap<any, any> {
23
- return val && val instanceof WeakMap
24
- }
25
- export function isWeakSet(val: any): val is WeakSet<any> {
26
- return val && val instanceof WeakSet
27
- }
28
- export function isNumberLike(index: any): index is number {
29
- return isNum(index) || /^\d+$/.test(index)
30
- }
31
- export function isObj(val: unknown): val is object {
32
- return typeof val === 'object'
33
- }
34
- export const isRegExp = isType<RegExp>('RegExp')
35
- export function isReactElement(obj: any): boolean {
36
- return obj && obj['$$typeof'] && obj['_owner']
37
- }
38
- export function isHTMLElement(target: any): target is EventTarget {
39
- return Object.prototype.toString.call(target).indexOf('HTML') > -1
40
- }
41
-
42
- export type Subscriber<S> = (payload: S) => void
43
-
44
- export interface Subscription<S> {
45
- notify?: (payload: S) => void | boolean
46
- filter?: (payload: S) => any
47
- }
package/src/clone.ts DELETED
@@ -1,66 +0,0 @@
1
- import { isFn, isPlainObj } from './checkers'
2
-
3
- export function shallowClone(values: any) {
4
- if (Array.isArray(values)) {
5
- return values.slice(0)
6
- } else if (isPlainObj(values)) {
7
- const plain = values as Record<string, any>
8
- if ('$$typeof' in plain && '_owner' in plain) {
9
- return values
10
- }
11
- if (plain['_isAMomentObject']) {
12
- return values
13
- }
14
- if (plain['_isJSONSchemaObject']) {
15
- return values
16
- }
17
- if (isFn(plain['toJS'])) {
18
- return values
19
- }
20
- if (isFn(plain['toJSON'])) {
21
- return values
22
- }
23
- return {
24
- ...plain,
25
- }
26
- } else if (typeof values === 'object') {
27
- return new values.constructor(values)
28
- }
29
- return values
30
- }
31
-
32
- export function clone<T>(values: T): T {
33
- if (Array.isArray(values)) {
34
- const res: any[] = []
35
- values.forEach(item => {
36
- res.push(clone(item))
37
- })
38
- return res as T
39
- } else if (isPlainObj(values)) {
40
- const plain = values as Record<string, any>
41
- if ('$$typeof' in plain && '_owner' in plain) {
42
- return values
43
- }
44
- if (plain['_isAMomentObject']) {
45
- return values
46
- }
47
- if (plain['_isJSONSchemaObject']) {
48
- return values
49
- }
50
- if (isFn(plain['toJS'])) {
51
- return plain['toJS']()
52
- }
53
- if (isFn(plain['toJSON'])) {
54
- return plain['toJSON']()
55
- }
56
- const res: any = {}
57
- for (const key in plain) {
58
- if (Object.hasOwnProperty.call(plain, key)) {
59
- res[key] = clone(plain[key])
60
- }
61
- }
62
- return res as T
63
- } else {
64
- return values
65
- }
66
- }
package/src/compare.ts DELETED
@@ -1,136 +0,0 @@
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 DELETED
@@ -1,44 +0,0 @@
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 DELETED
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,23 +0,0 @@
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 DELETED
@@ -1,15 +0,0 @@
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'
package/src/instanceof.ts DELETED
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,89 +0,0 @@
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
- }