@hestia-earth/schema-convert 7.3.0 → 7.5.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/json.d.ts +3 -2
- package/json.js +22 -12
- package/package.json +1 -1
package/json.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SchemaType } from '@hestia-earth/schema';
|
|
2
|
-
import { definitions } from '@hestia-earth/json-schema';
|
|
2
|
+
import { definitions } from '@hestia-earth/json-schema/types';
|
|
3
3
|
export interface INode {
|
|
4
4
|
type: SchemaType;
|
|
5
5
|
id?: string;
|
|
@@ -24,7 +24,8 @@ export interface ICSVError {
|
|
|
24
24
|
}
|
|
25
25
|
export declare const throwCSVError: <T extends ICSVError>(error: T) => never;
|
|
26
26
|
export declare const cleanStringValue: (value: string) => string;
|
|
27
|
-
export declare const formatNode: (schemas: definitions, type: SchemaType, data: ICSVContent, ignoreInternal: boolean) => any;
|
|
27
|
+
export declare const formatNode: (schemas: definitions, type: SchemaType, data: ICSVContent, ignoreInternal: boolean, top?: boolean) => any;
|
|
28
|
+
export declare const filterEmptyNode: (schemas: definitions, { schemaVersion, ...node }: any) => boolean;
|
|
28
29
|
/**
|
|
29
30
|
* Convert CSV to Hestia JSON-LD format.
|
|
30
31
|
*
|
package/json.js
CHANGED
|
@@ -58,14 +58,22 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
58
58
|
return t;
|
|
59
59
|
};
|
|
60
60
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
61
|
-
exports.toJson = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = void 0;
|
|
61
|
+
exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = void 0;
|
|
62
62
|
var csvtojson = require("csvtojson");
|
|
63
63
|
var schema_1 = require("@hestia-earth/schema");
|
|
64
|
-
var
|
|
64
|
+
var types_1 = require("@hestia-earth/json-schema/types");
|
|
65
65
|
var utils_1 = require("./utils");
|
|
66
66
|
var IGNORE_FIELD_KEY = '-';
|
|
67
67
|
var VALUE_TYPE_KEY = 'valueType';
|
|
68
|
-
var
|
|
68
|
+
var DEFAULT_ARRAY_DELIMITER = ';';
|
|
69
|
+
var arrayDelimiter = function () {
|
|
70
|
+
try {
|
|
71
|
+
return process.env.CSV_ARRAY_DELIMITER || DEFAULT_ARRAY_DELIMITER;
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
return DEFAULT_ARRAY_DELIMITER;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
69
77
|
var safeParseJSON = function (obj) {
|
|
70
78
|
// throw already handled error or throw a generic invalid-format error
|
|
71
79
|
var data;
|
|
@@ -159,7 +167,7 @@ var propertyTypeToValue = {
|
|
|
159
167
|
},
|
|
160
168
|
array: function (value, schemas, _a, _i) {
|
|
161
169
|
var items = _a.items;
|
|
162
|
-
return (Array.isArray(value) ? value : value.split(
|
|
170
|
+
return (Array.isArray(value) ? value : value.split(arrayDelimiter()))
|
|
163
171
|
.map(handleArrayError(function (val) { return items ? ('$ref' in items ?
|
|
164
172
|
propertyTypeToValue.object(val, schemas, items, _i) :
|
|
165
173
|
Array.isArray(items.type) ?
|
|
@@ -208,7 +216,7 @@ var setDefaultProperty = function (prop, def, node, ignoreInternal) {
|
|
|
208
216
|
};
|
|
209
217
|
var getDefaultProperties = function (schema, node, ignoreInternal) {
|
|
210
218
|
if (ignoreInternal === void 0) { ignoreInternal = false; }
|
|
211
|
-
return
|
|
219
|
+
return types_1.defaultProperties(schema)
|
|
212
220
|
.map(function (property) {
|
|
213
221
|
var def = schema.properties[property];
|
|
214
222
|
return setDefaultProperty(property, def, node, ignoreInternal) ? { property: property, defaultValue: def.default } : null;
|
|
@@ -227,10 +235,11 @@ var nodeType = function (id, type, existingNode) {
|
|
|
227
235
|
if (existingNode === void 0) { existingNode = false; }
|
|
228
236
|
return type ? (schema_1.isTypeNode(type) ? (existingNode ? { '@type': type } : { id: id, type: type }) : { type: type }) : {};
|
|
229
237
|
};
|
|
230
|
-
var extendDataFromSchema = function (_a, schema, type, ignoreInternal) {
|
|
238
|
+
var extendDataFromSchema = function (_a, schema, type, ignoreInternal, top) {
|
|
231
239
|
if (ignoreInternal === void 0) { ignoreInternal = false; }
|
|
240
|
+
if (top === void 0) { top = false; }
|
|
232
241
|
var id = _a.id, _t = _a.type, data = __rest(_a, ["id", "type"]);
|
|
233
|
-
return utils_1.reduceUndefinedValues(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (schema ?
|
|
242
|
+
return utils_1.reduceUndefinedValues(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (top && schema ?
|
|
234
243
|
getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) {
|
|
235
244
|
var _b;
|
|
236
245
|
var property = _a.property, defaultValue = _a.defaultValue;
|
|
@@ -256,12 +265,13 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (j
|
|
|
256
265
|
return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (isEmptyValue(newValue) ? {} : (_a = {}, _a[key] = newValue, _a)));
|
|
257
266
|
}, {});
|
|
258
267
|
}; };
|
|
259
|
-
exports.formatNode = function (schemas, type, data, ignoreInternal) {
|
|
268
|
+
exports.formatNode = function (schemas, type, data, ignoreInternal, top) {
|
|
269
|
+
if (top === void 0) { top = false; }
|
|
260
270
|
var schema = type in schemas ? schemas[type] : schemaNotFoundError();
|
|
261
271
|
var content = mapContent(schemas, schema, ignoreInternal)(data);
|
|
262
|
-
return utils_1.reduceUndefinedValues(__assign({ id: !nonEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal)));
|
|
272
|
+
return utils_1.reduceUndefinedValues(__assign({ id: !nonEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal, top)));
|
|
263
273
|
};
|
|
264
|
-
|
|
274
|
+
exports.filterEmptyNode = function (schemas, _a) {
|
|
265
275
|
var schemaVersion = _a.schemaVersion, node = __rest(_a, ["schemaVersion"]);
|
|
266
276
|
// make sure defaultProperties are not counted
|
|
267
277
|
var schema = node.type ? schemas[node.type] : null;
|
|
@@ -271,8 +281,8 @@ var filterEmptyNode = function (schemas, _a) {
|
|
|
271
281
|
var convetToNode = function (schemas) { return function (data) {
|
|
272
282
|
return Object.keys(data)
|
|
273
283
|
.filter(utils_1.nonEmptyValue)
|
|
274
|
-
.map(function (type) { return exports.formatNode(schemas, schema_1.typeToSchemaType(type), data[type], true); })
|
|
275
|
-
.filter(function (node) { return filterEmptyNode(schemas, node); });
|
|
284
|
+
.map(function (type) { return exports.formatNode(schemas, schema_1.typeToSchemaType(type), data[type], true, true); })
|
|
285
|
+
.filter(function (node) { return exports.filterEmptyNode(schemas, node); });
|
|
276
286
|
}; };
|
|
277
287
|
/**
|
|
278
288
|
* Convert CSV to Hestia JSON-LD format.
|