Skip to content

Commit

Permalink
feat(utils): isNumber helper and added strict mode to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Dec 12, 2022
1 parent 9cb3bfe commit c0b6bf8
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export function isFunction(value: unknown): value is Function {
return typeof value === "function";
}

export function isNumber(value: unknown): value is number {
return typeof value === "number";
}

export function isEmpty(value: any): boolean {
if (isArray(value)) {
return value.length === 0;
Expand Down Expand Up @@ -112,8 +116,24 @@ export function deepClone<T>(value: T): T {
}
}

export function merge<P = any, T = any>(defaultOptions: P, options: T): P & T {
return Object.assign({}, defaultOptions, options);
export function merge<P = any, T = any>(
defaultOptions: P,
options: T,
strict: boolean = false
): P & T {
if (strict) {
const filteredOptions = keys(options).reduce((acc, key) => {
if (options[key]) {
acc[key] = options[key];
}

return acc;
}, {} as T);

return Object.assign({}, defaultOptions, filteredOptions);
} else {
return Object.assign({}, defaultOptions, options);
}
}

export function deepMerge<T extends IObject[]>(...objects: T): UnionToIntersection<T[any]> {
Expand Down Expand Up @@ -217,7 +237,7 @@ export function argumentIsNotProvided(value: unknown): boolean {
return value === undefined || value === null;
}

export function combinedStringss(delimiter = "", ...strings: string[]): string {
export function combineStrings(delimiter = "", ...strings: string[]): string {
return strings.filter((string) => !isEmpty(string)).join(delimiter);
}

Expand Down Expand Up @@ -249,12 +269,12 @@ export function safeJSONParse(value: string, returnValueOnError = false): any {
}
}

export function createEmptyScheme<T>(keys: string[]): T {
export function createEmptyScheme<T>(keys: string[], value = null): T {
return keys.reduce((acc, key) => {
const keys = key.split(".");

if (keys.length === 1) {
acc[key] = null;
acc[key] = value;
} else {
const [object, ...nestedKeys] = keys;

Expand Down

0 comments on commit c0b6bf8

Please sign in to comment.