@hestia-earth/schema-convert 7.7.1 → 7.10.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 +29 -12
  2. package/package.json +1 -1
package/json.js CHANGED
@@ -151,8 +151,9 @@ var parseError = function (err) { return safeParseJSON(err.message); };
151
151
  exports.throwCSVError = function (error) {
152
152
  throw new Error(JSON.stringify(error));
153
153
  };
154
- var schemaNotFoundError = function () { return exports.throwCSVError({
155
- message: 'schema-not-found'
154
+ var schemaNotFoundError = function (schema) { return exports.throwCSVError({
155
+ message: 'schema-not-found',
156
+ schema: schema
156
157
  }); };
157
158
  var propertyNotFoundError = function (schema, key, value) { return exports.throwCSVError({
158
159
  schema: schema.title,
@@ -194,10 +195,13 @@ var handleArrayError = function (func) { return function (value, index) {
194
195
  }; };
195
196
  /**
196
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`.
197
199
  *
198
200
  * @param value The value mapped as a Ref
199
201
  */
200
- 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
+ };
201
205
  exports.cleanStringValue = function (value) { return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim(); };
202
206
  var propertyTypeToValue = {
203
207
  null: function (value) {
@@ -216,7 +220,15 @@ var propertyTypeToValue = {
216
220
  })() : +value;
217
221
  },
218
222
  integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
219
- 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
+ },
220
232
  object: function (value, schemas, _a, _i) {
221
233
  var $ref = _a.$ref, required = _a.required;
222
234
  return $ref ?
@@ -248,7 +260,7 @@ var propertyTypeToValue = {
248
260
  var $ref = _a.$ref;
249
261
  var schemaType = schema_1.refToSchemaType($ref);
250
262
  var schema = schemaType ? schemas[schemaType] : undefined;
251
- var data = schema ? mapContent(schemas, schema, _i)(schemaRefValue(value)) : safeParseJSON(value);
263
+ var data = schema ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType)) : safeParseJSON(value);
252
264
  return utils_1.isEmpty(data) ? {} : (schema ? extendDataFromSchema(data, schema, schemaType, _i) : data);
253
265
  }
254
266
  };
@@ -322,7 +334,7 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (j
322
334
  }; };
323
335
  exports.formatNode = function (schemas, type, data, ignoreInternal, top) {
324
336
  if (top === void 0) { top = false; }
325
- var schema = type in schemas ? schemas[type] : schemaNotFoundError();
337
+ var schema = type in schemas ? schemas[type] : schemaNotFoundError(type);
326
338
  var content = mapContent(schemas, schema, ignoreInternal)(data);
327
339
  return utils_1.reduceUndefinedValues(__assign({ id: isEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal, top)), true);
328
340
  };
@@ -344,9 +356,14 @@ var convetToNode = function (schemas) { return function (data) {
344
356
  * @param content The content of the CSV as a string
345
357
  * @returns A list of JSON-LD content.
346
358
  */
347
- exports.toJson = function (schemas, content) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
348
- switch (_a.label) {
349
- case 0: return [4 /*yield*/, csvtojson({ delimiter: 'auto' }).fromString(content)];
350
- case 1: return [2 /*return*/, (_a.sent()).flatMap(convetToNode(schemas))];
351
- }
352
- }); }); };
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.1",
3
+ "version": "7.10.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",