naming-conventions-modeler
Version:
Naming Conventions Modeler
126 lines • 4.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Modeler = exports.lookup = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const tools_1 = require("./tools");
/**
* It takes a target object and a property name, and returns the property name and value of the
* property that matches the given property name
*
* @param target - The object to search for the property.
* @param {string | symbol} prop - The property name to find.
*
* @returns An object with a key and value.
*/
const find = (target, prop) => {
var _a, _b;
if (typeof prop === 'symbol')
return { key: prop, value: target[prop] };
if (prop in target || '_' + prop in target)
return { key: prop, value: (_a = target[prop]) !== null && _a !== void 0 ? _a : target['_' + prop] };
for (const c of [tools_1.toSnakeCase, tools_1.toCamelCase, tools_1.toPascalCase, tools_1.toMacroCase, tools_1.toKebabCase, tools_1.toFlatCase, tools_1.toTrainCase, tools_1.toNoCase]) {
const property = { key: c(prop), value: (_b = target[c(prop)]) !== null && _b !== void 0 ? _b : target['_' + c(prop)] };
if (typeof property.value !== 'undefined')
return property;
}
return { key: prop, value: undefined };
};
/**
* It takes an object and a convention, and returns a new object with the same keys, but with the
* values transformed by the convention
*
* @param {any} data - any - The data to be converted.
* @param {Convention} $ - Convention
*
* @returns A function that takes two arguments, data and $, and returns a nested object.
*/
const nested = (data, $) => {
if (!!data && typeof data === 'object') {
if (Array.isArray(data))
return data.map((val) => nested(val, $));
else
return build(data, $);
}
else
return data;
};
/**
* It takes an object and a convention, and returns a new object that uses the convention to access its
* properties
*
* @param {object} data - object - The object to be converted.
* @param {Convention} $ - Convention
*
* @returns A Proxy object
*/
const build = (data, $) => {
return new Proxy(data, {
set(obj, prop, value) {
if (typeof prop === 'symbol')
obj[prop] = value;
else
obj[$.to(prop)] = value;
return true;
},
get(target, prop) {
if (typeof target[prop] === 'function')
return target[prop];
else {
const { key, value } = find(target, prop);
if (typeof key === 'string' && !$.is(key)) {
delete target[key];
if (typeof value !== 'undefined')
target[$.to(key)] = value;
}
if (typeof value === 'object') {
return nested(value, $);
}
else
return value;
}
},
});
};
/**
* It takes a JSON object and a lookup table, and returns a new JSON object with the keys in the lookup
* table replaced with their corresponding values
*
* @param {any} data - The data to be translated.
* @param table - The table of replacements.
*
* @returns A function that takes two arguments, data and table, and returns a new object.
*/
const lookup = (data, table) => {
let str = JSON.stringify(data);
for (const key in table) {
str = str.replace(new RegExp(`"${key}":`, 'g'), `"${table[key]}":`);
}
return JSON.parse(str);
};
exports.lookup = lookup;
class Modeler {
/**
* It takes an object and a naming convention and returns a new object with the same properties but
* with the naming convention applied
*
* @param {object} data - The data object that you want to convert.
* @param {NamingConvention} name - NamingConvention
*
* @returns The return value is the result of the nested function.
*/
static build(data, name) {
const $ = (0, tools_1.convention)(name);
return nested(data, $);
}
/**
* It takes an convention model and converts all properties
*
* @param {object} data - The data to be converted.
*/
static convert(data) {
JSON.stringify(data);
return data;
}
}
exports.Modeler = Modeler;
//# sourceMappingURL=modeler.js.map