UNPKG

trilogy

Version:

TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.

61 lines (60 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeDirPath = exports.invariant = exports.firstOrValue = exports.toArray = exports.isEmpty = exports.isNil = exports.isNumber = exports.isString = exports.isFunction = exports.isObject = exports.mapObj = void 0; const path_1 = require("path"); const fs_1 = require("fs"); function mapObj(collection, fn) { const result = {}; for (const [key, value] of Object.entries(collection)) { result[key] = fn(value, key); } return result; } exports.mapObj = mapObj; exports.isObject = (value) => ((value === null || value === void 0 ? void 0 : value.constructor) === Object) || false; exports.isFunction = (value) => typeof value === 'function'; exports.isString = (value) => typeof value === 'string'; exports.isNumber = (value) => typeof value === 'number'; exports.isNil = (value) => value == null; exports.isEmpty = (value) => { if (exports.isNil(value)) return true; if (Array.isArray(value)) return value.length === 0; if (exports.isObject(value)) return Object.keys(value).length === 0; return false; }; exports.toArray = (value) => Array.isArray(value) ? value : exports.isNil(value) ? [] : [value]; exports.firstOrValue = (value) => Array.isArray(value) ? value[0] : value; function invariant(condition, message) { if (!condition) { throw new Error(message || 'Invariant Violation'); } return condition; } exports.invariant = invariant; function makeDirPath(path) { const mode = parseInt('0777', 8); try { fs_1.mkdirSync(path, mode); return true; } catch (err) { if (err.code === 'EEXIST') { return fs_1.statSync(path).isDirectory(); } if (err.code === 'ENOENT') { const target = path_1.dirname(path); return (target !== path && makeDirPath(target) && (Boolean(fs_1.mkdirSync(path, mode)) || true)); } return false; } } exports.makeDirPath = makeDirPath;