UNPKG

@egodigital/nef

Version:

Managed Extensibility Framework like library written for Node.js

120 lines 3.37 kB
"use strict"; /** * This file is part of the @egodigital/nef distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/nef is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/nef is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.toStringSafe = exports.toBooleanSafe = exports.loadModule = exports.getClassesFromObject = exports.asArray = void 0; const _ = require("lodash"); /** * Keeps sure to return a value as array. * * @param {T|T[]} val The input value. * * @return {T[]} The output value. */ function asArray(val) { if (!Array.isArray(val)) { val = [val]; } return val.filter(i => !_.isNil(i)); } exports.asArray = asArray; /** * Returns classes of an object deep. * * @param {any} obj The object. * * @return {any[]} The list of classes. */ function getClassesFromObject(obj) { return getClassesFromObjectInner(obj, []); } exports.getClassesFromObject = getClassesFromObject; function getClassesFromObjectInner(obj, alreadyHandled) { if (_.isNil(obj)) { return; } if (alreadyHandled.some(x => x === obj)) { return; } alreadyHandled.push(obj); const CLASS_LIST = []; for (const PROP in obj) { const VALUE = obj[PROP]; if (_.isFunction(VALUE) && _.isFunction(VALUE.constructor)) { CLASS_LIST.push(VALUE); } else { CLASS_LIST.push(...getClassesFromObjectInner(VALUE, alreadyHandled)); } } return CLASS_LIST; } /** * Loads a module. * * @param {string} file The path to the module. * @param {boolean} [useCache] Use cache or not. * * @return {TModule} The module. */ function loadModule(file, useCache = false) { file = require.resolve(toStringSafe(file)); if (!useCache) { delete require.cache[file]; } return require(file); } exports.loadModule = loadModule; /** * Converts a value to a boolean, if needed. * * @param {any} val The input value. * @param {boolean} [defaultValue] The custom default value. * * @return {string} The output value. */ function toBooleanSafe(val, defaultValue = false) { if (_.isNil(val)) { return !!defaultValue; } return !!val; } exports.toBooleanSafe = toBooleanSafe; /** * Converts a value to a string, if needed, that is not (null) and (undefined). * * @param {any} val The input value. * * @return {string} The output value. */ function toStringSafe(val) { if (_.isString(val)) { return val; } if (_.isNil(val)) { return ''; } if (_.isFunction(val)) { return val.name; } if (_.isFunction(val['toString'])) { return String(val.toString()); } return String(val); } exports.toStringSafe = toStringSafe; //# sourceMappingURL=util.js.map