@hestia-earth/schema-convert
Advanced tools
Comparing version 6.9.0 to 6.10.0
@@ -1,1 +0,1 @@ | ||
export declare const run: () => Promise<string[]>; | ||
export declare const run: (pivot?: boolean) => Promise<string[]>; |
@@ -58,2 +58,3 @@ "use strict"; | ||
var path_1 = require("path"); | ||
var csv_pivot_1 = require("./csv-pivot"); | ||
var csv_1 = require("./csv"); | ||
@@ -74,3 +75,3 @@ var _a = __read(process.argv.slice(2), 1), path = _a[0]; | ||
}); }; | ||
var processFile = function (filepath) { return __awaiter(void 0, void 0, void 0, function () { | ||
var processFile = function (filepath, pivot) { return __awaiter(void 0, void 0, void 0, function () { | ||
var content, _a, _b, nodes, res, destPath; | ||
@@ -85,3 +86,3 @@ return __generator(this, function (_c) { | ||
nodes = Array.isArray(content) ? content : ('nodes' in content ? content.nodes : [content]); | ||
res = csv_1.toCsv(nodes); | ||
res = pivot ? csv_pivot_1.toCsvPivot(nodes) : csv_1.toCsv(nodes); | ||
destPath = filepath.replace('.jsonld', '.csv').replace('.json', '.csv'); | ||
@@ -95,21 +96,24 @@ return [4 /*yield*/, writeFile(destPath, res, encoding)]; | ||
}); }; | ||
exports.run = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var stat, _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: return [4 /*yield*/, lstat(path)]; | ||
case 1: | ||
stat = _d.sent(); | ||
_b = (_a = Promise).all; | ||
if (!stat.isDirectory()) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, listFiles(path)]; | ||
case 2: | ||
_c = (_d.sent()).map(function (file) { return processFile(path_1.join(path, file)); }); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
_c = [processFile(path)]; | ||
_d.label = 4; | ||
case 4: return [2 /*return*/, _b.apply(_a, [_c])]; | ||
} | ||
exports.run = function (pivot) { | ||
if (pivot === void 0) { pivot = false; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var stat, _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: return [4 /*yield*/, lstat(path)]; | ||
case 1: | ||
stat = _d.sent(); | ||
_b = (_a = Promise).all; | ||
if (!stat.isDirectory()) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, listFiles(path)]; | ||
case 2: | ||
_c = (_d.sent()).map(function (file) { return processFile(path_1.join(path, file), pivot); }); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
_c = [processFile(path, pivot)]; | ||
_d.label = 4; | ||
case 4: return [2 /*return*/, _b.apply(_a, [_c])]; | ||
} | ||
}); | ||
}); | ||
}); }; | ||
}; |
export declare const csvColSep = ","; | ||
/** | ||
* Get the headers in a CSV file. | ||
* | ||
* @param csv The CSV content as string. | ||
* @returns headers as a list. | ||
*/ | ||
export declare const headersFromCsv: (csv: string) => string[]; | ||
@@ -10,3 +16,4 @@ /** | ||
* @param selectedHeaders Override list of headers in order. By default, compile all from nodes and sort as they appear. | ||
* @returns CSV content formatted for upload on Hestia. | ||
*/ | ||
export declare const toCsv: (nodes: any[], includeExising?: boolean, useBrackets?: boolean, selectedHeaders?: any[]) => string; |
16
csv.js
@@ -36,2 +36,3 @@ "use strict"; | ||
var get = require('lodash.get'); | ||
var utils_1 = require("./utils"); | ||
var noValue = '-'; | ||
@@ -44,3 +45,8 @@ exports.csvColSep = ','; | ||
var isIgnored = function (val) { return Object.values(ignoreValues).some(function (func) { return func(val); }); }; | ||
var isExpandable = function (val) { return !!val && typeof val === 'object' && (Array.isArray(val) ? val.every(isExpandable) : Object.keys(val).length); }; | ||
/** | ||
* Get the headers in a CSV file. | ||
* | ||
* @param csv The CSV content as string. | ||
* @returns headers as a list. | ||
*/ | ||
exports.headersFromCsv = function (csv) { return csv.split('\n')[0].split(exports.csvColSep); }; | ||
@@ -57,3 +63,6 @@ var convertValue = { | ||
var skipColumns = ['type', '@type', '@context']; | ||
var typeToColumnName = function (type) { return "" + type.substring(0, 1).toLowerCase() + type.substring(1); }; | ||
var typeToColumnName = function (type) { | ||
if (type === void 0) { type = ''; } | ||
return "" + type.substring(0, 1).toLowerCase() + type.substring(1); | ||
}; | ||
var getChildrenHeaders = function (key, value, useBrackets) { | ||
@@ -72,3 +81,3 @@ var headers = getHeaders(value, useBrackets); | ||
var value = node[key]; | ||
return isExpandable(value) && !isIgnored(value) ? getChildrenHeaders(key, value, useBrackets) : key; | ||
return utils_1.isExpandable(value) && !isIgnored(value) ? getChildrenHeaders(key, value, useBrackets) : key; | ||
}) | ||
@@ -88,2 +97,3 @@ .filter(function (key) { return !skipColumns.includes(key); }); | ||
* @param selectedHeaders Override list of headers in order. By default, compile all from nodes and sort as they appear. | ||
* @returns CSV content formatted for upload on Hestia. | ||
*/ | ||
@@ -90,0 +100,0 @@ exports.toCsv = function (nodes, includeExising, useBrackets, selectedHeaders) { |
@@ -0,2 +1,3 @@ | ||
export { toCsvPivot } from './csv-pivot'; | ||
export * from './csv'; | ||
export * from './json'; |
@@ -13,3 +13,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var csv_pivot_1 = require("./csv-pivot"); | ||
Object.defineProperty(exports, "toCsvPivot", { enumerable: true, get: function () { return csv_pivot_1.toCsvPivot; } }); | ||
__exportStar(require("./csv"), exports); | ||
__exportStar(require("./json"), exports); |
27
json.js
@@ -53,20 +53,3 @@ "use strict"; | ||
var schema_1 = require("@hestia-earth/schema"); | ||
var ignoreKey = function (key) { return !['@type', 'type'].includes(key); }; | ||
var isEmpty = function (value) { | ||
return value === null || | ||
typeof value === 'undefined' || | ||
(typeof value === 'object' ? | ||
(Array.isArray(value) ? | ||
!value.length : | ||
Object.keys(value).filter(ignoreKey).length < 1) : | ||
value === ''); | ||
}; | ||
var nonEmptyValue = function (value) { return !isEmpty(value) && value !== '-'; }; | ||
var nonEmptyNode = function (node) { | ||
return typeof node === 'object' ? | ||
Object.keys(node) | ||
.filter(function (key) { return ignoreKey(key) && !isEmpty(node[key]); }) | ||
.length > 0 : | ||
nonEmptyValue(node); | ||
}; | ||
var utils_1 = require("./utils"); | ||
var propertyRef = function (schema, key) { | ||
@@ -89,3 +72,3 @@ var property = schema && schema.properties ? schema.properties[key] : {}; | ||
.map(function (v) { return formatNode(schemas, nodeSchema, v); }) | ||
.filter(nonEmptyNode) | ||
.filter(utils_1.nonEmptyNode) | ||
.map(function (v) { return typeof v === 'object' ? (__assign(__assign({}, v), (type ? { type: type } : {}))) : v; }); | ||
@@ -100,3 +83,3 @@ return { | ||
var key = _a.key, value = _a.value; | ||
return nonEmptyValue(key) && nonEmptyNode(value); | ||
return utils_1.nonEmptyValue(key) && utils_1.nonEmptyNode(value); | ||
}) | ||
@@ -111,5 +94,5 @@ .reduce(function (prev, _a) { | ||
var convetToNode = function (schemas) { return function (data) { return Object.keys(data) | ||
.filter(nonEmptyValue) | ||
.filter(utils_1.nonEmptyValue) | ||
.map(function (type) { return (__assign({ type: schema_1.typeToSchemaType(type) }, formatNode(schemas, schemas[schema_1.typeToSchemaType(type)], data[type]))); }) | ||
.filter(nonEmptyNode); }; }; | ||
.filter(utils_1.nonEmptyNode); }; }; | ||
exports.toJson = function (schemas, content) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { | ||
@@ -116,0 +99,0 @@ switch (_a.label) { |
{ | ||
"name": "@hestia-earth/schema-convert", | ||
"version": "6.9.0", | ||
"version": "6.10.0", | ||
"description": "Hestia Schema Converters", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"bin": { | ||
"hestia-convert-to-csv-pivot": "bin/bin-convert-to-csv-pivot.js", | ||
"hestia-convert-to-csv": "bin/bin-convert-to-csv.js", | ||
@@ -31,5 +32,7 @@ "hestia-convert-to-json": "bin/bin-convert-to-json.js" | ||
"@hestia-earth/schema": "*", | ||
"csvtojson": "^2.0.10", | ||
"lodash.get": "^4.4.2" | ||
"csvtojson": "^2.0.0", | ||
"json-2-csv": "^3.14.0", | ||
"lodash.get": "^4.0.0", | ||
"lodash.unset": "^4.0.0" | ||
} | ||
} |
33508
22
697
6
+ Addedjson-2-csv@^3.14.0
+ Addedlodash.unset@^4.0.0
+ Addeddeeks@2.6.1(transitive)
+ Addeddoc-path@3.1.0(transitive)
+ Addedjson-2-csv@3.20.0(transitive)
+ Addedlodash.unset@4.5.2(transitive)
Updatedcsvtojson@^2.0.0
Updatedlodash.get@^4.0.0