@anycli/config
Version:
base config object and standard interfaces for anycli components
50 lines (49 loc) • 1.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable no-implicit-dependencies
const fs = require("fs");
function flatMap(arr, fn) {
return arr.reduce((arr, i) => arr.concat(fn(i)), []);
}
exports.flatMap = flatMap;
function mapValues(obj, fn) {
return Object.entries(obj)
.reduce((o, [k, v]) => {
o[k] = fn(v, k);
return o;
}, {});
}
exports.mapValues = mapValues;
function loadJSONSync(path) {
// let loadJSON
// try { loadJSON = require('load-json-file') } catch {}
// if (loadJSON) return loadJSON.sync(path)
return JSON.parse(fs.readFileSync(path, 'utf8'));
}
exports.loadJSONSync = loadJSONSync;
function exists(path) {
return new Promise(resolve => fs.exists(path, resolve));
}
exports.exists = exists;
function loadJSON(path) {
// let loadJSON
// try { loadJSON = require('load-json-file') } catch {}
// if (loadJSON) return loadJSON.sync(path)
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, d) => {
if (err)
reject(err);
else
resolve(JSON.parse(d));
});
});
}
exports.loadJSON = loadJSON;
function compact(a) {
return a.filter((a) => !!a);
}
exports.compact = compact;
function uniq(arr) {
return arr.filter((a, i) => arr.findIndex(b => b === a) === i);
}
exports.uniq = uniq;