node-git-2-json
Version:
Simple tool to get a JSON from your git log. Inspired by @fabien0102/git2json
76 lines • 3.07 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { heavySanitizeRules, lightSanitizeRules, sanitizeKeys } from './constants.js';
export function cleaner(data, cleanRules) {
return __awaiter(this, void 0, void 0, function* () {
let c = data;
if (cleanRules.keys != null) {
c = removeKeys(data, cleanRules.keys);
}
if (cleanRules.sanitize != null) {
if (cleanRules.sanitize == 'custom') {
if (cleanRules.sanitizeRule == null) {
throw new Error('Custom sanitize rule not provided with "custom" flag');
}
else if (!(cleanRules.sanitizeRule instanceof RegExp) || typeof (cleanRules.sanitizeReplace != 'string')) {
throw new Error('Custom rule types for one of or both are incorrect. "sanitizeRule" or "sanitizeReplace"');
}
}
c = sanitizeStrings(c, cleanRules);
}
return c;
});
}
export function removeKeys(data, keys) {
const tempData = data;
keys.forEach((val) => {
data.forEach((dval, didx) => {
try {
delete tempData[didx][val];
}
catch (err) {
console.warn(`key: ${val} not found in object ${dval}`);
}
});
});
return tempData;
}
export function sanitizeStrings(data, cleanRules) {
const tempData = data;
data.forEach((val, idx) => {
if (cleanRules.sanitize == 'light') {
sanitizeKeys.forEach((sval) => {
if (sval in val) {
lightSanitizeRules.forEach((rval) => {
val[sval].replace(rval[0], rval[1]);
});
}
});
}
else if (cleanRules.sanitize == 'heavy') {
sanitizeKeys.forEach((sval) => {
if (sval in val) {
heavySanitizeRules.forEach((rval) => {
val[sval].replace(rval[0], rval[1]);
});
}
});
}
else if (cleanRules.sanitize == 'custom') {
sanitizeKeys.forEach((sval) => {
if (sval in val) {
val[sval].replace(cleanRules.sanitizeRule, cleanRules.sanitizeReplace);
}
});
}
});
return tempData;
}
//# sourceMappingURL=cleaner.js.map