-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
164 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/** | ||
* 从数组中随机获取一项 | ||
* @param arr {T[]} | ||
* @param array {T[]} | ||
* @return {T} | ||
*/ | ||
function arrayRandom<T>(arr: T[]): T { | ||
return arr[Math.floor(Math.random() * arr.length)]; | ||
function arrayRandom<T>(array: T[]): T { | ||
return array[Math.floor(Math.random() * array.length)]; | ||
} | ||
|
||
export default arrayRandom; |
This file contains 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,16 @@ | ||
import arrayEachRight from './each-right'; | ||
|
||
/** | ||
* array splice 多个index | ||
* @param array | ||
* @param indexes | ||
*/ | ||
function arraySplice<T>(array: T[], ...indexes: number[]) { | ||
arrayEachRight(array, (_, index) => { | ||
if (indexes.includes(index)) { | ||
array.splice(index, 1); | ||
} | ||
}); | ||
} | ||
|
||
export default arraySplice; |
This file contains 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 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 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 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 @@ | ||
/** | ||
* 从右边foreach数组 | ||
* @param array | ||
* @param iteratee | ||
*/ | ||
function arrayEachRight<T>( | ||
array: T[], | ||
iteratee: (item: T, index: number, array: T[]) => false | void, | ||
) { | ||
let length = array == null ? 0 : array.length; | ||
|
||
while (length--) { | ||
if (iteratee(array[length], length, array) === false) { | ||
break; | ||
} | ||
} | ||
return array; | ||
} | ||
|
||
export default arrayEachRight; |
This file contains 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 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 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 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 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,12 @@ | ||
import type { AnyFn } from '../type'; | ||
import before from './before'; | ||
|
||
/** | ||
* 只执行一次,之后调用返回第一次的结果 | ||
* @param func | ||
*/ | ||
function once<FN extends AnyFn>(func: FN) { | ||
return before<FN>(2, func); | ||
} | ||
|
||
export default once; |
This file contains 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 |
---|---|---|
@@ -1,42 +1,28 @@ | ||
import requestAnimationFrame from '../bom/requestAnimationFrame'; | ||
import cancelAnimationFrame from '../bom/cancelAnimationFrame'; | ||
import incrementId from './increment-id'; | ||
|
||
const id = incrementId(); | ||
|
||
const timerIdMap: Record<number, number> = {}; | ||
import { prefSetTimeout, clearPrefTimeout } from './pref-setTimeout'; | ||
|
||
/** | ||
* 优先使用 requestAnimationFrame 实现 setInterval | ||
* 如果没有 requestAnimationFrame 则用 setTimeout 实现 setInterval | ||
* @note 当窗口未激活的时候会暂停 | ||
* @param handler | ||
* @param ms | ||
* @param args | ||
*/ | ||
export function prefSetInterval(handler: Function, ms?: number, ...args: any[]): number { | ||
const _id = id(); | ||
const interval = ms || 0; | ||
let startTime = Date.now(); | ||
let endTime = startTime; | ||
const loop = () => { | ||
timerIdMap[_id] = requestAnimationFrame(loop); | ||
endTime = Date.now(); | ||
if (endTime - startTime >= interval) { | ||
function loop() { | ||
return prefSetTimeout(() => { | ||
handler(...args); | ||
startTime = endTime; | ||
} | ||
}; | ||
timerIdMap[_id] = requestAnimationFrame(loop); | ||
return _id; | ||
loop(); | ||
}, interval); | ||
} | ||
return loop(); | ||
} | ||
|
||
/** | ||
* 取消 prefSetInterval | ||
* @param handle | ||
*/ | ||
export function clearPrefSetInterval(handle: number) { | ||
if (timerIdMap[handle]) { | ||
cancelAnimationFrame(timerIdMap[handle]); | ||
delete timerIdMap[handle]; | ||
} | ||
clearPrefTimeout(handle); | ||
} |
This file contains 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 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
File renamed without changes.
File renamed without changes.
This file contains 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,29 @@ | ||
import isFunction from './is-Function'; | ||
import isObject from './is-Object'; | ||
|
||
/** | ||
* 判断是否为平台原生对象 | ||
* @link https://davidwalsh.name/detect-native-function | ||
* @example | ||
* | ||
* isNative(window) // true | ||
* isNative(alert) // true | ||
*/ | ||
function isNative(value) { | ||
const toString = Object.prototype.toString; | ||
const fnToString = Function.prototype.toString; | ||
const regxHostCtor = /^[object .+?Constructor]$/; | ||
const reNative = RegExp( | ||
'^' + | ||
String(toString) | ||
.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&') | ||
.replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + | ||
'$', | ||
); | ||
|
||
return isFunction(value) | ||
? reNative.test(fnToString.call(value)) | ||
: (value && isObject(value) && regxHostCtor.test(toString.call(value))) || false; | ||
} | ||
|
||
export default isNative; |
Oops, something went wrong.