-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add lodash es5 * chore: lint lodash util Co-authored-by: yuqi.pyq <yuqi.pyq@antgroup.com>
- Loading branch information
Showing
116 changed files
with
1,929 additions
and
7 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,3 +1,4 @@ | ||
export * from './color'; | ||
export * from './matrix'; | ||
export * from './path'; | ||
export * from './lodash'; |
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 mix from './mix'; | ||
import isFunction from './is-function'; | ||
import toArray from './to-array'; | ||
|
||
const augment = function (...args: any[]) { | ||
const c = args[0]; | ||
for (let i = 1; i < args.length; i++) { | ||
let obj = args[i]; | ||
if (isFunction(obj)) { | ||
obj = obj.prototype; | ||
} | ||
mix(c.prototype, obj); | ||
} | ||
}; | ||
|
||
export default augment; |
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,31 @@ | ||
/** | ||
* k-v 存储 | ||
*/ | ||
export default class<T> { | ||
map: { [key: string]: T } = {}; | ||
|
||
has(key: string): boolean { | ||
return this.map[key] !== undefined; | ||
} | ||
|
||
get(key: string, def?: T): T | undefined { | ||
const v = this.map[key]; | ||
return v === undefined ? def : v; | ||
} | ||
|
||
set(key: string, value: T): void { | ||
this.map[key] = value; | ||
} | ||
|
||
clear() { | ||
this.map = {}; | ||
} | ||
|
||
delete(key: string) { | ||
delete this.map[key]; | ||
} | ||
|
||
size(): number { | ||
return Object.keys(this.map).length; | ||
} | ||
} |
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,10 @@ | ||
const clamp = function (a: number, min: number, max: number): number { | ||
if (a < min) { | ||
return min; | ||
} else if (a > max) { | ||
return max; | ||
} | ||
return a; | ||
}; | ||
|
||
export default clamp; |
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,13 @@ | ||
export default function cancelAnimationFrame(handler: number) { | ||
const method = | ||
window.cancelAnimationFrame || | ||
// @ts-ignore | ||
window.webkitCancelAnimationFrame || | ||
// @ts-ignore | ||
window.mozCancelAnimationFrame || | ||
// @ts-ignore | ||
window.msCancelAnimationFrame || | ||
clearTimeout; | ||
|
||
method(handler); | ||
} |
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,31 @@ | ||
import isArray from './is-array'; | ||
|
||
const clone = function (obj) { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return obj; | ||
} | ||
let rst; | ||
if (isArray(obj)) { | ||
rst = []; | ||
for (let i = 0, l = obj.length; i < l; i++) { | ||
if (typeof obj[i] === 'object' && obj[i] != null) { | ||
rst[i] = clone(obj[i]); | ||
} else { | ||
rst[i] = obj[i]; | ||
} | ||
} | ||
} else { | ||
rst = {}; | ||
for (const k in obj) { | ||
if (typeof obj[k] === 'object' && obj[k] != null) { | ||
rst[k] = clone(obj[k]); | ||
} else { | ||
rst[k] = obj[k]; | ||
} | ||
} | ||
} | ||
|
||
return rst; | ||
}; | ||
|
||
export default clone; |
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,10 @@ | ||
import isArrayLike from './is-array-like'; | ||
|
||
const contains = function (arr: any[], value: any): boolean { | ||
if (!isArrayLike(arr)) { | ||
return false; | ||
} | ||
return arr.indexOf(value) > -1; | ||
}; | ||
|
||
export default contains; |
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,21 @@ | ||
function debounce(func: Function, wait?: number, immediate?: boolean) { | ||
let timeout; | ||
return function () { | ||
const context = this, | ||
args = arguments; | ||
const later = function () { | ||
timeout = null; | ||
if (!immediate) { | ||
func.apply(context, args); | ||
} | ||
}; | ||
const callNow = immediate && !timeout; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, wait); | ||
if (callNow) { | ||
func.apply(context, args); | ||
} | ||
}; | ||
} | ||
|
||
export default debounce; |
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,39 @@ | ||
import isArray from './is-array'; | ||
import isPlainObject from './is-plain-object'; | ||
|
||
const MAX_MIX_LEVEL = 5; | ||
|
||
function _deepMix(dist, src, level?, maxLevel?) { | ||
level = level || 0; | ||
maxLevel = maxLevel || MAX_MIX_LEVEL; | ||
for (const key in src) { | ||
if (src.hasOwnProperty(key)) { | ||
const value = src[key]; | ||
if (value !== null && isPlainObject(value)) { | ||
if (!isPlainObject(dist[key])) { | ||
dist[key] = {}; | ||
} | ||
if (level < maxLevel) { | ||
_deepMix(dist[key], value, level + 1, maxLevel); | ||
} else { | ||
dist[key] = src[key]; | ||
} | ||
} else if (isArray(value)) { | ||
dist[key] = []; | ||
dist[key] = dist[key].concat(value); | ||
} else if (value !== undefined) { | ||
dist[key] = value; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// todo 重写 | ||
const deepMix = function (rst: any, ...args: any[]) { | ||
for (let i = 0; i < args.length; i += 1) { | ||
_deepMix(rst, args[i]); | ||
} | ||
return rst; | ||
}; | ||
|
||
export default deepMix; |
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,17 @@ | ||
import filter from './filter'; | ||
import contains from './contains'; | ||
|
||
/** | ||
* Flattens `array` a single level deep. | ||
* | ||
* @param {Array} arr The array to inspect. | ||
* @param {Array} values The values to exclude. | ||
* @return {Array} Returns the new array of filtered values. | ||
* @example | ||
* difference([2, 1], [2, 3]); // => [1] | ||
*/ | ||
const difference = function <T>(arr: T[], values: T[] = []): T[] { | ||
return filter(arr, (value: any) => !contains(values, value)); | ||
}; | ||
|
||
export default difference; |
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,28 @@ | ||
import isArray from './is-array'; | ||
import isObject from './is-object'; | ||
|
||
function each(elements: any[] | object, func: (v: any, k: any) => any): void { | ||
if (!elements) { | ||
return; | ||
} | ||
let rst; | ||
if (isArray(elements)) { | ||
for (let i = 0, len = elements.length; i < len; i++) { | ||
rst = func(elements[i], i); | ||
if (rst === false) { | ||
break; | ||
} | ||
} | ||
} else if (isObject(elements)) { | ||
for (const k in elements) { | ||
if (elements.hasOwnProperty(k)) { | ||
rst = func(elements[k], k); | ||
if (rst === false) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default each; |
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,10 @@ | ||
import isArray from './is-array'; | ||
import isString from './is-string'; | ||
|
||
function endsWith(arr: string, e: string): boolean; | ||
function endsWith<T>(arr: T[], e: T): boolean; | ||
function endsWith<T>(arr: string | T[], e: string | T): boolean { | ||
return isArray(arr) || isString(arr) ? arr[arr.length - 1] === e : false; | ||
} | ||
|
||
export default endsWith; |
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,14 @@ | ||
/** | ||
* 只要有一个不满足条件就返回 false | ||
* @param arr | ||
* @param func | ||
*/ | ||
const every = function <T>(arr: T[], func: (v: T, idx?: number) => any): boolean { | ||
for (let i = 0; i < arr.length; i++) { | ||
if (!func(arr[i], i)) return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
export default every; |
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,36 @@ | ||
import mix from './mix'; | ||
import isFunction from './is-function'; | ||
|
||
const extend = function (subclass, superclass, overrides?, staticOverrides?) { | ||
// 如果只提供父类构造函数,则自动生成子类构造函数 | ||
if (!isFunction(superclass)) { | ||
overrides = superclass; | ||
superclass = subclass; | ||
subclass = function () {}; | ||
} | ||
|
||
const create = Object.create | ||
? function (proto, c) { | ||
return Object.create(proto, { | ||
constructor: { | ||
value: c, | ||
}, | ||
}); | ||
} | ||
: function (proto, c) { | ||
function Tmp() {} | ||
Tmp.prototype = proto; | ||
const o = new Tmp(); | ||
o.constructor = c; | ||
return o; | ||
}; | ||
|
||
const superObj = create(superclass.prototype, subclass); // new superclass(),//实例化父类作为子类的prototype | ||
subclass.prototype = mix(superObj, subclass.prototype); // 指定子类的prototype | ||
subclass.superclass = create(superclass.prototype, superclass); | ||
mix(superObj, overrides); | ||
mix(subclass, staticOverrides); | ||
return subclass; | ||
}; | ||
|
||
export default extend; |
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,18 @@ | ||
import isArrayLike from './is-array-like'; | ||
|
||
const filter = function <T>(arr: T[], func: (v: T, idx: number) => boolean): T[] { | ||
if (!isArrayLike(arr)) { | ||
return arr; | ||
} | ||
const result: T[] = []; | ||
for (let index = 0; index < arr.length; index++) { | ||
const value = arr[index]; | ||
if (func(value, index)) { | ||
result.push(value); | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export default filter; |
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 @@ | ||
function findIndex<T>(arr: T[], predicate: (item: T, idx?: number) => boolean, fromIndex: number = 0): number { | ||
for (let i = fromIndex; i < arr.length; i++) { | ||
if (predicate(arr[i], i)) { | ||
// 找到终止循环 | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
export default findIndex; |
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 isMatch from './is-match'; | ||
import isArray from './is-array'; | ||
import isPlainObject from './is-plain-object'; | ||
|
||
function find<T>(arr: T[], predicate: Function): T; | ||
function find<T>(arr: T[], predicate: object): T; | ||
|
||
function find<T>(arr: T[], predicate: Function | object): T { | ||
if (!isArray(arr)) return null; | ||
|
||
let _predicate; | ||
if (isFunction(predicate)) { | ||
_predicate = predicate; | ||
} | ||
if (isPlainObject(predicate)) { | ||
_predicate = (a) => isMatch(a, predicate); | ||
} | ||
if (_predicate) { | ||
for (let i = 0; i < arr.length; i += 1) { | ||
if (_predicate(arr[i])) { | ||
return arr[i]; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
export default find; |
Oops, something went wrong.