gio-miniprogram-sdk 4.4.2 → 4.4.3
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/alipay.js +1 -1
- package/dist/baidu.js +1 -1
- package/dist/bytedance.js +1 -1
- package/dist/jingdong.js +1 -1
- package/dist/kuaishou.js +1 -1
- package/dist/plugins/gioABTest.js +1 -1
- package/dist/plugins/gioCompress.js +1 -1
- package/dist/plugins/gioEventAutoTracking.js +1 -1
- package/dist/plugins/gioImpressionTracking.js +1 -1
- package/dist/plugins/gioMultipleInstances.js +1 -1
- package/dist/plugins/gioPerformance.js +1 -1
- package/dist/qq.js +1 -1
- package/dist/quickapp.js +1 -1
- package/dist/taobao.js +1 -1
- package/dist/taro.js +1 -1
- package/dist/uniapp.js +1 -1
- package/dist/wechat.js +1 -1
- package/dist/xiaohongshu.js +1 -1
- package/package.json +7 -1
- package/.eslintignore +0 -11
- package/.eslintrc.js +0 -120
- package/.github/workflows/greetings.yml +0 -13
- package/.github/workflows/publish.yml +0 -45
- package/.prettierignore +0 -19
- package/.prettierrc.js +0 -9
- package/babel.config.js +0 -6
- package/rollup.config.js +0 -100
- package/rollup.plugins.config.js +0 -84
- package/src/adapters/gioTaobaoAdapter.ts +0 -202
- package/src/adapters/gioTaroAdapter.ts +0 -467
- package/src/adapters/gioUniAppAdapter.ts +0 -431
- package/src/constants/config.ts +0 -170
- package/src/constants/emitMsg.ts +0 -24
- package/src/constants/regex.ts +0 -18
- package/src/core/dataStore/eventContextBuilder.ts +0 -95
- package/src/core/dataStore/eventHooks/appEffects.ts +0 -218
- package/src/core/dataStore/eventHooks/index.ts +0 -468
- package/src/core/dataStore/eventHooks/minipPage.ts +0 -221
- package/src/core/dataStore/eventHooks/pageEffects.ts +0 -189
- package/src/core/dataStore/index.ts +0 -661
- package/src/core/growingIO.ts +0 -637
- package/src/core/pluginsInstance/base.ts +0 -157
- package/src/core/pluginsInstance/taro.ts +0 -15
- package/src/core/pluginsInstance/tb.ts +0 -15
- package/src/core/pluginsInstance/uniapp.ts +0 -15
- package/src/core/uploader.ts +0 -272
- package/src/core/userStore.ts +0 -267
- package/src/index.ts +0 -77
- package/src/minipInstance/base.ts +0 -352
- package/src/minipInstance/framework.ts +0 -24
- package/src/minipInstance/jd.ts +0 -11
- package/src/minipInstance/ks.ts +0 -12
- package/src/minipInstance/my.ts +0 -131
- package/src/minipInstance/qq.ts +0 -12
- package/src/minipInstance/quickapp.ts +0 -233
- package/src/minipInstance/swan.ts +0 -20
- package/src/minipInstance/tb.ts +0 -58
- package/src/minipInstance/tt.ts +0 -12
- package/src/minipInstance/wx.ts +0 -27
- package/src/minipInstance/xhs.ts +0 -20
- package/src/platformConfig/base.ts +0 -82
- package/src/platformConfig/framework.ts +0 -31
- package/src/platformConfig/jd.ts +0 -11
- package/src/platformConfig/ks.ts +0 -11
- package/src/platformConfig/my.ts +0 -16
- package/src/platformConfig/qq.ts +0 -13
- package/src/platformConfig/quickapp.ts +0 -48
- package/src/platformConfig/swan.ts +0 -11
- package/src/platformConfig/tb.ts +0 -29
- package/src/platformConfig/tt.ts +0 -11
- package/src/platformConfig/wx.ts +0 -20
- package/src/platformConfig/xhs.ts +0 -27
- package/src/plugins/gioABTest.ts +0 -348
- package/src/plugins/gioCompress.ts +0 -253
- package/src/plugins/gioEventAutoTracking.ts +0 -310
- package/src/plugins/gioImpressionTracking.ts +0 -386
- package/src/plugins/gioMultipleInstances.ts +0 -189
- package/src/plugins/gioPerformance/exception.ts +0 -152
- package/src/plugins/gioPerformance/monitor.ts +0 -284
- package/src/plugins/gioPerformance/network.ts +0 -197
- package/src/plugins/gioPerformance.ts +0 -131
- package/src/types/base.ts +0 -167
- package/src/types/dataStore.ts +0 -62
- package/src/types/eventHooks.ts +0 -146
- package/src/types/gioCustomTracking.ts +0 -9
- package/src/types/gioEventTracking.ts +0 -10
- package/src/types/gioPerformance.ts +0 -8
- package/src/types/growingIO.ts +0 -137
- package/src/types/minipInstance.ts +0 -133
- package/src/types/platforms.ts +0 -64
- package/src/types/plugins.ts +0 -24
- package/src/types/uploader.ts +0 -30
- package/src/types/userStore.ts +0 -28
- package/src/utils/glodash.ts +0 -430
- package/src/utils/tools.ts +0 -511
- package/src/utils/verifiers.ts +0 -52
- package/tsconfig.json +0 -32
- package/typings/global.d.ts +0 -105
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { PLATFORMS } from './platforms';
|
|
2
|
-
|
|
3
|
-
export interface NavigateToMiniProgramOption {
|
|
4
|
-
appId: string;
|
|
5
|
-
path?: string;
|
|
6
|
-
extraData?: any;
|
|
7
|
-
envVersion?: any;
|
|
8
|
-
success: (res: any) => void;
|
|
9
|
-
fail: (err: any) => void;
|
|
10
|
-
complete: () => void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface AppSource {
|
|
14
|
-
extra: any;
|
|
15
|
-
packageName: string;
|
|
16
|
-
type: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface UserInfo {
|
|
20
|
-
nickname: string;
|
|
21
|
-
avatarUrl: string;
|
|
22
|
-
gender: number;
|
|
23
|
-
province: string;
|
|
24
|
-
city: string;
|
|
25
|
-
country: string;
|
|
26
|
-
}
|
|
27
|
-
export interface SystemInfo {
|
|
28
|
-
// 设备品牌
|
|
29
|
-
brand: string;
|
|
30
|
-
// 设备型号
|
|
31
|
-
model: string;
|
|
32
|
-
// 设备像素比
|
|
33
|
-
pixelRatio: number;
|
|
34
|
-
// 屏幕宽度,单位px
|
|
35
|
-
screenWidth: number;
|
|
36
|
-
// 屏幕高度,单位px
|
|
37
|
-
screenHeight: number;
|
|
38
|
-
// 语言
|
|
39
|
-
language: string;
|
|
40
|
-
// 平台容器版本号
|
|
41
|
-
version: string;
|
|
42
|
-
// 操作系统及版本
|
|
43
|
-
system: string;
|
|
44
|
-
// 客户端基础库版本
|
|
45
|
-
SDKVersion: string;
|
|
46
|
-
// 客户端平台
|
|
47
|
-
platform: string;
|
|
48
|
-
}
|
|
49
|
-
export interface Location {
|
|
50
|
-
// 纬度,范围为 -90~90,负数表示南纬
|
|
51
|
-
latitude: number;
|
|
52
|
-
// 经度,范围为 -180~180,负数表示西经
|
|
53
|
-
longitude: number;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface MinipInstanceType {
|
|
57
|
-
// 小程序原生对象 wx/my/tt/swan/qq/ks/jd/xhs
|
|
58
|
-
readonly minip: any;
|
|
59
|
-
// 当前平台
|
|
60
|
-
platform: PLATFORMS;
|
|
61
|
-
// 场景值前缀
|
|
62
|
-
scnPrefix: string;
|
|
63
|
-
// 地理位置
|
|
64
|
-
location: Location;
|
|
65
|
-
// 快应用来源
|
|
66
|
-
appSource: AppSource;
|
|
67
|
-
// 系统信息
|
|
68
|
-
systemInfo: any;
|
|
69
|
-
// 网络信息
|
|
70
|
-
network: any;
|
|
71
|
-
// hook动态设置页面标题方法
|
|
72
|
-
hookSetTitle: () => void;
|
|
73
|
-
// 获取快应用来源信息
|
|
74
|
-
getAppSource?: () => AppSource;
|
|
75
|
-
// 保留当前页面,跳转到应用内的某个页面
|
|
76
|
-
navigateTo?: (opt: any) => void;
|
|
77
|
-
// 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
|
|
78
|
-
switchTab?: (opt: any) => void;
|
|
79
|
-
// 打开另一个小程序
|
|
80
|
-
navigateToMiniProgram?: (opt: NavigateToMiniProgramOption) => void;
|
|
81
|
-
// 获取图片信息
|
|
82
|
-
getImageInfo: (args: any) => any;
|
|
83
|
-
// 采集曝光事件
|
|
84
|
-
initImpression?: (collectPage: any) => void;
|
|
85
|
-
// 获取快应用分享信息
|
|
86
|
-
initShareAppMessage?: (growingio: any) => void;
|
|
87
|
-
// 获取当前页面栈
|
|
88
|
-
getCurrentPage: () => any;
|
|
89
|
-
// 获取当前页面路由
|
|
90
|
-
getCurrentPath: (page?: any) => string;
|
|
91
|
-
// 获取页面标题
|
|
92
|
-
getPageTitle: (page: any) => string;
|
|
93
|
-
// 同步获取存储数据
|
|
94
|
-
getStorageSync: (key: string) => any;
|
|
95
|
-
// 异步获取存储数据
|
|
96
|
-
getStorage: (key: string) => Promise<string>;
|
|
97
|
-
// 同步存储数据
|
|
98
|
-
setStorageSync: (
|
|
99
|
-
key: string,
|
|
100
|
-
value: any,
|
|
101
|
-
expiredAt?: string | number
|
|
102
|
-
) => void;
|
|
103
|
-
// 异步存储数据
|
|
104
|
-
setStorage: (key: string, value: any) => void;
|
|
105
|
-
// 同步移除指定数据
|
|
106
|
-
removeStorageSync: (key: string) => void;
|
|
107
|
-
// 异步移除指定数据
|
|
108
|
-
removeStorage: (key: string) => void;
|
|
109
|
-
// 获取网络类型
|
|
110
|
-
getNetworkType: () => Promise<{ networkType: string }>;
|
|
111
|
-
// 网络请求
|
|
112
|
-
request: ({
|
|
113
|
-
url,
|
|
114
|
-
data,
|
|
115
|
-
header,
|
|
116
|
-
timeout,
|
|
117
|
-
method,
|
|
118
|
-
fail,
|
|
119
|
-
success,
|
|
120
|
-
complete
|
|
121
|
-
}: any) => void;
|
|
122
|
-
// 获取小程序系统信息
|
|
123
|
-
getSystemInfo?: () => Promise<SystemInfo>;
|
|
124
|
-
// 获取小程序设置
|
|
125
|
-
getSetting?: () => any;
|
|
126
|
-
// 监听网络变更
|
|
127
|
-
setNetworkStatusListener?: () => any;
|
|
128
|
-
// 执行特有的分享promise
|
|
129
|
-
handleSharePromise?: (
|
|
130
|
-
originResult: any,
|
|
131
|
-
handlePromiseResult: () => void
|
|
132
|
-
) => any;
|
|
133
|
-
}
|
package/src/types/platforms.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
export type PLATFORMS =
|
|
2
|
-
| 'MinP'
|
|
3
|
-
| 'alip'
|
|
4
|
-
| 'tbp'
|
|
5
|
-
| 'baidup'
|
|
6
|
-
| 'qq'
|
|
7
|
-
| 'bytedance'
|
|
8
|
-
| 'kuaishoup'
|
|
9
|
-
| 'jdp'
|
|
10
|
-
| 'xhsp'
|
|
11
|
-
| 'quickapp';
|
|
12
|
-
export type PLATFORMTYPES =
|
|
13
|
-
| 'wx'
|
|
14
|
-
| 'my'
|
|
15
|
-
| 'tb'
|
|
16
|
-
| 'swan'
|
|
17
|
-
| 'qq'
|
|
18
|
-
| 'tt'
|
|
19
|
-
| 'ks'
|
|
20
|
-
| 'jd'
|
|
21
|
-
| 'xhs'
|
|
22
|
-
| 'quickapp';
|
|
23
|
-
|
|
24
|
-
export type FRAMEWORKS = 'taro' | 'uniapp' | 'full';
|
|
25
|
-
|
|
26
|
-
export interface PlatformConfigType {
|
|
27
|
-
name?: string;
|
|
28
|
-
platform?: PLATFORMS;
|
|
29
|
-
scnPrefix?: string;
|
|
30
|
-
appHandlers?: string[];
|
|
31
|
-
pageHandlers?: string[];
|
|
32
|
-
shareEventTypes?: string[];
|
|
33
|
-
actionEventTypes?: string[];
|
|
34
|
-
hooks?: {
|
|
35
|
-
App: boolean;
|
|
36
|
-
Page: boolean;
|
|
37
|
-
Component: boolean;
|
|
38
|
-
Behavior: boolean;
|
|
39
|
-
};
|
|
40
|
-
canHook: boolean;
|
|
41
|
-
listeners?: {
|
|
42
|
-
app: {
|
|
43
|
-
appLaunch: string;
|
|
44
|
-
appShow: string;
|
|
45
|
-
appClose: string;
|
|
46
|
-
};
|
|
47
|
-
page: {
|
|
48
|
-
pageLoad: string;
|
|
49
|
-
pageShow: string;
|
|
50
|
-
pageReady: string;
|
|
51
|
-
pageHide: string;
|
|
52
|
-
pageUnload: string;
|
|
53
|
-
tabTap: string;
|
|
54
|
-
shareApp: string;
|
|
55
|
-
shareTime?: string;
|
|
56
|
-
addFavorites?: string;
|
|
57
|
-
};
|
|
58
|
-
actions: {
|
|
59
|
-
click: string[];
|
|
60
|
-
change: string[];
|
|
61
|
-
submit: string[];
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
}
|
package/src/types/plugins.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export interface PluginItem {
|
|
2
|
-
name: string;
|
|
3
|
-
method: (growingIO) => void;
|
|
4
|
-
options?: any;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface PluginsType {
|
|
8
|
-
gioCompress?: any;
|
|
9
|
-
gioCustomTracking?: any;
|
|
10
|
-
gioEventAutoTracking?: any;
|
|
11
|
-
gioImpressionTracking?: any;
|
|
12
|
-
gioPerformance?: any;
|
|
13
|
-
gioTaobaoAdapter?: any;
|
|
14
|
-
gioTaroAdapter?: any;
|
|
15
|
-
gioUniAppAdapter?: any;
|
|
16
|
-
gioMultipleInstances?: any;
|
|
17
|
-
innerPluginInit: () => void;
|
|
18
|
-
outerPluginInit?: (folder?: string) => void;
|
|
19
|
-
install: (pluginName: string, showLog: boolean) => boolean;
|
|
20
|
-
installAll: (plugins: PluginItem[]) => void;
|
|
21
|
-
pluginItems: PluginItem[];
|
|
22
|
-
uninstall: (pluginName: string) => boolean;
|
|
23
|
-
uninstallAll: () => void;
|
|
24
|
-
}
|
package/src/types/uploader.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { EVENT, EXTEND_EVENT } from './base';
|
|
2
|
-
|
|
3
|
-
export interface UploaderType {
|
|
4
|
-
// 因forceLogin配置积压的事件请求队列
|
|
5
|
-
hoardingQueue: any;
|
|
6
|
-
// 请求队列
|
|
7
|
-
requestQueue: any;
|
|
8
|
-
// 请求限制最大数
|
|
9
|
-
requestLimit: number;
|
|
10
|
-
// 请求重试限制最大数
|
|
11
|
-
retryLimit: number;
|
|
12
|
-
// 请求重试的原请求id
|
|
13
|
-
retryIds: any;
|
|
14
|
-
// 请求中的请求数
|
|
15
|
-
requestingNum: number;
|
|
16
|
-
// 获取积压列表
|
|
17
|
-
getHoardingQueue: (trackingId: string) => EXTEND_EVENT[];
|
|
18
|
-
// 获取请求列表
|
|
19
|
-
getRequestQueue: (trackingId: string) => EXTEND_EVENT[];
|
|
20
|
-
// 提交请求至队列
|
|
21
|
-
commitRequest: (commitData: EXTEND_EVENT) => void;
|
|
22
|
-
// 初始化请求
|
|
23
|
-
initiateRequest: (trackingId: string, forceSend?: boolean) => void;
|
|
24
|
-
// 发送事件
|
|
25
|
-
sendEvent: (eventsQueue: EXTEND_EVENT[], requestData: EVENT[]) => void;
|
|
26
|
-
// 请求错误回调
|
|
27
|
-
requestFailFn: (event: EXTEND_EVENT) => void;
|
|
28
|
-
// 生成上报地址
|
|
29
|
-
generateURL: (trackingId: string) => void;
|
|
30
|
-
}
|
package/src/types/userStore.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export interface UserStoreType {
|
|
2
|
-
// 获取设备Id
|
|
3
|
-
getUid: () => string;
|
|
4
|
-
// 设置设备Id
|
|
5
|
-
setUid: (id: string) => void;
|
|
6
|
-
// 获取sessionId
|
|
7
|
-
getSessionId: (trackingId: string) => string;
|
|
8
|
-
// 设置sessionId
|
|
9
|
-
setSessionId: (trackingId: string, id?: string) => void;
|
|
10
|
-
// session超时时刻
|
|
11
|
-
_sessionExpires: number;
|
|
12
|
-
// 获取userId
|
|
13
|
-
getUserId: (trackingId: string) => string;
|
|
14
|
-
// 设置userId
|
|
15
|
-
setUserId: (trackingId: string, id?: string) => void;
|
|
16
|
-
// 获取userKey
|
|
17
|
-
getUserKey: (trackingId: string) => string;
|
|
18
|
-
// 设置userKey
|
|
19
|
-
setUserKey: (trackingId: string, key?: string) => void;
|
|
20
|
-
// 获取gioId
|
|
21
|
-
getGioId: (trackingId: string) => string;
|
|
22
|
-
// 设置gioId
|
|
23
|
-
setGioId: (trackingId: string, id?: string) => void;
|
|
24
|
-
// 初始化用户信息
|
|
25
|
-
initUserInfo: (trackingId: string) => void;
|
|
26
|
-
// 退出小程序时在存储中同步用户信息
|
|
27
|
-
saveUserInfo: () => void;
|
|
28
|
-
}
|
package/src/utils/glodash.ts
DELETED
|
@@ -1,430 +0,0 @@
|
|
|
1
|
-
export const isNil = (o: any): boolean =>
|
|
2
|
-
['undefined', 'null'].includes(typeOf(o));
|
|
3
|
-
|
|
4
|
-
export const isString = (o: any): boolean => typeOf(o) === 'string';
|
|
5
|
-
|
|
6
|
-
export const isNaN = (value: any) => toString(Number(value)) === 'NaN';
|
|
7
|
-
|
|
8
|
-
export const isNumber = (o: any): boolean => typeOf(o) === 'number';
|
|
9
|
-
|
|
10
|
-
export const isBoolean = (o: any): boolean => typeOf(o) === 'boolean';
|
|
11
|
-
|
|
12
|
-
export const isObject = (o: any): boolean =>
|
|
13
|
-
Object.prototype.toString.call(o) === '[object Object]' && !isNil(o);
|
|
14
|
-
|
|
15
|
-
export const isRegExp = (o: any): boolean =>
|
|
16
|
-
Object.prototype.toString.call(o) === '[object RegExp]';
|
|
17
|
-
|
|
18
|
-
export const isFunction = (o: any): boolean =>
|
|
19
|
-
['function', 'asyncfunction'].includes(typeOf(o));
|
|
20
|
-
|
|
21
|
-
export const isArray = (o: any): boolean =>
|
|
22
|
-
Array.isArray(o) && typeOf(o) === 'array';
|
|
23
|
-
|
|
24
|
-
export const isDate = (o: any): boolean => typeOf(o) === 'date';
|
|
25
|
-
|
|
26
|
-
// Number
|
|
27
|
-
/**
|
|
28
|
-
* 格式化数字,四舍五入取指定小数点后位数
|
|
29
|
-
* @param o number 需要被格式化的数字
|
|
30
|
-
* @param n number 指定格式化位数
|
|
31
|
-
* @returns number
|
|
32
|
-
*/
|
|
33
|
-
export const fixed = (o: any, n?: number): number => {
|
|
34
|
-
if (isNumber(o)) {
|
|
35
|
-
return Number(o.toFixed(isNumber(n) ? n : 2));
|
|
36
|
-
} else if (isString(o) && toString(Number(o)) !== 'NaN') {
|
|
37
|
-
return Number(Number(o).toFixed(isNumber(n) ? n : 2));
|
|
38
|
-
} else {
|
|
39
|
-
return o;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Array
|
|
44
|
-
/**
|
|
45
|
-
* 获取数组第一个元素(类数组无法使用,例:arguments)
|
|
46
|
-
* @param o array
|
|
47
|
-
* @returns target | undefined
|
|
48
|
-
*/
|
|
49
|
-
export const head = (o: any): any => {
|
|
50
|
-
try {
|
|
51
|
-
return Array.from(o)[0];
|
|
52
|
-
} catch (error) {
|
|
53
|
-
return undefined;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 获取数组最后一个元素
|
|
59
|
-
* @param o array
|
|
60
|
-
* @returns target | undefined
|
|
61
|
-
*/
|
|
62
|
-
export const last = (o: any): any => {
|
|
63
|
-
try {
|
|
64
|
-
const ar = Array.from(o);
|
|
65
|
-
return ar[ar.length - 1];
|
|
66
|
-
} catch (error) {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 移除数组前n个元素,默认1
|
|
73
|
-
* @param o array
|
|
74
|
-
* @param n ?number
|
|
75
|
-
* @returns new array
|
|
76
|
-
*/
|
|
77
|
-
export const drop = (o: any, n = 1): any =>
|
|
78
|
-
isArray(o) && isNumber(n) ? o.slice(n > 0 ? n : 1, o.length) : o;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 移除数组符合条件的元素
|
|
82
|
-
* @param o array
|
|
83
|
-
* @param fn function,需返回条件
|
|
84
|
-
* @returns new array
|
|
85
|
-
*/
|
|
86
|
-
export const dropWhile = (o: any, fn: (currentValue: any) => boolean): any => {
|
|
87
|
-
if (isArray(o)) {
|
|
88
|
-
return o.filter((c: any) => !fn(c));
|
|
89
|
-
} else {
|
|
90
|
-
return o;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 移除数组中非法数据(undefined、null、0、{}、[]、false)
|
|
96
|
-
* @param o array
|
|
97
|
-
* @returns new array
|
|
98
|
-
*/
|
|
99
|
-
export const compact = (o: any, ignoreZero = false): any => {
|
|
100
|
-
if (isArray(o)) {
|
|
101
|
-
let resIndex = 0;
|
|
102
|
-
const result: any = [];
|
|
103
|
-
for (const value of o) {
|
|
104
|
-
if (value && !isEmpty(value)) {
|
|
105
|
-
result[resIndex++] = value;
|
|
106
|
-
}
|
|
107
|
-
if (ignoreZero && value === 0) {
|
|
108
|
-
result[resIndex++] = value;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return result;
|
|
112
|
-
} else {
|
|
113
|
-
return o;
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* 查找数组中符合条件的元素
|
|
119
|
-
* @param o array
|
|
120
|
-
* @param fn function,需返回条件
|
|
121
|
-
* @returns target | undefined
|
|
122
|
-
*/
|
|
123
|
-
export const find = (o: any, fn: (currentValue: any) => any): any => {
|
|
124
|
-
if (isArray(o)) {
|
|
125
|
-
const i = o.findIndex(fn);
|
|
126
|
-
return i >= 0 ? o[i] : undefined;
|
|
127
|
-
} else {
|
|
128
|
-
return undefined;
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 判断两个数组中的元素是否一致
|
|
134
|
-
* @param a array1
|
|
135
|
-
* @param b array2
|
|
136
|
-
* @returns boolean
|
|
137
|
-
*/
|
|
138
|
-
export const isEqualArray = (a: any, b: any): boolean => {
|
|
139
|
-
if (!isArray(a) || !isArray(b) || a.length !== b.length) {
|
|
140
|
-
return false;
|
|
141
|
-
} else {
|
|
142
|
-
return a.every((o, i) => o === b[i]);
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
// String
|
|
147
|
-
/**
|
|
148
|
-
* 将undefined和null以外的数据转为字符串
|
|
149
|
-
* @param o any
|
|
150
|
-
* @returns '' | string
|
|
151
|
-
*/
|
|
152
|
-
export const toString = (o: any): string => (isNil(o) ? '' : `${o}`);
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* 将字符串按指定分隔符分隔为数组
|
|
156
|
-
* @param s string
|
|
157
|
-
* @param separator 分隔符
|
|
158
|
-
* @returns array
|
|
159
|
-
*/
|
|
160
|
-
export const split = (s: any, separator?: string): string[] | any =>
|
|
161
|
-
typeOf(s) === 'string' ? s.split(separator) : s;
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* 将字符串第一个字符转为小写
|
|
165
|
-
* @param s string
|
|
166
|
-
* @returns string
|
|
167
|
-
*/
|
|
168
|
-
export const lowerFirst = (s: any): string | any => {
|
|
169
|
-
if (isString(s)) {
|
|
170
|
-
const sa = split(s, '');
|
|
171
|
-
return `${(head(sa) as string).toLowerCase()}${drop(sa).join('')}`;
|
|
172
|
-
} else {
|
|
173
|
-
return s;
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* 将字符串第一个字符转为大写
|
|
179
|
-
* @param s string
|
|
180
|
-
* @returns string
|
|
181
|
-
*/
|
|
182
|
-
export const upperFirst = (s: any): string | any => {
|
|
183
|
-
if (isString(s)) {
|
|
184
|
-
const sa = split(s, '');
|
|
185
|
-
return `${(head(sa) as string).toUpperCase()}${drop(sa).join('')}`;
|
|
186
|
-
} else {
|
|
187
|
-
return s;
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* 判断字符串是否以指定字符开头
|
|
193
|
-
* @param s string
|
|
194
|
-
* @param target string
|
|
195
|
-
* @returns boolean
|
|
196
|
-
*/
|
|
197
|
-
export const startsWith = (s: string, target: string): boolean => {
|
|
198
|
-
if (!isString(s)) {
|
|
199
|
-
s = toString(s);
|
|
200
|
-
}
|
|
201
|
-
return s.slice(0, target.length) === target;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* 判断字符串是否以指定字符结尾
|
|
206
|
-
* @param s string
|
|
207
|
-
* @param target string
|
|
208
|
-
* @returns boolean
|
|
209
|
-
*/
|
|
210
|
-
export const endsWith = (s: string, target: string): boolean => {
|
|
211
|
-
const { length } = s;
|
|
212
|
-
let position = length;
|
|
213
|
-
if (position > length) {
|
|
214
|
-
position = length;
|
|
215
|
-
}
|
|
216
|
-
const end = position;
|
|
217
|
-
position -= target.length;
|
|
218
|
-
return position >= 0 && s.slice(position, end) === target;
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
// Object
|
|
222
|
-
export const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
223
|
-
/**
|
|
224
|
-
* 判断对象中是否存在指定属性
|
|
225
|
-
* @param o object
|
|
226
|
-
* @param key string
|
|
227
|
-
* @returns boolean
|
|
228
|
-
*/
|
|
229
|
-
export const has = (o: object, key: string): boolean =>
|
|
230
|
-
!isNil(o) && hasOwnProperty.call(o, key);
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* 获取对象中所有的属性名
|
|
234
|
-
* @param o object
|
|
235
|
-
* @returns array
|
|
236
|
-
*/
|
|
237
|
-
export const keys = (o: any): string[] => (isObject(o) ? Object.keys(o) : []);
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* 对象遍历
|
|
241
|
-
* @param o object
|
|
242
|
-
* @param fn 遍历回调
|
|
243
|
-
*/
|
|
244
|
-
export const forEach = (
|
|
245
|
-
o: any,
|
|
246
|
-
fn: (value: any, key: string) => void
|
|
247
|
-
): void => {
|
|
248
|
-
keys(o).forEach((k: string) => fn(o[k], k));
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* 判断两个对象显式属性与对应值是否相等
|
|
253
|
-
* @param o1 object
|
|
254
|
-
* @param o2 object
|
|
255
|
-
* @returns boolean
|
|
256
|
-
*/
|
|
257
|
-
export const isEqual = (o1: any, o2: any): boolean => {
|
|
258
|
-
const ks = keys(o1);
|
|
259
|
-
if (!isObject(o1) || !isObject(o2) || ks.length !== keys(o2).length) {
|
|
260
|
-
return false;
|
|
261
|
-
} else {
|
|
262
|
-
return !ks
|
|
263
|
-
.map((k, i) => {
|
|
264
|
-
if (isObject(o1[k])) {
|
|
265
|
-
return isEqual(o1[k], o2[k]);
|
|
266
|
-
} else {
|
|
267
|
-
return o1[k] === o2[k];
|
|
268
|
-
}
|
|
269
|
-
})
|
|
270
|
-
.includes(false);
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* 获取对象中指定路径的值
|
|
276
|
-
* @param o object
|
|
277
|
-
* @param path string
|
|
278
|
-
* @param defaultValue any
|
|
279
|
-
* @returns target | defaultValue | undefined
|
|
280
|
-
*/
|
|
281
|
-
export const get = (o: any, path: string, defaultValue?: any): any => {
|
|
282
|
-
let target = o;
|
|
283
|
-
if (!isObject(o)) return defaultValue;
|
|
284
|
-
const keys = path.split('.');
|
|
285
|
-
keys.forEach((k: string) => {
|
|
286
|
-
if (target) {
|
|
287
|
-
target = target[k];
|
|
288
|
-
} else {
|
|
289
|
-
target = defaultValue;
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
return target;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* 移除对象中指定属性
|
|
297
|
-
* @param o object
|
|
298
|
-
* @param targetProp string | string[] | RegExp
|
|
299
|
-
* @returns boolean | boolean[]
|
|
300
|
-
*/
|
|
301
|
-
export const unset = (
|
|
302
|
-
o: any,
|
|
303
|
-
targetProp: string | string[] | RegExp
|
|
304
|
-
): boolean | boolean[] => {
|
|
305
|
-
if (isObject(o)) {
|
|
306
|
-
try {
|
|
307
|
-
if (typeOf(targetProp) === 'string') {
|
|
308
|
-
// 本来要用Reflect.deleteProperty,但是IE不支持,就用delete
|
|
309
|
-
return delete o[targetProp as string];
|
|
310
|
-
} else if (typeOf(targetProp) === 'array') {
|
|
311
|
-
return (targetProp as string[]).map((s) => delete o[s as string]);
|
|
312
|
-
} else if (
|
|
313
|
-
typeOf(targetProp) === 'object' &&
|
|
314
|
-
targetProp.constructor === RegExp
|
|
315
|
-
) {
|
|
316
|
-
keys(o).forEach((k) => {
|
|
317
|
-
if (targetProp.test(k)) {
|
|
318
|
-
unset(o, k);
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
} catch (error) {
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
} else {
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
// Function
|
|
331
|
-
/**
|
|
332
|
-
* 节流函数
|
|
333
|
-
* @param callback function 回调函数
|
|
334
|
-
* @param delay number(ms) 默认250
|
|
335
|
-
* @param noTrailing boolean 如果 noTrailing 为 true,则回调将仅delay在调用节流函数时每delay ms执行一次。如果 noTrailing 为 false 或未指定,回调将在最后一次节流函数调用后执行最后一次。
|
|
336
|
-
* @param debounceMode boolean 如果debounceMode为 true,则安排clear在delay ms之后执行。如果debounceMode为 false,则安排callback在delay ms之后执行 。
|
|
337
|
-
* @returns function
|
|
338
|
-
*/
|
|
339
|
-
export const throttle = (callback, delay = 250, noTrailing, debounceMode?) => {
|
|
340
|
-
let timeoutID;
|
|
341
|
-
let cancelled = false;
|
|
342
|
-
let lastExec = 0;
|
|
343
|
-
function clearExistingTimeout() {
|
|
344
|
-
if (timeoutID) {
|
|
345
|
-
clearTimeout(timeoutID);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
if (typeOf(noTrailing) !== 'boolean') {
|
|
349
|
-
debounceMode = callback;
|
|
350
|
-
callback = noTrailing;
|
|
351
|
-
noTrailing = undefined;
|
|
352
|
-
}
|
|
353
|
-
function wrapper(...arguments_) {
|
|
354
|
-
let self = this;
|
|
355
|
-
let elapsed = Date.now() - lastExec;
|
|
356
|
-
if (cancelled) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
function exec() {
|
|
360
|
-
lastExec = Date.now();
|
|
361
|
-
callback.apply(self, arguments_);
|
|
362
|
-
}
|
|
363
|
-
function clear() {
|
|
364
|
-
timeoutID = undefined;
|
|
365
|
-
}
|
|
366
|
-
if (debounceMode && !timeoutID) {
|
|
367
|
-
exec();
|
|
368
|
-
}
|
|
369
|
-
clearExistingTimeout();
|
|
370
|
-
if (debounceMode === undefined && elapsed > delay) {
|
|
371
|
-
exec();
|
|
372
|
-
} else if (noTrailing !== true) {
|
|
373
|
-
timeoutID = setTimeout(
|
|
374
|
-
debounceMode ? clear : exec,
|
|
375
|
-
debounceMode === undefined ? delay - elapsed : delay
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
return wrapper;
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* 判断目标值是否为空([]、{}、''、0、false)
|
|
384
|
-
* @param o any
|
|
385
|
-
* @returns boolean
|
|
386
|
-
*/
|
|
387
|
-
export const isEmpty = (o: any): boolean => {
|
|
388
|
-
if (isArray(o)) {
|
|
389
|
-
return o.length === 0;
|
|
390
|
-
} else if (isObject(o)) {
|
|
391
|
-
return keys(o).length === 0;
|
|
392
|
-
} else {
|
|
393
|
-
return !o;
|
|
394
|
-
}
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* 判断类型
|
|
399
|
-
* @param o any
|
|
400
|
-
* @returns string
|
|
401
|
-
*/
|
|
402
|
-
export const typeOf = (o: any): string => {
|
|
403
|
-
return Object.prototype.toString.call(o).slice(8, -1).toLowerCase();
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* 格式化时间
|
|
408
|
-
* @param o any
|
|
409
|
-
* @returns string
|
|
410
|
-
*/
|
|
411
|
-
export const formatDate = (e) => {
|
|
412
|
-
function t(e) {
|
|
413
|
-
return e < 10 ? '0' + e : e;
|
|
414
|
-
}
|
|
415
|
-
return (
|
|
416
|
-
e.getFullYear() +
|
|
417
|
-
'-' +
|
|
418
|
-
t(e.getMonth() + 1) +
|
|
419
|
-
'-' +
|
|
420
|
-
t(e.getDate()) +
|
|
421
|
-
' ' +
|
|
422
|
-
t(e.getHours()) +
|
|
423
|
-
':' +
|
|
424
|
-
t(e.getMinutes()) +
|
|
425
|
-
':' +
|
|
426
|
-
t(e.getSeconds()) +
|
|
427
|
-
'.' +
|
|
428
|
-
t(e.getMilliseconds())
|
|
429
|
-
);
|
|
430
|
-
};
|