Skip to content

Commit

Permalink
refactor: cleanup helpers (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmoravek authored Mar 9, 2024
1 parent 8f83b71 commit 8676d84
Showing 1 changed file with 65 additions and 130 deletions.
195 changes: 65 additions & 130 deletions packages/oruga/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* Generates a random string
*/
export function uuid(): string {
return Math.random().toString(36).substring(2, 15);
}
export const uuid = (): string => Math.random().toString(36).substring(2, 15);

/**
* +/- function to native math sign
Expand All @@ -14,25 +12,13 @@ function signPoly(value: number): number {
}
export const sign = Math.sign || signPoly;

/**
* Checks if the flag is set
* @param val
* @param flag
* @returns {boolean}
*/
export function hasFlag(val: number, flag: number): boolean {
return (val & flag) === flag;
}

/**
* Native modulo bug with negative numbers
* @param n
* @param mod
* @returns {number}
*/
export function mod(n: number, mod: number): number {
return ((n % mod) + mod) % mod;
}
export const mod = (n: number, mod: number): number => ((n % mod) + mod) % mod;

/**
* Asserts a value is beetween min and max
Expand All @@ -45,52 +31,25 @@ export function bound(val: number, min: number, max: number): number {
return Math.max(min, Math.min(max, val));
}

/**
* Get a value of an object property/path even if it's nested
*/
export function getValueByPath<T = any>(
obj: Record<string, any>,
path: string,
defaultValue?: T,
): T {
const value = path
.split(".")
.reduce(
(o, i) => (typeof o !== "undefined" ? o[i] : undefined),
obj,
) as T;
return typeof value !== "undefined" ? value : defaultValue;
}
export const isObject = <T>(obj: T): boolean =>
obj && typeof obj === "object" && !Array.isArray(obj);

/**
* Set a value of an object property/path even if it's nested
*/
export function setValueByPath<T>(
obj: Record<string, any>,
path: string,
value: T,
): void {
const p = path.split(".");
if (p.length === 1) {
obj[path] = value;
return;
}
const field = p[0];
if (typeof obj[field] === "undefined") obj[field] = {};
return setValueByPath(obj[field], p.slice(1).join("."), value);
}
export const isDefined = <T>(d: T): boolean => d !== null && d !== undefined;

export function getStyleValue(value: any): any {
if (typeof value === "object") {
for (const key in value) {
if (value[key]) return key;
}
return "";
}
return value;
}
export const blankIfUndefined = (value: string): string =>
typeof value !== "undefined" && value !== null ? value : "";

export const defaultIfUndefined = <T>(
value: T | undefined,
defaultValue: T,
): T => (typeof value !== "undefined" && value !== null ? value : defaultValue);

/** Extension of indexOf method by equality function if specified */
export const toCssDimension = (width: string | number): string | number =>
!isDefined(width) ? null : isNaN(width as number) ? width : width + "px";

/**
* Extension of indexOf method by equality function if specified
*/
export function indexOf<T>(
array: T[],
obj: T,
Expand All @@ -101,28 +60,9 @@ export function indexOf<T>(
return array.findIndex((value, index, arr) => fn(value, arr));
}

export const isObject = <T>(obj: T): boolean =>
obj && typeof obj === "object" && !Array.isArray(obj);

export const isDefined = <T>(d: T): boolean => d !== null && d !== undefined;

export function blankIfUndefined(value: string): string {
return typeof value !== "undefined" && value !== null ? value : "";
}

export function defaultIfUndefined<T>(
value: T | undefined,
defaultValue: T,
): T {
return typeof value !== "undefined" && value !== null
? value
: defaultValue;
}

export function clone<T extends object>(obj: T): T {
return Object.assign({}, obj);
}

/**
* Deeply check if two values are equal
*/
export function isEqual(valueA: unknown, valueB: unknown): boolean {
// Check if only one value is empty.
if ((!valueA && !!valueB) || (!!valueA && !valueB)) return false;
Expand Down Expand Up @@ -167,14 +107,18 @@ export function isEqual(valueA: unknown, valueB: unknown): boolean {
return false;
}

/**
* Clone an obj with Object.assign
*/
export function clone<T extends object>(obj: T): T {
return Object.assign({}, obj);
}

/**
* Merge function to replace Object.assign with deep merging possibility
*/
export function merge(target: any, source: any, deep = false): any {
if (!isObject(target) || !isObject(source)) {
return source;
}

if (!isObject(target) || !isObject(source)) return source;
if (!deep) return Object.assign(target, source);
else return mergeDeep(target, source);
}
Expand All @@ -186,11 +130,7 @@ export function merge(target: any, source: any, deep = false): any {
* @author inspired by [jhildenbiddle](https://stackoverflow.com/a/48218209).
*/
export function mergeDeep(target: any, source: any): any {
const isObject = (obj: any): any => obj && typeof obj === "object";

if (!isObject(target) || !isObject(source)) {
return source;
}
if (!isObject(target) || !isObject(source)) return source;

Object.getOwnPropertyNames(source).forEach((key) => {
const targetValue = target[key];
Expand All @@ -211,6 +151,41 @@ export function mergeDeep(target: any, source: any): any {
return target;
}

/**
* Get a value of an object property/path even if it's nested
*/
export function getValueByPath<T = any>(
obj: Record<string, any>,
path: string,
defaultValue?: T,
): T {
const value = path
.split(".")
.reduce(
(o, i) => (typeof o !== "undefined" ? o[i] : undefined),
obj,
) as T;
return typeof value !== "undefined" ? value : defaultValue;
}

/**
* Set a value of an object property/path even if it's nested
*/
export function setValueByPath<T = any>(
obj: Record<string, any>,
path: string,
value: T,
): void {
const p = path.split(".");
if (p.length === 1) {
obj[path] = value;
return;
}
const field = p[0];
if (typeof obj[field] === "undefined") obj[field] = {};
return setValueByPath(obj[field], p.slice(1).join("."), value);
}

export function removeElement(el: Element): void {
if (typeof el.remove !== "undefined") {
el.remove();
Expand All @@ -231,17 +206,6 @@ export function createAbsoluteElement(el: Element): HTMLDivElement {
return root;
}

export function createNewEvent(eventName: string): Event {
let event: any;
if (typeof Event === "function") {
event = new Event(eventName);
} else {
event = document.createEvent("Event");
event.initEvent(eventName, true, true);
}
return event;
}

/**
* Escape regex characters
* http://stackoverflow.com/a/6969486
Expand All @@ -252,35 +216,6 @@ export function escapeRegExpChars(value: string): string {
return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

export function toCssDimension(width: string | number): string | number {
return width === undefined
? null
: isNaN(width as number)
? width
: width + "px";
}

/**
* @deprecated use useDebounce composable instead
*/
export function debounce<A extends Array<unknown>>(
func: (...args: A[]) => void,
wait: number,
immediate?: boolean,
): (...args: A[]) => void {
let timeout: NodeJS.Timeout;
return (...args: A[]) => {
const later = () => {
timeout = null;
if (!immediate) func.apply(this, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(this, args);
};
}

/**
* Remove accents/diacritics in a string in JavaScript
* https://stackoverflow.com/a/37511463
Expand Down

0 comments on commit 8676d84

Please sign in to comment.