@formily-design/shared 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/LICENSE.md +20 -0
- package/README.md +1 -0
- package/dist/designable.shared.umd.production.js +1 -0
- package/dist/designable.shared.umd.production.min.js +2051 -0
- package/esm/animation.d.ts +2 -0
- package/esm/animation.js +33 -0
- package/esm/array.d.ts +37 -0
- package/esm/array.js +111 -0
- package/esm/clone.d.ts +4 -0
- package/esm/clone.js +97 -0
- package/esm/compose.d.ts +1 -0
- package/esm/compose.js +7 -0
- package/esm/coordinate.d.ts +106 -0
- package/esm/coordinate.js +475 -0
- package/esm/element.d.ts +6 -0
- package/esm/element.js +137 -0
- package/esm/event.d.ts +96 -0
- package/esm/event.js +213 -0
- package/esm/globalThisPolyfill.d.ts +1 -0
- package/esm/globalThisPolyfill.js +22 -0
- package/esm/index.d.ts +17 -0
- package/esm/index.js +17 -0
- package/esm/instanceof.d.ts +1 -0
- package/esm/instanceof.js +11 -0
- package/esm/keycode.d.ts +147 -0
- package/esm/keycode.js +150 -0
- package/esm/lru.d.ts +73 -0
- package/esm/lru.js +293 -0
- package/esm/observer.d.ts +9 -0
- package/esm/observer.js +29 -0
- package/esm/request-idle.d.ts +10 -0
- package/esm/request-idle.js +8 -0
- package/esm/scroller.d.ts +10 -0
- package/esm/scroller.js +82 -0
- package/esm/subscribable.d.ts +14 -0
- package/esm/subscribable.js +48 -0
- package/esm/types.d.ts +13 -0
- package/esm/types.js +21 -0
- package/esm/uid.d.ts +1 -0
- package/esm/uid.js +9 -0
- package/lib/animation.d.ts +2 -0
- package/lib/animation.js +38 -0
- package/lib/array.d.ts +37 -0
- package/lib/array.js +125 -0
- package/lib/clone.d.ts +4 -0
- package/lib/clone.js +102 -0
- package/lib/compose.d.ts +1 -0
- package/lib/compose.js +11 -0
- package/lib/coordinate.d.ts +106 -0
- package/lib/coordinate.js +504 -0
- package/lib/element.d.ts +6 -0
- package/lib/element.js +145 -0
- package/lib/event.d.ts +96 -0
- package/lib/event.js +218 -0
- package/lib/globalThisPolyfill.d.ts +1 -0
- package/lib/globalThisPolyfill.js +25 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +33 -0
- package/lib/instanceof.d.ts +1 -0
- package/lib/instanceof.js +15 -0
- package/lib/keycode.d.ts +147 -0
- package/lib/keycode.js +154 -0
- package/lib/lru.d.ts +73 -0
- package/lib/lru.js +297 -0
- package/lib/observer.d.ts +9 -0
- package/lib/observer.js +33 -0
- package/lib/request-idle.d.ts +10 -0
- package/lib/request-idle.js +13 -0
- package/lib/scroller.d.ts +10 -0
- package/lib/scroller.js +88 -0
- package/lib/subscribable.d.ts +14 -0
- package/lib/subscribable.js +52 -0
- package/lib/types.d.ts +13 -0
- package/lib/types.js +29 -0
- package/lib/uid.d.ts +1 -0
- package/lib/uid.js +12 -0
- package/package.json +32 -0
- package/rollup.config.js +3 -0
- package/src/animation.ts +38 -0
- package/src/array.ts +301 -0
- package/src/clone.ts +96 -0
- package/src/compose.ts +7 -0
- package/src/coordinate.ts +633 -0
- package/src/element.ts +144 -0
- package/src/event.ts +379 -0
- package/src/globalThisPolyfill.ts +19 -0
- package/src/index.ts +17 -0
- package/src/instanceof.ts +10 -0
- package/src/keycode.ts +150 -0
- package/src/lru.ts +322 -0
- package/src/observer.ts +38 -0
- package/src/request-idle.ts +22 -0
- package/src/scroller.ts +113 -0
- package/src/subscribable.ts +60 -0
- package/src/types.ts +27 -0
- package/src/uid.ts +10 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +5 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare const UNSUBSCRIBE_ID_SYMBOL: unique symbol;
|
|
2
|
+
export interface ISubscriber<Payload = any> {
|
|
3
|
+
(payload: Payload): void | boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare class Subscribable<ExtendsType = any> {
|
|
6
|
+
private subscribers;
|
|
7
|
+
dispatch<T extends ExtendsType = any>(event: T, context?: any): boolean;
|
|
8
|
+
subscribe(subscriber: ISubscriber): {
|
|
9
|
+
(): void;
|
|
10
|
+
[UNSUBSCRIBE_ID_SYMBOL]: number;
|
|
11
|
+
};
|
|
12
|
+
unsubscribe: (id?: number | string | (() => void)) => void;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Subscribable = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const UNSUBSCRIBE_ID_SYMBOL = Symbol('UNSUBSCRIBE_ID_SYMBOL');
|
|
6
|
+
class Subscribable {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.subscribers = {
|
|
9
|
+
index: 0,
|
|
10
|
+
};
|
|
11
|
+
this.unsubscribe = (id) => {
|
|
12
|
+
if (id === undefined || id === null) {
|
|
13
|
+
for (const key in this.subscribers) {
|
|
14
|
+
this.unsubscribe(key);
|
|
15
|
+
}
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (!(0, types_1.isFn)(id)) {
|
|
19
|
+
delete this.subscribers[id];
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
delete this.subscribers[id[UNSUBSCRIBE_ID_SYMBOL]];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
dispatch(event, context) {
|
|
27
|
+
let interrupted = false;
|
|
28
|
+
for (const key in this.subscribers) {
|
|
29
|
+
if ((0, types_1.isFn)(this.subscribers[key])) {
|
|
30
|
+
event['context'] = context;
|
|
31
|
+
if (this.subscribers[key](event) === false) {
|
|
32
|
+
interrupted = true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return interrupted ? false : true;
|
|
37
|
+
}
|
|
38
|
+
subscribe(subscriber) {
|
|
39
|
+
let id;
|
|
40
|
+
if ((0, types_1.isFn)(subscriber)) {
|
|
41
|
+
id = this.subscribers.index + 1;
|
|
42
|
+
this.subscribers[id] = subscriber;
|
|
43
|
+
this.subscribers.index++;
|
|
44
|
+
}
|
|
45
|
+
const unsubscribe = () => {
|
|
46
|
+
this.unsubscribe(id);
|
|
47
|
+
};
|
|
48
|
+
unsubscribe[UNSUBSCRIBE_ID_SYMBOL] = id;
|
|
49
|
+
return unsubscribe;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.Subscribable = Subscribable;
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const getType: (obj: any) => any;
|
|
2
|
+
export declare const isFn: (obj: unknown) => obj is (...args: any[]) => any;
|
|
3
|
+
export declare const isWindow: (obj: unknown) => obj is Window;
|
|
4
|
+
export declare const isHTMLElement: (obj: any) => obj is HTMLElement;
|
|
5
|
+
export declare const isArr: (arg: any) => arg is any[];
|
|
6
|
+
export declare const isPlainObj: (obj: unknown) => obj is object;
|
|
7
|
+
export declare const isStr: (obj: unknown) => obj is string;
|
|
8
|
+
export declare const isBool: (obj: unknown) => obj is boolean;
|
|
9
|
+
export declare const isNum: (obj: unknown) => obj is number;
|
|
10
|
+
export declare const isObj: (val: unknown) => val is object;
|
|
11
|
+
export declare const isRegExp: (obj: unknown) => obj is RegExp;
|
|
12
|
+
export declare const isValid: (val: any) => boolean;
|
|
13
|
+
export declare const isValidNumber: (val: any) => val is number;
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidNumber = exports.isValid = exports.isRegExp = exports.isObj = exports.isNum = exports.isBool = exports.isStr = exports.isPlainObj = exports.isArr = exports.isHTMLElement = exports.isWindow = exports.isFn = exports.getType = void 0;
|
|
4
|
+
const isType = (type) => (obj) => obj != null &&
|
|
5
|
+
(Array.isArray(type) ? type : [type]).some((t) => (0, exports.getType)(obj) === `[object ${t}]`);
|
|
6
|
+
const getType = (obj) => Object.prototype.toString.call(obj);
|
|
7
|
+
exports.getType = getType;
|
|
8
|
+
exports.isFn = isType([
|
|
9
|
+
'Function',
|
|
10
|
+
'AsyncFunction',
|
|
11
|
+
'GeneratorFunction',
|
|
12
|
+
]);
|
|
13
|
+
exports.isWindow = isType('Window');
|
|
14
|
+
const isHTMLElement = (obj) => {
|
|
15
|
+
return (obj === null || obj === void 0 ? void 0 : obj['nodeName']) || (obj === null || obj === void 0 ? void 0 : obj['tagName']);
|
|
16
|
+
};
|
|
17
|
+
exports.isHTMLElement = isHTMLElement;
|
|
18
|
+
exports.isArr = Array.isArray;
|
|
19
|
+
exports.isPlainObj = isType('Object');
|
|
20
|
+
exports.isStr = isType('String');
|
|
21
|
+
exports.isBool = isType('Boolean');
|
|
22
|
+
exports.isNum = isType('Number');
|
|
23
|
+
const isObj = (val) => typeof val === 'object';
|
|
24
|
+
exports.isObj = isObj;
|
|
25
|
+
exports.isRegExp = isType('RegExp');
|
|
26
|
+
const isValid = (val) => val !== null && val !== undefined;
|
|
27
|
+
exports.isValid = isValid;
|
|
28
|
+
const isValidNumber = (val) => !isNaN(val) && (0, exports.isNum)(val);
|
|
29
|
+
exports.isValidNumber = isValidNumber;
|
package/lib/uid.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uid(len?: number): string;
|
package/lib/uid.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uid = uid;
|
|
4
|
+
let IDX = 36, HEX = '';
|
|
5
|
+
while (IDX--)
|
|
6
|
+
HEX += IDX.toString(36);
|
|
7
|
+
function uid(len) {
|
|
8
|
+
let str = '', num = len || 11;
|
|
9
|
+
while (num--)
|
|
10
|
+
str += HEX[(Math.random() * 36) | 0];
|
|
11
|
+
return str;
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@formily-design/shared",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "lib",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"engines": {
|
|
8
|
+
"npm": ">=3.0.0"
|
|
9
|
+
},
|
|
10
|
+
"module": "esm",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/alibaba/designable.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/alibaba/designable/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/alibaba/designable#readme",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm && npm run build:umd",
|
|
21
|
+
"build:cjs": "tsc --project tsconfig.build.json",
|
|
22
|
+
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm",
|
|
23
|
+
"build:umd": "rollup --config"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"requestidlecallback": "^0.3.0"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "f3496a519e4be3f14900ddbe44744ae482247047"
|
|
32
|
+
}
|
package/rollup.config.js
ADDED
package/src/animation.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export const createUniformSpeedAnimation = (
|
|
2
|
+
speed = 10,
|
|
3
|
+
callback: (delta: number) => void
|
|
4
|
+
) => {
|
|
5
|
+
let request = null
|
|
6
|
+
let startTime = null
|
|
7
|
+
const start = () => {
|
|
8
|
+
if (request) return
|
|
9
|
+
request = requestAnimationFrame((timestamp) => {
|
|
10
|
+
if (startTime === null) {
|
|
11
|
+
startTime = timestamp
|
|
12
|
+
}
|
|
13
|
+
const deltaTime = timestamp - startTime
|
|
14
|
+
const delta = (deltaTime / 1000) * speed
|
|
15
|
+
callback(delta)
|
|
16
|
+
request = null
|
|
17
|
+
start()
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
start()
|
|
22
|
+
|
|
23
|
+
return () => {
|
|
24
|
+
if (request) {
|
|
25
|
+
cancelAnimationFrame(request)
|
|
26
|
+
request = null
|
|
27
|
+
}
|
|
28
|
+
startTime = null
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//越接近阈值,速度越小,越远离阈值,速度越大
|
|
33
|
+
export const calcSpeedFactor = (delta = 0, threshold = Infinity) => {
|
|
34
|
+
if (threshold >= delta) {
|
|
35
|
+
return (threshold - delta) / threshold
|
|
36
|
+
}
|
|
37
|
+
return 0
|
|
38
|
+
}
|
package/src/array.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { isArr, isObj, isStr } from './types'
|
|
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> = (
|
|
6
|
+
currentValue: T,
|
|
7
|
+
key: string
|
|
8
|
+
) => void | boolean
|
|
9
|
+
type MapArrayIterator<TItem, TResult> = (
|
|
10
|
+
currentValue: TItem,
|
|
11
|
+
key: number
|
|
12
|
+
) => TResult
|
|
13
|
+
type MapStringIterator<TResult> = (currentValue: string, key: number) => TResult
|
|
14
|
+
type MapObjectIterator<TItem, TResult> = (
|
|
15
|
+
currentValue: TItem,
|
|
16
|
+
key: string
|
|
17
|
+
) => TResult
|
|
18
|
+
type MemoArrayIterator<T, U> = (
|
|
19
|
+
previousValue: U,
|
|
20
|
+
currentValue: T,
|
|
21
|
+
key: number
|
|
22
|
+
) => U
|
|
23
|
+
type MemoStringIterator<T> = (
|
|
24
|
+
previousValue: T,
|
|
25
|
+
currentValue: string,
|
|
26
|
+
key: number
|
|
27
|
+
) => T
|
|
28
|
+
type MemoObjectIterator<TValue, TResult> = (
|
|
29
|
+
previousValue: TResult,
|
|
30
|
+
currentValue: TValue,
|
|
31
|
+
key: string
|
|
32
|
+
) => TResult
|
|
33
|
+
|
|
34
|
+
export const toArr = (val: any): any[] => (isArr(val) ? val : val ? [val] : [])
|
|
35
|
+
export function each(
|
|
36
|
+
val: string,
|
|
37
|
+
iterator: EachStringIterator,
|
|
38
|
+
revert?: boolean
|
|
39
|
+
): void
|
|
40
|
+
export function each<T>(
|
|
41
|
+
val: T[],
|
|
42
|
+
iterator: EachArrayIterator<T>,
|
|
43
|
+
revert?: boolean
|
|
44
|
+
): void
|
|
45
|
+
export function each<T extends {}, TValue = T[keyof T]>(
|
|
46
|
+
val: T,
|
|
47
|
+
iterator: EachObjectIterator<TValue>,
|
|
48
|
+
revert?: boolean
|
|
49
|
+
): void
|
|
50
|
+
export function each(val: any, iterator: any, revert?: boolean): void {
|
|
51
|
+
if (isArr(val) || isStr(val)) {
|
|
52
|
+
if (revert) {
|
|
53
|
+
for (let i: number = val.length - 1; i >= 0; i--) {
|
|
54
|
+
if (iterator(val[i], i) === false) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
for (let i = 0; i < val.length; i++) {
|
|
60
|
+
if (iterator(val[i], i) === false) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} else if (isObj(val)) {
|
|
66
|
+
let key: string
|
|
67
|
+
for (key in val) {
|
|
68
|
+
if (Object.hasOwnProperty.call(val, key)) {
|
|
69
|
+
if (iterator(val[key], key) === false) {
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function map<T>(
|
|
78
|
+
val: string,
|
|
79
|
+
iterator: MapStringIterator<T>,
|
|
80
|
+
revert?: boolean
|
|
81
|
+
): any
|
|
82
|
+
export function map<TItem, TResult>(
|
|
83
|
+
val: TItem[],
|
|
84
|
+
iterator: MapArrayIterator<TItem, TResult>,
|
|
85
|
+
revert?: boolean
|
|
86
|
+
): any
|
|
87
|
+
export function map<T extends {}, TResult>(
|
|
88
|
+
val: T,
|
|
89
|
+
iterator: MapObjectIterator<T[keyof T], TResult>,
|
|
90
|
+
revert?: boolean
|
|
91
|
+
): any
|
|
92
|
+
export function map(val: any, iterator: any, revert?: boolean): any {
|
|
93
|
+
const res = isArr(val) || isStr(val) ? [] : {}
|
|
94
|
+
each(
|
|
95
|
+
val,
|
|
96
|
+
(item, key) => {
|
|
97
|
+
const value = iterator(item, key)
|
|
98
|
+
if (isArr(res)) {
|
|
99
|
+
;(res as any).push(value)
|
|
100
|
+
} else {
|
|
101
|
+
res[key] = value
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
revert
|
|
105
|
+
)
|
|
106
|
+
return res
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function reduce<T, U>(
|
|
110
|
+
val: T[],
|
|
111
|
+
iterator: MemoArrayIterator<T, U>,
|
|
112
|
+
accumulator?: U,
|
|
113
|
+
revert?: boolean
|
|
114
|
+
): U
|
|
115
|
+
export function reduce<T>(
|
|
116
|
+
val: string,
|
|
117
|
+
iterator: MemoStringIterator<T>,
|
|
118
|
+
accumulator?: T,
|
|
119
|
+
revert?: boolean
|
|
120
|
+
): T
|
|
121
|
+
export function reduce<T extends {}, TValue = T[keyof T], TResult = any>(
|
|
122
|
+
val: T,
|
|
123
|
+
iterator: MemoObjectIterator<TValue, TResult>,
|
|
124
|
+
accumulator?: TResult,
|
|
125
|
+
revert?: boolean
|
|
126
|
+
): TResult
|
|
127
|
+
export function reduce(
|
|
128
|
+
val: any,
|
|
129
|
+
iterator: any,
|
|
130
|
+
accumulator?: any,
|
|
131
|
+
revert?: boolean
|
|
132
|
+
): any {
|
|
133
|
+
let result = accumulator
|
|
134
|
+
each(
|
|
135
|
+
val,
|
|
136
|
+
(item, key) => {
|
|
137
|
+
result = iterator(result, item, key)
|
|
138
|
+
},
|
|
139
|
+
revert
|
|
140
|
+
)
|
|
141
|
+
return result
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function every<T extends string>(
|
|
145
|
+
val: T,
|
|
146
|
+
iterator: EachStringIterator,
|
|
147
|
+
revert?: boolean
|
|
148
|
+
): boolean
|
|
149
|
+
export function every<T>(
|
|
150
|
+
val: T[],
|
|
151
|
+
iterator: EachArrayIterator<T>,
|
|
152
|
+
revert?: boolean
|
|
153
|
+
): boolean
|
|
154
|
+
export function every<T extends {}, TValue = T[keyof T]>(
|
|
155
|
+
val: T,
|
|
156
|
+
iterator: EachObjectIterator,
|
|
157
|
+
revert?: boolean
|
|
158
|
+
): boolean
|
|
159
|
+
export function every(val: any, iterator: any, revert?: boolean): boolean {
|
|
160
|
+
let res = true
|
|
161
|
+
each(
|
|
162
|
+
val,
|
|
163
|
+
(item, key) => {
|
|
164
|
+
if (!iterator(item, key)) {
|
|
165
|
+
res = false
|
|
166
|
+
return false
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
revert
|
|
170
|
+
)
|
|
171
|
+
return res
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function some<T extends string>(
|
|
175
|
+
val: T,
|
|
176
|
+
iterator: EachStringIterator,
|
|
177
|
+
revert?: boolean
|
|
178
|
+
): boolean
|
|
179
|
+
export function some<T>(
|
|
180
|
+
val: T[],
|
|
181
|
+
iterator: EachArrayIterator<T>,
|
|
182
|
+
revert?: boolean
|
|
183
|
+
): boolean
|
|
184
|
+
export function some<T extends {}, TValue = T[keyof T]>(
|
|
185
|
+
val: T,
|
|
186
|
+
iterator: EachObjectIterator,
|
|
187
|
+
revert?: boolean
|
|
188
|
+
): boolean
|
|
189
|
+
export function some(val: any, iterator: any, revert?: boolean): boolean {
|
|
190
|
+
let res = false
|
|
191
|
+
each(
|
|
192
|
+
val,
|
|
193
|
+
(item, key) => {
|
|
194
|
+
if (iterator(item, key)) {
|
|
195
|
+
res = true
|
|
196
|
+
return false
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
revert
|
|
200
|
+
)
|
|
201
|
+
return res
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function findIndex<T extends string>(
|
|
205
|
+
val: T,
|
|
206
|
+
iterator: EachStringIterator,
|
|
207
|
+
revert?: boolean
|
|
208
|
+
): number
|
|
209
|
+
export function findIndex<T>(
|
|
210
|
+
val: T[],
|
|
211
|
+
iterator: EachArrayIterator<T>,
|
|
212
|
+
revert?: boolean
|
|
213
|
+
): number
|
|
214
|
+
export function findIndex<T extends {}, TValue = T[keyof T]>(
|
|
215
|
+
val: T,
|
|
216
|
+
iterator: EachObjectIterator,
|
|
217
|
+
revert?: boolean
|
|
218
|
+
): keyof T
|
|
219
|
+
export function findIndex(
|
|
220
|
+
val: any,
|
|
221
|
+
iterator: any,
|
|
222
|
+
revert?: boolean
|
|
223
|
+
): string | number {
|
|
224
|
+
let res: number | string = -1
|
|
225
|
+
each(
|
|
226
|
+
val,
|
|
227
|
+
(item, key) => {
|
|
228
|
+
if (iterator(item, key)) {
|
|
229
|
+
res = key
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
revert
|
|
234
|
+
)
|
|
235
|
+
return res
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function find<T extends string>(
|
|
239
|
+
val: T,
|
|
240
|
+
iterator: EachStringIterator,
|
|
241
|
+
revert?: boolean
|
|
242
|
+
): any
|
|
243
|
+
export function find<T>(
|
|
244
|
+
val: T[],
|
|
245
|
+
iterator: EachArrayIterator<T>,
|
|
246
|
+
revert?: boolean
|
|
247
|
+
): T
|
|
248
|
+
export function find<T extends {}, TValue = T[keyof T]>(
|
|
249
|
+
val: T,
|
|
250
|
+
iterator: EachObjectIterator,
|
|
251
|
+
revert?: boolean
|
|
252
|
+
): T[keyof T]
|
|
253
|
+
export function find(val: any, iterator: any, revert?: boolean): any {
|
|
254
|
+
let res: any
|
|
255
|
+
each(
|
|
256
|
+
val,
|
|
257
|
+
(item, key) => {
|
|
258
|
+
if (iterator(item, key)) {
|
|
259
|
+
res = item
|
|
260
|
+
return false
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
revert
|
|
264
|
+
)
|
|
265
|
+
return res
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function includes<T extends string>(
|
|
269
|
+
val: T,
|
|
270
|
+
searchElement: string,
|
|
271
|
+
revert?: boolean
|
|
272
|
+
): boolean
|
|
273
|
+
export function includes<T>(
|
|
274
|
+
val: T[],
|
|
275
|
+
searchElement: T,
|
|
276
|
+
revert?: boolean
|
|
277
|
+
): boolean
|
|
278
|
+
export function includes(val: any, searchElement: any, revert?: boolean) {
|
|
279
|
+
if (isStr(val)) return val.includes(searchElement)
|
|
280
|
+
return some(val, (item) => item === searchElement, revert)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function includesWith<T extends string>(
|
|
284
|
+
val: T,
|
|
285
|
+
search: (item: string) => boolean
|
|
286
|
+
): boolean
|
|
287
|
+
export function includesWith<T>(val: T[], search: (item: T) => boolean): boolean
|
|
288
|
+
export function includesWith(val: any, search: any) {
|
|
289
|
+
if (isArr(val)) {
|
|
290
|
+
return val.some((item) => search(item))
|
|
291
|
+
} else {
|
|
292
|
+
return false
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export const flat = <T>(array: Array<T | T[]>): T[] => {
|
|
297
|
+
return toArr(array).reduce((buf, item) => {
|
|
298
|
+
if (isArr(item)) return buf.concat(flat(item))
|
|
299
|
+
return buf.concat(item)
|
|
300
|
+
}, [])
|
|
301
|
+
}
|
package/src/clone.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { isFn } from './types'
|
|
2
|
+
import { instOf } from './instanceof'
|
|
3
|
+
type Filter = (value: any, key: string) => boolean
|
|
4
|
+
|
|
5
|
+
const NATIVE_KEYS = [
|
|
6
|
+
['Map', (map: any) => new Map(map)],
|
|
7
|
+
['WeakMap', (map: any) => new WeakMap(map)],
|
|
8
|
+
['WeakSet', (set: any) => new WeakSet(set)],
|
|
9
|
+
['Set', (set: any) => new Set(set)],
|
|
10
|
+
['Date', (date: any) => new Date(date)],
|
|
11
|
+
'FileList',
|
|
12
|
+
'File',
|
|
13
|
+
'URL',
|
|
14
|
+
'RegExp',
|
|
15
|
+
[
|
|
16
|
+
'Promise',
|
|
17
|
+
(promise: Promise<any>) =>
|
|
18
|
+
new Promise((resolve, reject) => promise.then(resolve, reject)),
|
|
19
|
+
],
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
const isNativeObject = (values: any): any => {
|
|
23
|
+
for (let i = 0; i < NATIVE_KEYS.length; i++) {
|
|
24
|
+
const item = NATIVE_KEYS[i]
|
|
25
|
+
if (Array.isArray(item) && item[0]) {
|
|
26
|
+
if (instOf(values, item[0])) {
|
|
27
|
+
return item[1] ? item[1] : item[0]
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
if (instOf(values, item)) {
|
|
31
|
+
return item
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const shallowClone = (values: any) => {
|
|
38
|
+
let nativeClone: (values: any) => any
|
|
39
|
+
if (Array.isArray(values)) {
|
|
40
|
+
return values.slice(0)
|
|
41
|
+
} else if (isNativeObject(values)) {
|
|
42
|
+
nativeClone = isNativeObject(values)
|
|
43
|
+
return isFn(nativeClone) ? nativeClone(values) : values
|
|
44
|
+
} else if (typeof values === 'object' && !!values) {
|
|
45
|
+
return {
|
|
46
|
+
...values,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const clone = (values: any, filter?: Filter) => {
|
|
52
|
+
let nativeClone: (values: any) => any
|
|
53
|
+
if (Array.isArray(values)) {
|
|
54
|
+
return values.map((item) => clone(item, filter))
|
|
55
|
+
} else if (isNativeObject(values)) {
|
|
56
|
+
nativeClone = isNativeObject(values)
|
|
57
|
+
return isFn(nativeClone) ? nativeClone(values) : values
|
|
58
|
+
} else if (typeof values === 'object' && !!values) {
|
|
59
|
+
if ('$$typeof' in values && '_owner' in values) {
|
|
60
|
+
return values
|
|
61
|
+
}
|
|
62
|
+
if (values._isAMomentObject) {
|
|
63
|
+
return values
|
|
64
|
+
}
|
|
65
|
+
if (values._isJSONSchemaObject) {
|
|
66
|
+
return values
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (isFn(values.toJS)) {
|
|
70
|
+
return values
|
|
71
|
+
}
|
|
72
|
+
if (isFn(values.toJSON)) {
|
|
73
|
+
return values
|
|
74
|
+
}
|
|
75
|
+
if (Object.getOwnPropertySymbols(values || {}).length) {
|
|
76
|
+
return values
|
|
77
|
+
}
|
|
78
|
+
const res = {}
|
|
79
|
+
for (const key in values) {
|
|
80
|
+
if (Object.hasOwnProperty.call(values, key)) {
|
|
81
|
+
if (isFn(filter)) {
|
|
82
|
+
if (filter(values[key], key)) {
|
|
83
|
+
res[key] = clone(values[key], filter)
|
|
84
|
+
} else {
|
|
85
|
+
res[key] = values[key]
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
res[key] = clone(values[key], filter)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return res
|
|
93
|
+
} else {
|
|
94
|
+
return values
|
|
95
|
+
}
|
|
96
|
+
}
|