@hv-kit/hexpress
Version:
facilitates typescript backend development with express
139 lines • 5.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetBodyForUpdate2 = exports.GetBodyForUpdate = exports.MapAll = exports.RemovePointer = void 0;
const hexpress_config_json_1 = __importDefault(require("../../../../hexpress.config.json"));
function RemovePointer(element) {
let result;
if (typeof element === 'object') {
result = (Array.isArray(element) === true) ? [
...element,
] : Object.assign({}, element);
}
else {
result = element;
}
if (typeof result === 'object') {
const res = result;
Object.keys(res).forEach((key) => {
result[key] = RemovePointer(res[key]);
});
}
return result;
}
exports.RemovePointer = RemovePointer;
function MapAll(element, mapper = (data) => data, keysExcept = []) {
let result;
if (typeof element === 'object'
&& element) {
result = element;
Object.keys(result).forEach((key) => {
if (!keysExcept.includes(key)) {
result[key] = MapAll(result[key], mapper);
}
});
}
else {
result = mapper(element);
}
return result;
}
exports.MapAll = MapAll;
function cmpForGetBodyForUpdate(initialBody, newBody, key, nullableProperties = []) {
if (initialBody instanceof Date
&& !(newBody instanceof Date)) {
newBody = undefined;
}
if (typeof initialBody === 'object'
&& Array.isArray(initialBody) === false
&& typeof newBody === 'object'
&& Array.isArray(newBody) === false
&& (newBody)) {
initialBody = GetBodyForUpdate(initialBody, newBody, nullableProperties);
}
else if (typeof initialBody === 'object'
&& Array.isArray(initialBody) === true
&& typeof newBody === 'object'
&& Array.isArray(newBody) === true
&& (newBody)) {
initialBody = initialBody.map((value, index) => GetBodyForUpdate(value[0], newBody[0], nullableProperties));
}
else if ((initialBody != newBody
&& (newBody
|| typeof (newBody) === 'boolean')) || (nullableProperties.includes(key)
&& newBody === null)) {
initialBody = newBody;
}
return initialBody;
}
function GetBodyForUpdate(initialBody, newBody, nullableProperties = []) {
let result;
if (hexpress_config_json_1.default.debug) {
console.log('----> GetBodyForUpdate > initialBody:: ', initialBody);
console.log('----> GetBodyForUpdate > newBody:: ', newBody);
}
if (typeof initialBody === 'object'
&& Array.isArray(initialBody) === false
&& typeof newBody === 'object'
&& Array.isArray(newBody) === false) {
result = initialBody;
Object.keys(newBody).forEach((key, index) => {
result[key] = cmpForGetBodyForUpdate(initialBody[key], newBody[key], key, nullableProperties);
});
}
if (typeof initialBody === typeof newBody
&& initialBody != newBody) {
result = newBody;
}
else {
result = initialBody;
}
if (hexpress_config_json_1.default.debug) {
console.log('----> GetBodyForUpdate > result:: ', result);
}
return result;
}
exports.GetBodyForUpdate = GetBodyForUpdate;
function GetBodyForUpdate2(initialBody, newBody) {
let result;
if (hexpress_config_json_1.default.debug) {
console.log('----> GetBodyForUpdate2 > initialBody:: ', initialBody);
console.log('----> GetBodyForUpdate2 > newBody:: ', newBody);
}
if (typeof initialBody === 'object'
&& Array.isArray(initialBody) === false
&& typeof newBody === 'object'
&& Array.isArray(newBody) === false) {
result = initialBody;
if (hexpress_config_json_1.default.debug) {
console.log('----> GetBodyForUpdate2 > Object.keys(newBody):: ', Object.keys(newBody));
console.log('----> GetBodyForUpdate2 > Object.values(newBody):: ', Object.values(newBody));
}
Object.keys(newBody).forEach((key, index) => {
if (hexpress_config_json_1.default.debug) {
console.log('----> GetBodyForUpdate2 > index::', index, ' - key:: ', key);
}
if (initialBody[key] instanceof Date
&& !(newBody[key] instanceof Date)) {
newBody[key] = undefined;
}
if ((newBody[key])) {
result[key] = newBody[key];
}
});
}
else {
result = initialBody;
}
return result;
}
exports.GetBodyForUpdate2 = GetBodyForUpdate2;
exports.default = {
RemovePointer,
MapAll,
GetBodyForUpdate,
GetBodyForUpdate2,
};
//# sourceMappingURL=json.js.map