@hestia-earth/schema-convert 11.0.0 → 11.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ import { SchemaType, JSON as HestiaJson, Cycle, Product, Site, Organisation, Term, ImpactAssessment } from '@hestia-earth/schema';
2
+ export declare const primaryProduct: (products?: Product[], defaultValue?: Product) => Product;
3
+ export declare const extendCycle: (nodes: HestiaJson<SchemaType>[], cycle: Cycle) => Cycle;
4
+ export declare const extendImpactAssessment: (_n: HestiaJson<SchemaType>[], impactAssessment: ImpactAssessment) => {
5
+ name: string;
6
+ version?: string;
7
+ versionDetails?: string;
8
+ organisation?: Organisation;
9
+ cycle?: Cycle;
10
+ endDate?: string;
11
+ startDate?: string;
12
+ site?: Site;
13
+ country?: Term;
14
+ region?: Term;
15
+ product?: Term;
16
+ functionalUnitQuantity?: number;
17
+ allocationMethod?: import("../../../dist/@hestia-earth/schema").ImpactAssessmentAllocationMethod;
18
+ source?: import("../../../dist/@hestia-earth/schema").Source;
19
+ emissionsResourceUse?: import("../../../dist/@hestia-earth/schema").Indicator[];
20
+ impacts?: import("../../../dist/@hestia-earth/schema").Indicator[];
21
+ endpoints?: import("../../../dist/@hestia-earth/schema").Indicator[];
22
+ organic?: boolean;
23
+ irrigated?: boolean;
24
+ productValue?: number;
25
+ autoGenerated?: boolean;
26
+ originalId?: string;
27
+ schemaVersion?: string;
28
+ added?: string[];
29
+ addedVersion?: string[];
30
+ updated?: string[];
31
+ updatedVersion?: string[];
32
+ aggregated?: boolean;
33
+ aggregatedVersion?: string;
34
+ aggregatedQualityScore?: number;
35
+ dataPrivate?: boolean;
36
+ type: SchemaType.ImpactAssessment;
37
+ id?: string;
38
+ };
39
+ export declare const extendSite: (nodes: HestiaJson<SchemaType>[], site: Site) => Site;
40
+ export declare const setDefaultValues: (nodes: HestiaJson<any>[]) => (HestiaJson<SchemaType> | Partial<Partial<HestiaJson<SchemaType.Cycle>>> | Partial<Partial<HestiaJson<SchemaType.ImpactAssessment>>> | Partial<Partial<HestiaJson<SchemaType.Site>>> | Partial<Partial<HestiaJson<SchemaType.Actor>>> | Partial<Partial<HestiaJson<SchemaType.Bibliography>>> | Partial<Partial<HestiaJson<SchemaType.Completeness>>> | Partial<Partial<HestiaJson<SchemaType.Emission>>> | Partial<Partial<HestiaJson<SchemaType.Indicator>>> | Partial<Partial<HestiaJson<SchemaType.Infrastructure>>> | Partial<Partial<HestiaJson<SchemaType.Input>>> | Partial<Partial<HestiaJson<SchemaType.Measurement>>> | Partial<Partial<HestiaJson<SchemaType.Organisation>>> | Partial<Partial<HestiaJson<SchemaType.Practice>>> | Partial<Partial<HestiaJson<SchemaType.Product>>> | Partial<Partial<HestiaJson<SchemaType.Property>>> | Partial<Partial<HestiaJson<SchemaType.Source>>> | Partial<Partial<HestiaJson<SchemaType.Term>>> | Partial<Partial<HestiaJson<SchemaType.Transformation>>> | Partial<Partial<HestiaJson<SchemaType.Transport>>>)[];
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var _a;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.setDefaultValues = exports.extendSite = exports.extendImpactAssessment = exports.extendCycle = exports.primaryProduct = void 0;
16
+ var schema_1 = require("@hestia-earth/schema");
17
+ var utils_1 = require("./utils");
18
+ var findLinkedNode = function (nodes, type, node) {
19
+ return node ? (nodes.find(function (v) {
20
+ return (v.type === type && v.id === node.id) || (v['@type'] === type && v['@id'] === node['@id']);
21
+ }) || node) : undefined;
22
+ };
23
+ var siteLocationName = function (region, country) { return [
24
+ region === null || region === void 0 ? void 0 : region.name,
25
+ // make sure country doesnt appear in region already, if so don't add it
26
+ (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
27
+ ].filter(Boolean).join(', '); };
28
+ exports.primaryProduct = function (products, defaultValue) {
29
+ if (defaultValue === void 0) { defaultValue = {}; }
30
+ return (products && products.length ? (products.find(function (product) { return product.primary || product.economicValueShare > 50; }) || products[0]) : null) || defaultValue;
31
+ };
32
+ var defaultName = function (product, country, region, endDate, treatment, description) {
33
+ return [
34
+ (product === null || product === void 0 ? void 0 : product.name) || 'No Product',
35
+ siteLocationName(region, country),
36
+ endDate,
37
+ treatment,
38
+ utils_1.ellipsis(description, 30)
39
+ ].filter(Boolean).join(' - ');
40
+ };
41
+ var cycleDefaultName = function (_a, site, product) {
42
+ var endDate = _a.endDate, treatment = _a.treatment, description = _a.description;
43
+ return defaultName(product === null || product === void 0 ? void 0 : product.term, site === null || site === void 0 ? void 0 : site.country, site === null || site === void 0 ? void 0 : site.region, endDate, treatment, description);
44
+ };
45
+ var cycleDurationDefault = 365;
46
+ var calculateCycleDuration = function (_a) {
47
+ var cycleDuration = _a.cycleDuration, startDate = _a.startDate, endDate = _a.endDate;
48
+ return (!cycleDuration || cycleDuration === cycleDurationDefault) &&
49
+ startDate && endDate && (new Date(startDate).getTime() < new Date(endDate).getTime());
50
+ };
51
+ var cycleDefaultDuration = function (_a) {
52
+ var startDate = _a.startDate, endDate = _a.endDate;
53
+ return utils_1.diffInDays(startDate, endDate);
54
+ };
55
+ exports.extendCycle = function (nodes, cycle) {
56
+ var product = exports.primaryProduct(cycle.products);
57
+ var site = findLinkedNode(nodes, schema_1.SchemaType.Site, cycle.site);
58
+ return __assign(__assign(__assign({}, cycle), { name: cycleDefaultName(cycle, site, product) }), (calculateCycleDuration(cycle) ? { cycleDuration: cycleDefaultDuration(cycle) } : {}));
59
+ };
60
+ var impactAssessmentDefaultName = function (_a) {
61
+ var product = _a.product, country = _a.country, region = _a.region, endDate = _a.endDate;
62
+ return defaultName(product, country, region, endDate);
63
+ };
64
+ exports.extendImpactAssessment = function (_n, impactAssessment) { return (__assign(__assign({}, impactAssessment), { name: impactAssessment.name || impactAssessmentDefaultName(impactAssessment) })); };
65
+ var siteDefaultName = function (_a, organisation) {
66
+ var siteType = _a.siteType, region = _a.region, country = _a.country, description = _a.description;
67
+ return [
68
+ siteType ? utils_1.keyToLabel(siteType) : null,
69
+ organisation === null || organisation === void 0 ? void 0 : organisation.name,
70
+ siteLocationName(region, country),
71
+ utils_1.ellipsis(description, 30)
72
+ ].filter(Boolean).join(' - ');
73
+ };
74
+ exports.extendSite = function (nodes, site) {
75
+ var org = findLinkedNode(nodes, schema_1.SchemaType.Organisation, site.organisation);
76
+ return __assign(__assign({}, site), { name: siteDefaultName(site, org) });
77
+ };
78
+ var extendNodeType = (_a = {},
79
+ _a[schema_1.SchemaType.Cycle] = exports.extendCycle,
80
+ _a[schema_1.SchemaType.ImpactAssessment] = exports.extendImpactAssessment,
81
+ _a[schema_1.SchemaType.Site] = exports.extendSite,
82
+ _a);
83
+ var extendNode = function (nodes) { return function (node) {
84
+ return node.type in extendNodeType ?
85
+ utils_1.reduceUndefinedValues(extendNodeType[node.type](nodes, node), true) :
86
+ node;
87
+ }; };
88
+ exports.setDefaultValues = function (nodes) { return nodes.map(extendNode(nodes)); };
package/json.js CHANGED
@@ -86,6 +86,7 @@ var wkt_1 = require("@terraformer/wkt");
86
86
  var schema_1 = require("@hestia-earth/schema");
87
87
  var types_1 = require("@hestia-earth/json-schema/types");
88
88
  var utils_1 = require("./utils");
89
+ var default_values_1 = require("./default-values");
89
90
  var IGNORE_FIELD_KEY = '-';
90
91
  var VALUE_TYPE_KEY = 'valueType';
91
92
  var DEFAULT_ARRAY_DELIMITER = ';';
@@ -462,13 +463,16 @@ var convetToNode = function (schemas) { return function (data) {
462
463
  * @returns A list of JSON-LD content.
463
464
  */
464
465
  exports.toJson = function (schemas, content) { return __awaiter(void 0, void 0, void 0, function () {
466
+ var nodes;
465
467
  return __generator(this, function (_a) {
466
468
  switch (_a.label) {
467
469
  case 0: return [4 /*yield*/, csvtojson({
468
470
  delimiter: 'auto',
469
471
  ignoreColumns: /^$/
470
472
  }).fromString(content)];
471
- case 1: return [2 /*return*/, (_a.sent()).flatMap(convetToNode(schemas))];
473
+ case 1:
474
+ nodes = (_a.sent()).flatMap(convetToNode(schemas));
475
+ return [2 /*return*/, default_values_1.setDefaultValues(nodes)];
472
476
  }
473
477
  });
474
478
  }); };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "11.0.0",
3
+ "version": "11.2.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
package/utils.d.ts CHANGED
@@ -7,3 +7,6 @@ export declare const isEmpty: (value: any, minKeys?: number) => boolean;
7
7
  export declare const nonEmptyValue: (value: any) => boolean;
8
8
  export declare const nonEmptyNode: (node: any) => boolean;
9
9
  export declare const reduceUndefinedValues: <T>(obj: T, allowNull?: boolean) => Partial<T>;
10
+ export declare const ellipsis: (text?: string, maxlength?: number) => string;
11
+ export declare const keyToLabel: (key: string) => string;
12
+ export declare const diffInDays: (date1: Date | string, date2: Date | string) => number;
package/utils.js CHANGED
@@ -11,7 +11,7 @@ var __assign = (this && this.__assign) || function () {
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.reduceUndefinedValues = exports.nonEmptyNode = exports.nonEmptyValue = exports.isEmpty = exports.isNumber = exports.isBoolean = exports.isIri = exports.isExpandable = exports.omit = void 0;
14
+ exports.diffInDays = exports.keyToLabel = exports.ellipsis = exports.reduceUndefinedValues = exports.nonEmptyNode = exports.nonEmptyValue = exports.isEmpty = exports.isNumber = exports.isBoolean = exports.isIri = exports.isExpandable = exports.omit = void 0;
15
15
  var unset = require('lodash.unset');
16
16
  exports.omit = function (data, keys) {
17
17
  var obj = __assign({}, data);
@@ -53,3 +53,14 @@ exports.reduceUndefinedValues = function (obj, allowNull) {
53
53
  return __assign(__assign({}, prev), (isUndefined(value, allowNull) ? {} : (_a = {}, _a[key] = value, _a)));
54
54
  }, {});
55
55
  };
56
+ exports.ellipsis = function (text, maxlength) {
57
+ if (text === void 0) { text = ''; }
58
+ if (maxlength === void 0) { maxlength = 20; }
59
+ return text.length > maxlength ? text.substring(0, maxlength) + "..." : text;
60
+ };
61
+ exports.keyToLabel = function (key) {
62
+ return "" + key[0].toUpperCase() + key.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/([_])([a-zA-Z])/g, function (g) { return " " + g[1].toUpperCase(); }).substring(1);
63
+ };
64
+ exports.diffInDays = function (date1, date2) {
65
+ return Math.abs(Math.round((new Date(date2).getTime() - new Date(date1).getTime()) / (24 * 60 * 60 * 1000)));
66
+ };