UNPKG

@ng-formworks/core

Version:

Angular ng-formworks - JSON Schema Form builder core

181 lines (180 loc) 7.09 kB
import { PlainObject } from './validator.functions'; /** * Utility function library: * * addClasses, copy, forEach, forEachCopy, hasOwn, mergeFilteredObject, * uniqueItems, commonItems, fixTitle, toTitleCase */ /** * 'addClasses' function * * Merges two space-delimited lists of CSS classes and removes duplicates. * * // {string | string[] | Set<string>} oldClasses * // {string | string[] | Set<string>} newClasses * // {string | string[] | Set<string>} - Combined classes */ export declare function addClasses(oldClasses: string | string[] | Set<string>, newClasses: string | string[] | Set<string>): string | string[] | Set<string>; /** * 'copy' function * * Makes a shallow copy of a JavaScript object, array, Map, or Set. * If passed a JavaScript primitive value (string, number, boolean, or null), * it returns the value. * * // {Object|Array|string|number|boolean|null} object - The object to copy * // {boolean = false} errors - Show errors? * // {Object|Array|string|number|boolean|null} - The copied object */ export declare function copy(object: any, errors?: boolean): any; /** * 'forEach' function * * Iterates over all items in the first level of an object or array * and calls an iterator funciton on each item. * * The iterator function is called with four values: * 1. The current item's value * 2. The current item's key * 3. The parent object, which contains the current item * 4. The root object * * Setting the optional third parameter to 'top-down' or 'bottom-up' will cause * it to also recursively iterate over items in sub-objects or sub-arrays in the * specified direction. * * // {Object|Array} object - The object or array to iterate over * // {function} fn - the iterator funciton to call on each item * // {boolean = false} errors - Show errors? * // {void} */ export declare function forEach(object: any, fn: (v: any, k?: string | number, c?: any, rc?: any) => any, recurse?: boolean | string, rootObject?: any, errors?: boolean): void; /** * 'forEachCopy' function * * Iterates over all items in the first level of an object or array * and calls an iterator function on each item. Returns a new object or array * with the same keys or indexes as the original, and values set to the results * of the iterator function. * * Does NOT recursively iterate over items in sub-objects or sub-arrays. * * // {Object | Array} object - The object or array to iterate over * // {function} fn - The iterator funciton to call on each item * // {boolean = false} errors - Show errors? * // {Object | Array} - The resulting object or array */ export declare function forEachCopy(object: any, fn: (v: any, k?: string | number, o?: any, p?: string) => any, errors?: boolean): any; /** * 'hasOwn' utility function * * Checks whether an object or array has a particular property. * * // {any} object - the object to check * // {string} property - the property to look for * // {boolean} - true if object has property, false if not */ export declare function hasOwn(object: any, property: string): boolean; /** * Types of possible expressions which the app is able to evaluate. */ export declare enum ExpressionType { EQUALS = 0, NOT_EQUALS = 1, NOT_AN_EXPRESSION = 2 } /** * Detects the type of expression from the given candidate. `==` for equals, * `!=` for not equals. If none of these are contained in the candidate, the candidate * is not considered to be an expression at all and thus `NOT_AN_EXPRESSION` is returned. * // {expressionCandidate} expressionCandidate - potential expression */ export declare function getExpressionType(expressionCandidate: string): ExpressionType; export declare function isEqual(expressionType: any): boolean; export declare function isNotEqual(expressionType: any): boolean; export declare function isNotExpression(expressionType: any): boolean; /** * Splits the expression key by the expressionType on a pair of values * before and after the equals or nor equals sign. * // {expressionType} enum of an expression type * // {key} the given key from a for loop iver all conditions */ export declare function getKeyAndValueByExpressionType(expressionType: ExpressionType, key: string): string[]; export declare function cleanValueOfQuotes(keyAndValue: any): String; /** * 'mergeFilteredObject' utility function * * Shallowly merges two objects, setting key and values from source object * in target object, excluding specified keys. * * Optionally, it can also use functions to transform the key names and/or * the values of the merging object. * * // {PlainObject} targetObject - Target object to add keys and values to * // {PlainObject} sourceObject - Source object to copy keys and values from * // {string[]} excludeKeys - Array of keys to exclude * // {(string: string) => string = (k) => k} keyFn - Function to apply to keys * // {(any: any) => any = (v) => v} valueFn - Function to apply to values * // {PlainObject} - Returns targetObject */ export declare function mergeFilteredObject(targetObject: PlainObject, sourceObject: PlainObject, excludeKeys?: string[], keyFn?: (key: string) => string, valFn?: (val: any) => any): PlainObject; /** * 'uniqueItems' function * * Accepts any number of string value inputs, * and returns an array of all input vaues, excluding duplicates. * * // {...string} ...items - * // {string[]} - */ export declare function uniqueItems(...items: any[]): string[]; /** * 'commonItems' function * * Accepts any number of strings or arrays of string values, * and returns a single array containing only values present in all inputs. * * // {...string|string[]} ...arrays - * // {string[]} - */ export declare function commonItems(...arrays: any[]): string[]; /** * 'fixTitle' function * * * // {string} input - * // {string} - */ export declare function fixTitle(name: string): string; /** * 'toTitleCase' function * * Intelligently converts an input string to Title Case. * * Accepts an optional second parameter with a list of additional * words and abbreviations to force into a particular case. * * This function is built on prior work by John Gruber and David Gouch: * http://daringfireball.net/2008/08/title_case_update * https://github.com/gouch/to-title-case * * // {string} input - * // {string|string[]} forceWords? - * // {string} - */ export declare function toTitleCase(input: string, forceWords?: string | string[]): string; /** * Recursively checks if at least one property of the given object (including nested objects) * has a non-null and non-undefined value. * * @param obj - The object to check. * @returns `true` if at least one property has a non-null and non-undefined value, otherwise `false`. * * @example * const testObj = { a: null, b: { b1: null, b2: undefined } }; * console.log(hasNonNullValue(testObj)); // Output: false * * const testObj2 = { a: 1, b: { b1: null, b2: undefined } }; * console.log(hasNonNullValue(testObj2)); // Output: true */ export declare function hasNonNullValue(obj: Record<string, any>): boolean;