webstudio/ui/src/core/extensions/extensions.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-05-13 20:43:11 +03:00
import { Result } from "../helper/result";
2023-11-10 12:06:40 +03:00
import { ArrayExtensions } from "./array";
2024-08-13 13:03:28 +03:00
import { DateExtensions } from "./date";
2023-12-10 21:44:41 +03:00
import { MapExtensions } from "./map";
2024-02-16 14:16:35 +03:00
import { NumberExtensions } from "./number";
2023-11-10 21:43:57 +03:00
import { StringExtensions } from "./string";
2023-11-10 12:06:40 +03:00
2023-12-28 17:18:12 +03:00
export type CallBackVoidFunction = <T>(value: T) => void;
2024-02-27 18:48:35 +03:00
2023-12-28 17:18:12 +03:00
export type CallBackStringVoidFunction = (value: string) => void;
2024-02-05 15:28:09 +03:00
export type CallBackEventTarget = (value: EventTarget) => void;
2024-02-27 18:48:35 +03:00
export type OptionalProperties<T> = {
[P in keyof T]?: T[P];
};
2023-12-10 21:44:41 +03:00
2023-11-10 21:43:57 +03:00
declare global {
interface Array<T> {
// @strict: The parameter is determined whether the arrays must be exactly the same in content and order of this relationship or simply follow the same requirements.
equals(array: Array<T>, strict: boolean): boolean;
lastElement(): T | undefined;
isEmpty(): boolean;
2023-12-10 21:44:41 +03:00
isNotEmpty(): boolean;
hasIncludeElement(element: T): boolean;
2024-04-11 22:02:57 +03:00
repeat(quantity: number): Array<T>;
2024-05-29 15:38:36 +03:00
rFind<T>(predicate: (value: T, index: number, obj: never[]) => boolean, thisArg?: any): Result<void, T>;
maxLength(length: number): Array<T>;
2024-07-13 18:18:14 +03:00
add(element: T): Array<T>;
2024-08-13 13:03:28 +03:00
indexOfR(element: T): Result<void, Array<T>>;
2024-08-29 12:26:54 +03:00
replacePropIndex(property: Partial<T>, index: number): T[];
someR(predicate: (value: T) => boolean): Result<void, Array<T>>;
2024-09-20 13:56:33 +03:00
updateAll(value: Partial<T>): Array<T>;
2024-10-20 13:09:48 +03:00
atR(index: number | undefined): Result<void, T>;
2024-12-31 01:27:46 +03:00
whereOne(predicate: (value: T) => boolean): Result<void, T>;
2024-08-13 13:03:28 +03:00
}
interface Date {
formatDate(): string;
fromUnixDate(unix: number): Date;
2023-11-10 21:43:57 +03:00
}
2024-02-16 14:16:35 +03:00
interface Number {
fromArray(): number[];
toPx(): string;
unixFromDate(): string;
2024-04-12 20:43:36 +03:00
isValid(str: string): boolean;
2024-04-26 15:44:09 +03:00
randRange(min: number, max: number): number;
isPositive(): boolean;
isNegative(): boolean;
2024-06-25 12:43:41 +03:00
isEven(): boolean;
isOdd(): boolean;
2024-07-13 18:18:14 +03:00
isEqualR(number: number): Result<void, void>;
2024-02-16 14:16:35 +03:00
}
2023-11-10 21:43:57 +03:00
interface String {
isEmpty(): boolean;
2024-01-23 17:23:10 +03:00
isNotEmpty(): boolean;
2024-07-13 18:18:14 +03:00
isNotEmptyR(): Result<void, string>;
replaceMany(searchValues: string[], replaceValue: string): string;
isEqual(str: string): boolean;
isEqualMany(str: string[]): boolean;
2024-05-29 15:38:36 +03:00
hasPattern(pattern: string): boolean;
hasNoPattern(pattern: string): boolean;
2024-06-25 12:43:41 +03:00
divideByIndex(index: number): string[];
2024-07-03 15:25:51 +03:00
isEqualR(str: string): Result<void, string>;
2023-11-10 21:43:57 +03:00
}
2024-05-29 15:38:36 +03:00
2023-12-10 21:44:41 +03:00
interface Map<K, V> {
2023-12-28 17:18:12 +03:00
addValueOrMakeCallback(key: K, value: V, callBack: CallBackVoidFunction): void;
2024-02-27 18:48:35 +03:00
getKeyFromValueIsExists(value: V): K | undefined;
overrideValue(key: K, value: OptionalProperties<V>): void;
keysToJson(): string;
toArray(): V[];
2024-09-26 18:41:51 +03:00
toArrayEntries(): { key: K; value: V }[];
getPredicateKey(callBack: (value: V) => boolean): K[];
2024-05-29 15:38:36 +03:00
incrementValue(key: K): void;
2023-12-10 21:44:41 +03:00
}
2024-07-13 18:18:14 +03:00
interface Boolean {
r(): Result<void, void>;
}
2023-11-10 12:06:40 +03:00
}
2023-11-10 21:43:57 +03:00
export const extensions = () => {
StringExtensions();
ArrayExtensions();
2024-02-16 14:16:35 +03:00
NumberExtensions();
2023-12-10 21:44:41 +03:00
MapExtensions();
2024-08-13 13:03:28 +03:00
DateExtensions();
2023-11-10 21:43:57 +03:00
};