UNPKG

entity-baker

Version:

Generates simple and powerful entity classes for ORM systems, like Doctrine and/or Entity Framework.

561 lines 15.3 kB
"use strict"; /** * This file is part of the node-entity-baker distribution. * Copyright (c) Marcel Joachim Kloubert. * * node-entity-baker 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. * * node-entity-baker 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/>. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const FS = require("fs"); const Glob = require("glob"); const MergeDeep = require('merge-deep'); const OS = require("os"); /** * Converts an input value to an array. * * @param {T|T[]} val The input value. * @param {boolean} [removeEmpty] Removes items which are (null) or (undefined). * * @return {T[]} The value as array. */ function asArray(val, removeEmpty = true) { removeEmpty = toBooleanSafe(removeEmpty, true); if (!Array.isArray(val)) { val = [val]; } return val.filter(i => { if (removeEmpty) { return !isNullOrUndefined(i); } return true; }); } exports.asArray = asArray; /** * Compares two values for sort operations. * * @param {T} x The left value. * @param {T} y The right value. * * @return {number} The comparer value. */ function compareValues(x, y) { return compareValuesBy(x, y, i => i); } exports.compareValues = compareValues; /** * Compares two values for sort operations by using a selector. * * @param {T} x The left value. * @param {T} y The right value. * @param {Function} selector The selector to use. * * @return {number} The comparer value. */ function compareValuesBy(x, y, selector) { const MAPPED_X = selector(x); const MAPPED_Y = selector(y); if (MAPPED_X !== MAPPED_Y) { if (MAPPED_X < MAPPED_Y) { return -1; } if (MAPPED_X > MAPPED_Y) { return 1; } } return 0; } exports.compareValuesBy = compareValuesBy; /** * Removes duplicate entries from an array. * * @param {T[]} arr The input array. * * @return {T[]} The distincted array. */ function distinctArray(arr) { return distinctArrayBy(arr, i => i); } exports.distinctArray = distinctArray; /** * Removes duplicate entries from an array by using a selector. * * @param {T[]} arr The input array. * @param {Function} selector The selector. * * @return {T[]} The distincted array. */ function distinctArrayBy(arr, selector) { if (!arr) { return arr; } const MAPPED = arr.map(x => selector(x)); return arr.filter((x, i) => { return i === MAPPED.indexOf(selector(x)); }); } exports.distinctArrayBy = distinctArrayBy; /** * Promise version of 'Glob()' function. * * @param {string} pattern The pattern. * @param {Glob.IOptions} [options] Custom options. * * @return {Promise<string[]>} The promise with the matches. */ function glob(pattern, options) { return __awaiter(this, void 0, void 0, function* () { const DEFAULT_OPTS = { absolute: true, cwd: process.cwd(), dot: true, nocase: true, nodir: true, nonull: false, nosort: true, nounique: false, root: process.cwd(), sync: false, }; pattern = toStringSafe(pattern); if (isEmptyString(pattern)) { pattern = '**'; } return new Promise((resolve, reject) => { try { Glob(pattern, MergeDeep(DEFAULT_OPTS, options), (err, matches) => { if (err) { reject(err); } else { resolve(matches); } }); } catch (e) { reject(e); } }); }); } exports.glob = glob; /** * Promise version of 'FS.exists()' function. * * @param {FS.PathLike} path The path. * * @return {Promise<Buffer>} The promise that indicates if item exists or not. */ function exists(path) { return __awaiter(this, void 0, void 0, function* () { if (!Buffer.isBuffer(path)) { if (!isObj(path)) { path = toStringSafe(path); } } return new Promise((resolve, reject) => { try { FS.exists(path, (exists) => { resolve(exists); }); } catch (e) { reject(e); } }); }); } exports.exists = exists; /** * s. 'Glob.sync()' * * @param {string} pattern The pattern. * @param {Glob.IOptions} [options] Custom options. * * @return {string[]} The matches. */ function globSync(pattern, options) { const DEFAULT_OPTS = { absolute: true, cwd: process.cwd(), dot: true, nocase: true, nodir: true, nonull: false, nosort: true, nounique: false, root: process.cwd(), sync: true, }; pattern = toStringSafe(pattern); if (isEmptyString(pattern)) { pattern = '**'; } return Glob.sync(pattern, MergeDeep(DEFAULT_OPTS, options)); } exports.globSync = globSync; /** * Checks if a value is a boolean. * * @param {any} val The value to check. * * @return {boolean} Is boolean. */ function isBool(val) { return !isNullOrUndefined(val) && 'boolean' === typeof val; } exports.isBool = isBool; /** * Checks if the string representation of a value is an empty string * or contains whitespaces only. * * @param {any} val The value to check. * * @return {boolean} Is empty string or not. */ function isEmptyString(val) { return '' === toStringSafe(val).trim(); } exports.isEmptyString = isEmptyString; /** * Checks if a value is a function. * * @param {any} val The value to check. * * @return {boolean} Is function. */ function isFunc(val) { return !isNullOrUndefined(val) && 'function' === typeof val; } exports.isFunc = isFunc; /** * Checks if a value is (null) or (undefined). * * @param {any} val The value to check. * * @return {boolean} Is (null) or (undefined). */ function isNullOrUndefined(val) { return null === val || 'undefined' === typeof val; } exports.isNullOrUndefined = isNullOrUndefined; /** * Checks if a value is a number. * * @param {any} val The value to check. * * @return {boolean} Is number. */ function isNumber(val) { return !isNullOrUndefined(val) && 'number' === typeof val; } exports.isNumber = isNumber; /** * Checks if a value is an object. * * @param {any} val The value to check. * * @return {boolean} Is object. */ function isObj(val) { return !isNullOrUndefined(val) && !Array.isArray(val) && 'object' === typeof val; } exports.isObj = isObj; /** * Checks if a value is a string. * * @param {any} val The value to check. * * @return {boolean} Is string. */ function isString(val) { return !isNullOrUndefined(val) && 'string' === typeof val; } exports.isString = isString; /** * Checks if a value is a symbol. * * @param {any} val The value to check. * * @return {boolean} Is symbol. */ function isSymbol(val) { return !isNullOrUndefined(val) && 'symbol' === typeof val; } exports.isSymbol = isSymbol; /** * Pushes a list of items to an array. * * @param {T[]} arr The array to push to. * @param {T[]} items The items to push. */ function pushMany(arr, items) { if (!items) { items = []; } arr.push .apply(arr, items); } exports.pushMany = pushMany; /** * Normalizes a value to a string for comparison. * * @param {any} val The value to normalize. * * @return {string} The normalized value. */ function normalizeString(val) { return toStringSafe(val).toLowerCase().trim(); } exports.normalizeString = normalizeString; /** * Promise version of 'FS.readFile()' function. * * @param {FS.PathLike|number} path The path or descriptor to the file. * * @return {Promise<Buffer>} The promise with the read data. */ function readFile(path) { return __awaiter(this, void 0, void 0, function* () { if (!Buffer.isBuffer(path)) { if (!isObj(path)) { if (!isNumber(path)) { path = toStringSafe(path); } } } return new Promise((resolve, reject) => { try { FS.readFile(path, (err, data) => { if (err) { reject(err); } else { resolve(data); } }); } catch (e) { reject(e); } }); }); } exports.readFile = readFile; /** * Handles a value as string and replaces all sub strings with another string. * * @param {any} val The input value. * @param {any} searchFor The value to search for. * @param {any} replaceWith The value to replace 'searchFor' with. * * @return {string} The new value. */ function replaceAll(val, searchFor, replaceWith) { searchFor = toStringSafe(searchFor); replaceWith = toStringSafe(replaceWith); if (isNullOrUndefined(val)) { return val; } return toStringSafe(val).split(searchFor) .join(replaceWith); } exports.replaceAll = replaceAll; /** * Converts a value to a boolean. * * @param {any} val The value to convert. * @param {any} [defaultValue] The custom default value if 'val' is (null) or (undefined). * * @return {boolean} The converted value. */ function toBooleanSafe(val, defaultValue = false) { if (isBool(val)) { return val; } if (isEmptyString(val)) { return !!defaultValue; } switch (normalizeString(val)) { case '0': case 'false': case 'n': case 'no': return false; case '1': case 'true': case 'y': case 'yes': return true; } return !!val; } exports.toBooleanSafe = toBooleanSafe; /** * Converts a value to a string. * * @param {any} val The value to convert. * @param {any} [defaultValue] The custom default value if 'val' is (null) or (undefined). * * @return {string} The converted value. */ function toStringSafe(val, defaultValue = '') { if (isString(val)) { return val; } try { if (isNullOrUndefined(val)) { return '' + defaultValue; } if (val instanceof Error) { return '' + val.message; } if (isFunc(val['toString'])) { return '' + val.toString(); } return '' + val; } catch (e) { // console.debug(e); } return ''; } exports.toStringSafe = toStringSafe; /** * Writes a message to a stream. * * @param {any} msg The message to write. * @param {NodeJS.WritableStream} [stream] The custom output stream. Default: stdout */ function write(msg, stream) { if (arguments.length < 2) { stream = process.stdout; } if (!Buffer.isBuffer(msg)) { msg = toStringSafe(msg); } if (msg.length > 0) { stream.write(msg); } } exports.write = write; /** * Writes a message to stderr. * * @param {any} msg The message to write. */ function write_err(msg) { write(msg, process.stderr); } exports.write_err = write_err; /** * Writes an optional message to stderr and appends a new line. * * @param {any} [msg] The message to write. */ function write_err_ln(msg) { write_ln(msg, process.stderr); } exports.write_err_ln = write_err_ln; /** * Writes an optional message to a stream and appends a new line. * * @param {any} [msg] The message to write. * @param {NodeJS.WritableStream} [stream] The custom output stream. Default: stdout */ function write_ln(msg, stream) { if (arguments.length < 2) { stream = process.stdout; } if (Buffer.isBuffer(msg)) { msg = Buffer.concat([ msg, new Buffer(OS.EOL, 'binary') ]); } else { msg = toStringSafe(msg) + OS.EOL; } write(msg, stream); } exports.write_ln = write_ln; /** * Promise version of 'FS.writeFile()' function. * * @param {FS.PathLike|number} path The path or descriptor to the file. * @param {any} data The data to write. * @param {string} [encoding] The encoding to use. * * @return {Promise<Buffer>} The promise with the read data. */ function writeFile(path, data, encoding) { return __awaiter(this, void 0, void 0, function* () { if (!Buffer.isBuffer(path)) { if (!isObj(path)) { if (!isNumber(path)) { path = toStringSafe(path); } } } if (isNullOrUndefined(encoding)) { encoding = undefined; } else { encoding = normalizeString(encoding); } if (isNullOrUndefined(data)) { data = Buffer.alloc(0); } if (!Buffer.isBuffer(data)) { if (encoding) { data = new Buffer(toStringSafe(data), encoding); } else { data = new Buffer(toStringSafe(data)); } } return new Promise((resolve, reject) => { try { FS.writeFile(path, data, encoding, (err) => { if (err) { reject(err); } else { resolve(); } }); } catch (e) { reject(e); } }); }); } exports.writeFile = writeFile; //# sourceMappingURL=helpers.js.map