@larsgw/formica 0.1.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.
- package/.eslintrc.js +16 -0
- package/LICENSE +21 -0
- package/README.md +19 -0
- package/lib/bin/process-resources-index.d.ts +1 -0
- package/lib/bin/process-resources-index.js +142 -0
- package/lib/bin/process-resources.d.ts +1 -0
- package/lib/bin/process-resources.js +594 -0
- package/lib/bin/util.d.ts +16 -0
- package/lib/bin/util.js +125 -0
- package/lib/bin/validate-catalog.d.ts +2 -0
- package/lib/bin/validate-catalog.js +92 -0
- package/lib/bin/validate-resources-text.d.ts +2 -0
- package/lib/bin/validate-resources-text.js +78 -0
- package/lib/catalog/entities.d.ts +12 -0
- package/lib/catalog/entities.js +111 -0
- package/lib/catalog/entity.d.ts +13 -0
- package/lib/catalog/entity.js +103 -0
- package/lib/catalog/index.d.ts +4 -0
- package/lib/catalog/index.js +33 -0
- package/lib/catalog/tables/author.d.ts +4 -0
- package/lib/catalog/tables/author.js +33 -0
- package/lib/catalog/tables/index.d.ts +2 -0
- package/lib/catalog/tables/index.js +13 -0
- package/lib/catalog/tables/place.d.ts +4 -0
- package/lib/catalog/tables/place.js +32 -0
- package/lib/catalog/tables/publisher.d.ts +4 -0
- package/lib/catalog/tables/publisher.js +33 -0
- package/lib/catalog/tables/work.d.ts +5 -0
- package/lib/catalog/tables/work.js +82 -0
- package/lib/catalog/value.d.ts +19 -0
- package/lib/catalog/value.js +49 -0
- package/lib/csv.d.ts +2 -0
- package/lib/csv.js +37 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/resources/diff-resource.d.ts +7 -0
- package/lib/resources/diff-resource.js +152 -0
- package/lib/resources/index.d.ts +1 -0
- package/lib/resources/index.js +6 -0
- package/lib/resources/parse-text.d.ts +2 -0
- package/lib/resources/parse-text.js +499 -0
- package/lib/types.d.ts +62 -0
- package/lib/types.js +0 -0
- package/package.json +42 -0
- package/src/bin/process-resources-index.ts +73 -0
- package/src/bin/process-resources.ts +406 -0
- package/src/bin/util.ts +74 -0
- package/src/bin/validate-catalog.ts +37 -0
- package/src/bin/validate-resources-text.ts +25 -0
- package/src/catalog/entities.ts +62 -0
- package/src/catalog/entity.ts +113 -0
- package/src/catalog/index.ts +32 -0
- package/src/catalog/tables/author.ts +13 -0
- package/src/catalog/tables/index.ts +12 -0
- package/src/catalog/tables/place.ts +12 -0
- package/src/catalog/tables/publisher.ts +13 -0
- package/src/catalog/tables/work.ts +62 -0
- package/src/catalog/value.ts +48 -0
- package/src/csv.ts +33 -0
- package/src/index.ts +3 -0
- package/src/module.d.ts +105 -0
- package/src/resources/diff-resource.ts +155 -0
- package/src/resources/index.ts +4 -0
- package/src/resources/parse-text.ts +519 -0
- package/tsconfig.json +11 -0
package/lib/bin/util.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.runCommand = exports.promptForAnswers = exports.prompt = exports.numericSort = void 0;
|
|
40
|
+
var readline = require("readline");
|
|
41
|
+
var child_process_1 = require("child_process");
|
|
42
|
+
/**
|
|
43
|
+
* Comparison function to sort strings with numerical components.
|
|
44
|
+
*/
|
|
45
|
+
function numericSort(a, b) {
|
|
46
|
+
var as = a.split(/(\d+)/);
|
|
47
|
+
var bs = b.split(/(\d+)/);
|
|
48
|
+
for (var i = 0; i < Math.max(as.length, bs.length); i++) {
|
|
49
|
+
var ai = as[i];
|
|
50
|
+
var bi = bs[i];
|
|
51
|
+
if (ai === bi) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
else if (!ai) {
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
else if (!bi) {
|
|
58
|
+
return -1;
|
|
59
|
+
}
|
|
60
|
+
else if (i % 2) {
|
|
61
|
+
return parseInt(ai) - parseInt(bi);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return ai > bi ? -1 : 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
exports.numericSort = numericSort;
|
|
70
|
+
/**
|
|
71
|
+
* Create a CLI prompt and return any answer.
|
|
72
|
+
*/
|
|
73
|
+
function prompt(question) {
|
|
74
|
+
var rl = readline.createInterface({
|
|
75
|
+
input: process.stdin,
|
|
76
|
+
output: process.stdout
|
|
77
|
+
});
|
|
78
|
+
return new Promise(function (resolve) {
|
|
79
|
+
rl.question(question, function (answer) {
|
|
80
|
+
rl.close();
|
|
81
|
+
resolve(answer);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
exports.prompt = prompt;
|
|
86
|
+
/**
|
|
87
|
+
* Create a CLI prompt and only accept certain answers.
|
|
88
|
+
*/
|
|
89
|
+
function promptForAnswers(question, answers) {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
91
|
+
var answer;
|
|
92
|
+
return __generator(this, function (_a) {
|
|
93
|
+
switch (_a.label) {
|
|
94
|
+
case 0: return [4 /*yield*/, prompt(question)];
|
|
95
|
+
case 1:
|
|
96
|
+
answer = (_a.sent())[0];
|
|
97
|
+
_a.label = 2;
|
|
98
|
+
case 2:
|
|
99
|
+
if (!answers.includes(answer)) return [3 /*break*/, 0];
|
|
100
|
+
_a.label = 3;
|
|
101
|
+
case 3: return [2 /*return*/, answer];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
exports.promptForAnswers = promptForAnswers;
|
|
107
|
+
/**
|
|
108
|
+
* Run a command and return stdout.
|
|
109
|
+
*/
|
|
110
|
+
function runCommand(command, args, options) {
|
|
111
|
+
return new Promise(function (resolve, reject) {
|
|
112
|
+
var proc = (0, child_process_1.spawn)(command, args, options);
|
|
113
|
+
var stdout = '';
|
|
114
|
+
proc.stdout.on('data', function (data) { stdout += data; });
|
|
115
|
+
proc.on('close', function (code) {
|
|
116
|
+
if (code === 0) {
|
|
117
|
+
resolve(stdout);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
reject();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
exports.runCommand = runCommand;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
13
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
14
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
15
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
|
+
function step(op) {
|
|
17
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
19
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
|
+
switch (op[0]) {
|
|
22
|
+
case 0: case 1: t = op; break;
|
|
23
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
24
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
25
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
26
|
+
default:
|
|
27
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
28
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
29
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
30
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
31
|
+
if (t[2]) _.ops.pop();
|
|
32
|
+
_.trys.pop(); continue;
|
|
33
|
+
}
|
|
34
|
+
op = body.call(thisArg, _);
|
|
35
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
36
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
var fs_1 = require("fs");
|
|
41
|
+
var path = require("path");
|
|
42
|
+
var index_1 = require("../index");
|
|
43
|
+
function validateFile(arg) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
45
|
+
var filePath, file, sheet;
|
|
46
|
+
return __generator(this, function (_a) {
|
|
47
|
+
switch (_a.label) {
|
|
48
|
+
case 0:
|
|
49
|
+
filePath = path.resolve(arg);
|
|
50
|
+
return [4 /*yield*/, fs_1.promises.readFile(filePath, 'utf8')];
|
|
51
|
+
case 1:
|
|
52
|
+
file = _a.sent();
|
|
53
|
+
sheet = path.basename(filePath, '.csv');
|
|
54
|
+
return [2 /*return*/, {
|
|
55
|
+
filePath: filePath,
|
|
56
|
+
errors: index_1.catalog.loadData(file, sheet).validate()
|
|
57
|
+
}];
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function main(args) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
64
|
+
var exitStatus, results, _i, results_1, result;
|
|
65
|
+
return __generator(this, function (_a) {
|
|
66
|
+
switch (_a.label) {
|
|
67
|
+
case 0:
|
|
68
|
+
exitStatus = 0;
|
|
69
|
+
return [4 /*yield*/, Promise.allSettled(args.map(validateFile))];
|
|
70
|
+
case 1:
|
|
71
|
+
results = _a.sent();
|
|
72
|
+
for (_i = 0, results_1 = results; _i < results_1.length; _i++) {
|
|
73
|
+
result = results_1[_i];
|
|
74
|
+
if (result.status === 'rejected') {
|
|
75
|
+
console.error(result.reason);
|
|
76
|
+
console.error();
|
|
77
|
+
exitStatus = 1;
|
|
78
|
+
}
|
|
79
|
+
else if (result.value.errors.length > 0) {
|
|
80
|
+
console.error("".concat(result.value.filePath, ":"));
|
|
81
|
+
console.table(result.value.errors);
|
|
82
|
+
console.error();
|
|
83
|
+
exitStatus = 1;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
process.exit(exitStatus);
|
|
87
|
+
return [2 /*return*/];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
main(process.argv.slice(2));
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
13
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
14
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
15
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
16
|
+
function step(op) {
|
|
17
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
18
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
19
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
20
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
21
|
+
switch (op[0]) {
|
|
22
|
+
case 0: case 1: t = op; break;
|
|
23
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
24
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
25
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
26
|
+
default:
|
|
27
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
28
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
29
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
30
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
31
|
+
if (t[2]) _.ops.pop();
|
|
32
|
+
_.trys.pop(); continue;
|
|
33
|
+
}
|
|
34
|
+
op = body.call(thisArg, _);
|
|
35
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
36
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
var fs_1 = require("fs");
|
|
41
|
+
var path = require("path");
|
|
42
|
+
var index_1 = require("../index");
|
|
43
|
+
function main(args) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
45
|
+
var exitStatus, _i, args_1, arg, filePath, file, id;
|
|
46
|
+
return __generator(this, function (_a) {
|
|
47
|
+
switch (_a.label) {
|
|
48
|
+
case 0:
|
|
49
|
+
exitStatus = 0;
|
|
50
|
+
_i = 0, args_1 = args;
|
|
51
|
+
_a.label = 1;
|
|
52
|
+
case 1:
|
|
53
|
+
if (!(_i < args_1.length)) return [3 /*break*/, 4];
|
|
54
|
+
arg = args_1[_i];
|
|
55
|
+
filePath = path.resolve(arg);
|
|
56
|
+
return [4 /*yield*/, fs_1.promises.readFile(filePath, 'utf8')];
|
|
57
|
+
case 2:
|
|
58
|
+
file = _a.sent();
|
|
59
|
+
id = path.basename(filePath, '.txt');
|
|
60
|
+
try {
|
|
61
|
+
index_1.resources.parseTextFile(file, id);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error(filePath + '\n ' + error.message + '\n');
|
|
65
|
+
exitStatus = 1;
|
|
66
|
+
}
|
|
67
|
+
_a.label = 3;
|
|
68
|
+
case 3:
|
|
69
|
+
_i++;
|
|
70
|
+
return [3 /*break*/, 1];
|
|
71
|
+
case 4:
|
|
72
|
+
process.exit(exitStatus);
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
main(process.argv.slice(2));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Entity } from './entity';
|
|
2
|
+
export declare class Entities {
|
|
3
|
+
entities: Entity[];
|
|
4
|
+
indexField: string;
|
|
5
|
+
index: Record<string, Entity>;
|
|
6
|
+
constructor(entities: Entity[], indexField: string);
|
|
7
|
+
[Symbol.iterator](): Generator<Entity, void, unknown>;
|
|
8
|
+
get(key: string): Entity;
|
|
9
|
+
has(key: string): boolean;
|
|
10
|
+
validate(): WorkError[];
|
|
11
|
+
toTable(): string[][];
|
|
12
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Entities = void 0;
|
|
40
|
+
var Entities = /** @class */ (function () {
|
|
41
|
+
function Entities(entities, indexField) {
|
|
42
|
+
this.entities = entities;
|
|
43
|
+
this.indexField = indexField;
|
|
44
|
+
this.index = {};
|
|
45
|
+
for (var _i = 0, _a = this.entities; _i < _a.length; _i++) {
|
|
46
|
+
var entity = _a[_i];
|
|
47
|
+
var key = entity.get(indexField);
|
|
48
|
+
if (typeof key !== 'string') {
|
|
49
|
+
throw new TypeError('Entity missing index field');
|
|
50
|
+
}
|
|
51
|
+
this.index[key] = entity;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
Entities.prototype[Symbol.iterator] = function () {
|
|
55
|
+
var _i, _a, entity;
|
|
56
|
+
return __generator(this, function (_b) {
|
|
57
|
+
switch (_b.label) {
|
|
58
|
+
case 0:
|
|
59
|
+
_i = 0, _a = this.entities;
|
|
60
|
+
_b.label = 1;
|
|
61
|
+
case 1:
|
|
62
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
63
|
+
entity = _a[_i];
|
|
64
|
+
return [4 /*yield*/, entity];
|
|
65
|
+
case 2:
|
|
66
|
+
_b.sent();
|
|
67
|
+
_b.label = 3;
|
|
68
|
+
case 3:
|
|
69
|
+
_i++;
|
|
70
|
+
return [3 /*break*/, 1];
|
|
71
|
+
case 4: return [2 /*return*/];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
Entities.prototype.get = function (key) {
|
|
76
|
+
return this.index[key];
|
|
77
|
+
};
|
|
78
|
+
Entities.prototype.has = function (key) {
|
|
79
|
+
return key in this.index;
|
|
80
|
+
};
|
|
81
|
+
Entities.prototype.validate = function () {
|
|
82
|
+
var errors = [];
|
|
83
|
+
for (var _i = 0, _a = this.entities; _i < _a.length; _i++) {
|
|
84
|
+
var entity = _a[_i];
|
|
85
|
+
for (var _b = 0, _c = entity.validate(); _b < _c.length; _b++) {
|
|
86
|
+
var _d = _c[_b], field = _d.field, error = _d.error;
|
|
87
|
+
errors.push({
|
|
88
|
+
entity: (entity.get(this.indexField) || '[missing]'),
|
|
89
|
+
field: field,
|
|
90
|
+
error: error
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return errors;
|
|
95
|
+
};
|
|
96
|
+
Entities.prototype.toTable = function () {
|
|
97
|
+
if (this.entities.length === 0) {
|
|
98
|
+
return [[]];
|
|
99
|
+
}
|
|
100
|
+
var header = Object.keys(this.entities[0].fields);
|
|
101
|
+
var table = this.entities.map(function (entity) {
|
|
102
|
+
return header.map(function (field) {
|
|
103
|
+
var value = entity.get(field) || '';
|
|
104
|
+
return Array.isArray(value) ? value.join('; ') : value;
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
return __spreadArray([header], table, true);
|
|
108
|
+
};
|
|
109
|
+
return Entities;
|
|
110
|
+
}());
|
|
111
|
+
exports.Entities = Entities;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class Entity {
|
|
2
|
+
fields: Record<string, Value>;
|
|
3
|
+
schema: Schema;
|
|
4
|
+
derivedFields: Record<string, Value>;
|
|
5
|
+
constructor(values: Record<string, string>, schema: Schema);
|
|
6
|
+
deriveFields(): void;
|
|
7
|
+
get(key: string): Value | undefined;
|
|
8
|
+
has(key: string): boolean;
|
|
9
|
+
validate(): FieldError[];
|
|
10
|
+
_validateField(field: string): string[];
|
|
11
|
+
_validateValueCount(field: string, value: Value): string | undefined;
|
|
12
|
+
_validateSingleValue(field: string, value: SingleValue): string | undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Entity = void 0;
|
|
4
|
+
var value_1 = require("./value");
|
|
5
|
+
var Entity = /** @class */ (function () {
|
|
6
|
+
function Entity(values, schema) {
|
|
7
|
+
this.fields = {};
|
|
8
|
+
for (var key in values) {
|
|
9
|
+
var value = (0, value_1.parseValue)(values[key], !!schema[key].multiple);
|
|
10
|
+
if (value !== null) {
|
|
11
|
+
this.fields[key] = value;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
this.schema = schema;
|
|
15
|
+
this.derivedFields = {};
|
|
16
|
+
}
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
18
|
+
Entity.prototype.deriveFields = function () {
|
|
19
|
+
};
|
|
20
|
+
/* eslint-enable @typescript-eslint/no-empty-function */
|
|
21
|
+
Entity.prototype.get = function (key) {
|
|
22
|
+
if (key in this.fields) {
|
|
23
|
+
return this.fields[key];
|
|
24
|
+
}
|
|
25
|
+
else if (key in this.derivedFields) {
|
|
26
|
+
return this.derivedFields[key];
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
Entity.prototype.has = function (key) {
|
|
30
|
+
return key in this.fields || key in this.derivedFields;
|
|
31
|
+
};
|
|
32
|
+
Entity.prototype.validate = function () {
|
|
33
|
+
var errors = [];
|
|
34
|
+
for (var field in this.fields) {
|
|
35
|
+
for (var _i = 0, _a = this._validateField(field); _i < _a.length; _i++) {
|
|
36
|
+
var error = _a[_i];
|
|
37
|
+
errors.push({ field: field, error: error });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return errors;
|
|
41
|
+
};
|
|
42
|
+
Entity.prototype._validateField = function (field) {
|
|
43
|
+
var errors = [];
|
|
44
|
+
if (!(field in this.fields)) {
|
|
45
|
+
if (this.schema[field].required) {
|
|
46
|
+
errors.push('Value(s) required but missing');
|
|
47
|
+
}
|
|
48
|
+
return errors;
|
|
49
|
+
}
|
|
50
|
+
var valueCountError = this._validateValueCount(field, this.fields[field]);
|
|
51
|
+
if (valueCountError !== undefined) {
|
|
52
|
+
errors.push(valueCountError);
|
|
53
|
+
}
|
|
54
|
+
var values = this.fields[field];
|
|
55
|
+
if (!Array.isArray(values)) {
|
|
56
|
+
values = [values];
|
|
57
|
+
}
|
|
58
|
+
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
|
|
59
|
+
var value = values_1[_i];
|
|
60
|
+
var result = this._validateSingleValue(field, value);
|
|
61
|
+
if (result !== undefined) {
|
|
62
|
+
errors.push(result);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return errors;
|
|
66
|
+
};
|
|
67
|
+
Entity.prototype._validateValueCount = function (field, value) {
|
|
68
|
+
var multipleConfig = this.schema[field].multiple;
|
|
69
|
+
var allowMultiple = typeof multipleConfig === 'function'
|
|
70
|
+
? multipleConfig.call(null, this.fields)
|
|
71
|
+
: multipleConfig;
|
|
72
|
+
if (allowMultiple === false) {
|
|
73
|
+
var multiple = false;
|
|
74
|
+
if (Array.isArray(value) && value.length > 2) {
|
|
75
|
+
multiple = true;
|
|
76
|
+
}
|
|
77
|
+
else if (typeof value === 'string' && value.includes('; ')) {
|
|
78
|
+
multiple = true;
|
|
79
|
+
}
|
|
80
|
+
if (multiple) {
|
|
81
|
+
return 'Multiple values but only one expected';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
Entity.prototype._validateSingleValue = function (field, value) {
|
|
87
|
+
var config = this.schema[field].format;
|
|
88
|
+
if (config === undefined) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
else if (Array.isArray(config) && (value === null || !config.includes(value))) {
|
|
92
|
+
return "The value \"".concat(value, "\" is not included: ").concat(config.join(', '));
|
|
93
|
+
}
|
|
94
|
+
else if (config instanceof RegExp && !config.test(value)) {
|
|
95
|
+
return "The value \"".concat(value, "\" does not conform to pattern: ").concat(config.source);
|
|
96
|
+
}
|
|
97
|
+
else if (typeof config === 'function' && !config.call(null, value)) {
|
|
98
|
+
return "The value \"".concat(value, "\" does not conform to pattern: ").concat(config.name);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return Entity;
|
|
102
|
+
}());
|
|
103
|
+
exports.Entity = Entity;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadData = exports.Entity = exports.Entities = void 0;
|
|
4
|
+
var entities_1 = require("./entities");
|
|
5
|
+
Object.defineProperty(exports, "Entities", { enumerable: true, get: function () { return entities_1.Entities; } });
|
|
6
|
+
var entity_1 = require("./entity");
|
|
7
|
+
Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return entity_1.Entity; } });
|
|
8
|
+
var index_1 = require("./tables/index");
|
|
9
|
+
var csv_1 = require("../csv");
|
|
10
|
+
function getTypeInfo(type) {
|
|
11
|
+
switch (type) {
|
|
12
|
+
case 'authors': return index_1.TYPE_INFO.authors;
|
|
13
|
+
case 'places': return index_1.TYPE_INFO.places;
|
|
14
|
+
case 'publishers': return index_1.TYPE_INFO.publishers;
|
|
15
|
+
case 'catalog': return index_1.TYPE_INFO.catalog;
|
|
16
|
+
default: throw new TypeError("Unknown type \"".concat(type, "\""));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function loadData(file, type) {
|
|
20
|
+
var _a = getTypeInfo(type), subClass = _a[0], indexField = _a[1];
|
|
21
|
+
var entities = [];
|
|
22
|
+
var _b = (0, csv_1.parseCsv)(file), header = _b[0], rows = _b.slice(1);
|
|
23
|
+
for (var _i = 0, rows_1 = rows; _i < rows_1.length; _i++) {
|
|
24
|
+
var row = rows_1[_i];
|
|
25
|
+
var data = {};
|
|
26
|
+
for (var index = 0; index < header.length && index < row.length; index++) {
|
|
27
|
+
data[header[index]] = row[index];
|
|
28
|
+
}
|
|
29
|
+
entities.push(new subClass(data, {}));
|
|
30
|
+
}
|
|
31
|
+
return new entities_1.Entities(entities, indexField);
|
|
32
|
+
}
|
|
33
|
+
exports.loadData = loadData;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Author = void 0;
|
|
19
|
+
var value_1 = require("../value");
|
|
20
|
+
var entity_1 = require("../entity");
|
|
21
|
+
var Author = /** @class */ (function (_super) {
|
|
22
|
+
__extends(Author, _super);
|
|
23
|
+
function Author(values) {
|
|
24
|
+
return _super.call(this, values, {
|
|
25
|
+
name: { required: true, multiple: false },
|
|
26
|
+
qid: { required: false, multiple: false, format: value_1.FORMATS.QID },
|
|
27
|
+
main_full_name: { required: false, multiple: false },
|
|
28
|
+
full_names: { required: false, multiple: true }
|
|
29
|
+
}) || this;
|
|
30
|
+
}
|
|
31
|
+
return Author;
|
|
32
|
+
}(entity_1.Entity));
|
|
33
|
+
exports.Author = Author;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TYPE_INFO = void 0;
|
|
4
|
+
var author_1 = require("./author");
|
|
5
|
+
var publisher_1 = require("./publisher");
|
|
6
|
+
var place_1 = require("./place");
|
|
7
|
+
var work_1 = require("./work");
|
|
8
|
+
exports.TYPE_INFO = {
|
|
9
|
+
authors: [author_1.Author, 'name'],
|
|
10
|
+
publishers: [publisher_1.Publisher, 'name'],
|
|
11
|
+
places: [place_1.Place, 'name'],
|
|
12
|
+
catalog: [work_1.Work, 'id']
|
|
13
|
+
};
|