Skip to content

Commit

Permalink
Working unit tests for src/utils/object.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Robin committed Jul 7, 2024
1 parent b32be7f commit ed3bf3c
Show file tree
Hide file tree
Showing 3 changed files with 769 additions and 86 deletions.
37 changes: 24 additions & 13 deletions src/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,16 @@ export function values<T = unknown>(obj: unknown): T[] {
}

export function once<T = unknown>(
this: any,
func: (...args: any[]) => T,
): (...args: any[]) => T {
let called = false;
let value: T;

return function (...args: any[]): T {
return (...args: any[]): T => {
if (called === false) {
called = true;
value = func.apply(args);
value = func.apply(this, args);
}

return value;
Expand All @@ -228,26 +229,30 @@ export function flatten<T = unknown>(arrays: (T | T[])[]): T[] {
return out;
}

export function get<T = unknown>(obj: unknown, path: string[]): T | undefined {
export function get<T = unknown>(obj: unknown, path: Key[]): T | undefined {
for (let i = 0, l = path.length; i < l; ++i) {
const key = path[i];

if (!isObject(obj)) {
return undefined;
}

const rec = obj as GenericRecord<T>;
obj = rec[key];
if (Array.isArray(obj) && !isNaN(Number(key))) {
obj = obj[Number(key)];
} else {
const rec = obj as GenericRecord<T>;
obj = rec[key];
}
}

return obj as T;
}

export function set(
obj: GenericRecord,
path: (string | number)[],
obj: GenericRecord | unknown[],
path: Key[],
value: unknown,
): GenericRecord {
): GenericRecord | unknown[] {
const inputObj = obj;
let currentValue: GenericRecord | unknown[] = obj;

Expand All @@ -258,8 +263,10 @@ export function set(
return inputObj;
}

let child: GenericRecord | unknown[] =
(currentValue as GenericRecord)[key] as GenericRecord | unknown[];
let child: GenericRecord | unknown =
Array.isArray(currentValue) && !isNaN(Number(key))
? currentValue[Number(key)]
: (currentValue as GenericRecord)[key];

if (!isObject(child)) {
const nextKey = path[i + 1];
Expand All @@ -273,15 +280,15 @@ export function set(
(currentValue as GenericRecord)[key] = child;
}

currentValue = child;
currentValue = child as (GenericRecord | unknown[]);
}

if (path.length > 0 && isObject(currentValue)) {
const key = path[path.length - 1];

if (isSafeKey(key)) {
if (typeof key === "number") {
(currentValue as unknown[])[key] = value;
if (!isNaN(Number(key))) {
(currentValue as unknown[])[Number(key)] = value;
} else {
(currentValue as GenericRecord)[key] = value;
}
Expand Down Expand Up @@ -311,6 +318,10 @@ export function zipObject<T = unknown>(
export function chunk<T = unknown>(arr: T[], chunkSize: number): T[][] {
const out: T[][] = [];

if (chunkSize <= 0) {
return out;
}

for (let i = 0, l = arr.length; i < l; ++i) {
const item = arr[i];

Expand Down
72 changes: 0 additions & 72 deletions test.ts

This file was deleted.

Loading

0 comments on commit ed3bf3c

Please sign in to comment.