@hestia-earth/schema-convert 7.6.1 → 7.6.2

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.js CHANGED
@@ -57,6 +57,26 @@ var __rest = (this && this.__rest) || function (s, e) {
57
57
  }
58
58
  return t;
59
59
  };
60
+ var __read = (this && this.__read) || function (o, n) {
61
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
62
+ if (!m) return o;
63
+ var i = m.call(o), r, ar = [], e;
64
+ try {
65
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
66
+ }
67
+ catch (error) { e = { error: error }; }
68
+ finally {
69
+ try {
70
+ if (r && !r.done && (m = i["return"])) m.call(i);
71
+ }
72
+ finally { if (e) throw e.error; }
73
+ }
74
+ return ar;
75
+ };
76
+ var __spread = (this && this.__spread) || function () {
77
+ for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
78
+ return ar;
79
+ };
60
80
  Object.defineProperty(exports, "__esModule", { value: true });
61
81
  exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = void 0;
62
82
  var csvtojson = require("csvtojson");
@@ -87,7 +107,8 @@ var internalProperties = function (_a) {
87
107
  var properties = _a.properties;
88
108
  return Object.keys(properties).filter(function (property) { return properties[property].internal; });
89
109
  };
90
- var nonEmptyCell = function (value) { return value !== IGNORE_FIELD_KEY && value !== ''; };
110
+ var isEmptyCell = function (value) { return value === IGNORE_FIELD_KEY || value === ''; };
111
+ var nonEmptyCell = function (value) { return !isEmptyCell(value); };
91
112
  var compileFullKey = function (key1, key2) {
92
113
  // handle arrays
93
114
  var joinKey = (key2 || '').startsWith('[') ? '' : '.';
@@ -151,13 +172,22 @@ var handleArrayError = function (func) { return function (value, index) {
151
172
  var schemaRefValue = function (value) { return typeof value === 'object' ? value : ({ name: value }); };
152
173
  exports.cleanStringValue = function (value) { return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim(); };
153
174
  var propertyTypeToValue = {
175
+ null: function (value) {
176
+ var args = [];
177
+ for (var _a = 1; _a < arguments.length; _a++) {
178
+ args[_a - 1] = arguments[_a];
179
+ }
180
+ return isEmptyCell(value) ? null : propertyTypeToValue.auto.apply(propertyTypeToValue, __spread([value], args));
181
+ },
154
182
  string: function (value) { return exports.cleanStringValue(value || ''); },
155
183
  number: function (value) {
156
- return isNaN(+value) ? (function () {
157
- throw new Error('failed to parse number');
158
- })() : +value;
184
+ return isEmptyCell(value) ?
185
+ undefined :
186
+ isNaN(+value) ? (function () {
187
+ throw new Error('failed to parse number');
188
+ })() : +value;
159
189
  },
160
- integer: function (value) { return +value; },
190
+ integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
161
191
  boolean: function (value) { return value.toLowerCase() === 'true'; },
162
192
  object: function (value, schemas, _a, _i) {
163
193
  var $ref = _a.$ref, required = _a.required;
@@ -206,7 +236,7 @@ var getValueType = function (schema, json) {
206
236
  };
207
237
  var getPropertyType = function (def, key, valueType) {
208
238
  // default type is object (like $ref)
209
- return (key === 'value' && valueType ? valueType : (Array.isArray(def.type) ? 'auto' : def.type)) || 'object';
239
+ return (key === 'value' && valueType ? valueType : (Array.isArray(def.type) ? (def.type.includes('null') ? 'null' : 'auto') : def.type)) || 'object';
210
240
  };
211
241
  var setDefaultProperty = function (prop, def, node, ignoreInternal) {
212
242
  if (ignoreInternal === void 0) { ignoreInternal = false; }
@@ -245,14 +275,14 @@ var extendDataFromSchema = function (_a, schema, type, ignoreInternal, top) {
245
275
  var property = _a.property, defaultValue = _a.defaultValue;
246
276
  return (__assign(__assign({}, prev), (_b = {}, _b[property] = defaultValue, _b)));
247
277
  }, {}) :
248
- {})), nodeType(id, type, '@id' in data)));
278
+ {})), nodeType(id, type, '@id' in data)), true);
249
279
  };
