@hestia-earth/schema-convert 24.2.0 → 24.3.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.
- package/csv.d.ts +21 -5
- package/csv.js +37 -11
- package/package.json +1 -1
package/csv.d.ts
CHANGED
|
@@ -6,13 +6,29 @@ export declare const csvColSep = ",";
|
|
|
6
6
|
* @returns headers as a list.
|
|
7
7
|
*/
|
|
8
8
|
export declare const headersFromCsv: (csv: string) => string[];
|
|
9
|
+
export interface IConvertCSVOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Include top-level existing nodes. Defaults to `true`.
|
|
12
|
+
*/
|
|
13
|
+
includeExising?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Use `[0]` notation for arrays instead of `.0`. Defaults to `false`.
|
|
16
|
+
*/
|
|
17
|
+
useBrackets?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* By default, internal properties are not included. Set to `true` to include them.
|
|
20
|
+
*/
|
|
21
|
+
includeAllHeaders?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Override list of headers in order. By default, compile all from nodes and sort as they appear.
|
|
24
|
+
*/
|
|
25
|
+
selectedHeaders?: string[];
|
|
26
|
+
}
|
|
9
27
|
/**
|
|
10
|
-
* CSV format
|
|
28
|
+
* Convert a list of Nodes to CSV format.
|
|
11
29
|
*
|
|
12
30
|
* @param nodes List of nodes to convert.
|
|
13
|
-
* @param
|
|
14
|
-
* @param useBrackets Use `[0]` notation for arrays instead of `.0`.
|
|
15
|
-
* @param selectedHeaders Override list of headers in order. By default, compile all from nodes and sort as they appear.
|
|
31
|
+
* @param options Conversion options.
|
|
16
32
|
* @returns CSV content formatted for upload on Hestia.
|
|
17
33
|
*/
|
|
18
|
-
export declare const toCsv: (nodes: any[],
|
|
34
|
+
export declare const toCsv: (nodes: any[], options?: IConvertCSVOptions) => string;
|
package/csv.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
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
|
+
};
|
|
2
13
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
14
|
var t = {};
|
|
4
15
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
@@ -37,6 +48,9 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
37
48
|
};
|
|
38
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
50
|
exports.toCsv = exports.headersFromCsv = exports.csvColSep = void 0;
|
|
51
|
+
var schema_1 = require("@hestia-earth/schema");
|
|
52
|
+
var json_schema_1 = require("@hestia-earth/json-schema");
|
|
53
|
+
var schema_utils_1 = require("@hestia-earth/json-schema/schema-utils");
|
|
40
54
|
var get = require('lodash.get');
|
|
41
55
|
var utils_1 = require("./utils");
|
|
42
56
|
var noValue = '-';
|
|
@@ -89,19 +103,30 @@ var isIndexedNode = function (headers) {
|
|
|
89
103
|
var topLevel = headers.filter(function (header) { return !header.includes('.'); });
|
|
90
104
|
return topLevel.some(function (header) { return header === '@id'; });
|
|
91
105
|
};
|
|
106
|
+
var defaultOptions = {
|
|
107
|
+
includeExising: true,
|
|
108
|
+
useBrackets: false,
|
|
109
|
+
includeAllHeaders: false,
|
|
110
|
+
selectedHeaders: []
|
|
111
|
+
};
|
|
112
|
+
var filterTermHeaders = function (schemas, header) {
|
|
113
|
+
var title = (0, schema_utils_1.schemaFromKey)(schemas, header).title;
|
|
114
|
+
return title !== schema_1.NodeType.Term || header.endsWith('id') || header.endsWith('name');
|
|
115
|
+
};
|
|
116
|
+
var filterHeaders = function (headers) {
|
|
117
|
+
var schemas = (0, json_schema_1.loadSchemas)();
|
|
118
|
+
var filterIncluded = (0, schema_utils_1.isCSVIncluded)(schemas);
|
|
119
|
+
return headers.filter(function (header) { return filterIncluded(header) && filterTermHeaders(schemas, header); });
|
|
120
|
+
};
|
|
92
121
|
/**
|
|
93
|
-
* CSV format
|
|
122
|
+
* Convert a list of Nodes to CSV format.
|
|
94
123
|
*
|
|
95
124
|
* @param nodes List of nodes to convert.
|
|
96
|
-
* @param
|
|
97
|
-
* @param useBrackets Use `[0]` notation for arrays instead of `.0`.
|
|
98
|
-
* @param selectedHeaders Override list of headers in order. By default, compile all from nodes and sort as they appear.
|
|
125
|
+
* @param options Conversion options.
|
|
99
126
|
* @returns CSV content formatted for upload on Hestia.
|
|
100
127
|
*/
|
|
101
|
-
var toCsv = function (nodes,
|
|
102
|
-
|
|
103
|
-
if (useBrackets === void 0) { useBrackets = false; }
|
|
104
|
-
if (selectedHeaders === void 0) { selectedHeaders = []; }
|
|
128
|
+
var toCsv = function (nodes, options) {
|
|
129
|
+
var _a = __assign(__assign({}, defaultOptions), options), includeExising = _a.includeExising, useBrackets = _a.useBrackets, includeAllHeaders = _a.includeAllHeaders, selectedHeaders = _a.selectedHeaders;
|
|
105
130
|
var headersByType = nodes.reduce(function (prev, _a) {
|
|
106
131
|
var type = _a.type, _t = _a["@type"], node = __rest(_a, ["type", '@type']);
|
|
107
132
|
type = type || _t;
|
|
@@ -110,9 +135,10 @@ var toCsv = function (nodes, includeExising, useBrackets, selectedHeaders) {
|
|
|
110
135
|
prev[type] = Array.from(new Set(__spreadArray(__spreadArray([], __read(prev[type]), false), __read((!includeExising && isIndexedNode(headers) ? [] : headers)), false)));
|
|
111
136
|
return prev;
|
|
112
137
|
}, {});
|
|
113
|
-
var
|
|
114
|
-
|
|
115
|
-
|
|
138
|
+
var headers = Object.keys(headersByType).flatMap(function (type) {
|
|
139
|
+
return headersByType[type].map(function (key) { return "".concat(typeToColumnName(type), ".").concat(key); });
|
|
140
|
+
});
|
|
141
|
+
var allHeaders = selectedHeaders.length ? selectedHeaders : includeAllHeaders ? headers : filterHeaders(headers);
|
|
116
142
|
return [
|
|
117
143
|
allHeaders.join(exports.csvColSep),
|
|
118
144
|
nodes
|