trilogy
Version:
TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.
55 lines (54 loc) • 1.79 kB
JavaScript
import { dirname } from 'path';
import { mkdirSync, statSync } from 'fs';
export function mapObj(collection, fn) {
const result = {};
for (const [key, value] of Object.entries(collection)) {
result[key] = fn(value, key);
}
return result;
}
export const isObject = (value) => ((value === null || value === void 0 ? void 0 : value.constructor) === Object) || false;
export const isFunction = (value) => typeof value === 'function';
export const isString = (value) => typeof value === 'string';
export const isNumber = (value) => typeof value === 'number';
export const isNil = (value) => value == null;
export const isEmpty = (value) => {
if (isNil(value))
return true;
if (Array.isArray(value))
return value.length === 0;
if (isObject(value))
return Object.keys(value).length === 0;
return false;
};
export const toArray = (value) => Array.isArray(value)
? value
: isNil(value)
? []
: [value];
export const firstOrValue = (value) => Array.isArray(value) ? value[0] : value;
export function invariant(condition, message) {
if (!condition) {
throw new Error(message || 'Invariant Violation');
}
return condition;
}
export function makeDirPath(path) {
const mode = parseInt('0777', 8);
try {
mkdirSync(path, mode);
return true;
}
catch (err) {
if (err.code === 'EEXIST') {
return statSync(path).isDirectory();
}
if (err.code === 'ENOENT') {
const target = dirname(path);
return (target !== path &&
makeDirPath(target) &&
(Boolean(mkdirSync(path, mode)) || true));
}
return false;
}
}