UNPKG

for-promise

Version:

A lightweight utility to run async loops with full control using Promises — like for, forEach, and while, but smarter.

65 lines (64 loc) 2.62 kB
/** * Validates the total value based on the type of the input. * * - If the input is a number, subtracts 1 and returns it. * - If it's an object or array, returns its size using `countObj`. * - Otherwise, returns 0. * * @param {*} obj - The input to validate and process. * @returns {number} - The resulting total based on the input type. * * @example * validateTotal(5); // 4 * validateTotal([1, 2, 3]); // 3 * validateTotal({ a: 1, b: 2 }); // 2 * validateTotal('invalid'); // 0 */ export function validateTotal(obj: any): number; /** * Performs a structured validation on a given object and returns a confirmation result. * * - Confirms validity if `obj.data` is an object, array, or number. * - Also confirms if `obj.type` is `'while'`, and `obj.while` is defined, and `obj.checker` is a function. * * @param {import("../index.mjs").forPromiseIteration} obj - The input object to validate. * @returns {{ confirmed: boolean, type?: string }} - Returns an object with a `confirmed` boolean. * If a "while" validation is detected, includes `type: 'while'`. * * @example * superValidator({ data: [1, 2, 3] }); // { confirmed: true } * superValidator({ type: 'while', while: true, checker: () => true }); // { confirmed: true, type: 'while' } * superValidator({}); // { confirmed: false } */ export function superValidator(obj: import("../index.mjs").forPromiseIteration): { confirmed: boolean; type?: string; }; /** * Determines whether a given value is a pure JSON object (plain object). * * A pure object satisfies the following: * - It is not null. * - Its type is "object". * - Its internal [[Class]] is "[object Object]". * - It is not an instance of built-in types like Array, Date, Map, Set, etc. * * This function is useful for strict data validation when you want to ensure * a value is a clean JSON-compatible object, free of class instances or special types. * * @param {unknown} value - The value to test. * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object. */ export function isJsonObject(value: unknown): value is Record<string | number | symbol, unknown>; /** * Counts the number of elements in an array or the number of properties in an object. * * @param {*} obj - The array or object to count. * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object. * * @example * countObj([1, 2, 3]); // 3 * countObj({ a: 1, b: 2 }); // 2 * countObj('not an object'); // 0 */ export function countObj(obj: any): number;