@hestia-earth/schema-convert 8.0.0 → 8.3.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 (4) hide show
  1. package/csv.js +5 -1
  2. package/json.d.ts +1 -0
  3. package/json.js +20 -8
  4. package/package.json +2 -1
package/csv.js CHANGED
@@ -50,7 +50,11 @@ var isIgnored = function (val) { return Object.values(ignoreValues).some(functio
50
50
  exports.headersFromCsv = function (csv) { return csv.split('\n')[0].split(exports.csvColSep); };
51
51
  var convertValue = {
52
52
  default: function (value) { return "" + value; },
53
- object: function (value) { return value instanceof Date ? value.toJSON() : JSON.stringify(value); }
53
+ object: function (value) { return value === null
54
+ ? noValue
55
+ : value instanceof Date
56
+ ? value.toJSON()
57
+ : JSON.stringify(value); }
54
58
  };
55
59
  var escapeField = function (value) {
56
60
  var convertType = (typeof value) in convertValue ? typeof value : 'default';
package/json.d.ts CHANGED
@@ -21,6 +21,7 @@ export interface ICSVError {
21
21
  value?: any;
22
22
  error?: string;
23
23
  headers?: ICSVHeader[];
24
+ suggestions?: string[];
24
25
  }
25
26
  export declare const throwCSVError: <T extends ICSVError>(error: T) => never;
26
27
  export declare const cleanStringValue: (value: string) => string;
package/json.js CHANGED
@@ -81,6 +81,7 @@ var _a;
81
81
  Object.defineProperty(exports, "__esModule", { value: true });
82
82
  exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = void 0;
83
83
  var csvtojson = require("csvtojson");
84
+ var levenshtein = require("fast-levenshtein");
84
85
  var schema_1 = require("@hestia-earth/schema");
85
86
  var types_1 = require("@hestia-earth/json-schema/types");
86
87
  var utils_1 = require("./utils");
@@ -155,12 +156,21 @@ var schemaNotFoundError = function (schema) { return exports.throwCSVError({
155
156
  message: 'schema-not-found',
156
157
  schema: schema
157
158
  }); };
159
+ var computeSuggestions = function (_a, key) {
160
+ var properties = _a.properties;
161
+ var internal = internalProperties({ properties: properties });
162
+ var allKeys = Object.keys(properties).filter(function (k) { return !__spread([
163
+ '@type', 'type'
164
+ ], internal).includes(k); });
165
+ return allKeys.filter(function (v) { return levenshtein.get(v, key) <= 3; });
166
+ };
158
167
  var propertyNotFoundError = function (schema, key, value) { return exports.throwCSVError({
159
168
  schema: schema.title,
160
169
  message: 'property-not-found',
161
170
  schemaKey: key,
162
171
  key: key,
163
- value: value
172
+ value: value,
173
+ suggestions: computeSuggestions(schema, key)
164
174
  }); };
165
175
  var propertyInvalidFormat = function (schema, key, value, func) {
166
176
  try {
@@ -221,13 +231,15 @@ var propertyTypeToValue = {
221
231
  },
222
232
  integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
223
233
  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
- })();
234
+ return isEmptyCell(value)
235
+ ? undefined
236
+ : utils_1.isBoolean(value)
237
+ ? value.toLowerCase() === 'true'
238
+ : utils_1.isNumber(value)
239
+ ? +value === 1 // handle setting 1 or 1.0 as true
240
+ : (function () {
241
+ throw new Error('failed to parse boolean, expected true or false');
242
+ })();
231
243
  },
232
244
  object: function (value, schemas, _a, _i) {
233
245
  var $ref = _a.$ref, required = _a.required;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "8.0.0",
3
+ "version": "8.3.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -29,6 +29,7 @@
29
29
  "@hestia-earth/json-schema": "*",
30
30
  "@hestia-earth/schema": "*",
31
31
  "csvtojson": "^2.0.0",
32
+ "fast-levenshtein": "^3.0.0",
32
33
  "json-2-csv": "^3.14.0",
33
34
  "lodash.get": "^4.0.0",
34
35
  "lodash.unset": "^4.0.0"