sdj-esm
Version:
Self Described JSON - ESM
151 lines (150 loc) • 4.32 kB
JavaScript
/*
Copyright (c) 2023-2024 Will Rudolph <@willrudolph.com>
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { UUID } from "./func.std.js";
import { DATAJI_WORDS, DEF_DESC, ESDJ_LIMIT } from "../core/statics.js";
import { isArrayWithLen, isInfo } from "../core/validators.js";
import { cloneDeep, each, isBoolean, isNumber, isString, isUndefined, uniq } from "lodash-es";
import { validSDKey } from "../core/sdj-types.js";
export function newInfoJI(name = "", compress = false) {
return {
name,
uniqId: (!compress) ? UUID.GetNew() : UUID.GetCompress(),
created: Date.now(),
modified: Date.now()
};
}
export function blankInfoJI(name = "", compress = false) {
return {
name,
uniqId: UUID.GetEmpty(compress),
created: Date.now(),
modified: Date.now()
};
}
export function genSdKeyProps(inObj) {
const rtnJI = {};
each(inObj, (value, key) => {
if (isString(value) || isNumber(value) || isBoolean(value)) {
rtnJI[key] = value;
}
});
return rtnJI;
}
export function genEntityJI(entityJI) {
const rtnJI = {
sdId: entityJI.sdId,
sdKey: entityJI.sdKey
};
if (isArrayWithLen(entityJI.extendIds)) {
rtnJI.extendIds = uniq([...entityJI.extendIds]);
}
if (isArrayWithLen(entityJI.parentIds)) {
rtnJI.parentIds = uniq([...entityJI.parentIds]);
}
if (isArrayWithLen(entityJI.childIds)) {
rtnJI.childIds = uniq([...entityJI.childIds]);
}
if (isArrayWithLen(entityJI.sdItems)) {
rtnJI.sdItems = uniq([...entityJI.sdItems]);
}
if (entityJI.sdProps) {
rtnJI.sdProps = genSdKeyProps(entityJI.sdProps);
}
if (entityJI.limiter && entityJI.limiter !== ESDJ_LIMIT.NONE) {
rtnJI.limiter = entityJI.limiter;
}
return rtnJI;
}
export function genItemJI(itemJI) {
const rtnJI = {
sdId: itemJI.sdId,
sdKey: itemJI.sdKey,
type: itemJI.type
};
if (itemJI.limiter) {
rtnJI.limiter = itemJI.limiter;
}
return rtnJI;
}
export function genInfoJI(orgInfo) {
return {
name: orgInfo.name,
uniqId: orgInfo.uniqId,
created: orgInfo.created,
modified: orgInfo.modified
};
}
export function genDescriptionJI(descJI) {
const rtnJI = {
sdInfo: genInfoJI(descJI.sdInfo),
items: [],
graph: [],
};
if (descJI.lang) {
rtnJI.lang = descJI.lang;
}
if (isArrayWithLen(descJI.lexicons)) {
rtnJI.lexicons = [...descJI.lexicons];
}
each(descJI.items, (itemJI) => {
rtnJI.items.push(genItemJI(itemJI));
});
each(descJI.graph, (entityJI) => {
rtnJI.graph.push(genEntityJI(entityJI));
});
return rtnJI;
}
export function isBlankInfo(info, compress = false) {
let rtnVal = isInfo(info);
if (rtnVal) {
rtnVal = (info.uniqId === UUID.GetEmpty(compress));
}
return rtnVal;
}
export function blankDescriptionJI(descName) {
const newName = (validSDKey(descName)) ? descName : DEF_DESC;
return {
sdInfo: blankInfoJI(newName),
items: [],
graph: []
};
}
export function genKeyDataJI(orgData) {
const nonKeys = DATAJI_WORDS, rtnJI = {
sdId: orgData.sdId,
sdKey: orgData.sdKey,
};
each(orgData, (value, key) => {
if (nonKeys.indexOf(key) === -1 && value && !isUndefined(value)) {
rtnJI[key] = cloneDeep(value);
}
});
return rtnJI;
}
export function genDataJI(orgData) {
const rtnJI = genKeyDataJI(orgData);
if (orgData.sdInfo) {
rtnJI.sdInfo = genInfoJI(orgData.sdInfo);
}
if (orgData.sdChildren) {
each(orgData.sdChildren, (sdData) => {
if (!rtnJI.sdChildren) {
rtnJI.sdChildren = [];
}
rtnJI.sdChildren.push(genDataJI(sdData));
});
}
return rtnJI;
}
export function genIdArray(coreArray) {
const rtnArray = [];
each(coreArray, (coreSd) => {
rtnArray.push(coreSd.sdId);
});
return rtnArray;
}
//# sourceMappingURL=immutables.js.map