Home Manual Reference Source Repository

all/@jali-ms/util/src/type-guards.js

import * as ArgumentVerifiers from './argument-verifiers';
import * as Iterables from './iterables';
export function isError(value) {
    return value.message !== undefined;
}
export function makeIsIterable(elementTypeGuard, deep = false) {
    ArgumentVerifiers.verifyFunction('elementTypeGuard', elementTypeGuard);
    const test = (value) => {
        if (!isIterable(value)) {
            return false;
        }
        if (!deep) {
            const first = Iterables.find(value);
            if (first === undefined) {
                return true;
            }
            return elementTypeGuard(first);
        }
        for (const element of value) {
            if (!elementTypeGuard(element)) {
                return false;
            }
        }
        return true;
    };
    // Need type assertion: See https://github.com/Microsoft/TypeScript/issues/5951.
    return test;
}
export function isIterable(value) {
    return value && value[Symbol.iterator] !== undefined;
}