UNPKG

@finos/legend-shared

Version:
107 lines 4.2 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { EnrichedError } from './ErrorUtils.js'; export class AssertionError extends EnrichedError { constructor(error, message) { super('Assertion Error', error, message); } } export const isNullable = (value) => value === null || value === undefined; export const isNonNullable = (value) => value !== null && value !== undefined; export function assertNonNullable(value, message = '') { if (value === null || value === undefined) { throw new AssertionError(message || 'Value is nullable'); } } export const guaranteeNonNullable = (value, message = '') => { assertNonNullable(value, message); return value; }; export const isType = (value, clazz) => value instanceof clazz; export const filterByType = (clazz) => (value) => isType(value, clazz); // Aserts typing doesn't work with all arrow function type declaration form // So we can use this: export const assertType: <T>(value: unknown, clazz: Clazz<T>, message: string) => asserts value is T = (value, clazz, message = '') => { // or the normal function form // See https://github.com/microsoft/TypeScript/issues/34523 // See https://github.com/microsoft/TypeScript/pull/33622 export function assertType(value, clazz, message = '') { if (!(value instanceof clazz)) { throw new AssertionError(message || `Value is expected to be of type '${clazz.name}'`); } } export const guaranteeType = (value, clazz, message = '') => { assertType(value, clazz, message); return value; }; export function isNonEmptyString(str) { return isNonNullable(str) && str !== ''; } export function assertNonEmptyString(str, message = '') { if (guaranteeNonNullable(str, message) === '') { throw new AssertionError(message || `Expected string value to be non-empty`); } } export function guaranteeNonEmptyString(str, message = '') { assertNonEmptyString(str, message); return str; } export function assertTrue(assertionValue, message = '') { if (!assertionValue) { throw new AssertionError(message || `Expected predicate to be truthy`); } } export const isString = (val) => typeof val === 'string'; export const isNumber = (val) => typeof val === 'number' && !isNaN(val); export const isBoolean = (val) => typeof val === 'boolean'; export const isObject = (val) => typeof val === 'object' && val !== null; export const isPlainObject = (val) => isObject(val) && val.constructor.name === 'Object'; export function assertIsString(val, message = '') { if (!isString(val)) { throw new AssertionError(message || `Value is expected to be a string`); } } export function assertIsNumber(val, message = '') { if (!isNumber(val)) { throw new AssertionError(message || `Value is expected to be a number`); } } export function assertIsBoolean(val, message = '') { if (!isBoolean(val)) { throw new AssertionError(message || `Value is expected to be a boolean`); } } export function assertIsObject(val, message = '') { if (!isObject(val)) { throw new AssertionError(message || `Value is expected to be a object`); } } export const guaranteeIsString = (val, message = '') => { assertIsString(val, message); return val; }; export const guaranteeIsNumber = (val, message = '') => { assertIsNumber(val, message); return val; }; export const guaranteeIsBoolean = (val, message = '') => { assertIsBoolean(val, message); return val; }; export const guaranteeIsObject = (val, message = '') => { assertIsObject(val, message); return val; }; //# sourceMappingURL=AssertionUtils.js.map