250
280
  var isEmptyValue = function (value) {
251
281
  return (value['@type'] || value.type) === schema_1.SchemaType.Term ? !(value['@id'] || value.id || value.name) : utils_1.isEmpty(value);
252
282
  };
253
283
  var mapContent = function (schemas, schema, ignoreInternal) { return function (json) {
254
284
  return Object.keys(json)
255
- .filter(function (key) { return nonEmptyCell(key) && nonEmptyCell(json[key]); })
285
+ .filter(nonEmptyCell)
256
286
  .reduce(function (prev, key) {
257
287
  var _a;
258
288
  var value = json[key];
@@ -262,14 +292,14 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (j
262
292
  var newValue = key === VALUE_TYPE_KEY ? '' : propertyInvalidFormat(schema, key, value, function () {
263
293
  return Array.isArray(type) ? value : propertyTypeToValue[type](value, schemas, propertyDefinition, ignoreInternal);
264
294
  });
265
- return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (isEmptyValue(newValue) ? {} : (_a = {}, _a[key] = newValue, _a)));
295
+ return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (type !== 'null' && isEmptyValue(newValue) ? {} : (_a = {}, _a[key] = newValue, _a)));
266
296
  }, {});
267
297
  }; };
268
298
  exports.formatNode = function (schemas, type, data, ignoreInternal, top) {
269
299
  if (top === void 0) { top = false; }
270
300
  var schema = type in schemas ? schemas[type] : schemaNotFoundError();
271
301
  var content = mapContent(schemas, schema, ignoreInternal)(data);
272
- return utils_1.reduceUndefinedValues(__assign({ id: !nonEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal, top)));
302
+ return utils_1.reduceUndefinedValues(__assign({ id: isEmptyCell(data.id) ? undefined : data.id }, extendDataFromSchema(content, schema, type, ignoreInternal, top)), true);
273
303
  };
274
304
  exports.filterEmptyNode = function (schemas, _a) {
275
305
  var schemaVersion = _a.schemaVersion, node = __rest(_a, ["schemaVersion"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "7.6.1",
3
+ "version": "7.6.2",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
package/utils.d.ts CHANGED
@@ -6,4 +6,4 @@ export declare const isNumber: (n: string | number) => boolean;
6
6
  export declare const isEmpty: (value: any, minKeys?: number) => boolean;
7
7
  export declare const nonEmptyValue: (value: any) => boolean;
8
8
  export declare const nonEmptyNode: (node: any) => boolean;
9
- export declare const reduceUndefinedValues: <T>(obj: T) => Partial<T>;
9
+ export declare const reduceUndefinedValues: <T>(obj: T, allowNull?: boolean) => Partial<T>;
package/utils.js CHANGED
@@ -41,14 +41,15 @@ exports.nonEmptyNode = function (node) {
41
41
  .length > 0 :
42
42
  exports.nonEmptyValue(node);
43
43
  };
44
- var isUndefined = function (value) {
45
- return value === null ||
46
- typeof value === 'undefined' || (typeof value === 'object' && !(value instanceof Date) && !Object.keys(value).length);
44
+ var isUndefined = function (value, allowNull) {
45
+ return (value === null && !allowNull) ||
46
+ typeof value === 'undefined' || (typeof value === 'object' && value !== null && !(value instanceof Date) && !Object.keys(value).length);
47
47
  };
48
- exports.reduceUndefinedValues = function (obj) {
48
+ exports.reduceUndefinedValues = function (obj, allowNull) {
49
+ if (allowNull === void 0) { allowNull = false; }
49
50
  return Object.keys(obj).reduce(function (prev, key) {
50
51
  var _a;
51
52
  var value = obj[key];
52
- return __assign(__assign({}, prev), (isUndefined(value) ? {} : (_a = {}, _a[key] = value, _a)));
53
+ return __assign(__assign({}, prev), (isUndefined(value, allowNull) ? {} : (_a = {}, _a[key] = value, _a)));
53
54
  }, {});
54
55
  };