Skip to content

Commit

Permalink
refactor: optimize lodash utils (#118)
Browse files Browse the repository at this point in the history
* refactor: adjust utils of lodash

* test: add test case

* chore: config lint-staged and commitlint

* refactor: adjust memoize

* chore: update rollup config

* test: fix test case

* refactor: fix cr issue
  • Loading branch information
Aarebecca authored Sep 19, 2024
1 parent fd83261 commit cb026c1
Show file tree
Hide file tree
Showing 43 changed files with 578 additions and 154 deletions.
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no-install commitlint --edit ""
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx lint-staged
27 changes: 27 additions & 0 deletions __tests__/unit/color/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { gradient, toCSSGradient, toRGB } from '../../../src';

describe('color', function () {
let mockGetComputedStyle;
beforeAll(() => {
const colorMap = {
red: 'rgb(255, 0, 0)',
white: 'rgb(255, 255, 255)',
black: 'rgb(0, 0, 0)',
blue: 'rgb(0, 0, 255)',
'#ddd': 'rgb(221, 221, 221)',
'#eeeeee': 'rgb(238, 238, 238)',
};

// implement document defaultView getComputedStyle getPropertyValue in jsdom
mockGetComputedStyle = jest.spyOn(document.defaultView!, 'getComputedStyle').mockImplementation(

Check warning on line 16 in __tests__/unit/color/index.spec.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion
(el) =>
({
getPropertyValue: () => {
const color = (el as HTMLElement).style.color;
return colorMap[color] || color;
},
}) as any,
);
});

afterAll(() => {
mockGetComputedStyle?.mockRestore();
});

it('toRGB', () => {
expect(toRGB('red')).toBe('#ff0000');
expect(toRGB('white')).toBe('#ffffff');
Expand Down
23 changes: 23 additions & 0 deletions __tests__/unit/lodash/is-array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isArray } from '../../../src/lodash';

describe('isArray', () => {
it('array literal', () => {
expect(isArray([])).toBe(true);
expect(isArray([1, 2, 3])).toBe(true);
});

it('array object', () => {
expect(isArray(new Array(10))).toBe(true);
expect(isArray(new Array(10).fill(0))).toBe(true);
});

it('not array', () => {
expect(isArray(0)).toBe(false);
expect(isArray(123)).toBe(false);
expect(isArray('')).toBe(false);
expect(isArray('abc')).toBe(false);
expect(isArray({})).toBe(false);
expect(isArray(null)).toBe(false);
expect(isArray(undefined)).toBe(false);
});
});
19 changes: 19 additions & 0 deletions __tests__/unit/lodash/is-date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { isDate } from '../../../src/lodash';

describe('isDate', () => {
it('date', () => {
expect(isDate(new Date())).toBe(true);
expect(isDate(new Date('2024'))).toBe(true);
});

it('not date', () => {
expect(isDate(0)).toBe(false);
expect(isDate(123)).toBe(false);
expect(isDate('')).toBe(false);
expect(isDate('abc')).toBe(false);
expect(isDate([])).toBe(false);
expect(isDate({})).toBe(false);
expect(isDate(null)).toBe(false);
expect(isDate(undefined)).toBe(false);
});
});
23 changes: 23 additions & 0 deletions __tests__/unit/lodash/is-decimal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isDecimal } from '../../../src/lodash';

describe('isDecimal', () => {
it('decimal', () => {
expect(isDecimal(0.1)).toBe(true);
expect(isDecimal(0.123)).toBe(true);
expect(isDecimal(0.123456789)).toBe(true);
expect(isDecimal(0.1234567890123456789)).toBe(true);
expect(isDecimal(0.12345678901234567890123456789)).toBe(true);
expect(isDecimal(0.123456789012345678901234567890123456789)).toBe(true);
});

it('not decimal', () => {
expect(isDecimal(0)).toBe(false);
expect(isDecimal(123)).toBe(false);
expect(isDecimal('')).toBe(false);
expect(isDecimal('abc')).toBe(false);
expect(isDecimal([])).toBe(false);
expect(isDecimal({})).toBe(false);
expect(isDecimal(null)).toBe(false);
expect(isDecimal(undefined)).toBe(false);
});
});
22 changes: 22 additions & 0 deletions __tests__/unit/lodash/is-element.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isElement } from '../../../src/lodash';

describe('isElement', () => {
it('element', () => {
expect(isElement(document.createElement('div'))).toBe(true);
expect(isElement(document.createElement('span'))).toBe(true);
expect(isElement(document.createElement('a'))).toBe(true);
expect(isElement(document)).toBe(true);
});

it('not element', () => {
expect(isElement(window)).toBe(false);
expect(isElement(0)).toBe(false);
expect(isElement(123)).toBe(false);
expect(isElement('')).toBe(false);
expect(isElement('abc')).toBe(false);
expect(isElement([])).toBe(false);
expect(isElement({})).toBe(false);
expect(isElement(null)).toBe(false);
expect(isElement(undefined)).toBe(false);
});
});
31 changes: 31 additions & 0 deletions __tests__/unit/lodash/is-even.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isEven } from '../../../src/lodash';

describe('isEven', () => {
it('even', () => {
expect(isEven(0)).toBe(true);
expect(isEven(2)).toBe(true);
expect(isEven(4)).toBe(true);
expect(isEven(6)).toBe(true);
expect(isEven(8)).toBe(true);
expect(isEven(10)).toBe(true);
expect(isEven(12)).toBe(true);
expect(isEven(14)).toBe(true);
expect(isEven(16)).toBe(true);
expect(isEven(18)).toBe(true);
expect(isEven(20)).toBe(true);
});

it('not even', () => {
expect(isEven(1)).toBe(false);
expect(isEven(3)).toBe(false);
expect(isEven(5)).toBe(false);
expect(isEven(7)).toBe(false);
expect(isEven(9)).toBe(false);
expect(isEven(11)).toBe(false);
expect(isEven(13)).toBe(false);
expect(isEven(15)).toBe(false);
expect(isEven(17)).toBe(false);
expect(isEven(19)).toBe(false);
expect(isEven(21)).toBe(false);
});
});
20 changes: 20 additions & 0 deletions __tests__/unit/lodash/is-finite.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isFinite } from '../../../src/lodash';

