UNPKG

1.41 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3// tslint:disable no-implicit-dependencies
4const fs = require("fs");
5function flatMap(arr, fn) {
6 return arr.reduce((arr, i) => arr.concat(fn(i)), []);
7}
8exports.flatMap = flatMap;
9function mapValues(obj, fn) {
10 return Object.entries(obj)
11 .reduce((o, [k, v]) => {
12 o[k] = fn(v, k);
13 return o;
14 }, {});
15}
16exports.mapValues = mapValues;
17function loadJSONSync(path) {
18 // let loadJSON
19 // try { loadJSON = require('load-json-file') } catch {}
20 // if (loadJSON) return loadJSON.sync(path)
21 return JSON.parse(fs.readFileSync(path, 'utf8'));
22}
23exports.loadJSONSync = loadJSONSync;
24function exists(path) {
25 return new Promise(resolve => fs.exists(path, resolve));
26}
27exports.exists = exists;
28function loadJSON(path) {
29 // let loadJSON
30 // try { loadJSON = require('load-json-file') } catch {}
31 // if (loadJSON) return loadJSON.sync(path)
32 return new Promise((resolve, reject) => {
33 fs.readFile(path, 'utf8', (err, d) => {
34 if (err)
35 reject(err);
36 else
37 resolve(JSON.parse(d));
38 });
39 });
40}
41exports.loadJSON = loadJSON;
42function compact(a) {
43 return a.filter((a) => !!a);
44}
45exports.compact = compact;
46function uniq(arr) {
47 return arr.filter((a, i) => arr.findIndex(b => b === a) === i);
48}
49exports.uniq = uniq;