1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/services/state-util.js
2020-11-03 14:56:07 +01:00

43 lines
984 B
JavaScript

const fs = require('fs');
const mime = require('mime');
const YAML = require('js-yaml');
const readFile = file => {
return new Promise((resolve, reject) =>
fs.readFile(file, (err, v) => (err ? reject(err) : resolve(v))),
);
};
const parseFile = (file, data) => {
return mime.getType(file) === 'text/yaml'
? YAML.safeLoad(data)
: JSON.parse(data);
};
const filterExisitng = (keepExisting, exitingArray) => {
return item => {
if (keepExisting) {
const found = exitingArray.find(t => t.name === item.name);
return !found;
}
return true;
};
};
const filterEqual = exitingArray => {
return item => {
const toggle = exitingArray.find(t => t.name === item.name);
if (toggle) {
return JSON.stringify(toggle) !== JSON.stringify(item);
}
return true;
};
};
module.exports = {
readFile,
parseFile,
filterExisitng,
filterEqual,
};