@hestia-earth/schema-convert
Advanced tools
Comparing version 23.5.0 to 23.6.0
@@ -82,3 +82,3 @@ "use strict"; | ||
content = JSON.parse(data.trim()); | ||
nodes = Array.isArray(content) ? content : ('nodes' in content ? content.nodes : [content]); | ||
nodes = Array.isArray(content) ? content : 'nodes' in content ? content.nodes : [content]; | ||
if (!pivot) return [3 /*break*/, 3]; | ||
@@ -85,0 +85,0 @@ return [4 /*yield*/, (0, csv_pivot_1.toCsvPivot)(nodes)]; |
@@ -43,8 +43,12 @@ "use strict"; | ||
var omitKeys = __spreadArray([ | ||
'@context', 'schemaVersion', 'dataPrivate' | ||
], __read([ | ||
'bibliography', 'site', 'cycle', 'impactAssessment', 'defaultSource', 'source' | ||
].flatMap(function (k) { return ["".concat(k, ".id"), "".concat(k, ".type"), "".concat(k, ".@type")]; })), false); | ||
'@context', | ||
'schemaVersion', | ||
'dataPrivate' | ||
], __read(['bibliography', 'site', 'cycle', 'impactAssessment', 'defaultSource', 'source'].flatMap(function (k) { return [ | ||
"".concat(k, ".id"), | ||
"".concat(k, ".type"), | ||
"".concat(k, ".@type") | ||
]; })), false); | ||
var convertValue = function (value) { | ||
return Array.isArray(value) ? value.map(String).join(';') : (typeof value === 'object' ? JSON.stringify(value) : value); | ||
return Array.isArray(value) ? value.map(String).join(';') : typeof value === 'object' ? JSON.stringify(value) : value; | ||
}; | ||
@@ -67,25 +71,28 @@ var parseValue = function (value) { | ||
exports.toCsv = toCsv; | ||
var arrayKeys = function (node) { return Object.keys(node) | ||
.filter(function (key) { | ||
return Array.isArray(node[key]) && | ||
node[key].length && | ||
typeof node[key][0] === 'object' && | ||
('type' in node[key][0] || '@type' in node[key][0]); | ||
}); }; | ||
var termId = function (term) { return term ? term['@id'] || term.id : undefined; }; | ||
var convertTerm = function (term) { return term ? { methodModel: termId(term) } : {}; }; | ||
var pivotList = function (values) { return values.reduce(function (prev, _a) { | ||
var _b; | ||
var _t = _a["@type"], term = _a.term, methodModel = _a.methodModel, value = _a.value; | ||
var key = termId(term); | ||
var data = (0, utils_1.reduceUndefinedValues)(__assign(__assign({}, convertTerm(methodModel)), { value: convertValue(value) })); | ||
return __assign(__assign({}, prev), (key && Object.keys(data).length ? (_b = {}, _b[key] = (prev[key] || []).concat([data]), _b) : {})); | ||
}, {}); }; | ||
var arrayKeys = function (node) { | ||
return Object.keys(node).filter(function (key) { | ||
return Array.isArray(node[key]) && | ||
node[key].length && | ||
typeof node[key][0] === 'object' && | ||
('type' in node[key][0] || '@type' in node[key][0]); | ||
}); | ||
}; | ||
var termId = function (term) { return (term ? term['@id'] || term.id : undefined); }; | ||
var convertTerm = function (term) { return (term ? { methodModel: termId(term) } : {}); }; | ||
var pivotList = function (values) { | ||
return values.reduce(function (prev, _a) { | ||
var _b; | ||
var _t = _a["@type"], term = _a.term, methodModel = _a.methodModel, value = _a.value; | ||
var key = termId(term); | ||
var data = (0, utils_1.reduceUndefinedValues)(__assign(__assign({}, convertTerm(methodModel)), { value: convertValue(value) })); | ||
return __assign(__assign({}, prev), (key && Object.keys(data).length ? (_b = {}, _b[key] = (prev[key] || []).concat([data]), _b) : {})); | ||
}, {}); | ||
}; | ||
var pivotNode = function (node) { | ||
var keys = arrayKeys(node); | ||
return (0, utils_1.reduceUndefinedValues)(__assign(__assign({}, (0, utils_1.omit)(node, __spreadArray(__spreadArray([], __read(omitKeys), false), __read(keys), false))), (keys.reduce(function (prev, key) { | ||
return (0, utils_1.reduceUndefinedValues)(__assign(__assign({}, (0, utils_1.omit)(node, __spreadArray(__spreadArray([], __read(omitKeys), false), __read(keys), false))), keys.reduce(function (prev, key) { | ||
var _a; | ||
var value = pivotList(node[key]); | ||
return __assign(__assign({}, prev), (Object.keys(value).length ? (_a = {}, _a[key] = value, _a) : {})); | ||
}, {})))); | ||
}, {}))); | ||
}; | ||
@@ -92,0 +99,0 @@ exports.pivotNode = pivotNode; |
31
csv.js
@@ -59,10 +59,6 @@ "use strict"; | ||
default: function (value) { return "".concat(value); }, | ||
object: function (value) { return value === null | ||
? noValue | ||
: value instanceof Date | ||
? value.toJSON() | ||
: JSON.stringify(value); } | ||
object: function (value) { return (value === null ? noValue : value instanceof Date ? value.toJSON() : JSON.stringify(value)); } | ||
}; | ||
var escapeField = function (value) { | ||
var convertType = (typeof value) in convertValue ? typeof value : 'default'; | ||
var convertType = typeof value in convertValue ? typeof value : 'default'; | ||
var val = "".concat(convertValue[convertType](value)).replace(/"/g, '""').replace(/\r/g, '').replace('\n', ''); | ||
@@ -78,8 +74,8 @@ return val.includes(exports.csvColSep) ? "\"".concat(val, "\"") : val; | ||
var headers = getHeaders(value, useBrackets); | ||
return Array.isArray(value) ? | ||
headers.map(function (header) { | ||
return Array.isArray(value) | ||
? headers.map(function (header) { | ||
var _a = __read(header.split('.')), index = _a[0], rest = _a.slice(1); | ||
return "".concat(key).concat(useBrackets ? "[".concat(index, "]") : ".".concat(index), ".").concat(rest.join('.')); | ||
}) : | ||
headers.map(function (header) { return "".concat(key, ".").concat(header); }); | ||
}) | ||
: headers.map(function (header) { return "".concat(key, ".").concat(header); }); | ||
}; | ||
@@ -119,5 +115,5 @@ var getHeaders = function (node, useBrackets) { | ||
}, {}); | ||
var allHeaders = selectedHeaders.length ? | ||
selectedHeaders : | ||
Object.keys(headersByType).flatMap(function (type) { return headersByType[type].map(function (key) { return "".concat(typeToColumnName(type), ".").concat(key); }); }); | ||
var allHeaders = selectedHeaders.length | ||
? selectedHeaders | ||
: Object.keys(headersByType).flatMap(function (type) { return headersByType[type].map(function (key) { return "".concat(typeToColumnName(type), ".").concat(key); }); }); | ||
return [ | ||
@@ -129,3 +125,4 @@ allHeaders.join(exports.csvColSep), | ||
.map(function (node) { | ||
return allHeaders.map(function (header) { | ||
return allHeaders | ||
.map(function (header) { | ||
var _a = __read(header.split('.')), type = _a[0], rest = _a.slice(1); | ||
@@ -135,6 +132,8 @@ var ntype = node.type || node['@type']; | ||
return Array.isArray(value) ? escapeField(value.join(';')) : escapeField(value); | ||
}).join(exports.csvColSep); | ||
}).join('\n') | ||
}) | ||
.join(exports.csvColSep); | ||
}) | ||
.join('\n') | ||
].join('\n'); | ||
}; | ||
exports.toCsv = toCsv; |
@@ -9,4 +9,5 @@ import { SchemaType, JSON as HestiaJson, Cycle, Product, Site, Organisation, Term, ImpactAssessment } from '@hestia-earth/schema'; | ||
export declare const impactAssessmentDefaultName: ({ product, country, region, endDate }: Partial<ImpactAssessment>) => string; | ||
export declare const extendImpactAssessment: (_n: HestiaJson<SchemaType>[], impactAssessment: ImpactAssessment) => { | ||
export declare const extendImpactAssessment: (nodes: HestiaJson<SchemaType>[], impactAssessment: ImpactAssessment) => { | ||
name: string; | ||
source: import("@hestia-earth/schema").Source; | ||
version?: string; | ||
@@ -24,3 +25,2 @@ versionDetails?: string; | ||
organisation?: Organisation; | ||
source?: import("@hestia-earth/schema").Source; | ||
emissionsResourceUse?: import("@hestia-earth/schema").Indicator[]; | ||
@@ -27,0 +27,0 @@ impacts?: import("@hestia-earth/schema").Indicator[]; |
@@ -19,15 +19,21 @@ "use strict"; | ||
var findLinkedNode = function (nodes, type, node) { | ||
return node ? (nodes.find(function (v) { | ||
return (v.type === type && v.id === node.id) || (v['@type'] === type && v['@id'] === node['@id']); | ||
}) || node) : undefined; | ||
return node | ||
? nodes.find(function (v) { return (v.type === type && v.id === node.id) || (v['@type'] === type && v['@id'] === node['@id']); }) || node | ||
: undefined; | ||
}; | ||
var siteLocationName = function (region, country) { return [ | ||
region === null || region === void 0 ? void 0 : region.name, | ||
// make sure country doesnt appear in region already, if so don't add it | ||
(country === null || country === void 0 ? void 0 : country.name) ? ((region === null || region === void 0 ? void 0 : region.name) && region.name.includes(country === null || country === void 0 ? void 0 : country.name) ? undefined : country === null || country === void 0 ? void 0 : country.name) : undefined | ||
].filter(Boolean).join(', '); }; | ||
var siteLocationName = function (region, country) { | ||
return [ | ||
region === null || region === void 0 ? void 0 : region.name, | ||
// make sure country doesnt appear in region already, if so don't add it | ||
(country === null || country === void 0 ? void 0 : country.name) ? ((region === null || region === void 0 ? void 0 : region.name) && region.name.includes(country === null || country === void 0 ? void 0 : country.name) ? undefined : country === null || country === void 0 ? void 0 : country.name) : undefined | ||
] | ||
.filter(Boolean) | ||
.join(', '); | ||
}; | ||
exports.siteLocationName = siteLocationName; | ||
var primaryProduct = function (products, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = {}; } | ||
return (products && products.length ? (products.find(function (product) { return product.primary || product.economicValueShare > 50; }) || products[0]) : null) || defaultValue; | ||
return (products && products.length | ||
? products.find(function (product) { return product.primary || product.economicValueShare > 50; }) || products[0] | ||
: null) || defaultValue; | ||
}; | ||
@@ -42,3 +48,5 @@ exports.primaryProduct = primaryProduct; | ||
(0, utils_1.ellipsis)(description, 30) | ||
].filter(Boolean).join(' - '); | ||
] | ||
.filter(Boolean) | ||
.join(' - '); | ||
}; | ||
@@ -54,3 +62,5 @@ var cycleDefaultName = function (_a, site, product) { | ||
return (!cycleDuration || cycleDuration === cycleDurationDefault) && | ||
startDate && endDate && (new Date(startDate).getTime() < new Date(endDate).getTime()); | ||
startDate && | ||
endDate && | ||
new Date(startDate).getTime() < new Date(endDate).getTime(); | ||
}; | ||
@@ -74,3 +84,6 @@ exports.calculateCycleDuration = calculateCycleDuration; | ||
exports.impactAssessmentDefaultName = impactAssessmentDefaultName; | ||
var extendImpactAssessment = function (_n, impactAssessment) { return (__assign(__assign({}, impactAssessment), { name: impactAssessment.name || (0, exports.impactAssessmentDefaultName)(impactAssessment) })); }; | ||
var extendImpactAssessment = function (nodes, impactAssessment) { | ||
var _a; | ||
return (__assign(__assign({}, impactAssessment), { name: impactAssessment.name || (0, exports.impactAssessmentDefaultName)(impactAssessment), source: impactAssessment.source || ((_a = findLinkedNode(nodes, schema_1.SchemaType.Cycle, impactAssessment.cycle)) === null || _a === void 0 ? void 0 : _a.defaultSource) })); | ||
}; | ||
exports.extendImpactAssessment = extendImpactAssessment; | ||
@@ -84,3 +97,5 @@ var siteDefaultName = function (_a, organisation) { | ||
(0, utils_1.ellipsis)(description, 30) | ||
].filter(Boolean).join(' - '); | ||
] | ||
.filter(Boolean) | ||
.join(' - '); | ||
}; | ||
@@ -99,7 +114,5 @@ exports.siteDefaultName = siteDefaultName; | ||
var extendNode = function (nodes) { return function (node) { | ||
return node.type in extendNodeType ? | ||
(0, utils_1.reduceUndefinedValues)(extendNodeType[node.type](nodes, node), true) : | ||
node; | ||
return node.type in extendNodeType ? (0, utils_1.reduceUndefinedValues)(extendNodeType[node.type](nodes, node), true) : node; | ||
}; }; | ||
var setDefaultValues = function (nodes) { return nodes.map(extendNode(nodes)); }; | ||
exports.setDefaultValues = setDefaultValues; |
{ | ||
"name": "@hestia-earth/schema-convert", | ||
"version": "23.5.0", | ||
"version": "23.6.0", | ||
"description": "Hestia Schema Converters", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
30
utils.js
@@ -22,3 +22,5 @@ "use strict"; | ||
exports.omit = omit; | ||
var isExpandable = function (val) { return !!val && typeof val === 'object' && (Array.isArray(val) ? val.every(exports.isExpandable) : Object.keys(val).length); }; | ||
var isExpandable = function (val) { | ||
return !!val && typeof val === 'object' && (Array.isArray(val) ? val.every(exports.isExpandable) : Object.keys(val).length); | ||
}; | ||
exports.isExpandable = isExpandable; | ||
@@ -36,7 +38,7 @@ var isIri = function (value) { return (value || '').startsWith('http'); }; | ||
typeof value === 'undefined' || | ||
(typeof value === 'object' ? | ||
(Array.isArray(value) ? | ||
!value.length : | ||
Object.keys(value).filter(function (key) { return key !== 'type'; }).length < minKeys) : | ||
value === ''); | ||
(typeof value === 'object' | ||
? Array.isArray(value) | ||
? !value.length | ||
: Object.keys(value).filter(function (key) { return key !== 'type'; }).length < minKeys | ||
: value === ''); | ||
}; | ||
@@ -47,7 +49,5 @@ exports.isEmpty = isEmpty; | ||
var nonEmptyNode = function (node) { | ||
return typeof node === 'object' ? | ||
Object.keys(node) | ||
.filter(function (key) { return ignoreKey(key) && !(0, exports.isEmpty)(node[key]); }) | ||
.length > 0 : | ||
(0, exports.nonEmptyValue)(node); | ||
return typeof node === 'object' | ||
? Object.keys(node).filter(function (key) { return ignoreKey(key) && !(0, exports.isEmpty)(node[key]); }).length > 0 | ||
: (0, exports.nonEmptyValue)(node); | ||
}; | ||
@@ -57,3 +57,4 @@ exports.nonEmptyNode = nonEmptyNode; | ||
return (value === null && !allowNull) || | ||
typeof value === 'undefined' || (typeof value === 'object' && value !== null && !(value instanceof Date) && !Object.keys(value).length); | ||
typeof value === 'undefined' || | ||
(typeof value === 'object' && value !== null && !(value instanceof Date) && !Object.keys(value).length); | ||
}; | ||
@@ -76,3 +77,6 @@ var reduceUndefinedValues = function (obj, allowNull) { | ||
var keyToLabel = function (key) { | ||
return "".concat(key[0].toUpperCase()).concat(key.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/([_])([a-zA-Z])/g, function (g) { return " ".concat(g[1].toUpperCase()); }).substring(1)); | ||
return "".concat(key[0].toUpperCase()).concat(key | ||
.replace(/([a-z])([A-Z])/g, '$1 $2') | ||
.replace(/([_])([a-zA-Z])/g, function (g) { return " ".concat(g[1].toUpperCase()); }) | ||
.substring(1)); | ||
}; | ||
@@ -79,0 +83,0 @@ exports.keyToLabel = keyToLabel; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67768
1438