@larsgw/formica 0.10.2 → 0.10.3

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/README.md CHANGED
@@ -20,6 +20,8 @@ Run locally:
20
20
  - `loir-validate-resources [./B1234.txt]`:
21
21
  Validate the `.txt` files containing information on the taxonomic scope of keys
22
22
  ([documentation](https://github.com/identification-resources/catalog/blob/main/docs/resources-txt.md)).
23
+ - `loir-validate-integrity .`:
24
+ Check if all values are mapped as expected.
23
25
  - `loir-resources-process [./resources]`:
24
26
  Interactive tool to convert the aforementioned `.txt` files to Darwin Core archives
25
27
  ([documentation](https://github.com/identification-resources/catalog/blob/main/docs/resources-dwc.md)),
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ const fs_1 = require("fs");
47
+ const path = __importStar(require("path"));
48
+ const index_1 = require("../index");
49
+ function loadCatalogFile(file, base) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const contents = yield fs_1.promises.readFile(path.join(base, `${file}.csv`), 'utf8');
52
+ return index_1.catalog.loadData(contents, file);
53
+ });
54
+ }
55
+ function loadResourcesIndex(base) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const contents = yield fs_1.promises.readFile(path.join(base, 'resources', 'index.json'), 'utf8');
58
+ return JSON.parse(contents);
59
+ });
60
+ }
61
+ function getAll(entity, field) {
62
+ const value = entity.get(field);
63
+ if (Array.isArray(value)) {
64
+ return value;
65
+ }
66
+ else if (value) {
67
+ return [value];
68
+ }
69
+ else {
70
+ return [];
71
+ }
72
+ }
73
+ function checkIntegrity(catalog, resources, field, sheet) {
74
+ const errors = [];
75
+ const index = new Set();
76
+ for (const entity of sheet) {
77
+ for (const value of getAll(entity, 'name')) {
78
+ index.add(value);
79
+ }
80
+ }
81
+ for (const entity of catalog) {
82
+ const id = entity.get('id');
83
+ for (const value of getAll(entity, field)) {
84
+ if (!index.has(value)) {
85
+ errors.push({
86
+ entity: id,
87
+ field: field,
88
+ error: `Value "${value}" not mapped`
89
+ });
90
+ }
91
+ }
92
+ }
93
+ for (const id in resources) {
94
+ const resource = resources[id];
95
+ if (!resource.catalog || !Object.prototype.hasOwnProperty.call(resource.catalog, field)) {
96
+ continue;
97
+ }
98
+ const value = resource.catalog[field];
99
+ const values = Array.isArray(value) ? value : [value];
100
+ for (const value of values) {
101
+ if (!index.has(value)) {
102
+ errors.push({
103
+ entity: id,
104
+ field: field,
105
+ error: `Value "${value}" is not mapped`
106
+ });
107
+ }
108
+ }
109
+ }
110
+ return errors;
111
+ }
112
+ function main(args) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const base = path.resolve(args[0]);
115
+ const [catalog, authors, places, publishers, taxa, resources] = yield Promise.all([
116
+ loadCatalogFile('catalog', base),
117
+ loadCatalogFile('authors', base),
118
+ loadCatalogFile('places', base),
119
+ loadCatalogFile('publishers', base),
120
+ loadCatalogFile('taxa', base),
121
+ loadResourcesIndex(base)
122
+ ]);
123
+ const errors = [
124
+ ...checkIntegrity(catalog, resources, 'author', authors),
125
+ ...checkIntegrity(catalog, resources, 'publisher', publishers),
126
+ ...checkIntegrity(catalog, resources, 'taxon', taxa),
127
+ ...checkIntegrity(catalog, resources, 'region', places)
128
+ ];
129
+ console.table(errors);
130
+ process.exit(errors.length ? 1 : 0);
131
+ });
132
+ }
133
+ main(process.argv.slice(2));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@larsgw/formica",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "SDK and tools for data from the Library of Identification Resources",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -10,6 +10,7 @@
10
10
  "bin": {
11
11
  "loir-validate-catalog": "lib/bin/validate-catalog.js",
12
12
  "loir-validate-resources": "lib/bin/validate-resources-text.js",
13
+ "loir-validate-integrity": "lib/bin/validate-integrity.js",
13
14
  "loir-resources-process": "lib/bin/process-resources.js",
14
15
  "loir-resources-index": "lib/bin/process-resources-index.js",
15
16
  "loir-resources-check-diff": "lib/bin/check-resources-diff.js",