appwrite-utils
Version:
`appwrite-utils` is a comprehensive TypeScript library designed to streamline the development process for Appwrite projects. It provides a suite of utilities and helper functions that facilitate data manipulation, schema management, and seamless integrati
179 lines (178 loc) • 8.04 kB
TypeScript
import type { AttributeMappings } from "../schemas/attributeMappings.js";
export interface ConverterFunctions {
[key: string]: (value: any) => any;
}
export declare const converterFunctions: {
/**
* Converts any value to a string. Handles null and undefined explicitly.
* @param {any} value The value to convert.
* @return {string | null} The converted string or null if the value is null or undefined.
*/
anyToString(value: any): string | null;
/**
* Converts any value to a number. Returns null for non-numeric values, null, or undefined.
* @param {any} value The value to convert.
* @return {number | null} The converted number or null.
*/
anyToNumber(value: any): number | null;
/**
* Converts any value to a boolean. Specifically handles string representations.
* @param {any} value The value to convert.
* @return {boolean | null} The converted boolean or null if conversion is not possible.
*/
anyToBoolean(value: any): boolean | null;
/**
* Converts any value to an array, attempting to split strings by a specified separator.
* @param {any} value The value to convert.
* @param {string | undefined} separator The separator to use when splitting strings.
* @return {any[]} The resulting array after conversion.
*/
anyToAnyArray(value: any): any[];
/**
* Converts any value to an array of strings. If the input is already an array, returns it as is.
* Otherwise, if the input is a string, returns an array with the string as the only element.
* Otherwise, returns an empty array.
* @param {any} value The value to convert.
* @return {string[]} The resulting array after conversion.
*/
anyToStringArray(value: any): string[];
/**
* Converts any null, empty, or undefined value to an empty array
* Meant to be used if the value needs to be an array but you can't guarantee it is one
*/
onlyUnsetToArray(value: any): any[];
/**
* Flattens an array or a nested array of arrays into a single array.
*
* @param {any} value - The value to be flattened. Can be an array or a nested array of arrays.
* @return {any[]} - A flattened array of all the elements from the input array(s).
* If the input is null/undefined/empty, an empty array is returned.
* If the input is not an array or a nested array of arrays, the input is returned as is.
*/
flattenArray(value: any): any[];
/**
* A function that converts any type of value to an array of numbers.
*
* @param {any} value - the value to be converted
* @return {number[]} an array of numbers
*/
anyToNumberArray(value: any): number[];
trim(value: string): string;
/**
* Removes the start and end quotes from a string.
* @param value The string to remove quotes from.
* @return The string with quotes removed.
**/
removeStartEndQuotes(value: string): string;
/**
* Tries to split a string by different separators and returns the split that has the most uniform segment lengths.
* This can be particularly useful for structured data like phone numbers.
* @param value The string to split.
* @return The split string array that has the most uniform segment lengths.
*/
trySplitByDifferentSeparators(value: string): string[];
joinValues(values: any[]): any;
joinBySpace(values: any[]): any;
makeArrayUnique(values: any[]): any[];
joinByComma(values: any[]): any;
joinByPipe(values: any[]): any;
joinBySemicolon(values: any[]): any;
joinByColon(values: any[]): any;
joinBySlash(values: any[]): any;
joinByHyphen(values: any[]): any;
splitByComma(value: string): any;
splitByPipe(value: string): any;
splitBySemicolon(value: string): any;
splitByColon(value: string): any;
splitBySlash(value: string): any;
splitByBackslash(value: string): any;
splitBySpace(value: string): any;
splitByDot(value: string): any;
splitByUnderscore(value: string): any;
splitByHyphen(value: string): any;
/**
* Takes the first element of an array and returns it.
* @param {any[]} value The array to take the first element from.
* @return {any} The first element of the array.
*/
pickFirstElement(value: any[]): any;
/**
* Takes the last element of an array and returns it.
* @param {any[]} value The array to take the last element from.
* @return {any} The last element of the array.
*/
pickLastElement(value: any[]): any;
/**
* Converts an object to a JSON string.
* @param {any} object The object to convert.
* @return {string} The JSON string representation of the object.
*/
stringifyObject(object: any): string;
/**
* Converts a JSON string to an object.
* @param {string} jsonString The JSON string to convert.
* @return {any} The object representation of the JSON string.
*/
parseObject(jsonString: string): any;
convertPhoneStringToUSInternational(value: string): string;
convertEmptyToNull(value: any): any;
/**
* A function that removes invalid elements from an array
*
* @param {any[]} array - the input array
* @return {any[]} the filtered array without invalid elements
*/
removeInvalidElements(array: any[]): any[];
validateOrNullEmail(email: string): string | null;
/**
* Tries to parse a date from various formats using Luxon with enhanced error reporting.
* @param {string | number} input The input date as a string or timestamp.
* @return {string | null} The parsed date in ISO 8601 format or null if parsing failed.
*/
safeParseDate(input: string | number): string | null;
};
/**
* Deeply converts all properties of an object (or array) to strings.
* @param data The input data to convert.
* @returns The data with all its properties converted to strings.
*/
export declare const deepAnyToString: (data: any) => any;
/**
* Performs a deep conversion of all values in a nested structure to the specified type.
* Uses a conversion function like anyToString, anyToNumber, etc.
* @param data The data to convert.
* @param convertFn The conversion function to apply.
* @returns The converted data.
*/
export declare const deepConvert: <T>(data: any, convertFn: (value: any) => T) => any;
/**
* Converts an entire object's properties to different types based on a provided schema.
* @param obj The object to convert.
* @param schema A mapping of object keys to conversion functions.
* @returns The converted object.
*/
export declare const convertObjectBySchema: (obj: Record<string, any>, schema: Record<string, (value: any) => any>) => Record<string, any>;
/**
* Converts the keys of an object based on a provided attributeMappings.
* Each key in the object is checked against attributeMappings; if a matching entry is found,
* the key is renamed to the targetKey specified in attributeMappings.
*
* @param obj The object to convert.
* @param attributeMappings The attributeMappings defining how keys in the object should be converted.
* @returns The converted object with keys renamed according to attributeMappings.
*/
export declare const convertObjectByAttributeMappings: (obj: Record<string, any>, attributeMappings: AttributeMappings) => Record<string, any>;
/**
* Ensures data conversion without mutating the original input.
* @param data The data to convert.
* @param convertFn The conversion function to apply.
* @returns The converted data.
*/
export declare const immutableConvert: <T>(data: any, convertFn: (value: any) => T) => T;
/**
* Validates a string against a regular expression and returns the string if valid, or null.
* @param value The string to validate.
* @param pattern The regex pattern to validate against.
* @returns The original string if valid, otherwise null.
*/
export declare const validateString: (value: string, pattern: RegExp) => string | null;