describe('isFinite', () => {
it('finite', () => {
expect(isFinite(0)).toBe(true);
expect(isFinite(123)).toBe(true);
expect(isFinite(0.1)).toBe(true);
expect(isFinite(0.123)).toBe(true);
expect(isFinite(0.123456789)).toBe(true);
expect(isFinite(0.1234567890123456789)).toBe(true);
expect(isFinite(0.12345678901234567890123456789)).toBe(true);
expect(isFinite(0.123456789012345678901234567890123456789)).toBe(true);
});

it('not finite', () => {
expect(isFinite(Infinity)).toBe(false);
expect(isFinite(-Infinity)).toBe(false);
expect(isFinite(NaN)).toBe(false);
});
});
20 changes: 20 additions & 0 deletions __tests__/unit/lodash/is-function.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isFunction } from '../../../src/lodash';

describe('isFunction', () => {
it('function', () => {
expect(isFunction(() => {})).toBe(true);
expect(isFunction(function () {})).toBe(true);
expect(isFunction(new Function())).toBe(true);
});

it('not function', () => {
expect(isFunction(0)).toBe(false);
expect(isFunction(123)).toBe(false);
expect(isFunction('')).toBe(false);
expect(isFunction('abc')).toBe(false);
expect(isFunction([])).toBe(false);
expect(isFunction({})).toBe(false);
expect(isFunction(null)).toBe(false);
expect(isFunction(undefined)).toBe(false);
});
});
21 changes: 21 additions & 0 deletions __tests__/unit/lodash/is-integer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isInteger } from '../../../src/lodash';

describe('isInteger', () => {
it('integer', () => {
expect(isInteger(0)).toBe(true);
expect(isInteger(123)).toBe(true);
expect(isInteger(-123)).toBe(true);
});

it('not integer', () => {
expect(isInteger(0.1)).toBe(false);
expect(isInteger(0.123)).toBe(false);
expect(isInteger(0.123456789)).toBe(false);
expect(isInteger(0.1234567890123456789)).toBe(false);
expect(isInteger(0.12345678901234567890123456789)).toBe(false);
expect(isInteger(0.123456789012345678901234567890123456789)).toBe(false);
expect(isInteger(Infinity)).toBe(false);
expect(isInteger(-Infinity)).toBe(false);
expect(isInteger(NaN)).toBe(false);
});
});
24 changes: 24 additions & 0 deletions __tests__/unit/lodash/is-negative.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isNegative } from '../../../src/lodash';

describe('isNegative', () => {
it('negative', () => {
expect(isNegative(-1)).toBe(true);
expect(isNegative(-0.1)).toBe(true);
expect(isNegative(-0.123)).toBe(true);
expect(isNegative(-0.123456789)).toBe(true);
expect(isNegative(-0.1234567890123456789)).toBe(true);
expect(isNegative(-0.12345678901234567890123456789)).toBe(true);
expect(isNegative(-0.123456789012345678901234567890123456789)).toBe(true);
});

it('not negative', () => {
expect(isNegative(0)).toBe(false);
expect(isNegative(1)).toBe(false);
expect(isNegative(0.1)).toBe(false);
expect(isNegative(0.123)).toBe(false);
expect(isNegative(0.123456789)).toBe(false);
expect(isNegative(0.1234567890123456789)).toBe(false);
expect(isNegative(0.12345678901234567890123456789)).toBe(false);
expect(isNegative(0.123456789012345678901234567890123456789)).toBe(false);
});
});
17 changes: 17 additions & 0 deletions __tests__/unit/lodash/is-nil.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isNil } from '../../../src/lodash';

