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

64 lines
2.1 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";
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>;
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-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;
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-20 21:45:57 +03:00
divideByIndex(index: number): 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[];
getPredicateValue(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
}
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();
2023-11-10 21:43:57 +03:00
};