@hestia-earth/schema-convert 7.7.0 → 7.9.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.
Files changed (2) hide show
  1. package/json.js +51 -31
  2. package/package.json +1 -1
package/json.js CHANGED
@@ -110,6 +110,28 @@ var internalProperties = function (_a) {
110
110
  };
111
111
  var isEmptyCell = function (value) { return value === IGNORE_FIELD_KEY || value === ''; };
112
112
  var nonEmptyCell = function (value) { return !isEmptyCell(value); };
113
+ var isEmptyValueType = (_a = {
114
+ undefined: function () { return true; },
115
+ number: function (value) { return isNaN(value); }
116
+ },
117
+ _a[schema_1.SchemaType.Term] = function (value) { return !(value['@id'] || value.id || value.name); },
118
+ _a.BlankNode = function (value, schemas) {
119
+ return Object.values(value).filter(function (value) { return !utils_1.isEmpty(value); }).length <= schemaMinKeys(schemas, value);
120
+ },
121
+ _a.array = function (value) { return value.filter(function (v) { return !utils_1.isEmpty(v); }).length === 0; },
122
+ _a.object = function (value, schemas) {
123
+ return Array.isArray(value) ?
124
+ isEmptyValueType.array(value) :
125
+ (value['@type'] || value.type) === schema_1.SchemaType.Term ?
126
+ isEmptyValueType[schema_1.SchemaType.Term](value) :
127
+ schema_1.isBlankNode(value) ?
128
+ isEmptyValueType.BlankNode(value, schemas) :
129
+ Object.keys(value).length === 0;
130
+ },
131
+ _a);
132
+ var isEmptyValue = function (value, schemas) {
133
+ return (typeof value) in isEmptyValueType ? isEmptyValueType[typeof value](value, schemas) : isEmptyCell(value);
134
+ };
113
135
  var compileFullKey = function (key1, key2) {
114
136
  // handle arrays
115
137
  var joinKey = (key2 || '').startsWith('[') ? '' : '.';
@@ -129,8 +151,9 @@ var parseError = function (err) { return safeParseJSON(err.message); };
129
151
  exports.throwCSVError = function (error) {
130
152
  throw new Error(JSON.stringify(error));
131
153
  };
132
- var schemaNotFoundError = function () { return exports.throwCSVError({
133
- message: 'schema-not-found'
154
+ var schemaNotFoundError = function (schema) { return exports.throwCSVError({
155
+ message: 'schema-not-found',
156
+ schema: schema
134
157
  }); };
135
158
  var propertyNotFoundError = function (schema, key, value) { return exports.throwCSVError({
136
159
  schema: schema.title,
@@ -172,10 +195,13 @@ var handleArrayError = function (func) { return function (value, index) {
172
195
  }; };
173
196
  /**
174
197
  * If the user provided a non-object where an object was expected, assume it was meant to be the `name` property.
198
+ * For non-Terms, we assume it is the `id`.
175
199
  *
176
200
  * @param value The value mapped as a Ref
177
201
  */
178
- var schemaRefValue = function (value) { return typeof value === 'object' ? value : ({ name: value }); };
202
+ var schemaRefValue = function (value, schemaType) {
203
+ return typeof value === 'object' ? value : schemaType === schema_1.SchemaType.Term ? { name: value } : { id: value };
204
+ };
179
205
  exports.cleanStringValue = function (value) { return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim(); };
180
206
  var propertyTypeToValue = {
181
207
  null: function (value) {
@@ -194,7 +220,15 @@ var propertyTypeToValue = {
194
220
  })() : +value;
195
221
  },
196
222
  integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
197
- boolean: function (value) { return isEmptyCell(value) ? undefined : value.toLowerCase() === 'true'; },
223
+ boolean: function (value) {
224
+ return isEmptyCell(value) ?
225
+ undefined :
226
+ utils_1.isBoolean(value) ?
227
+ value.toLowerCase() === 'true' :
228
+ (function () {
229
+ throw new Error('failed to parse boolean, expected true or false');
230
+ })();
231
+ },
198
232
  object: function (value, schemas, _a, _i) {
199
233
  var $ref = _a.$ref, required = _a.required;
200
234
  return $ref ?
@@ -226,7 +260,7 @@ var propertyTypeToValue = {
226
260
  var $ref = _a.$ref;
227
261
  var schemaType = schema_1.refToSchemaType($ref);
228
262
  var schema = schemaType ? schemas[schemaType] : undefined;
229
- var data = schema ? mapContent(schemas, schema, _i)(schemaRefValue(value)) : safeParseJSON(value);
263
+ var data = schema ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType)) : safeParseJSON(value);
230
264
  return utils_1.isEmpty(data) ? {} : (schema ? extendDataFromSchema(data, schema, schemaType, _i) : data);
231
265
  }
232
266
  };
@@ -283,25 +317,6 @@ var extendDataFromSchema = function (_a, schema, type, ignoreInternal, top) {
283
317
  }, {}) :
284
318
  {})), nodeType(id, type, '@id' in data)), true);
285
319
  };
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);
304
- };
305
320
  var mapContent = function (schemas, schema, ignoreInternal) { return function (json) {
306
321
  return Object.keys(json)
307
322
  .filter(nonEmptyCell)
@@ -319,7 +334,7 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (j
319
334
  }; };
320
335
  exports.formatNode = function (schemas, type, data, ignoreInternal, top) {
321
336
  if (top === void 0) { top = false; }
322
- var schema = type in schemas ? schemas[type] : schemaNotFoundError();
337
+ var schema = type in schemas ? schemas[type] : schemaNotFoundError(type);
323
338
  var content = mapContent(schemas, schema, ignoreInternal)(data);
324
339
  return utils_1.reduceUndefinedValues(__assign({ id: isEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal, top)), true);
325
340
  };
@@ -341,9 +356,14 @@ var convetToNode = function (schemas) { return function (data) {
341
356
  * @param content The content of the CSV as a string
342
357
  * @returns A list of JSON-LD content.
343
358
  */
344
- exports.toJson = function (schemas, content) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
345
- switch (_a.label) {
346
- case 0: return [4 /*yield*/, csvtojson({ delimiter: 'auto' }).fromString(content)];
347
- case 1: return [2 /*return*/, (_a.sent()).flatMap(convetToNode(schemas))];
348
- }
349
- }); }); };
359
+ exports.toJson = function (schemas, content) { return __awaiter(void 0, void 0, void 0, function () {
360
+ return __generator(this, function (_a) {
361
+ switch (_a.label) {
362
+ case 0: return [4 /*yield*/, csvtojson({
363
+ delimiter: 'auto',
364
+ ignoreColumns: /^$/
365
+ }).fromString(content)];
366
+ case 1: return [2 /*return*/, (_a.sent()).flatMap(convetToNode(schemas))];
367
+ }
368
+ });
369
+ }); };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "7.7.0",
3
+ "version": "7.9.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",