1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/services/state-util.ts
renovate[bot] 120eb2b2fa
fix(deps): update dependency js-yaml to v4 (#985)
* fix(deps): update dependency js-yaml to v4

* fix: upgrade to js-yaml v4

* fix: upgrade to js-yaml 4.1.0

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2021-09-28 20:53:39 +02:00

39 lines
1.1 KiB
TypeScript

import * as fs from 'fs';
import * as mime from 'mime';
import * as YAML from 'js-yaml';
export const readFile: (file: string) => Promise<string> = (file) =>
new Promise((resolve, reject) =>
fs.readFile(file, (err, v) =>
err ? reject(err) : resolve(v.toString('utf-8')),
),
);
export const parseFile: (file: string, data: string) => any = (
file: string,
data: string,
) => (mime.lookup(file) === 'text/yaml' ? YAML.load(data) : JSON.parse(data));
export const filterExisting: (
keepExisting: boolean,
existingArray: any[],
) => (item: any) => boolean =
(keepExisting, existingArray = []) =>
(item) => {
if (keepExisting) {
const found = existingArray.find((t) => t.name === item.name);
return !found;
}
return true;
};
export const filterEqual: (existingArray: any[]) => (item: any) => boolean =
(existingArray = []) =>
(item) => {
const toggle = existingArray.find((t) => t.name === item.name);
if (toggle) {
return JSON.stringify(toggle) !== JSON.stringify(item);
}
return true;
};