Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): add utils package #268

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,16 @@ module.exports = {
},
],
},
utils: {
path: 'src/packages/utils/src/index.ts',
unNeedSplit: true,
needCommonUtil: false,
pkgInfo: [
{
version: '1.0.0',
name: '@uni/utils',
exports: '',
},
],
},
};
2 changes: 1 addition & 1 deletion scripts/build-plugin/initPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const buildPkgJson = async (packageInfo, outputPath, isMain = false) => {
if (!packageJson.exports) {
delete packageJson.exports;
}
if (isMain || packageInfo.name == '@uni/env') {
if (isMain || packageInfo.name == '@uni/env' || packageInfo.name === '@uni/utils') {
delete packageJson.dependencies['@uni/env'];
cryzzchen marked this conversation as resolved.
Show resolved Hide resolved
}
if (isMain) {
Expand Down
Empty file added src/packages/utils/CHANGELOG.md
Empty file.
3 changes: 3 additions & 0 deletions src/packages/utils/docs/README.en-US.md.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @uni/utils

工具函数
3 changes: 3 additions & 0 deletions src/packages/utils/docs/README.md.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @uni/utils

工具函数
20 changes: 20 additions & 0 deletions src/packages/utils/src/callPlatformAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// eslint-disable-next-line import/no-extraneous-dependencies
// @ts-nocheck
import { isDingdingMiniapp, isMiniapp, isWeChatMiniProgram, isWeb, isKuaiShouMiniProgram, isBaiduSmartProgram } from '@uni/env';

export default (platformApi: string, ...args: any) => {
if (isWeb) {
return window[platformApi](args);
} else if (isDingdingMiniapp) {
return dd[platformApi](args);
} else if (isMiniapp) {
return my[platformApi](args);
} else if (isWeChatMiniProgram) {
return wx[platformApi](args);
} else if (isKuaiShouMiniProgram) {
return ks[platformApi](args);
} else if (isBaiduSmartProgram) {
return swan[platformApi](args);
}
};
9 changes: 9 additions & 0 deletions src/packages/utils/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const CONTAINER_NAME = {
WECHAT: 'wechatMiniProgram',
ALIPAY: 'aliMiniApp',
BYTE: 'bytedanceMicroApp',
WEB: 'web',
BAIDU: 'baiduSmartProgram',
KWAI: 'kuaishouMiniProgram',
};
export const JSONP_SIGN = '__uni_jsonp_handler_sign__';
54 changes: 54 additions & 0 deletions src/packages/utils/src/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export default class Events {
private events: any;

constructor() {
this.events = {};
}

emit(key: string, params: any) {
if (this.events[key] && this.events[key].size > 0) {
const _queue = new Set(Array.from(this.events[key]));

_queue.forEach(async (item: any) => {
item.handler(params);
if (item.once) {
this.events[key].delete(item);
}
});
}
}

// async _emit(key: string, params: any) {
// if (this.events[key] && this.events[key].length > 0) {
// const item = this.events[key].shift();
// if (isAsync(item)) {
// await item(params);
// } else {
// item(params);
// }
// this.emit(key, params);
// }
// }

once(key: string, cb: (val: any) => void) {
const item = {
once: true,
handler: cb,
};
this.events[key] ? this.events[key].add(item) : (this.events[key] = new Set([item]));
// return () => {
// this.events[key].delete(item);
// };
}

register(key: string, cb: (val: any) => void) {
const item = {
once: false,
handler: cb,
};
this.events[key] ? this.events[key].add(item) : (this.events[key] = new Set([item]));
return () => {
this.events[key].delete(item);
};
}
}
7 changes: 7 additions & 0 deletions src/packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './callPlatformAPI';
export * from './constant';
export * from './event';
export * from './miniappEnvApp';
export * from './promisify';
export * from './styleOptions';
export * from './tools';
5 changes: 5 additions & 0 deletions src/packages/utils/src/miniappEnvApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

function isUndef(type: string): boolean {
return type === 'undefined';
}
export const isDingdingMiniapp = !isUndef(typeof dd) && dd !== null && !isUndef(typeof dd.alert);
53 changes: 53 additions & 0 deletions src/packages/utils/src/promisify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export interface PromisifyArgs<SuccessArg, FailArg> {
success?: (args: SuccessArg) => void;
onSuccess?: (args: SuccessArg) => void;
fail?: (args: FailArg) => void;
onFail?: (args: FailArg) => void;
}
export function promisify<Arg = any, SuccessArg = any, FailArg = any>(
api: (arg: Arg & PromisifyArgs<SuccessArg, FailArg>) => void,
) {
return (arg: Arg & PromisifyArgs<SuccessArg, FailArg>) => {
return new Promise<SuccessArg>((resolve, reject) => {
const promisifyArg: any = arg;

api({
...promisifyArg,
success: (res: SuccessArg) => {
if (promisifyArg && typeof promisifyArg.success === 'function') {
promisifyArg.success(res);
}
resolve(res);
},
onSuccess: (res: SuccessArg) => {
if (promisifyArg && typeof promisifyArg.onSuccess === 'function') {
promisifyArg.onSuccess(res);
}
resolve(res);
},
fail: (res: FailArg) => {
if (promisifyArg && typeof promisifyArg.fail === 'function') {
promisifyArg.fail(res);
}
reject(res);
},
onFail: (res: FailArg) => {
if (promisifyArg && typeof promisifyArg.onFail === 'function') {
promisifyArg.onFail(res);
}
reject(res);
},
complete: (res: SuccessArg | FailArg) => {
if (promisifyArg && typeof promisifyArg.complete === 'function') {
promisifyArg.complete(res);
}
},
onComplete: (res: SuccessArg | FailArg) => {
if (promisifyArg && typeof promisifyArg.onComplete === 'function') {
promisifyArg.onComplete(res);
}
},
});
});
};
}
17 changes: 17 additions & 0 deletions src/packages/utils/src/styleOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const styleOut = (output: any, originalInput, originalOutput) => {
return {
...output,
_original: {
input: { ...originalInput }, // 实际调用 api 方法时传入的参数. 对入参进行 format 之后的结果
output: { ...originalOutput }, // 实际调用 api 方法时传入的参数. 返回值 format 之前的结果
},
};
};

export const styleIn = (options: any, baseName: string) => {
const { _ext = {}, ...rest } = options || {};
return {
...rest,
...(_ext[baseName] || {}),
};
};
21 changes: 21 additions & 0 deletions src/packages/utils/src/tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 比较传入版本1 是否小于传入版本2,如果相等或者大于 都返回false
export const isLessThanVersion = (target, current, splitSign = '.') => {
const targetArr = target.split(splitSign);
const currentArr = current.split(splitSign);

let res = false;
currentArr.some((i, idx) => {
if (!targetArr[idx]) {
return true;
}
if (i > targetArr[idx]) {
res = true;
return true;
} else if (i < targetArr[idx]) {
res = false;
return true;
}
return false;
});
return res;
};