-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
import { KeyValue, ValueKey } from '../models/index.js'; | ||
export declare abstract class ArrayHelper { | ||
static sortBy<T>(array: T[], field: string): T[]; | ||
static unique<T>(array: T[]): T[]; | ||
static recordToList<T extends string, U>(record: Record<T, U>): KeyValue<string, U>[]; | ||
static listToRecord<T extends { | ||
[K in keyof T]: string | number; | ||
}, K extends keyof T>(array: T[], selector: K): Record<T[K], T>; | ||
static enumToArray<V extends ValueKey = ValueKey, K extends ValueKey = ValueKey>(enumValue: Record<V, K>): KeyValue<K, V>[]; | ||
} | ||
//# sourceMappingURL=array.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export declare abstract class DateHelper { | ||
static seconds(date?: Date): number; | ||
static dayStart(date?: Date): Date; | ||
static dayEnd(date?: Date): Date; | ||
static calcHoursAfter(date: string | Date): number; | ||
} | ||
//# sourceMappingURL=date.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export declare abstract class DocumentHelper { | ||
static getTraverseChildren(elem: Element): Element[]; | ||
} | ||
//# sourceMappingURL=document.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export declare abstract class EnumHelper { | ||
static enumToRegex(enumValue: any): RegExp; | ||
} | ||
//# sourceMappingURL=enum.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { ValueKey } from '../models/index.js'; | ||
export declare abstract class MathHelper { | ||
static parseInt(value: ValueKey): number; | ||
static clamp(value: number, min: number, max: number): number; | ||
static round(value: number, decimal?: number): number; | ||
static floor(value: number, decimal?: number): number; | ||
static sum(numbers: number[]): number; | ||
static distance(lat1: number, lon1: number, lat2: number, lon2: number, unit?: string): number; | ||
} | ||
//# sourceMappingURL=math.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { AnyValue, GenericValueRecord } from '../models/index.js'; | ||
export declare abstract class ObjectHelper { | ||
static hasStringIndex<T = AnyValue>(value: unknown): value is GenericValueRecord<T>; | ||
static objectToRecord<V>(object: any): Record<string, V>; | ||
} | ||
//# sourceMappingURL=object.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export declare abstract class StringHelper { | ||
static stringify(value: unknown): string; | ||
static slugify: (str?: string) => string; | ||
} | ||
//# sourceMappingURL=string.helper.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './console.const.js'; | ||
export * from './console.interface.js'; | ||
export * from './key-value.interface.js'; | ||
export * from './value.const.js'; | ||
export * from './value.type.js'; | ||
//# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export declare interface KeyValue<K, V> { | ||
key: K; | ||
value: V; | ||
} | ||
//# sourceMappingURL=key-value.interface.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
import { StringHelper } from './string.helper.js'; | ||
import { KeyValue, ValueKey } from '../models/index.js'; | ||
|
||
export abstract class ArrayHelper { | ||
static sortBy<T>(array: T[], field: string): T[] { | ||
return array.sort((a: any, b: any) => { | ||
const aValue = StringHelper.slugify(a[field]!); | ||
const bValue = StringHelper.slugify(b[field]!); | ||
return (aValue > bValue) ? 1 : ((bValue > aValue) ? -1 : 0); | ||
}); | ||
} | ||
|
||
static unique<T>(array: T[]) { | ||
return array.filter((value, index, array) => array.indexOf(value) === index) | ||
} | ||
|
||
static recordToList<T extends string, U>(record: Record<T, U>): KeyValue<string, U>[] { | ||
return Object.entries<U>(record).map(([ t, u ]: [ string, U ]) => ({ | ||
key: t, | ||
value: u as U | ||
})); | ||
} | ||
|
||
static listToRecord< | ||
T extends { [K in keyof T]: string | number }, | ||
K extends keyof T | ||
>(array: T[], selector: K): Record<T[K], T> { | ||
return array.reduce( | ||
(acc, item) => { | ||
acc[item[selector]] = item; | ||
return acc; | ||
}, {} as Record<T[K], T> | ||
) | ||
} | ||
|
||
static enumToArray<V extends ValueKey = ValueKey, K extends ValueKey = ValueKey>(enumValue: Record<V, K>): KeyValue<K, V>[] { | ||
// Enum is a inversed record | ||
const values = Object.keys(enumValue) as V[]; | ||
|
||
return values.map((value: V) => { | ||
const key = enumValue[value] as K; | ||
return { key, value } | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
export abstract class DateHelper { | ||
static seconds(date: Date = new Date()) { | ||
return Math.round(date.getTime() / 1000); | ||
} | ||
|
||
static dayStart(date: Date = new Date()) { | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
date.setSeconds(0); | ||
date.setMilliseconds(0); | ||
return date; | ||
} | ||
|
||
static dayEnd(date: Date = new Date()) { | ||
date.setHours(23); | ||
date.setMinutes(59); | ||
date.setSeconds(59); | ||
date.setMilliseconds(59); | ||
return date; | ||
} | ||
|
||
static calcHoursAfter(date: string | Date): number { | ||
const dateTime = (new Date()).getTime(); | ||
const lastUpdatedTime = new Date(date).getTime(); | ||
return Math.abs(Math.round((dateTime - lastUpdatedTime) / (1000 * 60 * 60))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export abstract class DocumentHelper { | ||
static getTraverseChildren(elem: Element): Element[] { | ||
const children = []; | ||
const elementsToCheck: Element[] = [ elem ]; | ||
|
||
while (elementsToCheck.length > 0) { | ||
const currentElement = elementsToCheck.pop() as HTMLElement; | ||
children.push(currentElement); | ||
|
||
for (let i = 0; i < currentElement.children.length; i++) { | ||
elementsToCheck.push(currentElement.children[i]); | ||
} | ||
} | ||
|
||
// console.log("traverseChildren", children); | ||
return children; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export abstract class EnumHelper { | ||
static enumToRegex(enumValue: any): RegExp { | ||
const keys = Object.keys(enumValue); | ||
return new RegExp(keys.join('|')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
import { TypeHelper } from './type.helper.js'; | ||
|
||
export abstract class StringHelper { | ||
static stringify(value: unknown): string { | ||
if (TypeHelper.isEvaluable(value)) { | ||
if (typeof value.toString !== undefined | ||
|| TypeHelper.isSymbol(value) | ||
) { | ||
return value.toString(); | ||
} | ||
return value + ""; | ||
} | ||
return value === null ? "null" : "undefined"; | ||
static stringify(value: unknown): string { | ||
if (TypeHelper.isEvaluable(value)) { | ||
if (typeof value.toString !== undefined | ||
|| TypeHelper.isSymbol(value) | ||
) { | ||
return value.toString(); | ||
} | ||
return value + ""; | ||
} | ||
return value === null ? "null" : "undefined"; | ||
} | ||
|
||
static slugify = function (str?: string) { | ||
if (!str) { | ||
return ''; | ||
} | ||
|
||
str = str.trim().replace(/^\s+|\s+$/g, ''); | ||
|
||
// Make the string lowercase | ||
str = str.toLowerCase(); | ||
|
||
// Remove accents, swap ñ for n, etc | ||
const from = 'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;'; | ||
const to = 'AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------'; | ||
for (let i = 0, l = from.length; i < l; i++) { | ||
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); | ||
} | ||
|
||
// Remove invalid chars | ||
str = str.replace(/[^a-z0-9 -]/g, '') | ||
// Collapse whitespace and replace by - | ||
.replace(/\s+/g, '-') | ||
// Collapse dashes | ||
.replace(/-+/g, '-'); | ||
|
||
return str; | ||
}; | ||
|
||
} |