@swaptoshi/utils
Version:
Library containing generic utility functions for use with Swaptoshi-related software
170 lines • 6.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeProperty = removeProperty;
exports.getSchemaByPath = getSchemaByPath;
exports.getArrayTypeBySchema = getArrayTypeBySchema;
exports.valueToString = valueToString;
exports.getUpdatedProperties = getUpdatedProperties;
exports.getValueFromPath = getValueFromPath;
exports.updateValueFromPath = updateValueFromPath;
exports.pathExists = pathExists;
exports.serializer = serializer;
const cryptography = require("@klayr/cryptography");
function removeProperty(obj, propsToRemove) {
if (typeof obj !== 'object' || obj === null)
return obj;
const newObj = Array.isArray(obj) ? [] : {};
for (const [key, value] of Object.entries(obj)) {
if (propsToRemove.includes(key)) {
continue;
}
newObj[key] = removeProperty(value, propsToRemove);
}
return newObj;
}
function getSchemaByPath(schema, path) {
const pathParts = path.split('.').filter(Boolean);
let currentSchema = schema;
for (const part of pathParts) {
if ((currentSchema === null || currentSchema === void 0 ? void 0 : currentSchema.properties) && currentSchema.properties[part]) {
currentSchema = currentSchema.properties[part];
}
else if (currentSchema.type === 'array' && (currentSchema === null || currentSchema === void 0 ? void 0 : currentSchema.items)) {
currentSchema = currentSchema.items;
}
else {
throw new Error(`Schema not found for path: ${path}`);
}
}
return currentSchema;
}
function getArrayTypeBySchema(schemaDef) {
if (!(schemaDef === null || schemaDef === void 0 ? void 0 : schemaDef.items))
throw new Error('Unknown array type');
if (schemaDef.items.dataType)
return `${schemaDef.items.dataType}[]`;
if (schemaDef.items.type && schemaDef.items.type !== 'array')
return `${schemaDef.items.type}[]`;
if (schemaDef.items.type === 'array' && schemaDef.items.items)
return `${getArrayTypeBySchema(schemaDef.items)}[]`;
throw new Error('Unknown array type');
}
function valueToString(val) {
if (val === undefined)
return '';
if (typeof val === 'object' && val !== null)
return JSON.stringify(val);
return typeof val === 'bigint' ? val.toString() : String(val);
}
function getUpdatedProperties(oldObject, newObject, schema) {
const updatedProperties = [];
function deepCompare(oldVal, newVal, path, schemaDef) {
if (Array.isArray(oldVal) || Array.isArray(newVal)) {
const oldStr = valueToString(oldVal);
const newStr = JSON.stringify(newVal);
if (oldStr !== newStr) {
const type = getArrayTypeBySchema(schemaDef);
updatedProperties.push({ path, old: oldStr, new: newStr, type });
}
return;
}
if (typeof newVal === 'object' && newVal !== null) {
const oldValue = typeof oldVal === 'object' && oldVal !== null ? oldVal : {};
const keys = new Set([...Object.keys(oldValue), ...Object.keys(newVal)]);
keys.forEach(key => {
const newSchema = getSchemaByPath(schema, path ? `${path}.${key}` : key);
deepCompare(oldValue[key], newVal[key], path ? `${path}.${key}` : key, newSchema);
});
return;
}
const oldStr = valueToString(oldVal);
const newStr = valueToString(newVal);
if (oldStr !== newStr) {
let type;
if (schemaDef === null || schemaDef === void 0 ? void 0 : schemaDef.dataType) {
type = schemaDef.dataType;
}
else if ((schemaDef === null || schemaDef === void 0 ? void 0 : schemaDef.type) === 'array') {
type = getArrayTypeBySchema(schemaDef);
}
else if (schemaDef === null || schemaDef === void 0 ? void 0 : schemaDef.type) {
type = schemaDef.type;
}
else {
throw new Error(`Unknown type for path: ${path}`);
}
updatedProperties.push({ path, old: oldStr, new: newStr, type });
}
}
deepCompare(oldObject, newObject, '', schema);
return updatedProperties;
}
function getValueFromPath(obj, path) {
return path.split('.').reduce((acc, part) => (acc && typeof acc === 'object' ? acc[part] : undefined), obj);
}
function updateValueFromPath(obj, path, value) {
const ret = { ...obj };
const parts = path.split('.');
let current = ret;
for (let i = 0; i < parts.length - 1; i += 1) {
const part = parts[i];
if (!current[part] || typeof current[part] !== 'object') {
current[part] = {};
}
current = current[part];
}
current[parts[parts.length - 1]] = value;
return ret;
}
function pathExists(obj, path) {
let current = obj;
return path.split('.').every(part => {
if (typeof current !== 'object' || current === null || !(part in current)) {
return false;
}
current = current[part];
return true;
});
}
function serializer(data, schema) {
if (data === undefined)
return data;
return JSON.parse(JSON.stringify(data, (key, value) => {
if (Array.isArray(value)) {
return value.map(item => replacer(item, schema, key));
}
return replacer(value, schema, key);
}));
}
function replacer(value, schema, key, parentPath = '') {
const currentPath = parentPath && key ? `${parentPath}.${key}` : key;
if (typeof value === 'bigint') {
return value.toString();
}
if (Buffer.isBuffer(value)) {
if (schema && currentPath) {
const schemaForKey = getSchemaByPath(schema, currentPath);
if (schemaForKey && schemaForKey.dataType === 'string' && schemaForKey.format === 'klayr32') {
return cryptography.address.getKlayr32AddressFromAddress(value);
}
}
return value.toString('hex');
}
if (Array.isArray(value)) {
return value.map((item, index) => replacer(item, schema, index.toString(), currentPath));
}
if (typeof value === 'object' && value !== null) {
return Object.fromEntries(Object.entries(value).map(([nestedKey, nestedValue]) => [nestedKey, replacer(nestedValue, schema, nestedKey, currentPath)]));
}
if (typeof value === 'string' && value.length === 40) {
if (schema && currentPath) {
const schemaForKey = getSchemaByPath(schema, currentPath);
if (schemaForKey && schemaForKey.dataType === 'string' && schemaForKey.format === 'klayr32') {
return cryptography.address.getKlayr32AddressFromAddress(Buffer.from(value, 'hex'));
}
}
return value;
}
return value;
}
//# sourceMappingURL=object.js.map