@ibgib/helper-gib
Version:
common helper/utils/etc used in ibgib libs. Node v19+ needed for heavily-used isomorphic webcrypto hashing consumed in both node and browsers.
240 lines • 7.6 kB
text/typescript
export type HashAlgorithm = 'SHA-256' | 'SHA-512';
export declare const HashAlgorithm: {
[ ]: HashAlgorithm;
};
export declare function clone(obj: any): any;
export declare function getTimestamp(): string;
/**
* Simple hash function.
*
* NOTE:
* This is not used for ibGib.gib values (ATOW)
* but rather as a helper function for generating random UUIDs.
*
* @param s string to hash
* @param algorithm to use, currently only 'SHA-256'
*/
export declare function hash({ s, algorithm, }: {
s: string;
algorithm?: HashAlgorithm;
}): Promise<string>;
/**
* Simple func to generate UUID (sha-256 hash basically).
*
* @param seedSize size of seed for UUID generation
*/
export declare function getUUID(seedSize?: number): Promise<string>;
/**
* Syntactic sugar for JSON.stringify(obj, null, 2);
*
* @param obj to pretty stringify
*/
export declare function pretty(obj: any): string;
/**
* Just delays given number of ms.
*
* @param ms milliseconds to delay
*/
export declare function delay(ms: number): Promise<void>;
/**
* extracts the error message from an error object/string/falsy arg.
*
* ## notes
*
* * some libs throw errors, some throw just strings.
* * who knows what else it could be.
*
* ## todo
*
* * extract inner errors/causes if we ever use this function extensively.
*
* @param error the error object in the catch area of the try..catch block.
* @returns error.message if it's a string, error itself if it's a string, or canned error messages if it's falsy or none of the above.
*/
export declare function extractErrorMsg(error: any): string;
export declare function groupBy<TItem>({ items, keyFn, }: {
items: TItem[];
keyFn: (x: TItem) => string;
}): {
[ ]: TItem[];
};
/**
* Just trying to centralize and standardize regular expressions here...
*/
export declare function getRegExp({ min, max, chars, noSpaces, }: {
min?: number;
max?: number;
chars?: string;
noSpaces?: boolean;
}): RegExp;
/**
* syntactic sugar for `(new Date()).getTime().toString()`
* @returns ticks string
*/
export declare function getTimestampInTicks(timestamp?: string): string;
/**
* ## requires
* at least either `startDate` or one of the intervals to be truthy.
*
* ## thanks
*
* https://stackoverflow.com/questions/8609261/how-to-determine-one-year-from-now-in-javascript
*
* ## tested manually eek
```
console.log(new Date().toUTCString());
// Mon, 14 Feb 2022 14:19:32 GMT
console.log(getExpirationUTCString({years: 1}));
// Tue, 14 Feb 2023 14:19:32 GMT
console.log(getExpirationUTCString({months: 13}));
// Tue, 14 Mar 2023 13:19:32 GMT
console.log(getExpirationUTCString({days: 365}));
// Tue, 14 Feb 2023 14:19:32 GMT
console.log(getExpirationUTCString({days: 45}));
// Thu, 31 Mar 2022 13:19:32 GMT
console.log(getExpirationUTCString({years: 1, days: 45, hours: 25, seconds: 70}));
// Sat, 01 Apr 2023 14:20:42 GMT
console.log(getExpirationUTCString({days: 10, hours: 10, seconds: 10}));
// Fri, 25 Feb 2022 00:19:42 GMT
console.log(getExpirationUTCString({years: 1, days: 45, hours: 25, seconds: 70}));
// Sat, 01 Apr 2023 14:20:42 GMT
console.log(getExpirationUTCString({years: 1, days: 45, hours: 25, seconds: 35}));
// Sat, 01 Apr 2023 14:20:07 GMT
```
*/
export declare function getExpirationUTCString({ startDate, years, months, days, hours, seconds, }: {
startDate?: Date;
years?: number;
months?: number;
days?: number;
hours?: number;
seconds?: number;
}): string;
export declare function addTimeToDate({ startDate, years, months, days, hours, seconds, }: {
startDate?: Date;
years?: number;
months?: number;
days?: number;
hours?: number;
seconds?: number;
}): Date;
export declare function isExpired({ expirationTimestampUTC, }: {
expirationTimestampUTC: string;
}): boolean;
/**
* Creates a new array that is a unique set of incoming `arr`.
* @param arr array to make unique
* @returns new array with unique items
*/
export declare function unique<T>(arr: T[]): T[];
export declare function patchObject({ obj, value, path, pathDelimiter, logalot, }: {
obj: Object;
value: any;
path: string;
pathDelimiter?: string;
logalot?: number | boolean;
}): void;
export declare function getIdPool({ n, }: {
n: number;
}): Promise<string[]>;
export declare function getSaferSubstring({ text, length, keepLiterals, replaceMap, }: {
text: string;
length?: number;
/**
* list of strings that you want to keep in the resultant string verbatim (without alteration).
*
* ## driving use case
*
* I want user comments that start with a question mark (?) to signify a
* request to a robbot, e.g. "?start someAddr^gib" or whatever. So I want to
* keep the question mark. I thought of an encoding mapping, like ? =>
* "__qstmark__" but it's easier just to keep it, as this function was
* originally intended to just nerf text in general because there was no
* reason not to. well now there is a reason.
*
* I'm adding in a couple other characters in common use for whenever I get around
* to making those mean something in the app (#, @)
*/
keepLiterals?: string[];
/**
*
*/
replaceMap?: {
[s: string]: string;
};
}): string;
/**
* picks a random item from an array
*/
export declare function pickRandom<T extends any>({ x }: {
x: T[];
}): T | undefined;
/**
* NOT strong crypto!
*
* returns `count` number of letters concatenated into a string.
*/
export declare function pickRandom_Letters({ count }: {
count: number;
}): string;
/**
* creates a text selection of the entire element's text.
*
* ty https://stackoverflow.com/questions/985272/selecting-text-in-an-element-akin-to-highlighting-with-your-mouse
*
* @param el element whose text we're selecting
*/
export declare function selectElementText(el: HTMLElement): void;
/**
* replaces an individual character at a position.
*
* ## driving use case
*
* part of functionality to replace entire words with underscores (_'s) for blanking out
* stimulations in wordy robbot.
*
* @returns string with replaced characters
*/
export declare function replaceCharAt({ s, pos, newChar, }: {
s: string;
pos: number;
newChar: string;
}): string;
/**
* Apparently it's a pain to determine if a keyboard event is hitting the
* "enter" key across platforms.
*
* @link https://bugs.chromium.org/p/chromium/issues/detail?id=79407
* @link https://stackoverflow.com/questions/3883543/javascript-different-keycodes-on-different-browsers
*
* @returns true if the event is the user pressing the "Enter" key, else false
*/
export declare function isKeyboardEvent_Enter(event: KeyboardEvent): boolean;
/**
* https://github.com/ionic-team/capacitor/issues/1564
*
* Still doesn't work...hmm
*/
export declare function getFileReaderHack(): FileReader;
/**
* checks for mouse/trackball presence and infers keyboard when one is detected.
*
* ## aside
*
* It's amazing this isn't in an API...
*
* @returns true if by magical inference there is probably* a keyboard
*/
export declare function weHaveAPhysicalKeyboardProbably(): boolean;
/**
* Check for either a physical keyboard or a relatively large window
*
* ## notes
*
* Such a long silly name because it's silly we don't have a better way of
* detecting this with an official API.
*
* @returns true if we think that we're running on mobile
*/
export declare function weAreRunningOnMobileProbably(): boolean;
//# sourceMappingURL=utils-helper.d.mts.map