Skip to content

Commit

Permalink
feat: add lodash es5 (#86)
Browse files Browse the repository at this point in the history
* feat: add lodash es5

* chore: lint lodash util

Co-authored-by: yuqi.pyq <yuqi.pyq@antgroup.com>
  • Loading branch information
xiaoiver and xiaoiver authored Jun 28, 2022
1 parent 6a15aac commit 18ebf55
Show file tree
Hide file tree
Showing 116 changed files with 1,929 additions and 7 deletions.
7 changes: 3 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript"
],
"ignorePatterns": ["src/lodash/*.ts"],
"rules": {
"no-constant-condition": [
"error",
Expand All @@ -35,4 +34,4 @@
"node": true,
"jest": true
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/util",
"version": "3.1.2",
"version": "3.2.0",
"license": "MIT",
"sideEffects": false,
"main": "lib/index.js",
Expand Down Expand Up @@ -92,6 +92,7 @@
}
},
"dependencies": {
"fast-deep-equal": "^3.1.3"
"fast-deep-equal": "^3.1.3",
"tslib": "^2.3.1"
}
}
1 change: 1 addition & 0 deletions src/index.ts
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';
16 changes: 16 additions & 0 deletions src/lodash/augment.ts
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;
31 changes: 31 additions & 0 deletions src/lodash/cache.ts
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;
}
}
10 changes: 10 additions & 0 deletions src/lodash/clamp.ts
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;
13 changes: 13 additions & 0 deletions src/lodash/clear-animation-frame.ts
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);
}
31 changes: 31 additions & 0 deletions src/lodash/clone.ts
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;
10 changes: 10 additions & 0 deletions src/lodash/contains.ts
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;
21 changes: 21 additions & 0 deletions src/lodash/debounce.ts
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;
39 changes: 39 additions & 0 deletions src/lodash/deep-mix.ts
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;
17 changes: 17 additions & 0 deletions src/lodash/difference.ts
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;
28 changes: 28 additions & 0 deletions src/lodash/each.ts
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;
10 changes: 10 additions & 0 deletions src/lodash/ends-with.ts
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;
14 changes: 14 additions & 0 deletions src/lodash/every.ts
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;
36 changes: 36 additions & 0 deletions src/lodash/extend.ts
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;
18 changes: 18 additions & 0 deletions src/lodash/filter.ts
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;
12 changes: 12 additions & 0 deletions src/lodash/find-index.ts
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;
29 changes: 29 additions & 0 deletions src/lodash/find.ts
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;
Loading

0 comments on commit 18ebf55

Please sign in to comment.