describe('isNil', () => {
it('nil', () => {
expect(isNil(null)).toBe(true);
expect(isNil(undefined)).toBe(true);
});

it('not nil', () => {
expect(isNil(0)).toBe(false);
expect(isNil(123)).toBe(false);
expect(isNil('')).toBe(false);
expect(isNil('abc')).toBe(false);
expect(isNil([])).toBe(false);
expect(isNil({})).toBe(false);
});
});
17 changes: 17 additions & 0 deletions __tests__/unit/lodash/is-null.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isNull } from '../../../src/lodash';

describe('isNull', () => {
it('null', () => {
expect(isNull(null)).toBe(true);
});

it('not null', () => {
expect(isNull(0)).toBe(false);
expect(isNull(123)).toBe(false);
expect(isNull('')).toBe(false);
expect(isNull('abc')).toBe(false);
expect(isNull([])).toBe(false);
expect(isNull({})).toBe(false);
expect(isNull(undefined)).toBe(false);
});
});
24 changes: 24 additions & 0 deletions __tests__/unit/lodash/is-number-equal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isNumberEqual } from '../../../src/lodash';

describe('isNumberEqual', () => {
it('equal', () => {
expect(isNumberEqual(0, 0)).toBe(true);
expect(isNumberEqual(1, 1)).toBe(true);
expect(isNumberEqual(0.1, 0.1)).toBe(true);
expect(isNumberEqual(0.123, 0.123)).toBe(true);
expect(isNumberEqual(0.123456789, 0.123456789)).toBe(true);
expect(isNumberEqual(0.1234567890123456789, 0.1234567890123456789)).toBe(true);
expect(isNumberEqual(0.12345678901234567890123456789, 0.12345678901234567890123456789)).toBe(true);
expect(isNumberEqual(0.123456789012345678901234567890123456789, 0.123456789012345678901234567890123456789)).toBe(
true,
);
});

it('not equal', () => {
expect(isNumberEqual(0, 1)).toBe(false);
expect(isNumberEqual(1, 0)).toBe(false);
expect(isNumberEqual(0.1, 0.2)).toBe(false);
expect(isNumberEqual(0.123, 0.124)).toBe(false);
expect(isNumberEqual(0.123456789, 0.123456788, 0.0000000001)).toBe(false);
});
});
30 changes: 30 additions & 0 deletions __tests__/unit/lodash/is-number.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isNumber } from '../../../src/lodash';

describe('isNumber', () => {
it('number literal', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(0.1)).toBe(true);
expect(isNumber(123.4)).toBe(true);
expect(isNumber(-0)).toBe(true);
expect(isNumber(-123)).toBe(true);
expect(isNumber(-0.1)).toBe(true);
expect(isNumber(-123.4)).toBe(true);
});

it('number object', () => {
expect(isNumber(new Number(0))).toBe(false);
expect(isNumber(new Number(123))).toBe(false);
expect(isNumber(new Number(0.1))).toBe(false);
expect(isNumber(new Number(123.4))).toBe(false);
});

it('not number', () => {
expect(isNumber('')).toBe(false);
expect(isNumber('abc')).toBe(false);
expect(isNumber([])).toBe(false);
expect(isNumber({})).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isNumber(undefined)).toBe(false);
});
});
30 changes: 30 additions & 0 deletions __tests__/unit/lodash/is-odd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isOdd } from '../../../src/lodash';

describe('isOdd', () => {
it('odd', () => {
expect(isOdd(1)).toBe(true);
expect(isOdd(3)).toBe(true);
expect(isOdd(5)).toBe(true);
expect(isOdd(7)).toBe(true);
expect(isOdd(9)).toBe(true);
expect(isOdd(11)).toBe(true);
expect(isOdd(13)).toBe(true);
expect(isOdd(15)).toBe(true);
expect(isOdd(17)).toBe(true);
expect(isOdd(19)).toBe(true);
});

it('not odd', () => {
expect(isOdd(0)).toBe(false);
expect(isOdd(2)).toBe(false);
expect(isOdd(4)).toBe(false);
expect(isOdd(6)).toBe(false);
expect(isOdd(8)).toBe(false);
expect(isOdd(10)).toBe(false);
expect(isOdd(12)).toBe(false);
expect(isOdd(14)).toBe(false);
expect(isOdd(16)).toBe(false);
expect(isOdd(18)).toBe(false);
expect(isOdd(20)).toBe(false);
});
});
24 changes: 24 additions & 0 deletions __tests__/unit/lodash/is-string.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isString } from '../../../src/lodash';

describe('isString', () => {
it('string literal', () => {
expect(isString('')).toBe(true);
expect(isString('abc')).toBe(true);
expect(isString('123')).toBe(true);
});

it('string object', () => {
expect(isString(new String(''))).toBe(false);
expect(isString(new String('abc'))).toBe(false);
expect(isString(new String('123'))).toBe(false);
});

it('not string', () => {
expect(isString(0)).toBe(false);
expect(isString(123)).toBe(false);
expect(isString([])).toBe(false);
expect(isString({})).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
});
});
Loading

0 comments on commit cb026c1

Please sign in to comment.