UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

129 lines (128 loc) 8.14 kB
/** this file will only use basic type checker and types, do not add functions that require * a reference to tother helpers */ import { IDictionary } from "../types/common.types"; /** this will support HtmlCollectionOf, Arrays, and any other types that have a length and indexer Issue 568 */ export interface IndexedCollection<ElementType> { length: number; [index: number]: ElementType; } /** Finds an object in array based on a filter and moves it to the start of the array */ export declare function moveToStart<T>(arr: T[], filter?: (item: T) => boolean): void; /** Finds an object in array based on a filter and moves it to the end of the array */ export declare function moveToEnd<T>(arr: T[], filter?: (item: T) => boolean): void; /** Get the first index of an object of an array, or -1 if the array is empty / null */ export declare function firstIndexOf<T>(arr: IndexedCollection<T>, filter?: (item: T, index?: number) => boolean, startFrom?: number): number; /** Get the first object of an array, or null if the array is empty / null * If you pass a filter, it will find the first element that matches the filter and return it, stopping the loop when it is found * */ export declare function firstOrNull<T>(arr: IndexedCollection<T>, filter?: (item: T, index?: number) => boolean): T; /** Get the last index of an object of an array, or -1 if the array is empty / null */ export declare function lastIndexOf<T>(arr: IndexedCollection<T>, filter?: (item: T) => boolean): number; /** get the last element or null */ export declare function lastOrNull<T>(arr: IndexedCollection<T>, filter?: (item: T) => boolean): T; /** Get the first index of an object of an array, or -1 if the array is empty / null */ export declare function firstIndexOfAsync<T>(arr: IndexedCollection<T>, filter?: (item: T, index?: number) => Promise<boolean>, startFrom?: number): Promise<number>; /** Get the first object of an array, or null if the array is empty / null * If you pass a filter, it will find the first element that matches the filter and return it, stopping the loop when it is found * */ export declare function firstOrNullAsync<T>(arr: IndexedCollection<T>, filter?: (item: T, index?: number) => Promise<boolean>): Promise<T>; /** Get the last index of an object of an array, or -1 if the array is empty / null */ export declare function lastIndexOfAsync<T>(arr: IndexedCollection<T>, filter?: (item: T) => Promise<boolean>): Promise<number>; /** get the last element or null */ export declare function lastOrNullAsync<T>(arr: IndexedCollection<T>, filter?: (item: T) => Promise<boolean>): Promise<T>; /** Sorts an array of complex objects, use defaultPrimitiveGetValue for default functionality */ export declare function sortArray<T>(arr: T[], getValue: (item: T) => number | string): T[]; /** removes null, undefined or "" elements from the array */ export declare function filterEmptyEntries<T>(arr: T[]): T[]; export declare function sortNumberArrayAsc(a: number, b: number): number; export declare function sortNumberArray(a: number, b: number): number; /** call a foreach on an object or an array, with an option to break when returning false */ export declare function forEach<T>(obj: IDictionary<T> | Array<T> | { [key: number]: T; length: number; }, func: (propertyName: string, propertyValue: T, _args?: any) => void | boolean, args?: any): void; export declare function forEachAsync<ElmType, ResultType>(arr: Array<ElmType> | IDictionary<ElmType>, handler: (elm: ElmType, index: number) => Promise<ResultType>, options?: { parallel?: boolean; }): Promise<ResultType[]>; export declare function sizeOf(obj: any): number; export declare function chunkArray<T>(array: T[], chunkSize: number): T[][]; /** Takes an array and transforms it into a hash. this will assign 1 item per key, assumig getKey will be unique per item. */ export declare function toHash<T, Y = T>(arr: T[], getKey: (element: T) => string | number, filter?: (element: T) => boolean, transformValue?: (element: T) => Y): IDictionary<Y>; /** Returns an array from the values of the dictionary. */ export declare function toArray<Source, Result = Source>(hash: { [key: string]: Source; }, filter?: (element: Source) => boolean, transform?: (key: string, element: Source) => Result): Result[]; /** returns a new dictionary, converting each entry in source using the transform function */ export declare function convertDictionary<S, R>(source: IDictionary<S>, transform: (sourceItem: S) => R): IDictionary<R>; export declare function flattenArray<T>(array: (T | T[])[]): T[]; /** careful, does not work for date/complex objects. Use GetUniqueArrayInfo if you suspect you might have Date/complex objects. */ export declare function makeUniqueArray<T>(arr: T[]): T[]; /** return an array of unique values, and the first index they were found, use defaultPrimitiveGetValue for default functionality */ export declare function GetUniqueArrayInfo<T, V>(arr: T[], getValue: (item: T) => V): { /** true if duplicate values found */ hasDuplicates: boolean; /** all duplicate item indexes */ duplicateIndexes: number[]; /** unique values and their info */ uniqueValues: { item: T; value: V; firstIndex: number; }[]; /** the unique version of this array */ uniqueArray: T[]; }; export interface IMultiLevelGroupItem<ItemType> { parentGroup: IMultiLevelGroup<ItemType>; } export interface IMultiLevelGroup<ItemType> { groupItems: (ItemType & IMultiLevelGroupItem<ItemType>)[]; subGroups: IDictionary<IMultiLevelGroup<ItemType>>; depth: number; parentGroup: IMultiLevelGroup<ItemType>; /** would contain the path to the group, such as: 0_1_3 for first group, second sub group, 4th sub-sub group */ key: string; index: number; title: string; /** Optional, add a prefix to the group. For example: "Priority > " to fullTitle will be "Priority > High" */ groupPrefix?: string; /** title with decorations, such as prefix */ fullTitle: string; } export type MultiLevelGroupItem<ItemType> = ItemType & IMultiLevelGroupItem<ItemType>; export type MultiLevelGroupOrItem<ItemType> = MultiLevelGroupItem<ItemType> | IMultiLevelGroup<ItemType>; /** returns true if the element is a group of items */ export declare function IsMultiLevelGroup<ItemType>(groupOrItem: MultiLevelGroupOrItem<ItemType>): groupOrItem is IMultiLevelGroup<ItemType>; /** returns a flat array of groups>items ordered by groups */ export declare function FlattenGroupItems<ItemType>(groups: IDictionary<IMultiLevelGroup<ItemType>>): MultiLevelGroupOrItem<ItemType>[]; /** split a collection by page size and return the info */ export declare function GetPagedCollectionInfo<T>(collection: Array<T>, pageSize: number, currentPage?: number): { /** nubmer of pages */ pages: number; /** page items, per page (Array of pages, each has an array of the page items) */ pagedItems: T[][]; /** the current page */ currentPage: number; /** the current page items */ currentPageItems: T[]; /** has more than 1 page */ hasPages: boolean; allowPrev: boolean; allowNext: boolean; }; /** use with sortArray or get unique array to handle premitive types or dates, with a JSON.stringify to all other values */ export declare function defaultPrimitiveGetValue<T>(item: T): string | number; export declare function RemoveItemFromArr<T>(arr: T[], item: T): void; export declare function PushNoDuplicate<T>(arr: T[], item: T): void; /** fills an array with a value. Array.fill isn't available on SPFx. */ export declare function ArrayFill<T>(arr: T[], value: T, onlyEmpty?: boolean): T[]; /** give a name and a collection, and it will return a unique name availalbe, suffixing a _# to the name * example: file * return file, file_2, file_9 etc... whichever is availalbe first. */ export declare function FindNextAvailableName(name: string, usedNames: string[], options?: { caseSensitive?: boolean; suffix?: string; }): string; export declare function numbersArray<T extends number>(length: number, startFrom?: number): T[];