@myrmidon/cadmus-profile-core 2.0.0 → 5.0.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/esm2022/lib/cadmus-profile-core.module.mjs +17 -0
- package/{esm2020 → esm2022}/lib/profile-models.mjs +1 -1
- package/{esm2020 → esm2022}/lib/services/csv-reader.mjs +160 -160
- package/{esm2020 → esm2022}/lib/services/csv-thesaurus-reader.mjs +83 -83
- package/esm2022/lib/services/profile-util.service.mjs +71 -0
- package/esm2022/lib/services/text-to-file.service.mjs +34 -0
- package/{esm2020 → esm2022}/myrmidon-cadmus-profile-core.mjs +4 -4
- package/{esm2020 → esm2022}/public-api.mjs +9 -9
- package/{fesm2020 → fesm2022}/myrmidon-cadmus-profile-core.mjs +351 -352
- package/{fesm2020 → fesm2022}/myrmidon-cadmus-profile-core.mjs.map +1 -1
- package/index.d.ts +5 -5
- package/lib/cadmus-profile-core.module.d.ts +6 -6
- package/lib/profile-models.d.ts +8 -8
- package/lib/services/csv-reader.d.ts +44 -44
- package/lib/services/csv-thesaurus-reader.d.ts +27 -27
- package/lib/services/profile-util.service.d.ts +38 -38
- package/lib/services/text-to-file.service.d.ts +17 -17
- package/package.json +8 -14
- package/public-api.d.ts +6 -6
- package/esm2020/lib/cadmus-profile-core.module.mjs +0 -16
- package/esm2020/lib/services/profile-util.service.mjs +0 -70
- package/esm2020/lib/services/text-to-file.service.mjs +0 -33
- package/fesm2015/myrmidon-cadmus-profile-core.mjs +0 -371
- package/fesm2015/myrmidon-cadmus-profile-core.mjs.map +0 -1
|
@@ -1,371 +1,370 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
2
|
import { Injectable, NgModule } from '@angular/core';
|
|
3
3
|
|
|
4
|
-
const DEFAULT_OPTIONS = {
|
|
5
|
-
fieldSeparator: ',',
|
|
6
|
-
lineSeparator: '\n',
|
|
7
|
-
quote: '"',
|
|
8
|
-
trimming: true
|
|
9
|
-
};
|
|
10
|
-
const CSV_INITAL_STATE = {
|
|
11
|
-
field: 0,
|
|
12
|
-
fieldOffset: 0,
|
|
13
|
-
line: 0,
|
|
14
|
-
lineOffset: 0,
|
|
15
|
-
quoted: false,
|
|
16
|
-
appendCell: false,
|
|
17
|
-
appendField: false,
|
|
18
|
-
appendRow: false,
|
|
19
|
-
};
|
|
20
|
-
class ParseError extends Error {
|
|
21
|
-
constructor(line, column) {
|
|
22
|
-
super(`Invalid CSV at ${line}:${column}`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* CSV text reader.
|
|
27
|
-
* Refactored from https://github.com/gregoranders/ts-csv/blob/master/src/index.ts.
|
|
28
|
-
*/
|
|
29
|
-
class CsvReader {
|
|
30
|
-
constructor(text, options) {
|
|
31
|
-
this._lastRow = [];
|
|
32
|
-
this._row = [];
|
|
33
|
-
this._cell = '';
|
|
34
|
-
this._options = DEFAULT_OPTIONS;
|
|
35
|
-
this._state = { ...CSV_INITAL_STATE };
|
|
36
|
-
this._index = 0;
|
|
37
|
-
this._current = '';
|
|
38
|
-
this._previous = '';
|
|
39
|
-
this._text = text;
|
|
40
|
-
this._options = Object.assign({}, DEFAULT_OPTIONS, options || {});
|
|
41
|
-
this.reset();
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Read the next row from the CSV text.
|
|
45
|
-
*
|
|
46
|
-
* @param text - CSV text
|
|
47
|
-
* @returns array of fields, each being an array of strings.
|
|
48
|
-
* @throws Error on parse error
|
|
49
|
-
*/
|
|
50
|
-
read() {
|
|
51
|
-
while (this._index < this._text.length) {
|
|
52
|
-
this._state.appendCell = true;
|
|
53
|
-
this._previous = this._current;
|
|
54
|
-
this._current = this._text[this._index];
|
|
55
|
-
if (this.handleNext()) {
|
|
56
|
-
this._index++;
|
|
57
|
-
return this._lastRow;
|
|
58
|
-
}
|
|
59
|
-
this._index++;
|
|
60
|
-
}
|
|
61
|
-
if (this._row.length > 0) {
|
|
62
|
-
this.addField(this.fieldValue(this._cell), this._row, this._state);
|
|
63
|
-
this.nextRow(this._row, this._state);
|
|
64
|
-
this._row.length = 0;
|
|
65
|
-
return this._lastRow;
|
|
66
|
-
}
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
handleNext() {
|
|
70
|
-
this.handleQuote() ||
|
|
71
|
-
this.handleFieldSeparator() ||
|
|
72
|
-
this.handleLineSeparator();
|
|
73
|
-
return this.processState();
|
|
74
|
-
}
|
|
75
|
-
handleQuote() {
|
|
76
|
-
if (this._current === this._options.quote) {
|
|
77
|
-
if (this._index && this._previous !== '\\') {
|
|
78
|
-
this.handleQuoteNotEscaped();
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
this.handleQuoteEscaped();
|
|
82
|
-
}
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
handleQuoteEscaped() {
|
|
88
|
-
this._cell = this._cell.slice(0, Math.max(0, this._cell.length - 1));
|
|
89
|
-
}
|
|
90
|
-
handleQuoteNotEscaped() {
|
|
91
|
-
if (this._cell.length === 0 || this._state.quoted) {
|
|
92
|
-
this._state.quoted = !this._state.quoted;
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
throw new ParseError(this._state.line, this._state.lineOffset);
|
|
96
|
-
}
|
|
97
|
-
this._state.appendCell = false;
|
|
98
|
-
}
|
|
99
|
-
handleFieldSeparator() {
|
|
100
|
-
if (this._current === this._options.fieldSeparator) {
|
|
101
|
-
if (!this._state.quoted) {
|
|
102
|
-
this._state.appendCell = false;
|
|
103
|
-
this._state.appendField = true;
|
|
104
|
-
}
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
handleLineSeparator() {
|
|
110
|
-
if (this._current === this._options.lineSeparator) {
|
|
111
|
-
if (!this._state.quoted) {
|
|
112
|
-
this._state.appendCell = false;
|
|
113
|
-
this._state.appendField = true;
|
|
114
|
-
this._state.appendRow = true;
|
|
115
|
-
}
|
|
116
|
-
return true;
|
|
117
|
-
}
|
|
118
|
-
return false;
|
|
119
|
-
}
|
|
120
|
-
processState() {
|
|
121
|
-
let end = false;
|
|
122
|
-
if (this._state.appendCell) {
|
|
123
|
-
this._cell += this._current;
|
|
124
|
-
}
|
|
125
|
-
if (this._state.appendField) {
|
|
126
|
-
this.addField(this.fieldValue(this._cell), this._row, this._state);
|
|
127
|
-
this._cell = '';
|
|
128
|
-
}
|
|
129
|
-
if (this._state.appendRow) {
|
|
130
|
-
this.nextRow(this._row, this._state);
|
|
131
|
-
this._row = [];
|
|
132
|
-
end = true;
|
|
133
|
-
}
|
|
134
|
-
this._state.lineOffset++;
|
|
135
|
-
this._state.fieldOffset++;
|
|
136
|
-
return end;
|
|
137
|
-
}
|
|
138
|
-
fieldValue(cell) {
|
|
139
|
-
return cell;
|
|
140
|
-
}
|
|
141
|
-
addField(field, row, state) {
|
|
142
|
-
row.push(this._options.trimming ? field.trim() : field);
|
|
143
|
-
state.field++;
|
|
144
|
-
state.fieldOffset = -1;
|
|
145
|
-
state.appendField = false;
|
|
146
|
-
}
|
|
147
|
-
nextRow(row, state) {
|
|
148
|
-
this._lastRow = [...row];
|
|
149
|
-
state.field = 0;
|
|
150
|
-
state.line++;
|
|
151
|
-
state.lineOffset = -1;
|
|
152
|
-
state.appendRow = false;
|
|
153
|
-
}
|
|
154
|
-
reset() {
|
|
155
|
-
this._lastRow = [];
|
|
156
|
-
this._row = [];
|
|
157
|
-
this._cell = '';
|
|
158
|
-
this._state = { ...CSV_INITAL_STATE };
|
|
159
|
-
this._index = 0;
|
|
160
|
-
this._current = '';
|
|
161
|
-
this._previous = '';
|
|
162
|
-
}
|
|
4
|
+
const DEFAULT_OPTIONS = {
|
|
5
|
+
fieldSeparator: ',',
|
|
6
|
+
lineSeparator: '\n',
|
|
7
|
+
quote: '"',
|
|
8
|
+
trimming: true
|
|
9
|
+
};
|
|
10
|
+
const CSV_INITAL_STATE = {
|
|
11
|
+
field: 0,
|
|
12
|
+
fieldOffset: 0,
|
|
13
|
+
line: 0,
|
|
14
|
+
lineOffset: 0,
|
|
15
|
+
quoted: false,
|
|
16
|
+
appendCell: false,
|
|
17
|
+
appendField: false,
|
|
18
|
+
appendRow: false,
|
|
19
|
+
};
|
|
20
|
+
class ParseError extends Error {
|
|
21
|
+
constructor(line, column) {
|
|
22
|
+
super(`Invalid CSV at ${line}:${column}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* CSV text reader.
|
|
27
|
+
* Refactored from https://github.com/gregoranders/ts-csv/blob/master/src/index.ts.
|
|
28
|
+
*/
|
|
29
|
+
class CsvReader {
|
|
30
|
+
constructor(text, options) {
|
|
31
|
+
this._lastRow = [];
|
|
32
|
+
this._row = [];
|
|
33
|
+
this._cell = '';
|
|
34
|
+
this._options = DEFAULT_OPTIONS;
|
|
35
|
+
this._state = { ...CSV_INITAL_STATE };
|
|
36
|
+
this._index = 0;
|
|
37
|
+
this._current = '';
|
|
38
|
+
this._previous = '';
|
|
39
|
+
this._text = text;
|
|
40
|
+
this._options = Object.assign({}, DEFAULT_OPTIONS, options || {});
|
|
41
|
+
this.reset();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Read the next row from the CSV text.
|
|
45
|
+
*
|
|
46
|
+
* @param text - CSV text
|
|
47
|
+
* @returns array of fields, each being an array of strings.
|
|
48
|
+
* @throws Error on parse error
|
|
49
|
+
*/
|
|
50
|
+
read() {
|
|
51
|
+
while (this._index < this._text.length) {
|
|
52
|
+
this._state.appendCell = true;
|
|
53
|
+
this._previous = this._current;
|
|
54
|
+
this._current = this._text[this._index];
|
|
55
|
+
if (this.handleNext()) {
|
|
56
|
+
this._index++;
|
|
57
|
+
return this._lastRow;
|
|
58
|
+
}
|
|
59
|
+
this._index++;
|
|
60
|
+
}
|
|
61
|
+
if (this._row.length > 0) {
|
|
62
|
+
this.addField(this.fieldValue(this._cell), this._row, this._state);
|
|
63
|
+
this.nextRow(this._row, this._state);
|
|
64
|
+
this._row.length = 0;
|
|
65
|
+
return this._lastRow;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
handleNext() {
|
|
70
|
+
this.handleQuote() ||
|
|
71
|
+
this.handleFieldSeparator() ||
|
|
72
|
+
this.handleLineSeparator();
|
|
73
|
+
return this.processState();
|
|
74
|
+
}
|
|
75
|
+
handleQuote() {
|
|
76
|
+
if (this._current === this._options.quote) {
|
|
77
|
+
if (this._index && this._previous !== '\\') {
|
|
78
|
+
this.handleQuoteNotEscaped();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
this.handleQuoteEscaped();
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
handleQuoteEscaped() {
|
|
88
|
+
this._cell = this._cell.slice(0, Math.max(0, this._cell.length - 1));
|
|
89
|
+
}
|
|
90
|
+
handleQuoteNotEscaped() {
|
|
91
|
+
if (this._cell.length === 0 || this._state.quoted) {
|
|
92
|
+
this._state.quoted = !this._state.quoted;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
throw new ParseError(this._state.line, this._state.lineOffset);
|
|
96
|
+
}
|
|
97
|
+
this._state.appendCell = false;
|
|
98
|
+
}
|
|
99
|
+
handleFieldSeparator() {
|
|
100
|
+
if (this._current === this._options.fieldSeparator) {
|
|
101
|
+
if (!this._state.quoted) {
|
|
102
|
+
this._state.appendCell = false;
|
|
103
|
+
this._state.appendField = true;
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
handleLineSeparator() {
|
|
110
|
+
if (this._current === this._options.lineSeparator) {
|
|
111
|
+
if (!this._state.quoted) {
|
|
112
|
+
this._state.appendCell = false;
|
|
113
|
+
this._state.appendField = true;
|
|
114
|
+
this._state.appendRow = true;
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
processState() {
|
|
121
|
+
let end = false;
|
|
122
|
+
if (this._state.appendCell) {
|
|
123
|
+
this._cell += this._current;
|
|
124
|
+
}
|
|
125
|
+
if (this._state.appendField) {
|
|
126
|
+
this.addField(this.fieldValue(this._cell), this._row, this._state);
|
|
127
|
+
this._cell = '';
|
|
128
|
+
}
|
|
129
|
+
if (this._state.appendRow) {
|
|
130
|
+
this.nextRow(this._row, this._state);
|
|
131
|
+
this._row = [];
|
|
132
|
+
end = true;
|
|
133
|
+
}
|
|
134
|
+
this._state.lineOffset++;
|
|
135
|
+
this._state.fieldOffset++;
|
|
136
|
+
return end;
|
|
137
|
+
}
|
|
138
|
+
fieldValue(cell) {
|
|
139
|
+
return cell;
|
|
140
|
+
}
|
|
141
|
+
addField(field, row, state) {
|
|
142
|
+
row.push(this._options.trimming ? field.trim() : field);
|
|
143
|
+
state.field++;
|
|
144
|
+
state.fieldOffset = -1;
|
|
145
|
+
state.appendField = false;
|
|
146
|
+
}
|
|
147
|
+
nextRow(row, state) {
|
|
148
|
+
this._lastRow = [...row];
|
|
149
|
+
state.field = 0;
|
|
150
|
+
state.line++;
|
|
151
|
+
state.lineOffset = -1;
|
|
152
|
+
state.appendRow = false;
|
|
153
|
+
}
|
|
154
|
+
reset() {
|
|
155
|
+
this._lastRow = [];
|
|
156
|
+
this._row = [];
|
|
157
|
+
this._cell = '';
|
|
158
|
+
this._state = { ...CSV_INITAL_STATE };
|
|
159
|
+
this._index = 0;
|
|
160
|
+
this._current = '';
|
|
161
|
+
this._previous = '';
|
|
162
|
+
}
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
/**
|
|
166
|
-
* CSV thesaurus reader. This reads any number of thesauri from a CSV text.
|
|
167
|
-
* By convention, the CSV has column 1=ID and column 2=value for each
|
|
168
|
-
* thesaurus entry. Each thesaurus is started by a row having in the ID
|
|
169
|
-
* the thesaurus ID and an empty value.
|
|
170
|
-
* If a thesaurus ID is just an alias, the value starts with an equal sign
|
|
171
|
-
* (e.g. "biblio-languages,=languages") and has no other entries.
|
|
172
|
-
*/
|
|
173
|
-
class CsvThesaurusReader {
|
|
174
|
-
constructor(text, options) {
|
|
175
|
-
this._csvReader = new CsvReader(text, options);
|
|
176
|
-
this._entries = [];
|
|
177
|
-
}
|
|
178
|
-
parseLanguage(id) {
|
|
179
|
-
const i = id.lastIndexOf('@');
|
|
180
|
-
return i > -1 ? id.substring(i + 1) : 'en';
|
|
181
|
-
}
|
|
182
|
-
getThesaurus() {
|
|
183
|
-
if (!this._id) {
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
return {
|
|
187
|
-
id: this._id,
|
|
188
|
-
language: this.parseLanguage(this._id),
|
|
189
|
-
targetId: this._targetId,
|
|
190
|
-
entries: this._targetId ? undefined : [...this._entries],
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
reset() {
|
|
194
|
-
this._id = undefined;
|
|
195
|
-
this._targetId = undefined;
|
|
196
|
-
this._entries.length = 0;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Read the next thesaurus if any.
|
|
200
|
-
*
|
|
201
|
-
* @returns Thesaurus or null if end of CSV text reached.
|
|
202
|
-
*/
|
|
203
|
-
read() {
|
|
204
|
-
if (this._eof) {
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
while (true) {
|
|
208
|
-
// read the next row
|
|
209
|
-
const row = this._csvReader.read();
|
|
210
|
-
// if no more rows, ret the last thesaurus if any
|
|
211
|
-
if (!row) {
|
|
212
|
-
this._eof = true;
|
|
213
|
-
const last = this.getThesaurus();
|
|
214
|
-
this.reset();
|
|
215
|
-
return last;
|
|
216
|
-
}
|
|
217
|
-
// an empty value starts a new thesaurus
|
|
218
|
-
if (!row[1]) {
|
|
219
|
-
const prev = this.getThesaurus();
|
|
220
|
-
this.reset();
|
|
221
|
-
this._id = row[0];
|
|
222
|
-
// return the previous thesaurus if any
|
|
223
|
-
if (prev) {
|
|
224
|
-
return prev;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
else if (row[1].startsWith('=') && row[1].length > 1) {
|
|
228
|
-
// a value starting with = is the alias target
|
|
229
|
-
const prev = this.getThesaurus();
|
|
230
|
-
this.reset();
|
|
231
|
-
this._id = row[0];
|
|
232
|
-
this._targetId = row[1].substring(1);
|
|
233
|
-
// return the previous thesaurus if any
|
|
234
|
-
if (prev) {
|
|
235
|
-
return prev;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
this._entries.push({
|
|
240
|
-
id: row[0],
|
|
241
|
-
value: row[1],
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
165
|
+
/**
|
|
166
|
+
* CSV thesaurus reader. This reads any number of thesauri from a CSV text.
|
|
167
|
+
* By convention, the CSV has column 1=ID and column 2=value for each
|
|
168
|
+
* thesaurus entry. Each thesaurus is started by a row having in the ID
|
|
169
|
+
* the thesaurus ID and an empty value.
|
|
170
|
+
* If a thesaurus ID is just an alias, the value starts with an equal sign
|
|
171
|
+
* (e.g. "biblio-languages,=languages") and has no other entries.
|
|
172
|
+
*/
|
|
173
|
+
class CsvThesaurusReader {
|
|
174
|
+
constructor(text, options) {
|
|
175
|
+
this._csvReader = new CsvReader(text, options);
|
|
176
|
+
this._entries = [];
|
|
177
|
+
}
|
|
178
|
+
parseLanguage(id) {
|
|
179
|
+
const i = id.lastIndexOf('@');
|
|
180
|
+
return i > -1 ? id.substring(i + 1) : 'en';
|
|
181
|
+
}
|
|
182
|
+
getThesaurus() {
|
|
183
|
+
if (!this._id) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
id: this._id,
|
|
188
|
+
language: this.parseLanguage(this._id),
|
|
189
|
+
targetId: this._targetId,
|
|
190
|
+
entries: this._targetId ? undefined : [...this._entries],
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
reset() {
|
|
194
|
+
this._id = undefined;
|
|
195
|
+
this._targetId = undefined;
|
|
196
|
+
this._entries.length = 0;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Read the next thesaurus if any.
|
|
200
|
+
*
|
|
201
|
+
* @returns Thesaurus or null if end of CSV text reached.
|
|
202
|
+
*/
|
|
203
|
+
read() {
|
|
204
|
+
if (this._eof) {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
while (true) {
|
|
208
|
+
// read the next row
|
|
209
|
+
const row = this._csvReader.read();
|
|
210
|
+
// if no more rows, ret the last thesaurus if any
|
|
211
|
+
if (!row) {
|
|
212
|
+
this._eof = true;
|
|
213
|
+
const last = this.getThesaurus();
|
|
214
|
+
this.reset();
|
|
215
|
+
return last;
|
|
216
|
+
}
|
|
217
|
+
// an empty value starts a new thesaurus
|
|
218
|
+
if (!row[1]) {
|
|
219
|
+
const prev = this.getThesaurus();
|
|
220
|
+
this.reset();
|
|
221
|
+
this._id = row[0];
|
|
222
|
+
// return the previous thesaurus if any
|
|
223
|
+
if (prev) {
|
|
224
|
+
return prev;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else if (row[1].startsWith('=') && row[1].length > 1) {
|
|
228
|
+
// a value starting with = is the alias target
|
|
229
|
+
const prev = this.getThesaurus();
|
|
230
|
+
this.reset();
|
|
231
|
+
this._id = row[0];
|
|
232
|
+
this._targetId = row[1].substring(1);
|
|
233
|
+
// return the previous thesaurus if any
|
|
234
|
+
if (prev) {
|
|
235
|
+
return prev;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
this._entries.push({
|
|
240
|
+
id: row[0],
|
|
241
|
+
value: row[1],
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
/**
|
|
249
|
-
* Utility functions for Cadmus profile data.
|
|
250
|
-
*/
|
|
251
|
-
class ProfileUtilService {
|
|
252
|
-
constructor() { }
|
|
253
|
-
/**
|
|
254
|
-
* Group an array of objects by 1 or more of its string properties.
|
|
255
|
-
*
|
|
256
|
-
* @param arr The array of objects to group.
|
|
257
|
-
* @param keys The keys to be used in grouping objects. It is assumed
|
|
258
|
-
* that such keys are all string properties.
|
|
259
|
-
* @returns An object where each property is a key with value equal
|
|
260
|
-
* to an array of items.
|
|
261
|
-
*/
|
|
262
|
-
groupIntoObject(arr, keys) {
|
|
263
|
-
// https://gist.github.com/robmathers/1830ce09695f759bf2c4df15c29dd22d
|
|
264
|
-
return arr.reduce((storage, item) => {
|
|
265
|
-
const compositeKey = keys.map((key) => `${item[key]}`).join(':');
|
|
266
|
-
if (storage[compositeKey]) {
|
|
267
|
-
storage[compositeKey].push(item);
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
storage[compositeKey] = [item];
|
|
271
|
-
}
|
|
272
|
-
return storage;
|
|
273
|
-
}, {});
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Group an array of objects by 1 or more of its string properties,
|
|
277
|
-
* storing the result into an array of KeyedGroup<T>.
|
|
278
|
-
*
|
|
279
|
-
* @param arr The array of objects to group.
|
|
280
|
-
* @param keys The keys to be used in grouping objects. It is assumed
|
|
281
|
-
* that such keys are all string properties.
|
|
282
|
-
* @returns An array of KeyedGroup<T>.
|
|
283
|
-
*/
|
|
284
|
-
groupIntoKeyedGroups(arr, keys) {
|
|
285
|
-
let storage = new Map();
|
|
286
|
-
storage = arr.reduce((storage, item) => {
|
|
287
|
-
const compositeKey = keys.map((key) => `${item[key]}`).join(':');
|
|
288
|
-
if (storage.has(compositeKey)) {
|
|
289
|
-
storage.set(compositeKey, [...(storage.get(compositeKey) || []), item]);
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
storage.set(compositeKey, [item]);
|
|
293
|
-
}
|
|
294
|
-
return storage;
|
|
295
|
-
}, storage);
|
|
296
|
-
const groups = [];
|
|
297
|
-
const sortedKeys = [...storage.keys()].sort();
|
|
298
|
-
for (let key of sortedKeys) {
|
|
299
|
-
groups.push({
|
|
300
|
-
key: key,
|
|
301
|
-
items: storage.get(key),
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
return groups;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
310
|
-
type: Injectable,
|
|
311
|
-
args: [{
|
|
312
|
-
providedIn: 'root',
|
|
313
|
-
}]
|
|
248
|
+
/**
|
|
249
|
+
* Utility functions for Cadmus profile data.
|
|
250
|
+
*/
|
|
251
|
+
class ProfileUtilService {
|
|
252
|
+
constructor() { }
|
|
253
|
+
/**
|
|
254
|
+
* Group an array of objects by 1 or more of its string properties.
|
|
255
|
+
*
|
|
256
|
+
* @param arr The array of objects to group.
|
|
257
|
+
* @param keys The keys to be used in grouping objects. It is assumed
|
|
258
|
+
* that such keys are all string properties.
|
|
259
|
+
* @returns An object where each property is a key with value equal
|
|
260
|
+
* to an array of items.
|
|
261
|
+
*/
|
|
262
|
+
groupIntoObject(arr, keys) {
|
|
263
|
+
// https://gist.github.com/robmathers/1830ce09695f759bf2c4df15c29dd22d
|
|
264
|
+
return arr.reduce((storage, item) => {
|
|
265
|
+
const compositeKey = keys.map((key) => `${item[key]}`).join(':');
|
|
266
|
+
if (storage[compositeKey]) {
|
|
267
|
+
storage[compositeKey].push(item);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
storage[compositeKey] = [item];
|
|
271
|
+
}
|
|
272
|
+
return storage;
|
|
273
|
+
}, {});
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Group an array of objects by 1 or more of its string properties,
|
|
277
|
+
* storing the result into an array of KeyedGroup<T>.
|
|
278
|
+
*
|
|
279
|
+
* @param arr The array of objects to group.
|
|
280
|
+
* @param keys The keys to be used in grouping objects. It is assumed
|
|
281
|
+
* that such keys are all string properties.
|
|
282
|
+
* @returns An array of KeyedGroup<T>.
|
|
283
|
+
*/
|
|
284
|
+
groupIntoKeyedGroups(arr, keys) {
|
|
285
|
+
let storage = new Map();
|
|
286
|
+
storage = arr.reduce((storage, item) => {
|
|
287
|
+
const compositeKey = keys.map((key) => `${item[key]}`).join(':');
|
|
288
|
+
if (storage.has(compositeKey)) {
|
|
289
|
+
storage.set(compositeKey, [...(storage.get(compositeKey) || []), item]);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
storage.set(compositeKey, [item]);
|
|
293
|
+
}
|
|
294
|
+
return storage;
|
|
295
|
+
}, storage);
|
|
296
|
+
const groups = [];
|
|
297
|
+
const sortedKeys = [...storage.keys()].sort();
|
|
298
|
+
for (let key of sortedKeys) {
|
|
299
|
+
groups.push({
|
|
300
|
+
key: key,
|
|
301
|
+
items: storage.get(key),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return groups;
|
|
305
|
+
}
|
|
306
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: ProfileUtilService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
307
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: ProfileUtilService, providedIn: 'root' }); }
|
|
308
|
+
}
|
|
309
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: ProfileUtilService, decorators: [{
|
|
310
|
+
type: Injectable,
|
|
311
|
+
args: [{
|
|
312
|
+
providedIn: 'root',
|
|
313
|
+
}]
|
|
314
314
|
}], ctorParameters: function () { return []; } });
|
|
315
315
|
|
|
316
|
-
/**
|
|
317
|
-
* Service to let user download a text file generated into a string.
|
|
318
|
-
*/
|
|
319
|
-
class TextToFileService {
|
|
320
|
-
constructor() { }
|
|
321
|
-
/**
|
|
322
|
-
* Save the specified text into a file.
|
|
323
|
-
*
|
|
324
|
-
* @param text The text content to be saved.
|
|
325
|
-
* @param name The file name.
|
|
326
|
-
* @param contentType The content type.
|
|
327
|
-
*/
|
|
328
|
-
saveToFile(text, name, contentType = 'text/plain') {
|
|
329
|
-
// https://robkendal.co.uk/blog/2020-04-17-saving-text-to-client-side-file-using-vanilla-js
|
|
330
|
-
const a = document.createElement('a');
|
|
331
|
-
const file = new Blob([text], { type: contentType });
|
|
332
|
-
a.href = URL.createObjectURL(file);
|
|
333
|
-
a.download = name;
|
|
334
|
-
a.click();
|
|
335
|
-
URL.revokeObjectURL(a.href);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
341
|
-
type: Injectable,
|
|
342
|
-
args: [{
|
|
343
|
-
providedIn: 'root',
|
|
344
|
-
}]
|
|
316
|
+
/**
|
|
317
|
+
* Service to let user download a text file generated into a string.
|
|
318
|
+
*/
|
|
319
|
+
class TextToFileService {
|
|
320
|
+
constructor() { }
|
|
321
|
+
/**
|
|
322
|
+
* Save the specified text into a file.
|
|
323
|
+
*
|
|
324
|
+
* @param text The text content to be saved.
|
|
325
|
+
* @param name The file name.
|
|
326
|
+
* @param contentType The content type.
|
|
327
|
+
*/
|
|
328
|
+
saveToFile(text, name, contentType = 'text/plain') {
|
|
329
|
+
// https://robkendal.co.uk/blog/2020-04-17-saving-text-to-client-side-file-using-vanilla-js
|
|
330
|
+
const a = document.createElement('a');
|
|
331
|
+
const file = new Blob([text], { type: contentType });
|
|
332
|
+
a.href = URL.createObjectURL(file);
|
|
333
|
+
a.download = name;
|
|
334
|
+
a.click();
|
|
335
|
+
URL.revokeObjectURL(a.href);
|
|
336
|
+
}
|
|
337
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: TextToFileService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
338
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: TextToFileService, providedIn: 'root' }); }
|
|
339
|
+
}
|
|
340
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: TextToFileService, decorators: [{
|
|
341
|
+
type: Injectable,
|
|
342
|
+
args: [{
|
|
343
|
+
providedIn: 'root',
|
|
344
|
+
}]
|
|
345
345
|
}], ctorParameters: function () { return []; } });
|
|
346
346
|
|
|
347
|
-
class CadmusProfileCoreModule {
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
353
|
-
type: NgModule,
|
|
354
|
-
args: [{
|
|
355
|
-
declarations: [],
|
|
356
|
-
imports: [],
|
|
357
|
-
exports: [],
|
|
358
|
-
}]
|
|
347
|
+
class CadmusProfileCoreModule {
|
|
348
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: CadmusProfileCoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
|
|
349
|
+
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.1", ngImport: i0, type: CadmusProfileCoreModule }); }
|
|
350
|
+
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: CadmusProfileCoreModule }); }
|
|
351
|
+
}
|
|
352
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: CadmusProfileCoreModule, decorators: [{
|
|
353
|
+
type: NgModule,
|
|
354
|
+
args: [{
|
|
355
|
+
declarations: [],
|
|
356
|
+
imports: [],
|
|
357
|
+
exports: [],
|
|
358
|
+
}]
|
|
359
359
|
}] });
|
|
360
360
|
|
|
361
|
-
/*
|
|
362
|
-
* Public API Surface of cadmus-profile-core
|
|
361
|
+
/*
|
|
362
|
+
* Public API Surface of cadmus-profile-core
|
|
363
363
|
*/
|
|
364
364
|
|
|
365
|
-
/**
|
|
366
|
-
* Generated bundle index. Do not edit.
|
|
365
|
+
/**
|
|
366
|
+
* Generated bundle index. Do not edit.
|
|
367
367
|
*/
|
|
368
368
|
|
|
369
369
|
export { CadmusProfileCoreModule, CsvReader, CsvThesaurusReader, ProfileUtilService, TextToFileService };
|
|
370
370
|
//# sourceMappingURL=myrmidon-cadmus-profile-core.mjs.map
|
|
371
|
-
//# sourceMappingURL=myrmidon-cadmus-profile-core.mjs.map
|