namefully 1.3.0 → 2.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/dist/{lib → cjs}/builder.js +15 -13
- package/dist/cjs/config.js +100 -0
- package/dist/{lib → cjs}/constants.js +1 -1
- package/dist/{lib → cjs}/error.js +8 -5
- package/dist/cjs/fullname.js +102 -0
- package/dist/{lib → cjs}/index.js +18 -15
- package/dist/cjs/name.js +218 -0
- package/dist/cjs/namefully.js +391 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/parser.js +135 -0
- package/dist/{lib → cjs}/types.js +40 -36
- package/dist/{lib → cjs}/utils.js +17 -17
- package/dist/cjs/validator.js +266 -0
- package/dist/{types → esm}/builder.d.ts +8 -8
- package/dist/esm/builder.js +78 -0
- package/dist/{types → esm}/config.d.ts +1 -1
- package/dist/esm/config.js +96 -0
- package/dist/{types → esm}/constants.d.ts +1 -1
- package/dist/esm/constants.js +27 -0
- package/dist/{types → esm}/error.d.ts +2 -3
- package/dist/esm/error.js +87 -0
- package/dist/{types/full-name.d.ts → esm/fullname.d.ts} +3 -3
- package/dist/esm/fullname.js +98 -0
- package/dist/esm/index.d.ts +25 -0
- package/dist/esm/index.js +12 -0
- package/dist/{types → esm}/name.d.ts +2 -1
- package/dist/esm/name.js +211 -0
- package/dist/{types → esm}/namefully.d.ts +8 -8
- package/dist/esm/namefully.js +387 -0
- package/dist/esm/package.json +1 -0
- package/dist/{types → esm}/parser.d.ts +4 -4
- package/dist/esm/parser.js +127 -0
- package/dist/esm/types.js +106 -0
- package/dist/{types → esm}/utils.d.ts +1 -2
- package/dist/esm/utils.js +96 -0
- package/dist/{types → esm}/validator.d.ts +3 -3
- package/dist/esm/validator.js +259 -0
- package/dist/namefully.js +1580 -0
- package/dist/namefully.min.js +1 -0
- package/package.json +44 -27
- package/readme.md +1 -1
- package/dist/lib/config.js +0 -112
- package/dist/lib/full-name.js +0 -115
- package/dist/lib/name.js +0 -230
- package/dist/lib/namefully.js +0 -417
- package/dist/lib/parser.js +0 -144
- package/dist/lib/validator.js +0 -285
- package/dist/types/index.d.ts +0 -25
- package/dist/umd/namefully.js +0 -1931
- package/dist/umd/namefully.min.js +0 -1
- /package/dist/{types → esm}/types.d.ts +0 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Namefully = void 0;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const error_js_1 = require("./error.js");
|
|
6
|
+
const name_js_1 = require("./name.js");
|
|
7
|
+
const parser_js_1 = require("./parser.js");
|
|
8
|
+
const types_js_1 = require("./types.js");
|
|
9
|
+
const utils_js_1 = require("./utils.js");
|
|
10
|
+
class Namefully {
|
|
11
|
+
#fullName;
|
|
12
|
+
constructor(names, options) {
|
|
13
|
+
this.#fullName = this.#toParser(names).parse(options);
|
|
14
|
+
}
|
|
15
|
+
static tryParse(text, index) {
|
|
16
|
+
try {
|
|
17
|
+
return new Namefully(parser_js_1.Parser.build(text, index));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
static async parse(text, index) {
|
|
24
|
+
return parser_js_1.Parser.buildAsync(text, index).then((parser) => new Namefully(parser));
|
|
25
|
+
}
|
|
26
|
+
get config() {
|
|
27
|
+
return this.#fullName.config;
|
|
28
|
+
}
|
|
29
|
+
get length() {
|
|
30
|
+
return this.birth.length;
|
|
31
|
+
}
|
|
32
|
+
get prefix() {
|
|
33
|
+
return this.#fullName.prefix?.toString();
|
|
34
|
+
}
|
|
35
|
+
get first() {
|
|
36
|
+
return this.firstName();
|
|
37
|
+
}
|
|
38
|
+
get middle() {
|
|
39
|
+
return this.hasMiddle ? this.middleName()[0] : undefined;
|
|
40
|
+
}
|
|
41
|
+
get hasMiddle() {
|
|
42
|
+
return this.#fullName.has(types_js_1.Namon.MIDDLE_NAME);
|
|
43
|
+
}
|
|
44
|
+
get last() {
|
|
45
|
+
return this.lastName();
|
|
46
|
+
}
|
|
47
|
+
get suffix() {
|
|
48
|
+
return this.#fullName.suffix?.toString();
|
|
49
|
+
}
|
|
50
|
+
get birth() {
|
|
51
|
+
return this.birthName();
|
|
52
|
+
}
|
|
53
|
+
get short() {
|
|
54
|
+
return this.shorten();
|
|
55
|
+
}
|
|
56
|
+
get long() {
|
|
57
|
+
return this.birth;
|
|
58
|
+
}
|
|
59
|
+
get full() {
|
|
60
|
+
return this.fullName();
|
|
61
|
+
}
|
|
62
|
+
get public() {
|
|
63
|
+
return this.format('f $l');
|
|
64
|
+
}
|
|
65
|
+
get salutation() {
|
|
66
|
+
return this.format('p l');
|
|
67
|
+
}
|
|
68
|
+
toString() {
|
|
69
|
+
return this.full;
|
|
70
|
+
}
|
|
71
|
+
get(namon) {
|
|
72
|
+
if (namon.equal(types_js_1.Namon.PREFIX))
|
|
73
|
+
return this.#fullName.prefix;
|
|
74
|
+
if (namon.equal(types_js_1.Namon.FIRST_NAME))
|
|
75
|
+
return this.#fullName.firstName;
|
|
76
|
+
if (namon.equal(types_js_1.Namon.MIDDLE_NAME))
|
|
77
|
+
return this.#fullName.middleName;
|
|
78
|
+
if (namon.equal(types_js_1.Namon.LAST_NAME))
|
|
79
|
+
return this.#fullName.lastName;
|
|
80
|
+
if (namon.equal(types_js_1.Namon.SUFFIX))
|
|
81
|
+
return this.#fullName.suffix;
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
equal(other) {
|
|
85
|
+
return this.toString() === other.toString();
|
|
86
|
+
}
|
|
87
|
+
toJson() {
|
|
88
|
+
return {
|
|
89
|
+
prefix: this.prefix,
|
|
90
|
+
firstName: this.first,
|
|
91
|
+
middleName: this.middleName(),
|
|
92
|
+
lastName: this.last,
|
|
93
|
+
suffix: this.suffix,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
has(namon) {
|
|
97
|
+
return this.#fullName.has(namon);
|
|
98
|
+
}
|
|
99
|
+
fullName(orderedBy) {
|
|
100
|
+
const sep = this.config.ending ? ',' : '';
|
|
101
|
+
const names = [];
|
|
102
|
+
orderedBy = orderedBy || this.config.orderedBy;
|
|
103
|
+
if (this.prefix)
|
|
104
|
+
names.push(this.prefix);
|
|
105
|
+
if (orderedBy === types_js_1.NameOrder.FIRST_NAME) {
|
|
106
|
+
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
names.push(this.last, this.first, this.middleName().join(' ') + sep);
|
|
110
|
+
}
|
|
111
|
+
if (this.suffix)
|
|
112
|
+
names.push(this.suffix);
|
|
113
|
+
return names.join(' ').trim();
|
|
114
|
+
}
|
|
115
|
+
birthName(orderedBy) {
|
|
116
|
+
orderedBy = orderedBy || this.config.orderedBy;
|
|
117
|
+
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
118
|
+
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
119
|
+
: [this.last, this.first, ...this.middleName()].join(' ');
|
|
120
|
+
}
|
|
121
|
+
firstName(withMore = true) {
|
|
122
|
+
return this.#fullName.firstName.toString(withMore);
|
|
123
|
+
}
|
|
124
|
+
middleName() {
|
|
125
|
+
return this.#fullName.middleName.map((n) => n.value);
|
|
126
|
+
}
|
|
127
|
+
lastName(format) {
|
|
128
|
+
return this.#fullName.lastName.toString(format);
|
|
129
|
+
}
|
|
130
|
+
initials(options) {
|
|
131
|
+
const firstInits = this.#fullName.firstName.initials();
|
|
132
|
+
const midInits = this.#fullName.middleName.map((n) => n.initials()[0]);
|
|
133
|
+
const lastInits = this.#fullName.lastName.initials();
|
|
134
|
+
if (options?.asJson)
|
|
135
|
+
return { firstName: firstInits, middleName: midInits, lastName: lastInits };
|
|
136
|
+
const initials = [];
|
|
137
|
+
const { orderedBy = this.config.orderedBy, only = types_js_1.NameType.BIRTH_NAME } = options ?? {};
|
|
138
|
+
if (only !== types_js_1.NameType.BIRTH_NAME) {
|
|
139
|
+
if (only === types_js_1.NameType.FIRST_NAME) {
|
|
140
|
+
initials.push(...firstInits);
|
|
141
|
+
}
|
|
142
|
+
else if (only === types_js_1.NameType.MIDDLE_NAME) {
|
|
143
|
+
initials.push(...midInits);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
initials.push(...lastInits);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (orderedBy === types_js_1.NameOrder.FIRST_NAME) {
|
|
150
|
+
initials.push(...firstInits, ...midInits, ...lastInits);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
initials.push(...lastInits, ...firstInits, ...midInits);
|
|
154
|
+
}
|
|
155
|
+
return initials;
|
|
156
|
+
}
|
|
157
|
+
shorten(orderedBy) {
|
|
158
|
+
orderedBy = orderedBy || this.config.orderedBy;
|
|
159
|
+
return orderedBy === types_js_1.NameOrder.FIRST_NAME
|
|
160
|
+
? [this.#fullName.firstName.value, this.#fullName.lastName.toString()].join(' ')
|
|
161
|
+
: [this.#fullName.lastName.toString(), this.#fullName.firstName.value].join(' ');
|
|
162
|
+
}
|
|
163
|
+
flatten(options) {
|
|
164
|
+
const { by = types_js_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
165
|
+
if (this.length <= limit)
|
|
166
|
+
return this.full;
|
|
167
|
+
const sep = withPeriod ? '.' : '';
|
|
168
|
+
const fn = this.#fullName.firstName.toString();
|
|
169
|
+
const mn = this.middleName().join(' ');
|
|
170
|
+
const ln = this.#fullName.lastName.toString();
|
|
171
|
+
const hasMid = this.hasMiddle;
|
|
172
|
+
const f = this.#fullName.firstName.initials(withMore).join(sep + ' ') + sep;
|
|
173
|
+
const l = this.#fullName.lastName.initials(surname).join(sep + ' ') + sep;
|
|
174
|
+
const m = hasMid ? this.#fullName.middleName.map((n) => n.initials()[0]).join(sep + ' ') + sep : '';
|
|
175
|
+
let name = [];
|
|
176
|
+
if (this.config.orderedBy === types_js_1.NameOrder.FIRST_NAME) {
|
|
177
|
+
switch (by) {
|
|
178
|
+
case types_js_1.Flat.FIRST_NAME:
|
|
179
|
+
name = hasMid ? [f, mn, ln] : [f, ln];
|
|
180
|
+
break;
|
|
181
|
+
case types_js_1.Flat.LAST_NAME:
|
|
182
|
+
name = hasMid ? [fn, mn, l] : [fn, l];
|
|
183
|
+
break;
|
|
184
|
+
case types_js_1.Flat.MIDDLE_NAME:
|
|
185
|
+
name = hasMid ? [fn, m, ln] : [fn, ln];
|
|
186
|
+
break;
|
|
187
|
+
case types_js_1.Flat.FIRST_MID:
|
|
188
|
+
name = hasMid ? [f, m, ln] : [f, ln];
|
|
189
|
+
break;
|
|
190
|
+
case types_js_1.Flat.MID_LAST:
|
|
191
|
+
name = hasMid ? [fn, m, l] : [fn, l];
|
|
192
|
+
break;
|
|
193
|
+
case types_js_1.Flat.ALL:
|
|
194
|
+
name = hasMid ? [f, m, l] : [f, l];
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
switch (by) {
|
|
200
|
+
case types_js_1.Flat.FIRST_NAME:
|
|
201
|
+
name = hasMid ? [ln, f, mn] : [ln, f];
|
|
202
|
+
break;
|
|
203
|
+
case types_js_1.Flat.LAST_NAME:
|
|
204
|
+
name = hasMid ? [l, fn, mn] : [l, fn];
|
|
205
|
+
break;
|
|
206
|
+
case types_js_1.Flat.MIDDLE_NAME:
|
|
207
|
+
name = hasMid ? [ln, fn, m] : [ln, fn];
|
|
208
|
+
break;
|
|
209
|
+
case types_js_1.Flat.FIRST_MID:
|
|
210
|
+
name = hasMid ? [ln, f, m] : [ln, f];
|
|
211
|
+
break;
|
|
212
|
+
case types_js_1.Flat.MID_LAST:
|
|
213
|
+
name = hasMid ? [l, fn, m] : [l, fn];
|
|
214
|
+
break;
|
|
215
|
+
case types_js_1.Flat.ALL:
|
|
216
|
+
name = hasMid ? [l, f, m] : [l, f];
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const flat = name.join(' ');
|
|
221
|
+
if (recursive && flat.length > limit) {
|
|
222
|
+
const next = by === types_js_1.Flat.FIRST_NAME
|
|
223
|
+
? types_js_1.Flat.MIDDLE_NAME
|
|
224
|
+
: by === types_js_1.Flat.MIDDLE_NAME
|
|
225
|
+
? types_js_1.Flat.LAST_NAME
|
|
226
|
+
: by === types_js_1.Flat.LAST_NAME
|
|
227
|
+
? types_js_1.Flat.FIRST_MID
|
|
228
|
+
: by === types_js_1.Flat.FIRST_MID
|
|
229
|
+
? types_js_1.Flat.MID_LAST
|
|
230
|
+
: by === types_js_1.Flat.MID_LAST
|
|
231
|
+
? types_js_1.Flat.ALL
|
|
232
|
+
: by === types_js_1.Flat.ALL
|
|
233
|
+
? types_js_1.Flat.ALL
|
|
234
|
+
: by;
|
|
235
|
+
if (next === by)
|
|
236
|
+
return flat;
|
|
237
|
+
return this.flatten({ ...options, by: next });
|
|
238
|
+
}
|
|
239
|
+
return flat;
|
|
240
|
+
}
|
|
241
|
+
zip(by = types_js_1.Flat.MID_LAST, withPeriod = true) {
|
|
242
|
+
return this.flatten({ limit: 0, by, withPeriod });
|
|
243
|
+
}
|
|
244
|
+
format(pattern) {
|
|
245
|
+
if (pattern === 'short')
|
|
246
|
+
return this.short;
|
|
247
|
+
if (pattern === 'long')
|
|
248
|
+
return this.long;
|
|
249
|
+
if (pattern === 'public')
|
|
250
|
+
return this.public;
|
|
251
|
+
if (pattern === 'official')
|
|
252
|
+
pattern = 'o';
|
|
253
|
+
let group = '';
|
|
254
|
+
const formatted = [];
|
|
255
|
+
for (const char of pattern.split('')) {
|
|
256
|
+
if (constants_js_1.ALLOWED_TOKENS.indexOf(char) === -1) {
|
|
257
|
+
throw new error_js_1.NotAllowedError({
|
|
258
|
+
source: this.full,
|
|
259
|
+
operation: 'format',
|
|
260
|
+
message: `unsupported character <${char}> from ${pattern}.`,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
group += char;
|
|
264
|
+
if (char === '$')
|
|
265
|
+
continue;
|
|
266
|
+
formatted.push(this.#map(group) ?? '');
|
|
267
|
+
group = '';
|
|
268
|
+
}
|
|
269
|
+
return formatted.join('').trim();
|
|
270
|
+
}
|
|
271
|
+
flip() {
|
|
272
|
+
this.config.updateOrder(this.config.orderedBy === types_js_1.NameOrder.FIRST_NAME ? types_js_1.NameOrder.LAST_NAME : types_js_1.NameOrder.FIRST_NAME);
|
|
273
|
+
}
|
|
274
|
+
split(separator = /[' -]/g) {
|
|
275
|
+
return this.birth.replace(separator, ' ').split(' ');
|
|
276
|
+
}
|
|
277
|
+
join(separator = '') {
|
|
278
|
+
return this.split().join(separator);
|
|
279
|
+
}
|
|
280
|
+
toUpperCase() {
|
|
281
|
+
return this.birth.toUpperCase();
|
|
282
|
+
}
|
|
283
|
+
toLowerCase() {
|
|
284
|
+
return this.birth.toLowerCase();
|
|
285
|
+
}
|
|
286
|
+
toCamelCase() {
|
|
287
|
+
return (0, utils_js_1.decapitalize)(this.toPascalCase());
|
|
288
|
+
}
|
|
289
|
+
toPascalCase() {
|
|
290
|
+
return this.split()
|
|
291
|
+
.map((n) => (0, utils_js_1.capitalize)(n))
|
|
292
|
+
.join('');
|
|
293
|
+
}
|
|
294
|
+
toSnakeCase() {
|
|
295
|
+
return this.split()
|
|
296
|
+
.map((n) => n.toLowerCase())
|
|
297
|
+
.join('_');
|
|
298
|
+
}
|
|
299
|
+
toHyphenCase() {
|
|
300
|
+
return this.split()
|
|
301
|
+
.map((n) => n.toLowerCase())
|
|
302
|
+
.join('-');
|
|
303
|
+
}
|
|
304
|
+
toDotCase() {
|
|
305
|
+
return this.split()
|
|
306
|
+
.map((n) => n.toLowerCase())
|
|
307
|
+
.join('.');
|
|
308
|
+
}
|
|
309
|
+
toToggleCase() {
|
|
310
|
+
return (0, utils_js_1.toggleCase)(this.birth);
|
|
311
|
+
}
|
|
312
|
+
#toParser(raw) {
|
|
313
|
+
if (raw instanceof parser_js_1.Parser)
|
|
314
|
+
return raw;
|
|
315
|
+
if (typeof raw === 'string')
|
|
316
|
+
return new parser_js_1.StringParser(raw);
|
|
317
|
+
if ((0, utils_js_1.isStringArray)(raw))
|
|
318
|
+
return new parser_js_1.ArrayStringParser(raw);
|
|
319
|
+
if ((0, name_js_1.isNameArray)(raw))
|
|
320
|
+
return new parser_js_1.ArrayNameParser(raw);
|
|
321
|
+
if (typeof raw === 'object')
|
|
322
|
+
return new parser_js_1.NamaParser(raw);
|
|
323
|
+
throw new error_js_1.InputError({ source: raw, message: 'Cannot parse raw data. Review expected data types.' });
|
|
324
|
+
}
|
|
325
|
+
#map(char) {
|
|
326
|
+
switch (char) {
|
|
327
|
+
case '.':
|
|
328
|
+
case ',':
|
|
329
|
+
case ' ':
|
|
330
|
+
case '-':
|
|
331
|
+
case '_':
|
|
332
|
+
return char;
|
|
333
|
+
case 'b':
|
|
334
|
+
return this.birth;
|
|
335
|
+
case 'B':
|
|
336
|
+
return this.birth.toUpperCase();
|
|
337
|
+
case 'f':
|
|
338
|
+
return this.first;
|
|
339
|
+
case 'F':
|
|
340
|
+
return this.first.toUpperCase();
|
|
341
|
+
case 'l':
|
|
342
|
+
return this.last;
|
|
343
|
+
case 'L':
|
|
344
|
+
return this.last.toUpperCase();
|
|
345
|
+
case 'm':
|
|
346
|
+
case 'M':
|
|
347
|
+
return char === 'm' ? this.middleName().join(' ') : this.middleName().join(' ').toUpperCase();
|
|
348
|
+
case 'o':
|
|
349
|
+
case 'O':
|
|
350
|
+
return ((character) => {
|
|
351
|
+
const sep = this.config.ending ? ',' : '', names = [];
|
|
352
|
+
if (this.prefix)
|
|
353
|
+
names.push(this.prefix);
|
|
354
|
+
names.push(`${this.last},`.toUpperCase());
|
|
355
|
+
if (this.hasMiddle) {
|
|
356
|
+
names.push(this.first, this.middleName().join(' ') + sep);
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
names.push(this.first + sep);
|
|
360
|
+
}
|
|
361
|
+
if (this.suffix)
|
|
362
|
+
names.push(this.suffix);
|
|
363
|
+
const nama = names.join(' ').trim();
|
|
364
|
+
return character === 'o' ? nama : nama.toUpperCase();
|
|
365
|
+
})(char);
|
|
366
|
+
case 'p':
|
|
367
|
+
return this.prefix;
|
|
368
|
+
case 'P':
|
|
369
|
+
return this.prefix?.toUpperCase();
|
|
370
|
+
case 's':
|
|
371
|
+
return this.suffix;
|
|
372
|
+
case 'S':
|
|
373
|
+
return this.suffix?.toUpperCase();
|
|
374
|
+
case '$f':
|
|
375
|
+
case '$F':
|
|
376
|
+
return this.#fullName.firstName.initials()[0];
|
|
377
|
+
case '$l':
|
|
378
|
+
case '$L':
|
|
379
|
+
return this.#fullName.lastName.initials()[0];
|
|
380
|
+
case '$m':
|
|
381
|
+
case '$M':
|
|
382
|
+
return this.hasMiddle ? this.middle[0] : undefined;
|
|
383
|
+
default:
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
exports.Namefully = Namefully;
|
|
389
|
+
exports.default = (names, options) => {
|
|
390
|
+
return new Namefully(names, options);
|
|
391
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayNameParser = exports.NamaParser = exports.ArrayStringParser = exports.StringParser = exports.Parser = void 0;
|
|
4
|
+
const fullname_js_1 = require("./fullname.js");
|
|
5
|
+
const config_js_1 = require("./config.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const validator_js_1 = require("./validator.js");
|
|
8
|
+
const name_js_1 = require("./name.js");
|
|
9
|
+
const types_js_1 = require("./types.js");
|
|
10
|
+
const error_js_1 = require("./error.js");
|
|
11
|
+
class Parser {
|
|
12
|
+
raw;
|
|
13
|
+
constructor(raw) {
|
|
14
|
+
this.raw = raw;
|
|
15
|
+
}
|
|
16
|
+
static build(text, index) {
|
|
17
|
+
const parts = text.trim().split(types_js_1.Separator.SPACE.token);
|
|
18
|
+
const length = parts.length;
|
|
19
|
+
if (index instanceof utils_js_1.NameIndex) {
|
|
20
|
+
const names = Object.entries(index.toJson())
|
|
21
|
+
.filter(([, position]) => position > -1 && position < length)
|
|
22
|
+
.map(([key, position]) => new name_js_1.Name(parts[position], types_js_1.Namon.all.get(key)));
|
|
23
|
+
return new ArrayNameParser(names);
|
|
24
|
+
}
|
|
25
|
+
if (length < 2) {
|
|
26
|
+
throw new error_js_1.InputError({
|
|
27
|
+
source: text,
|
|
28
|
+
message: 'cannot build from invalid input',
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
else if (length === 2 || length === 3) {
|
|
32
|
+
return new StringParser(text);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const last = parts.pop();
|
|
36
|
+
const [first, ...middles] = parts;
|
|
37
|
+
return new ArrayStringParser([first, middles.join(' '), last]);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
static buildAsync(text, index) {
|
|
41
|
+
try {
|
|
42
|
+
return Promise.resolve(Parser.build(text, index));
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return Promise.reject(error);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.Parser = Parser;
|
|
50
|
+
class StringParser extends Parser {
|
|
51
|
+
parse(options) {
|
|
52
|
+
const config = config_js_1.Config.merge(options);
|
|
53
|
+
const names = this.raw.split(config.separator.token);
|
|
54
|
+
return new ArrayStringParser(names).parse(options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.StringParser = StringParser;
|
|
58
|
+
class ArrayStringParser extends Parser {
|
|
59
|
+
parse(options) {
|
|
60
|
+
const config = config_js_1.Config.merge(options);
|
|
61
|
+
const fullName = new fullname_js_1.FullName(config);
|
|
62
|
+
const raw = this.raw.map((n) => n.trim());
|
|
63
|
+
const index = utils_js_1.NameIndex.when(config.orderedBy, raw.length);
|
|
64
|
+
const validator = new validator_js_1.ArrayStringValidator(index);
|
|
65
|
+
if (config.bypass) {
|
|
66
|
+
validator.validateIndex(raw);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
validator.validate(raw);
|
|
70
|
+
}
|
|
71
|
+
const { firstName, lastName, middleName, prefix, suffix } = index;
|
|
72
|
+
fullName.setFirstName(new name_js_1.FirstName(raw[firstName]));
|
|
73
|
+
fullName.setLastName(new name_js_1.LastName(raw[lastName]));
|
|
74
|
+
if (raw.length >= 3)
|
|
75
|
+
fullName.setMiddleName(raw[middleName].split(config.separator.token));
|
|
76
|
+
if (raw.length >= 4)
|
|
77
|
+
fullName.setPrefix(name_js_1.Name.prefix(raw[prefix]));
|
|
78
|
+
if (raw.length === 5)
|
|
79
|
+
fullName.setSuffix(name_js_1.Name.suffix(raw[suffix]));
|
|
80
|
+
return fullName;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.ArrayStringParser = ArrayStringParser;
|
|
84
|
+
class NamaParser extends Parser {
|
|
85
|
+
parse(options) {
|
|
86
|
+
const config = config_js_1.Config.merge(options);
|
|
87
|
+
if (config.bypass) {
|
|
88
|
+
validator_js_1.NamaValidator.create().validateKeys(this.#asNama());
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
validator_js_1.NamaValidator.create().validate(this.#asNama());
|
|
92
|
+
}
|
|
93
|
+
return fullname_js_1.FullName.parse(this.raw, config);
|
|
94
|
+
}
|
|
95
|
+
#asNama() {
|
|
96
|
+
return new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
97
|
+
const namon = types_js_1.Namon.cast(key);
|
|
98
|
+
if (!namon) {
|
|
99
|
+
throw new error_js_1.InputError({
|
|
100
|
+
source: Object.values(this.raw).join(' '),
|
|
101
|
+
message: `unsupported key "${key}"`,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return [namon, value];
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.NamaParser = NamaParser;
|
|
109
|
+
class ArrayNameParser extends Parser {
|
|
110
|
+
parse(options) {
|
|
111
|
+
const config = config_js_1.Config.merge(options);
|
|
112
|
+
const fullName = new fullname_js_1.FullName(config);
|
|
113
|
+
validator_js_1.ArrayNameValidator.create().validate(this.raw);
|
|
114
|
+
for (const name of this.raw) {
|
|
115
|
+
if (name.isPrefix) {
|
|
116
|
+
fullName.setPrefix(name);
|
|
117
|
+
}
|
|
118
|
+
else if (name.isSuffix) {
|
|
119
|
+
fullName.setSuffix(name);
|
|
120
|
+
}
|
|
121
|
+
else if (name.isFirstName) {
|
|
122
|
+
fullName.setFirstName(name instanceof name_js_1.FirstName ? name : new name_js_1.FirstName(name.value));
|
|
123
|
+
}
|
|
124
|
+
else if (name.isMiddleName) {
|
|
125
|
+
fullName.middleName.push(name);
|
|
126
|
+
}
|
|
127
|
+
else if (name.isLastName) {
|
|
128
|
+
const lastName = new name_js_1.LastName(name.value, name instanceof name_js_1.LastName ? name.mother : undefined, config.surname);
|
|
129
|
+
fullName.setLastName(lastName);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return fullName;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.ArrayNameParser = ArrayNameParser;
|
|
@@ -41,6 +41,21 @@ var CapsRange;
|
|
|
41
41
|
CapsRange[CapsRange["ALL"] = 2] = "ALL";
|
|
42
42
|
})(CapsRange || (exports.CapsRange = CapsRange = {}));
|
|
43
43
|
class Namon {
|
|
44
|
+
index;
|
|
45
|
+
key;
|
|
46
|
+
static PREFIX = new Namon(0, 'prefix');
|
|
47
|
+
static FIRST_NAME = new Namon(1, 'firstName');
|
|
48
|
+
static MIDDLE_NAME = new Namon(2, 'middleName');
|
|
49
|
+
static LAST_NAME = new Namon(3, 'lastName');
|
|
50
|
+
static SUFFIX = new Namon(4, 'suffix');
|
|
51
|
+
static values = [Namon.PREFIX, Namon.FIRST_NAME, Namon.MIDDLE_NAME, Namon.LAST_NAME, Namon.SUFFIX];
|
|
52
|
+
static all = new Map([
|
|
53
|
+
[Namon.PREFIX.key, Namon.PREFIX],
|
|
54
|
+
[Namon.FIRST_NAME.key, Namon.FIRST_NAME],
|
|
55
|
+
[Namon.MIDDLE_NAME.key, Namon.MIDDLE_NAME],
|
|
56
|
+
[Namon.LAST_NAME.key, Namon.LAST_NAME],
|
|
57
|
+
[Namon.SUFFIX.key, Namon.SUFFIX],
|
|
58
|
+
]);
|
|
44
59
|
constructor(index, key) {
|
|
45
60
|
this.index = index;
|
|
46
61
|
this.key = key;
|
|
@@ -59,20 +74,32 @@ class Namon {
|
|
|
59
74
|
}
|
|
60
75
|
}
|
|
61
76
|
exports.Namon = Namon;
|
|
62
|
-
Namon.PREFIX = new Namon(0, 'prefix');
|
|
63
|
-
Namon.FIRST_NAME = new Namon(1, 'firstName');
|
|
64
|
-
Namon.MIDDLE_NAME = new Namon(2, 'middleName');
|
|
65
|
-
Namon.LAST_NAME = new Namon(3, 'lastName');
|
|
66
|
-
Namon.SUFFIX = new Namon(4, 'suffix');
|
|
67
|
-
Namon.values = [Namon.PREFIX, Namon.FIRST_NAME, Namon.MIDDLE_NAME, Namon.LAST_NAME, Namon.SUFFIX];
|
|
68
|
-
Namon.all = new Map([
|
|
69
|
-
[Namon.PREFIX.key, Namon.PREFIX],
|
|
70
|
-
[Namon.FIRST_NAME.key, Namon.FIRST_NAME],
|
|
71
|
-
[Namon.MIDDLE_NAME.key, Namon.MIDDLE_NAME],
|
|
72
|
-
[Namon.LAST_NAME.key, Namon.LAST_NAME],
|
|
73
|
-
[Namon.SUFFIX.key, Namon.SUFFIX],
|
|
74
|
-
]);
|
|
75
77
|
class Separator {
|
|
78
|
+
name;
|
|
79
|
+
token;
|
|
80
|
+
static COMMA = new Separator('comma', ',');
|
|
81
|
+
static COLON = new Separator('colon', ':');
|
|
82
|
+
static DOUBLE_QUOTE = new Separator('doubleQuote', '"');
|
|
83
|
+
static EMPTY = new Separator('empty', '');
|
|
84
|
+
static HYPHEN = new Separator('hyphen', '-');
|
|
85
|
+
static PERIOD = new Separator('period', '.');
|
|
86
|
+
static SEMI_COLON = new Separator('semiColon', ';');
|
|
87
|
+
static SINGLE_QUOTE = new Separator('singleQuote', `'`);
|
|
88
|
+
static SPACE = new Separator('space', ' ');
|
|
89
|
+
static UNDERSCORE = new Separator('underscore', '_');
|
|
90
|
+
static all = new Map([
|
|
91
|
+
[Separator.COMMA.name, Separator.COMMA],
|
|
92
|
+
[Separator.COLON.name, Separator.COLON],
|
|
93
|
+
[Separator.DOUBLE_QUOTE.name, Separator.DOUBLE_QUOTE],
|
|
94
|
+
[Separator.EMPTY.name, Separator.EMPTY],
|
|
95
|
+
[Separator.HYPHEN.name, Separator.HYPHEN],
|
|
96
|
+
[Separator.PERIOD.name, Separator.PERIOD],
|
|
97
|
+
[Separator.SEMI_COLON.name, Separator.SEMI_COLON],
|
|
98
|
+
[Separator.SINGLE_QUOTE.name, Separator.SINGLE_QUOTE],
|
|
99
|
+
[Separator.SPACE.name, Separator.SPACE],
|
|
100
|
+
[Separator.UNDERSCORE.name, Separator.UNDERSCORE],
|
|
101
|
+
]);
|
|
102
|
+
static tokens = [...Separator.all.values()].map((s) => s.token);
|
|
76
103
|
constructor(name, token) {
|
|
77
104
|
this.name = name;
|
|
78
105
|
this.token = token;
|
|
@@ -82,26 +109,3 @@ class Separator {
|
|
|
82
109
|
}
|
|
83
110
|
}
|
|
84
111
|
exports.Separator = Separator;
|
|
85
|
-
Separator.COMMA = new Separator('comma', ',');
|
|
86
|
-
Separator.COLON = new Separator('colon', ':');
|
|
87
|
-
Separator.DOUBLE_QUOTE = new Separator('doubleQuote', '"');
|
|
88
|
-
Separator.EMPTY = new Separator('empty', '');
|
|
89
|
-
Separator.HYPHEN = new Separator('hyphen', '-');
|
|
90
|
-
Separator.PERIOD = new Separator('period', '.');
|
|
91
|
-
Separator.SEMI_COLON = new Separator('semiColon', ';');
|
|
92
|
-
Separator.SINGLE_QUOTE = new Separator('singleQuote', `'`);
|
|
93
|
-
Separator.SPACE = new Separator('space', ' ');
|
|
94
|
-
Separator.UNDERSCORE = new Separator('underscore', '_');
|
|
95
|
-
Separator.all = new Map([
|
|
96
|
-
[Separator.COMMA.name, Separator.COMMA],
|
|
97
|
-
[Separator.COLON.name, Separator.COLON],
|
|
98
|
-
[Separator.DOUBLE_QUOTE.name, Separator.DOUBLE_QUOTE],
|
|
99
|
-
[Separator.EMPTY.name, Separator.EMPTY],
|
|
100
|
-
[Separator.HYPHEN.name, Separator.HYPHEN],
|
|
101
|
-
[Separator.PERIOD.name, Separator.PERIOD],
|
|
102
|
-
[Separator.SEMI_COLON.name, Separator.SEMI_COLON],
|
|
103
|
-
[Separator.SINGLE_QUOTE.name, Separator.SINGLE_QUOTE],
|
|
104
|
-
[Separator.SPACE.name, Separator.SPACE],
|
|
105
|
-
[Separator.UNDERSCORE.name, Separator.UNDERSCORE],
|
|
106
|
-
]);
|
|
107
|
-
Separator.tokens = [...Separator.all.values()].map((s) => s.token);
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const types_1 = require("./types");
|
|
3
|
+
exports.isStringArray = exports.toggleCase = exports.decapitalize = exports.capitalize = exports.NameIndex = void 0;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
7
6
|
class NameIndex {
|
|
7
|
+
prefix;
|
|
8
|
+
firstName;
|
|
9
|
+
middleName;
|
|
10
|
+
lastName;
|
|
11
|
+
suffix;
|
|
8
12
|
static get min() {
|
|
9
|
-
return
|
|
13
|
+
return constants_js_1.MIN_NUMBER_OF_NAME_PARTS;
|
|
10
14
|
}
|
|
11
15
|
static get max() {
|
|
12
|
-
return
|
|
16
|
+
return constants_js_1.MAX_NUMBER_OF_NAME_PARTS;
|
|
13
17
|
}
|
|
14
18
|
constructor(prefix, firstName, middleName, lastName, suffix) {
|
|
15
19
|
this.prefix = prefix;
|
|
@@ -22,7 +26,7 @@ class NameIndex {
|
|
|
22
26
|
return new this(-1, 0, -1, 1, -1);
|
|
23
27
|
}
|
|
24
28
|
static when(order, count = 2) {
|
|
25
|
-
if (order ===
|
|
29
|
+
if (order === types_js_1.NameOrder.FIRST_NAME) {
|
|
26
30
|
switch (count) {
|
|
27
31
|
case 2:
|
|
28
32
|
return new this(-1, 0, -1, 1, -1);
|
|
@@ -65,20 +69,20 @@ class NameIndex {
|
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
exports.NameIndex = NameIndex;
|
|
68
|
-
function capitalize(str, range =
|
|
69
|
-
if (!str || range ===
|
|
72
|
+
function capitalize(str, range = types_js_1.CapsRange.INITIAL) {
|
|
73
|
+
if (!str || range === types_js_1.CapsRange.NONE)
|
|
70
74
|
return str;
|
|
71
75
|
const initial = str[0].toUpperCase();
|
|
72
76
|
const rest = str.slice(1).toLowerCase();
|
|
73
|
-
return range ===
|
|
77
|
+
return range === types_js_1.CapsRange.INITIAL ? initial.concat(rest) : str.toUpperCase();
|
|
74
78
|
}
|
|
75
79
|
exports.capitalize = capitalize;
|
|
76
|
-
function decapitalize(str, range =
|
|
77
|
-
if (!str || range ===
|
|
80
|
+
function decapitalize(str, range = types_js_1.CapsRange.INITIAL) {
|
|
81
|
+
if (!str || range === types_js_1.CapsRange.NONE)
|
|
78
82
|
return str;
|
|
79
83
|
const initial = str[0].toLowerCase();
|
|
80
84
|
const rest = str.slice(1);
|
|
81
|
-
return range ===
|
|
85
|
+
return range === types_js_1.CapsRange.INITIAL ? initial.concat(rest) : str.toLowerCase();
|
|
82
86
|
}
|
|
83
87
|
exports.decapitalize = decapitalize;
|
|
84
88
|
function toggleCase(str) {
|
|
@@ -98,7 +102,3 @@ function isStringArray(value) {
|
|
|
98
102
|
return Array.isArray(value) && value.length > 0 && value.every((e) => typeof e === 'string');
|
|
99
103
|
}
|
|
100
104
|
exports.isStringArray = isStringArray;
|
|
101
|
-
function isNameArray(value) {
|
|
102
|
-
return Array.isArray(value) && value.length > 0 && value.every((e) => e instanceof name_1.Name);
|
|
103
|
-
}
|
|
104
|
-
exports.isNameArray = isNameArray;
|