-
Notifications
You must be signed in to change notification settings - Fork 63
feat(utils): add utils package #268
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
Open
cryzzchen
wants to merge
7
commits into
master
Choose a base branch
from
feat/utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f94bce
fix: add return value in tools
44c6bbd
chore: delete utils in main package & add env dependencies
36af0e1
fix: remove uni dep in main
ad59c17
chore: up migrate
02d4489
chore: up migrate
b0a13dd
chore: remove useless
d09cb05
feat: utils unuse del
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # @uni/utils | ||
|
|
||
| 工具函数 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # @uni/utils | ||
|
|
||
| 工具函数 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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__'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| }, | ||
| }); | ||
| }); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] || {}), | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.