@hestia-earth/schema-convert 7.6.4 → 7.7.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 +1 -1
- package/json.js +30 -12
- package/package.json +1 -1
package/json.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export interface ICSVError {
|
|
|
25
25
|
export declare const throwCSVError: <T extends ICSVError>(error: T) => never;
|
|
26
26
|
export declare const cleanStringValue: (value: string) => string;
|
|
27
27
|
export declare const formatNode: (schemas: definitions, type: SchemaType, data: ICSVContent, ignoreInternal: boolean, top?: boolean) => any;
|
|
28
|
-
export declare const filterEmptyNode: (schemas: definitions,
|
|
28
|
+
export declare const filterEmptyNode: (schemas: definitions, node: any) => boolean;
|
|
29
29
|
/**
|
|
30
30
|
* Convert CSV to Hestia JSON-LD format.
|
|
31
31
|
*
|
package/json.js
CHANGED
|
@@ -77,6 +77,7 @@ var __spread = (this && this.__spread) || function () {
|
|
|
77
77
|
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
78
78
|
return ar;
|
|
79
79
|
};
|
|
80
|
+
var _a;
|
|
80
81
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
81
82
|
exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = void 0;
|
|
82
83
|
var csvtojson = require("csvtojson");
|
|
@@ -114,6 +115,11 @@ var compileFullKey = function (key1, key2) {
|
|
|
114
115
|
var joinKey = (key2 || '').startsWith('[') ? '' : '.';
|
|
115
116
|
return [key1, key2].filter(Boolean).join(joinKey);
|
|
116
117
|
};
|
|
118
|
+
var schemaMinKeys = function (schemas, _a) {
|
|
119
|
+
var schemaVersion = _a.schemaVersion, node = __rest(_a, ["schemaVersion"]);
|
|
120
|
+
var schema = node.type ? schemas[node.type] : null;
|
|
121
|
+
return schema ? getDefaultProperties(schema).length + 1 : 1;
|
|
122
|
+
};
|
|
117
123
|
var propertyRequiredValue = function (value, required) {
|
|
118
124
|
var _a;
|
|
119
125
|
var val = typeof value === 'object' && required[0] in value ? value[required[0]] : value;
|
|
@@ -188,7 +194,7 @@ var propertyTypeToValue = {
|
|
|
188
194
|
})() : +value;
|
|
189
195
|
},
|
|
190
196
|
integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
|
|
191
|
-
boolean: function (value) { return value.toLowerCase() === 'true'; },
|
|
197
|
+
boolean: function (value) { return isEmptyCell(value) ? undefined : value.toLowerCase() === 'true'; },
|
|
192
198
|
object: function (value, schemas, _a, _i) {
|
|
193
199
|
var $ref = _a.$ref, required = _a.required;
|
|
194
200
|
return $ref ?
|
|
@@ -203,7 +209,7 @@ var propertyTypeToValue = {
|
|
|
203
209
|
Array.isArray(items.type) ?
|
|
204
210
|
propertyTypeToValue.auto(val, schemas, items, _i) :
|
|
205
211
|
propertyTypeToValue[items.type](val, schemas, items, _i)) : val; }))
|
|
206
|
-
.filter(function (val) { return !
|
|
212
|
+
.filter(function (val) { return !isEmptyValue(val, schemas); });
|
|
207
213
|
},
|
|
208
214
|
// try to determine the type automatically
|
|
209
215
|
auto: function (value, schemas, _d, _i) {
|
|
@@ -277,10 +283,24 @@ var extendDataFromSchema = function (_a, schema, type, ignoreInternal, top) {
|
|
|
277
283
|
}, {}) :
|
|
278
284
|
{})), nodeType(id, type, '@id' in data)), true);
|
|
279
285
|
};
|
|
280
|
-
var
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
286
|
+
var isEmptyValueType = (_a = {},
|
|
287
|
+
_a[schema_1.SchemaType.Term] = function (value) { return !(value['@id'] || value.id || value.name); },
|
|
288
|
+
_a.BlankNode = function (value, schemas) {
|
|
289
|
+
return Object.values(value).filter(function (value) { return !utils_1.isEmpty(value); }).length <= schemaMinKeys(schemas, value);
|
|
290
|
+
},
|
|
291
|
+
_a.array = function (value) { return value.filter(function (v) { return !utils_1.isEmpty(v); }).length === 0; },
|
|
292
|
+
_a.object = function (value, schemas) {
|
|
293
|
+
return Array.isArray(value) ?
|
|
294
|
+
isEmptyValueType.array(value) :
|
|
295
|
+
(value['@type'] || value.type) === schema_1.SchemaType.Term ?
|
|
296
|
+
isEmptyValueType[schema_1.SchemaType.Term](value) :
|
|
297
|
+
schema_1.isBlankNode(value) ?
|
|
298
|
+
isEmptyValueType.BlankNode(value, schemas) :
|
|
299
|
+
Object.keys(value).length === 0;
|
|
300
|
+
},
|
|
301
|
+
_a);
|
|
302
|
+
var isEmptyValue = function (value, schemas) {
|
|
303
|
+
return (typeof value) in isEmptyValueType ? isEmptyValueType[typeof value](value, schemas) : isEmptyCell(value);
|
|
284
304
|
};
|
|
285
305
|
var mapContent = function (schemas, schema, ignoreInternal) { return function (json) {
|
|
286
306
|
return Object.keys(json)
|
|
@@ -294,7 +314,7 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (j
|
|
|
294
314
|
var newValue = key === VALUE_TYPE_KEY ? '' : propertyInvalidFormat(schema, key, value, function () {
|
|
295
315
|
return Array.isArray(type) ? value : propertyTypeToValue[type](value, schemas, propertyDefinition, ignoreInternal);
|
|
296
316
|
});
|
|
297
|
-
return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (type !== 'null' && isEmptyValue(newValue) ? {} : (_a = {}, _a[key] = newValue, _a)));
|
|
317
|
+
return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (type !== 'null' && isEmptyValue(newValue, schemas) ? {} : (_a = {}, _a[key] = newValue, _a)));
|
|
298
318
|
}, {});
|
|
299
319
|
}; };
|
|
300
320
|
exports.formatNode = function (schemas, type, data, ignoreInternal, top) {
|
|
@@ -303,12 +323,10 @@ exports.formatNode = function (schemas, type, data, ignoreInternal, top) {
|
|
|
303
323
|
var content = mapContent(schemas, schema, ignoreInternal)(data);
|
|
304
324
|
return utils_1.reduceUndefinedValues(__assign({ id: isEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal, top)), true);
|
|
305
325
|
};
|
|
306
|
-
exports.filterEmptyNode = function (schemas,
|
|
307
|
-
var schemaVersion = _a.schemaVersion, node = __rest(_a, ["schemaVersion"]);
|
|
308
|
-
// make sure defaultProperties are not counted
|
|
326
|
+
exports.filterEmptyNode = function (schemas, node) {
|
|
309
327
|
var schema = node.type ? schemas[node.type] : null;
|
|
310
|
-
|
|
311
|
-
return !utils_1.isEmpty(node,
|
|
328
|
+
// make sure defaultProperties are not counted
|
|
329
|
+
return !!schema && !utils_1.isEmpty(node, schemaMinKeys(schemas, node));
|
|
312
330
|
};
|
|
313
331
|
var convetToNode = function (schemas) { return function (data) {
|
|
314
332
|
return Object.keys(data)
|