es-toolkit 0.0.1 → 1.0.0-dev.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +31 -0
- package/dist/array/chunk.d.ts +21 -5
- package/dist/array/difference.d.ts +20 -4
- package/dist/array/differenceBy.d.ts +23 -5
- package/dist/array/differenceWith.d.ts +19 -5
- package/dist/array/drop.d.ts +13 -4
- package/dist/array/dropRight.d.ts +13 -4
- package/dist/array/dropRightWhile.d.ts +15 -4
- package/dist/array/dropWhile.d.ts +15 -4
- package/dist/array/groupBy.d.ts +31 -1
- package/dist/array/groupBy.spec.d.ts +1 -0
- package/dist/array/index.js +437 -224
- package/dist/array/intersection.d.ts +13 -6
- package/dist/array/intersectionBy.d.ts +17 -8
- package/dist/array/intersectionWith.d.ts +18 -8
- package/dist/array/partition.d.ts +19 -5
- package/dist/array/sample.d.ts +10 -3
- package/dist/array/shuffle.d.ts +11 -4
- package/dist/array/takeRight.d.ts +0 -2
- package/dist/array/union.d.ts +13 -6
- package/dist/array/unionBy.d.ts +0 -3
- package/dist/array/unionWith.d.ts +15 -9
- package/dist/array/uniq.d.ts +9 -8
- package/dist/array/xor.d.ts +0 -2
- package/dist/array/zip.d.ts +18 -6
- package/dist/array/zipWith.d.ts +26 -3
- package/dist/function/debounce.d.ts +4 -4
- package/dist/function/index.js +4 -5
- package/dist/function/once.d.ts +0 -1
- package/dist/index.js +651 -248
- package/dist/math/clamp.d.ts +16 -8
- package/dist/math/index.js +114 -29
- package/dist/math/round.d.ts +11 -8
- package/dist/math/sum.d.ts +10 -9
- package/dist/object/index.js +122 -12
- package/dist/object/omit.d.ts +13 -4
- package/dist/object/omitBy.d.ts +19 -1
- package/dist/object/omitBy.spec.d.ts +1 -0
- package/dist/object/pick.d.ts +13 -4
- package/dist/object/pickBy.d.ts +19 -1
- package/dist/object/pickBy.spec.d.ts +1 -0
- package/dist/predicate/index.js +58 -10
- package/dist/predicate/isNil.d.ts +17 -3
- package/dist/predicate/isNotNil.d.ts +7 -3
- package/dist/predicate/isNull.d.ts +17 -2
- package/dist/predicate/isUndefined.d.ts +17 -2
- package/dist/promise/delay.d.ts +17 -4
- package/dist/promise/index.js +16 -3
- package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +1 -37
- package/esm/array/chunk.mjs +28 -25
- package/esm/array/difference.mjs +20 -4
- package/esm/array/differenceBy.mjs +23 -5
- package/esm/array/differenceWith.mjs +19 -5
- package/esm/array/drop.mjs +13 -4
- package/esm/array/dropRight.mjs +13 -4
- package/esm/array/dropRightWhile.mjs +15 -4
- package/esm/array/dropWhile.mjs +15 -4
- package/esm/array/groupBy.mjs +51 -2
- package/esm/array/intersection.mjs +13 -6
- package/esm/array/intersectionBy.mjs +17 -8
- package/esm/array/intersectionWith.mjs +18 -8
- package/esm/array/partition.mjs +19 -5
- package/esm/array/sample.mjs +10 -3
- package/esm/array/shuffle.mjs +12 -5
- package/esm/array/takeRight.mjs +0 -2
- package/esm/array/union.mjs +14 -10
- package/esm/array/unionBy.mjs +0 -3
- package/esm/array/unionWith.mjs +16 -11
- package/esm/array/uniq.mjs +28 -9
- package/esm/array/xor.mjs +0 -2
- package/esm/array/zip.mjs +18 -6
- package/esm/array/zipWith.mjs +26 -3
- package/esm/function/debounce.mjs +4 -4
- package/esm/function/once.mjs +0 -1
- package/esm/math/_virtual/_rollupPluginBabelHelpers.mjs +66 -0
- package/esm/math/clamp.mjs +15 -7
- package/esm/math/round.mjs +11 -8
- package/esm/math/sum.mjs +25 -14
- package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +37 -1
- package/esm/object/omit.mjs +13 -4
- package/esm/object/omitBy.mjs +32 -2
- package/esm/object/pick.mjs +13 -4
- package/esm/object/pickBy.mjs +32 -2
- package/esm/predicate/isNil.mjs +17 -3
- package/esm/predicate/isNotNil.mjs +7 -3
- package/esm/predicate/isNull.mjs +17 -2
- package/esm/predicate/isUndefined.mjs +17 -2
- package/esm/promise/delay.mjs +16 -3
- package/package.json +17 -3
- package/.vscode/extensions.json +0 -7
- package/.vscode/settings.json +0 -10
- package/.yarn/sdks/eslint/bin/eslint.js +0 -20
- package/.yarn/sdks/eslint/lib/api.js +0 -20
- package/.yarn/sdks/eslint/lib/unsupported-api.js +0 -20
- package/.yarn/sdks/eslint/package.json +0 -14
- package/.yarn/sdks/integrations.yml +0 -5
- package/.yarn/sdks/prettier/bin/prettier.cjs +0 -20
- package/.yarn/sdks/prettier/index.cjs +0 -20
- package/.yarn/sdks/prettier/package.json +0 -7
- package/.yarn/sdks/typescript/bin/tsc +0 -20
- package/.yarn/sdks/typescript/bin/tsserver +0 -20
- package/.yarn/sdks/typescript/lib/tsc.js +0 -20
- package/.yarn/sdks/typescript/lib/tsserver.js +0 -225
- package/.yarn/sdks/typescript/lib/tsserverlibrary.js +0 -225
- package/.yarn/sdks/typescript/lib/typescript.js +0 -20
- package/.yarn/sdks/typescript/package.json +0 -10
- package/babel.config.js +0 -6
- package/coverage/.tmp/coverage-0.json +0 -1
- package/coverage/.tmp/coverage-1.json +0 -1
- package/coverage/.tmp/coverage-10.json +0 -1
- package/coverage/.tmp/coverage-11.json +0 -1
- package/coverage/.tmp/coverage-12.json +0 -1
- package/coverage/.tmp/coverage-13.json +0 -1
- package/coverage/.tmp/coverage-14.json +0 -1
- package/coverage/.tmp/coverage-15.json +0 -1
- package/coverage/.tmp/coverage-16.json +0 -1
- package/coverage/.tmp/coverage-17.json +0 -1
- package/coverage/.tmp/coverage-18.json +0 -1
- package/coverage/.tmp/coverage-19.json +0 -1
- package/coverage/.tmp/coverage-2.json +0 -1
- package/coverage/.tmp/coverage-20.json +0 -1
- package/coverage/.tmp/coverage-21.json +0 -1
- package/coverage/.tmp/coverage-22.json +0 -1
- package/coverage/.tmp/coverage-23.json +0 -1
- package/coverage/.tmp/coverage-24.json +0 -1
- package/coverage/.tmp/coverage-25.json +0 -1
- package/coverage/.tmp/coverage-26.json +0 -1
- package/coverage/.tmp/coverage-27.json +0 -1
- package/coverage/.tmp/coverage-28.json +0 -1
- package/coverage/.tmp/coverage-29.json +0 -1
- package/coverage/.tmp/coverage-3.json +0 -1
- package/coverage/.tmp/coverage-30.json +0 -1
- package/coverage/.tmp/coverage-4.json +0 -1
- package/coverage/.tmp/coverage-5.json +0 -1
- package/coverage/.tmp/coverage-6.json +0 -1
- package/coverage/.tmp/coverage-7.json +0 -1
- package/coverage/.tmp/coverage-8.json +0 -1
- package/coverage/.tmp/coverage-9.json +0 -1
- package/rollup.config.js +0 -5
- package/src/array/chunk.spec.ts +0 -27
- package/src/array/chunk.ts +0 -24
- package/src/array/difference.spec.ts +0 -10
- package/src/array/difference.ts +0 -11
- package/src/array/differenceBy.spec.ts +0 -9
- package/src/array/differenceBy.ts +0 -14
- package/src/array/differenceWith.spec.ts +0 -8
- package/src/array/differenceWith.ts +0 -14
- package/src/array/drop.spec.ts +0 -9
- package/src/array/drop.ts +0 -9
- package/src/array/dropRight.spec.ts +0 -9
- package/src/array/dropRight.ts +0 -9
- package/src/array/dropRightWhile.spec.ts +0 -16
- package/src/array/dropRightWhile.ts +0 -13
- package/src/array/dropWhile.spec.ts +0 -14
- package/src/array/dropWhile.ts +0 -10
- package/src/array/groupBy.ts +0 -3
- package/src/array/index.ts +0 -30
- package/src/array/intersection.spec.ts +0 -11
- package/src/array/intersection.ts +0 -15
- package/src/array/intersectionBy.spec.ts +0 -9
- package/src/array/intersectionBy.ts +0 -18
- package/src/array/intersectionWith.spec.ts +0 -9
- package/src/array/intersectionWith.ts +0 -18
- package/src/array/partition.spec.ts +0 -23
- package/src/array/partition.ts +0 -21
- package/src/array/sample.spec.ts +0 -11
- package/src/array/sample.ts +0 -10
- package/src/array/shuffle.spec.ts +0 -11
- package/src/array/shuffle.ts +0 -18
- package/src/array/take.spec.ts +0 -25
- package/src/array/take.ts +0 -25
- package/src/array/takeRight.spec.ts +0 -25
- package/src/array/takeRight.ts +0 -29
- package/src/array/takeRightWhile.spec.ts +0 -16
- package/src/array/takeRightWhile.ts +0 -26
- package/src/array/takeWhile.spec.ts +0 -34
- package/src/array/takeWhile.ts +0 -31
- package/src/array/union.spec.ts +0 -10
- package/src/array/union.ts +0 -14
- package/src/array/unionBy.spec.ts +0 -12
- package/src/array/unionBy.ts +0 -35
- package/src/array/unionWith.spec.ts +0 -23
- package/src/array/unionWith.ts +0 -18
- package/src/array/uniq.spec.ts +0 -8
- package/src/array/uniq.ts +0 -16
- package/src/array/uniqBy.spec.ts +0 -8
- package/src/array/uniqBy.ts +0 -17
- package/src/array/uniqWith.spec.ts +0 -16
- package/src/array/uniqWith.ts +0 -29
- package/src/array/xor.spec.ts +0 -13
- package/src/array/xor.ts +0 -25
- package/src/array/xorBy.spec.ts +0 -16
- package/src/array/xorBy.ts +0 -29
- package/src/array/xorWith.spec.ts +0 -16
- package/src/array/xorWith.ts +0 -28
- package/src/array/zip.spec.ts +0 -14
- package/src/array/zip.ts +0 -34
- package/src/array/zipWith.spec.ts +0 -13
- package/src/array/zipWith.ts +0 -23
- package/src/function/debounce.spec.ts +0 -76
- package/src/function/debounce.ts +0 -42
- package/src/function/index.ts +0 -3
- package/src/function/once.spec.ts +0 -31
- package/src/function/once.ts +0 -34
- package/src/function/throttle.spec.ts +0 -32
- package/src/function/throttle.ts +0 -39
- package/src/index.ts +0 -6
- package/src/math/clamp.spec.ts +0 -18
- package/src/math/clamp.ts +0 -17
- package/src/math/index.ts +0 -3
- package/src/math/round.spec.ts +0 -53
- package/src/math/round.ts +0 -16
- package/src/math/sum.spec.ts +0 -29
- package/src/math/sum.ts +0 -17
- package/src/object/index.ts +0 -4
- package/src/object/omit.spec.ts +0 -10
- package/src/object/omit.ts +0 -15
- package/src/object/omitBy.ts +0 -3
- package/src/object/pick.spec.ts +0 -10
- package/src/object/pick.ts +0 -15
- package/src/object/pickBy.ts +0 -3
- package/src/predicate/index.ts +0 -4
- package/src/predicate/isNil.spec.ts +0 -23
- package/src/predicate/isNil.ts +0 -8
- package/src/predicate/isNotNil.spec.ts +0 -29
- package/src/predicate/isNotNil.ts +0 -14
- package/src/predicate/isNull.spec.ts +0 -23
- package/src/predicate/isNull.ts +0 -8
- package/src/predicate/isUndefined.spec.ts +0 -23
- package/src/predicate/isUndefined.ts +0 -8
- package/src/promise/delay.spec.ts +0 -13
- package/src/promise/delay.ts +0 -10
- package/src/promise/index.ts +0 -1
- package/tsconfig.json +0 -12
- package/vitest.config.mts +0 -13
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
-
import { once } from './once'; // adjust the import path as necessary
|
|
3
|
-
|
|
4
|
-
describe('once', () => {
|
|
5
|
-
it('should call the function only once', () => {
|
|
6
|
-
const func = vi.fn(() => 42);
|
|
7
|
-
const onceFunc = once(func);
|
|
8
|
-
|
|
9
|
-
expect(onceFunc()).toBe(42);
|
|
10
|
-
expect(onceFunc()).toBe(42);
|
|
11
|
-
expect(func).toHaveBeenCalledTimes(1);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('should work with functions returning undefined', () => {
|
|
15
|
-
const func = vi.fn(() => undefined);
|
|
16
|
-
const onceFunc = once(func);
|
|
17
|
-
|
|
18
|
-
expect(onceFunc()).toBeUndefined();
|
|
19
|
-
expect(onceFunc()).toBeUndefined();
|
|
20
|
-
expect(func).toHaveBeenCalledTimes(1);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should handle functions with no return value', () => {
|
|
24
|
-
const func = vi.fn(() => { console.log('Side effect'); });
|
|
25
|
-
const onceFunc = once(func);
|
|
26
|
-
|
|
27
|
-
onceFunc();
|
|
28
|
-
onceFunc();
|
|
29
|
-
expect(func).toHaveBeenCalledTimes(1);
|
|
30
|
-
});
|
|
31
|
-
});
|
package/src/function/once.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a function that is restricted to invoking the provided function `func` once.
|
|
3
|
-
* Repeated calls to the function will return the value from the first invocation.
|
|
4
|
-
*
|
|
5
|
-
* @template F - The type of the function to be invoked once.
|
|
6
|
-
* @param {F} func - The function to restrict.
|
|
7
|
-
* @returns {F} A new function that invokes `func` once and caches the result.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const initialize = once(() => {
|
|
11
|
-
* console.log('Initialized!');
|
|
12
|
-
* return true;
|
|
13
|
-
* });
|
|
14
|
-
*
|
|
15
|
-
* initialize(); // Logs: 'Initialized!' and returns true
|
|
16
|
-
* initialize(); // Returns true without logging
|
|
17
|
-
*/
|
|
18
|
-
export function once<F extends () => any>(func: F): F {
|
|
19
|
-
let called = false;
|
|
20
|
-
let cache: ReturnType<F> | undefined;
|
|
21
|
-
|
|
22
|
-
return function () {
|
|
23
|
-
if (called) {
|
|
24
|
-
return cache;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const result = func();
|
|
28
|
-
|
|
29
|
-
called = true;
|
|
30
|
-
cache = result;
|
|
31
|
-
|
|
32
|
-
return result;
|
|
33
|
-
} as F;
|
|
34
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
-
import { throttle } from './throttle';
|
|
3
|
-
import { delay } from '../promise';
|
|
4
|
-
|
|
5
|
-
describe('throttle', () => {
|
|
6
|
-
it('should throttle function calls', async () => {
|
|
7
|
-
const func = vi.fn();
|
|
8
|
-
const throttledFunc = throttle(func, 100);
|
|
9
|
-
|
|
10
|
-
throttledFunc();
|
|
11
|
-
throttledFunc();
|
|
12
|
-
throttledFunc();
|
|
13
|
-
|
|
14
|
-
expect(func).toHaveBeenCalledTimes(1);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('should execute the function immediately if not called within the wait time', async () => {
|
|
18
|
-
const func = vi.fn();
|
|
19
|
-
const waitMs = 50;
|
|
20
|
-
const throttledFunc = throttle(func, waitMs);
|
|
21
|
-
|
|
22
|
-
throttledFunc();
|
|
23
|
-
await delay(waitMs / 2);
|
|
24
|
-
throttledFunc();
|
|
25
|
-
|
|
26
|
-
expect(func).toHaveBeenCalledTimes(1);
|
|
27
|
-
|
|
28
|
-
await delay(waitMs / 2 + 1);
|
|
29
|
-
throttledFunc();
|
|
30
|
-
expect(func).toHaveBeenCalledTimes(2);
|
|
31
|
-
});
|
|
32
|
-
});
|
package/src/function/throttle.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a throttled function that only invokes the provided function at most once
|
|
3
|
-
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
4
|
-
* within the wait time will not trigger the execution of the original function.
|
|
5
|
-
*
|
|
6
|
-
* @param {() => void} func - The function to throttle.
|
|
7
|
-
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|
8
|
-
* @returns {() => void} A new throttled function.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* const throttledFunction = throttle(() => {
|
|
12
|
-
* console.log('Function executed');
|
|
13
|
-
* }, 1000);
|
|
14
|
-
*
|
|
15
|
-
* // Will log 'Function executed' immediately
|
|
16
|
-
* throttledFunction();
|
|
17
|
-
*
|
|
18
|
-
* // Will not log anything as it is within the throttle time
|
|
19
|
-
* throttledFunction();
|
|
20
|
-
*
|
|
21
|
-
* // After 1 second
|
|
22
|
-
* setTimeout(() => {
|
|
23
|
-
* throttledFunction(); // Will log 'Function executed'
|
|
24
|
-
* }, 1000);
|
|
25
|
-
*/
|
|
26
|
-
export function throttle(func: () => void, throttleMs: number) {
|
|
27
|
-
let lastCallTime: number | null;
|
|
28
|
-
|
|
29
|
-
const throttledFunction = function () {
|
|
30
|
-
const now = Date.now();
|
|
31
|
-
|
|
32
|
-
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
|
33
|
-
lastCallTime = now;
|
|
34
|
-
func();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return throttledFunction;
|
|
39
|
-
}
|
package/src/index.ts
DELETED
package/src/math/clamp.spec.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { clamp } from "./clamp";
|
|
3
|
-
|
|
4
|
-
describe("clamp function", () => {
|
|
5
|
-
it("works with max", () => {
|
|
6
|
-
expect(clamp(3, 5)).toBe(3);
|
|
7
|
-
expect(clamp(10, 6)).toBe(6);
|
|
8
|
-
expect(clamp(6, 10)).toBe(6);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("works with max and min", () => {
|
|
12
|
-
expect(clamp(3, 5, 10)).toBe(5);
|
|
13
|
-
expect(clamp(10, 6, 10)).toBe(10);
|
|
14
|
-
expect(clamp(6, 10, 10)).toBe(10);
|
|
15
|
-
expect(clamp(7, 5, 10)).toBe(7);
|
|
16
|
-
expect(clamp(100, 5, 6)).toBe(6);
|
|
17
|
-
});
|
|
18
|
-
});
|
package/src/math/clamp.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name clamp
|
|
3
|
-
* @description Checks if `value` is within the bounds, if not return the closest bound (bound1: min, bound2: max)
|
|
4
|
-
* function clamp(value: number, bound1: number, bound2?: number): number
|
|
5
|
-
* @example
|
|
6
|
-
* clamp(3, 1) // 3
|
|
7
|
-
* clamp(3, 1, 5) // 3
|
|
8
|
-
* clamp(3, 5) // 5
|
|
9
|
-
* clamp(7, 3, 5) // 5
|
|
10
|
-
*/
|
|
11
|
-
export function clamp(value: number, bound1: number, bound2?: number): number {
|
|
12
|
-
if (bound2 == null) {
|
|
13
|
-
return Math.min(value, bound1);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return Math.min(Math.max(value, bound1), bound2);
|
|
17
|
-
}
|
package/src/math/index.ts
DELETED
package/src/math/round.spec.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { round } from "./round";
|
|
3
|
-
|
|
4
|
-
describe("round function", () => {
|
|
5
|
-
it("rounds a number to zero decimal places by default", () => {
|
|
6
|
-
expect(round(1.5)).toBe(2);
|
|
7
|
-
expect(round(1.4)).toBe(1);
|
|
8
|
-
// JavaScript는 반올림 하는 부분이 정확히 음수 0.5라면 +∞방향, 즉, 정수로 반올림 한다.
|
|
9
|
-
// If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. (positive number)
|
|
10
|
-
expect(round(-1.5)).toBe(-1);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("rounds a number to a specified number of decimal places", () => {
|
|
14
|
-
expect(round(1.2345, 2)).toBe(1.23);
|
|
15
|
-
expect(round(1.2365, 2)).toBe(1.24);
|
|
16
|
-
expect(round(-1.2345, 2)).toBe(-1.23);
|
|
17
|
-
expect(round(-1.2365, 2)).toBe(-1.24);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("handles zero precision as default precision", () => {
|
|
21
|
-
expect(round(1.999)).toBe(2);
|
|
22
|
-
expect(round(-1.999)).toBe(-2);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("handles negative numbers properly", () => {
|
|
26
|
-
// JavaScript는 반올림 하는 부분이 정확히 음수 0.5라면 +∞방향, 즉, 정수로 반올림 한다.
|
|
27
|
-
// If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. (positive number)
|
|
28
|
-
expect(round(-1.2345, 3)).toBe(-1.234);
|
|
29
|
-
expect(round(-1.2344, 3)).toBe(-1.234);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("rounds correctly at high precision levels", () => {
|
|
33
|
-
expect(round(1.123456789, 5)).toBe(1.12346);
|
|
34
|
-
expect(round(-1.123456789, 5)).toBe(-1.12346);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("rounds correctly with edge cases", () => {
|
|
38
|
-
expect(round(1.25, 1)).toBe(1.3);
|
|
39
|
-
// JavaScript는 반올림 하는 부분이 정확히 음수 0.5라면 +∞방향, 즉, 정수로 반올림 한다.
|
|
40
|
-
// If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. (positive number)
|
|
41
|
-
expect(round(-1.25, 1)).toBe(-1.2);
|
|
42
|
-
// JavaScript는 일반적인 다른 언어들과 다르게 Round half to even 으로 동작하지 않는다.
|
|
43
|
-
// The round function in JavaScript does not work as 'Round half to Even'
|
|
44
|
-
expect(round(-1.35, 1)).toBe(-1.3);
|
|
45
|
-
});
|
|
46
|
-
it("works with zero", () => {
|
|
47
|
-
expect(round(0)).toBe(0);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("works with precision leading to no rounding", () => {
|
|
51
|
-
expect(round(8.88888, 5)).toBe(8.88888);
|
|
52
|
-
});
|
|
53
|
-
});
|
package/src/math/round.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name round
|
|
3
|
-
* @description Rounds given number to given precision
|
|
4
|
-
* ```typescript
|
|
5
|
-
* function round(value: number, precision: number = 0): number
|
|
6
|
-
* ```
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* round(1.2) === 1
|
|
10
|
-
* round(3.9) === 4
|
|
11
|
-
* round(8.5) === 9
|
|
12
|
-
*/
|
|
13
|
-
export function round(value: number, precision: number = 0): number {
|
|
14
|
-
const multiplier = Math.pow(10, precision);
|
|
15
|
-
return Math.round(value * multiplier) / multiplier;
|
|
16
|
-
}
|
package/src/math/sum.spec.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { sum } from "./sum";
|
|
3
|
-
|
|
4
|
-
describe("sum function", () => {
|
|
5
|
-
it("calculates the sum of a given numbers", () => {
|
|
6
|
-
const result = sum(1, 2, 3, 4);
|
|
7
|
-
expect(result).toBe(10);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("calculates the sum of an array of numbers", () => {
|
|
11
|
-
const result = sum([1, 2, 3, 4]);
|
|
12
|
-
expect(result).toBe(10);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("calculates the sum of an multiple array of numbers", () => {
|
|
16
|
-
const result = sum([1, 2], [3, 4]);
|
|
17
|
-
expect(result).toBe(10);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("returns 0 for an empty array", () => {
|
|
21
|
-
const result = sum();
|
|
22
|
-
expect(result).toBe(0);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("handles arrays with negative numbers", () => {
|
|
26
|
-
const result = sum([-1, -2, -3, 4]);
|
|
27
|
-
expect(result).toBe(-2);
|
|
28
|
-
});
|
|
29
|
-
});
|
package/src/math/sum.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name sum
|
|
3
|
-
* @description Return the sum of given values.
|
|
4
|
-
* ```typescript
|
|
5
|
-
* function sum(...nums: number[] | number[][]): number;
|
|
6
|
-
* ```
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* sum(1, 2, 3) === 6
|
|
10
|
-
* sum(...[1, 2, 3]) === 6
|
|
11
|
-
* sum([1, 2, 3]) === 6
|
|
12
|
-
*/
|
|
13
|
-
export function sum(...nums: number[] | number[][]) {
|
|
14
|
-
return nums.flat().reduce((acc, curr) => {
|
|
15
|
-
return acc + curr;
|
|
16
|
-
}, 0);
|
|
17
|
-
}
|
package/src/object/index.ts
DELETED
package/src/object/omit.spec.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { omit } from "./omit";
|
|
3
|
-
|
|
4
|
-
describe("omit", () => {
|
|
5
|
-
it("should omit properties from an object", () => {
|
|
6
|
-
const object = { foo: 1, bar: 2, baz: 3 };
|
|
7
|
-
|
|
8
|
-
expect(omit(object, ['foo', 'bar'])).toEqual({ baz: 3 });
|
|
9
|
-
});
|
|
10
|
-
});
|
package/src/object/omit.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name omit
|
|
3
|
-
* Omit properties from an object.
|
|
4
|
-
* @param obj The object to omit properties from
|
|
5
|
-
* @param keys The keys to omit from an object
|
|
6
|
-
*/
|
|
7
|
-
export function omit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
|
|
8
|
-
const result = { ...obj };
|
|
9
|
-
|
|
10
|
-
for (const key of keys) {
|
|
11
|
-
delete result[key];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return result as Omit<T, K>
|
|
15
|
-
}
|
package/src/object/omitBy.ts
DELETED
package/src/object/pick.spec.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { pick } from "./pick";
|
|
3
|
-
|
|
4
|
-
describe("pick", () => {
|
|
5
|
-
it("should pick properties from an object", () => {
|
|
6
|
-
const object = { foo: 1, bar: 2, baz: 3 };
|
|
7
|
-
|
|
8
|
-
expect(pick(object, ['foo', 'bar'])).toEqual({ foo: 1, bar: 2 });
|
|
9
|
-
});
|
|
10
|
-
});
|
package/src/object/pick.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name pick
|
|
3
|
-
* Pick properties from an given object.
|
|
4
|
-
* @param obj The object to pick properties from
|
|
5
|
-
* @param keys The keys to pick from an object
|
|
6
|
-
*/
|
|
7
|
-
export function pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
|
|
8
|
-
const result = {} as Pick<T, K>;
|
|
9
|
-
|
|
10
|
-
for (const key of keys) {
|
|
11
|
-
result[key] = obj[key];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return result;
|
|
15
|
-
}
|
package/src/object/pickBy.ts
DELETED
package/src/predicate/index.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
2
|
-
import { isNil } from "./isNil"
|
|
3
|
-
|
|
4
|
-
describe('isNil', () => {
|
|
5
|
-
it('returns true if the value is null or undefined', () => {
|
|
6
|
-
expect(isNil(null)).toBe(true);
|
|
7
|
-
expect(isNil(undefined)).toBe(true);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('returns false if the value is not null nor undefined', () => {
|
|
11
|
-
expect(isNil('')).toBe(false);
|
|
12
|
-
expect(isNil(123)).toBe(false);
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('can be used with TypeScript as a type predicate', () => {
|
|
16
|
-
const arr = [1, 2, null, 4, undefined];
|
|
17
|
-
|
|
18
|
-
const result = arr.filter(isNil);
|
|
19
|
-
|
|
20
|
-
// Here the type of result should be `null | undefined`.
|
|
21
|
-
expect(result).toStrictEqual([null, undefined]);
|
|
22
|
-
})
|
|
23
|
-
})
|
package/src/predicate/isNil.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
2
|
-
import { isNotNil } from "./isNotNil"
|
|
3
|
-
|
|
4
|
-
describe('isNotNil', () => {
|
|
5
|
-
it('returns false if the value is null or undefined', () => {
|
|
6
|
-
expect(isNotNil(null)).toBe(false);
|
|
7
|
-
expect(isNotNil(undefined)).toBe(false);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('returns true if the value is not null nor undefined', () => {
|
|
11
|
-
expect(isNotNil('')).toBe(true);
|
|
12
|
-
expect(isNotNil(123)).toBe(true);
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('can be used with TypeScript as a type predicate', () => {
|
|
16
|
-
const arr1 = [1, 2, null, 4, undefined];
|
|
17
|
-
const arr2 = [1, 2, null];
|
|
18
|
-
const arr3 = [1, 2, 4, undefined];
|
|
19
|
-
|
|
20
|
-
const result1 = arr1.filter(isNotNil);
|
|
21
|
-
const result2 = arr2.filter(isNotNil);
|
|
22
|
-
const result3 = arr3.filter(isNotNil);
|
|
23
|
-
|
|
24
|
-
// Here the type of result should be `number[]`.
|
|
25
|
-
expect(result1).toStrictEqual([1, 2, 4]);
|
|
26
|
-
expect(result2).toStrictEqual([1, 2]);
|
|
27
|
-
expect(result3).toStrictEqual([1, 2, 4]);
|
|
28
|
-
})
|
|
29
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name isNotNil
|
|
3
|
-
* Checks if the given value is not null nor undefined.
|
|
4
|
-
* The main use of this function is to used with TypeScript as a type predicate.
|
|
5
|
-
* @param x The value to test if it is not null nor undefined
|
|
6
|
-
* @example
|
|
7
|
-
* // Here the type of `arr` is (number | undefined)[]
|
|
8
|
-
* const arr = [1, undefined, 3];
|
|
9
|
-
* // Here the type of `result` is number[]
|
|
10
|
-
* const result = arr.filter(isNotNil);
|
|
11
|
-
*/
|
|
12
|
-
export function isNotNil<T>(x: T | null | undefined): x is T {
|
|
13
|
-
return x != null && x != undefined;
|
|
14
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
2
|
-
import { isNull } from "./isNull"
|
|
3
|
-
|
|
4
|
-
describe('isNull', () => {
|
|
5
|
-
it('returns true if the value is null', () => {
|
|
6
|
-
expect(isNull(null)).toBe(true);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('returns false if the value is not null', () => {
|
|
10
|
-
expect(isNull(undefined)).toBe(false);
|
|
11
|
-
expect(isNull('')).toBe(false);
|
|
12
|
-
expect(isNull(123)).toBe(false);
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('can be used with TypeScript as a type predicate', () => {
|
|
16
|
-
const arr = [1, 2, null, 4, undefined];
|
|
17
|
-
|
|
18
|
-
const result = arr.filter(isNull);
|
|
19
|
-
|
|
20
|
-
// Here the type of result should be `null[]`.
|
|
21
|
-
expect(result).toStrictEqual([null]);
|
|
22
|
-
})
|
|
23
|
-
})
|
package/src/predicate/isNull.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
2
|
-
import { isUndefined } from "./isUndefined"
|
|
3
|
-
|
|
4
|
-
describe('isUndefined', () => {
|
|
5
|
-
it('returns true if the value is undefined', () => {
|
|
6
|
-
expect(isUndefined(undefined)).toBe(true);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('returns false if the value is not undefined', () => {
|
|
10
|
-
expect(isUndefined(null)).toBe(false);
|
|
11
|
-
expect(isUndefined('')).toBe(false);
|
|
12
|
-
expect(isUndefined(123)).toBe(false);
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('can be used with TypeScript as a type predicate', () => {
|
|
16
|
-
const arr = [1, 2, null, 4, undefined];
|
|
17
|
-
|
|
18
|
-
const result = arr.filter(isUndefined);
|
|
19
|
-
|
|
20
|
-
// Here the type of result should be `null[]`.
|
|
21
|
-
expect(result).toStrictEqual([undefined]);
|
|
22
|
-
})
|
|
23
|
-
})
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { performance } from 'node:perf_hooks';
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { delay } from './delay';
|
|
4
|
-
|
|
5
|
-
describe('delay', () => {
|
|
6
|
-
it('pauses an async function for a given time', async () => {
|
|
7
|
-
const start = performance.now()
|
|
8
|
-
await delay(100);
|
|
9
|
-
const end = performance.now()
|
|
10
|
-
|
|
11
|
-
expect(end - start).greaterThanOrEqual(100)
|
|
12
|
-
});
|
|
13
|
-
})
|
package/src/promise/delay.ts
DELETED
package/src/promise/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { delay } from './delay';
|
package/tsconfig.json
DELETED
package/vitest.config.mts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'vitest/config';
|
|
2
|
-
import packageJson from './package.json';
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
test: {
|
|
6
|
-
name: packageJson.name,
|
|
7
|
-
dir: './src',
|
|
8
|
-
coverage: {
|
|
9
|
-
provider: 'istanbul',
|
|
10
|
-
include: ['src/**/*'],
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
});
|