- From npm:
npm install @alkemist/smart-tools
- From yarn:
yarn add @alkemist/smart-tools
- From npm:
npm test
- From yarn:
yarn test
This class is a mix between arrays and maps It extends maps by making it easier to retrieve keys and values, and to browse like an array with map/each/find/filter.
This class is an array that works like a json path, allowing you to quickly retrieve its string version.
Ex : The ar ray ['object', 'array', 0, 'otherProperty']
will be equivalent to object.array[0].otherProperty
type NotEvaluable = null | undefined;
type ValueKey = string | number
type ValuePrimitive = ValueKey | boolean | symbol
type ValueFunction = Function;
type ValueDate = Date;
type ValueArray = AnyValue[];
type GenericValueArray<T> = T[];
type ValueRecord = {
[key: ValueKey]: AnyValue;
};
type GenericValueRecord<T> = {
[key: ValueKey]: T;
};
type ValueTree = ValueArray | ValueRecord;
type GenericValueTree<T> = GenericValueArray<T> | GenericValueRecord<T>;
type Evaluable = ValuePrimitive | ValueFunction | ValueTree | ValueDate | object;
type AnyValue = Evaluable | NotEvaluable;
type ValueRecordKeys<T, V> = { [K in keyof T]: V };
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 ValueRecordKeys<T, ValueKey>,
K extends keyof T
>(array: T[], selector: K): Record<T[K], T>
static enumToArray<V extends ValueKey, K extends ValueKey>(enumValue: Record<V, K>): KeyValue<K, V>[]
}
abstract class DateHelper {
static seconds(date: Date = new Date()): number
static dayStart(date: Date = new Date()): Date
static dayEnd(date: Date = new Date()): Date
static calcHoursAfter(date: string | Date): number
}
abstract class MathHelper {
static parseInt(value: ValueKey): number
static clamp(value: number, min: number, max: number): number
static round(value: number, decimal: number = 1): number
static floor(value: number, decimal: number = 1): number
static sum(numbers: number[]): number
static distance(lat1: number, lon1: number, lat2: number, lon2: number): number
}
abstract class ObjectHelper {
static hasStringIndex<T = AnyValue>(value: AnyValue): value is GenericValueRecord<T>
static objectToRecord<V>(object: any): Record<string, V>
}
abstract class StringHelper {
static stringify(value: AnyValue): string
static slugify(value: string): string
}
abstract class TreeHelper {
static keys<
D extends AnyValue,
T extends GenericValueTree<D>,
R extends ValueKey = T extends GenericValueArray<D> ? string : number
>(tree: T): R[]
static getIn(object: unknown, path: string[]): unknown
static setIn(object: unknown, path: string[], value: unknown): unknown
static hasProperty(value: AnyValue, path: ValueKey[] | ValueKey): boolean
static hasOwn(tree: ValueTree, property: ValueKey): boolean
static getCycles(object: ValueRecord): string[]
}
interface LogInterface {
title: string;
data?: any | any[];
}
abstract class ConsoleHelper {
static group(parent: LogInterface | string | any, children: (LogInterface | string | any)[], colors?: string[]): void;
static log(title: string, style: string, isGroup?: boolean, ...data: any[]): void;
}
abstract class TypeHelper {
static getPrototypeOf(value: unknown): {}
static isEvaluable(value: AnyValue): value is Evaluable
static isBoolean(value: AnyValue): value is boolean
static isKey(value: AnyValue): value is ValueKey
static isNumber(value: AnyValue): value is number
static isSymbol(value: AnyValue): value is symbol
static isString(value: AnyValue): value is string
static isArray<T = AnyValue>(value: AnyValue): value is GenericValueArray<T>
static isRecord<T = AnyValue>(value: AnyValue): value is GenericValueRecord<T>
static isObject<T = AnyValue>(value: AnyValue): value is GenericValueRecord<T>
static isTree<T = AnyValue>(value: AnyValue): value is GenericValueTree<T>
static isFunction(value: AnyValue): value is ValueFunction
static isDate(value: AnyValue): value is ValueDate
static isPrimitive(value: AnyValue): value is ValuePrimitive
static isEqual(sideValue: AnyValue, otherSideValue: AnyValue): boolean
static serialize(value: AnyValue): string
static deepClone<T extends AnyValue>(source: T): T
}
interface KeyValue<K, V> {
key: K;
value: V;
}