Skip to content

Commit 52a0787

Browse files
committed
feat: add unit test
1 parent f1d71ef commit 52a0787

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+630
-427
lines changed

scripts/common/clone.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/common/clone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ObjectKey } from "@scripts/object/types/objectKey";
2-
import { getTypedEntries } from "../object/getTypedEntries";
2+
import { getEntries } from "../object/getEntries";
33
import { type SimplifyTypeForce } from "@scripts/common/types/simplifyTypeForce";
44

55
export function clone<
@@ -12,7 +12,7 @@ export function clone<
1212
} else if (unknownValue instanceof Array) {
1313
return <never>unknownValue.map(clone);
1414
} else {
15-
return <never>getTypedEntries(unknownValue)
15+
return <never>getEntries(unknownValue)
1616
.reduce<Record<ObjectKey, unknown>>(
1717
(pv, [key, value]) => {
1818
pv[key] = clone(value);

scripts/common/escapeRegExp.test.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/common/externalPromise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ export function createExternalPromise<
33
>() {
44
type PossibleValue = Awaited<GenericPromiseValue> | GenericPromiseValue | Promise<GenericPromiseValue>;
55

6-
let resolve = (_value: PossibleValue) => {};
7-
let reject = (_value: unknown) => {};
6+
let resolve = undefined as unknown as (_value: PossibleValue) => void;
7+
let reject = undefined as unknown as (_value: unknown) => void;
88
const promise = new Promise<Awaited<GenericPromiseValue>>((res, rej) => {
99
resolve = res as never;
1010
reject = rej;

scripts/common/interpolation.test.ts

Lines changed: 0 additions & 115 deletions
This file was deleted.

scripts/common/simpleClone.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/common/simpleClone.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getTypedEntries } from "../object/getTypedEntries";
1+
import { getEntries } from "../object/getEntries";
22

33
export function simpleClone<
4-
T extends unknown = unknown,
5-
>(unknownValue: T): T {
4+
GenericObject extends unknown = unknown,
5+
>(unknownValue: GenericObject): GenericObject {
66
if (!unknownValue) {
77
return unknownValue;
88
} else if (typeof unknownValue !== "object") {
@@ -11,15 +11,15 @@ export function simpleClone<
1111
unknownValue.constructor?.name === "Object"
1212
|| unknownValue.constructor === undefined
1313
) {
14-
return getTypedEntries(unknownValue).reduce(
14+
return getEntries(unknownValue).reduce(
1515
(pv, [key, value]) => {
1616
pv[key] = simpleClone(value);
1717
return pv;
1818
},
19-
{} as T,
19+
{} as GenericObject,
2020
);
2121
} else if (unknownValue instanceof Array && unknownValue.constructor.name === "Array") {
22-
return unknownValue.map(simpleClone) as T;
22+
return unknownValue.map(simpleClone) as GenericObject;
2323
} else {
2424
return unknownValue;
2525
}

scripts/common/sleep.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

scripts/common/stringToBytes.test.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

scripts/common/stringToBytes.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class InvalidBytesInStringError extends Error {
1313
}
1414
}
1515

16-
const parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;
16+
const parseRegExp = /(?<rawValue>[0-9.]+)(?<unit>b|kb|mb|gb|tb|pd)/;
1717

1818
const unitMapper = {
1919
b: 1,
@@ -33,17 +33,12 @@ export function stringToBytes(bytesInString: BytesInString | number) {
3333

3434
const regExpResults = parseRegExp.exec(bytesInString);
3535

36-
const floatValue = regExpResults
37-
? parseFloat(regExpResults[1]!)
38-
: parseInt(bytesInString, 10);
36+
const { rawValue, unit } = regExpResults?.groups ?? {};
37+
const value = parseFloat(rawValue ?? "");
3938

40-
const unit = regExpResults
41-
? regExpResults[4]!.toLowerCase()
42-
: "b";
43-
44-
if (!hasKey(unitMapper, unit) || isNaN(floatValue)) {
39+
if (isNaN(value) || !unit || !hasKey(unitMapper, unit)) {
4540
throw new InvalidBytesInStringError(bytesInString);
4641
}
4742

48-
return Math.floor(unitMapper[unit] * floatValue);
43+
return Math.floor(unitMapper[unit] * value);
4944
}

0 commit comments

Comments
 (0)