namefully 1.2.1 → 1.3.1
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/builder.js +80 -0
- package/dist/lib/config.js +10 -4
- package/dist/lib/constants.js +3 -26
- package/dist/lib/error.js +5 -10
- package/dist/lib/full-name.js +39 -14
- package/dist/lib/index.js +3 -0
- package/dist/lib/name.js +28 -29
- package/dist/lib/namefully.js +99 -78
- package/dist/lib/parser.js +33 -43
- package/dist/lib/types.js +19 -1
- package/dist/lib/utils.js +30 -30
- package/dist/lib/validator.js +42 -72
- package/dist/types/builder.d.ts +75 -0
- package/dist/types/config.d.ts +2 -5
- package/dist/types/constants.d.ts +2 -2
- package/dist/types/error.d.ts +11 -16
- package/dist/types/full-name.d.ts +16 -13
- package/dist/types/index.d.ts +5 -1
- package/dist/types/name.d.ts +25 -26
- package/dist/types/namefully.d.ts +126 -92
- package/dist/types/parser.d.ts +11 -13
- package/dist/types/types.d.ts +19 -48
- package/dist/types/utils.d.ts +7 -7
- package/dist/types/validator.d.ts +1 -1
- package/dist/umd/namefully.js +1149 -1058
- package/dist/umd/namefully.min.js +1 -1
- package/package.json +20 -17
- package/readme.md +63 -35
package/dist/lib/namefully.js
CHANGED
|
@@ -23,27 +23,29 @@ var _Namefully_instances, _Namefully_fullName, _Namefully_toParser, _Namefully_m
|
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.Namefully = void 0;
|
|
25
25
|
const constants_1 = require("./constants");
|
|
26
|
+
const name_1 = require("./name");
|
|
26
27
|
const error_1 = require("./error");
|
|
27
|
-
const parser_1 = require("./parser");
|
|
28
|
-
const types_1 = require("./types");
|
|
29
28
|
const utils_1 = require("./utils");
|
|
29
|
+
const types_1 = require("./types");
|
|
30
|
+
const parser_1 = require("./parser");
|
|
30
31
|
class Namefully {
|
|
31
32
|
constructor(names, options) {
|
|
32
33
|
_Namefully_instances.add(this);
|
|
33
34
|
_Namefully_fullName.set(this, void 0);
|
|
35
|
+
this.json = this.toJson;
|
|
34
36
|
__classPrivateFieldSet(this, _Namefully_fullName, __classPrivateFieldGet(this, _Namefully_instances, "m", _Namefully_toParser).call(this, names).parse(options), "f");
|
|
35
37
|
}
|
|
36
|
-
static tryParse(text) {
|
|
38
|
+
static tryParse(text, index) {
|
|
37
39
|
try {
|
|
38
|
-
return new
|
|
40
|
+
return new Namefully(parser_1.Parser.build(text, index));
|
|
39
41
|
}
|
|
40
|
-
catch (
|
|
42
|
+
catch (_a) {
|
|
41
43
|
return undefined;
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
|
-
static parse(text) {
|
|
46
|
+
static parse(text, index) {
|
|
45
47
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
return parser_1.Parser.buildAsync(text).then((parser) => new Namefully(parser));
|
|
48
|
+
return parser_1.Parser.buildAsync(text, index).then((parser) => new Namefully(parser));
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
51
|
get config() {
|
|
@@ -87,25 +89,46 @@ class Namefully {
|
|
|
87
89
|
get public() {
|
|
88
90
|
return this.format('f $l');
|
|
89
91
|
}
|
|
92
|
+
get salutation() {
|
|
93
|
+
return this.format('p l');
|
|
94
|
+
}
|
|
95
|
+
get parts() {
|
|
96
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").toIterable();
|
|
97
|
+
}
|
|
98
|
+
get size() {
|
|
99
|
+
return Array.from(this.parts).length;
|
|
100
|
+
}
|
|
101
|
+
*[(_Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), Symbol.iterator)]() {
|
|
102
|
+
yield* __classPrivateFieldGet(this, _Namefully_fullName, "f").toIterable(true);
|
|
103
|
+
}
|
|
90
104
|
toString() {
|
|
91
105
|
return this.full;
|
|
92
106
|
}
|
|
93
|
-
get(
|
|
94
|
-
|
|
107
|
+
get(key) {
|
|
108
|
+
const namon = typeof key === 'string' ? types_1.Namon.cast(key) : key;
|
|
109
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.PREFIX))
|
|
95
110
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").prefix;
|
|
96
|
-
if (namon.equal(types_1.Namon.FIRST_NAME))
|
|
111
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.FIRST_NAME))
|
|
97
112
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName;
|
|
98
|
-
if (namon.equal(types_1.Namon.MIDDLE_NAME))
|
|
113
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.MIDDLE_NAME))
|
|
99
114
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName;
|
|
100
|
-
if (namon.equal(types_1.Namon.LAST_NAME))
|
|
115
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.LAST_NAME))
|
|
101
116
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName;
|
|
102
|
-
if (namon.equal(types_1.Namon.SUFFIX))
|
|
117
|
+
if (namon === null || namon === void 0 ? void 0 : namon.equal(types_1.Namon.SUFFIX))
|
|
103
118
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").suffix;
|
|
104
119
|
return undefined;
|
|
105
120
|
}
|
|
106
121
|
equal(other) {
|
|
107
122
|
return this.toString() === other.toString();
|
|
108
123
|
}
|
|
124
|
+
deepEqual(other) {
|
|
125
|
+
const others = Array.from(other.parts);
|
|
126
|
+
for (const part of this.parts) {
|
|
127
|
+
if (!others.some((name) => name.equal(part)))
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
109
132
|
toJson() {
|
|
110
133
|
return {
|
|
111
134
|
prefix: this.prefix,
|
|
@@ -121,21 +144,26 @@ class Namefully {
|
|
|
121
144
|
fullName(orderedBy) {
|
|
122
145
|
const sep = this.config.ending ? ',' : '';
|
|
123
146
|
const names = [];
|
|
124
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
125
147
|
if (this.prefix)
|
|
126
148
|
names.push(this.prefix);
|
|
127
|
-
if (orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
149
|
+
if ((orderedBy !== null && orderedBy !== void 0 ? orderedBy : this.config.orderedBy) === types_1.NameOrder.FIRST_NAME) {
|
|
128
150
|
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
129
151
|
}
|
|
130
152
|
else {
|
|
131
|
-
names.push(this.last
|
|
153
|
+
names.push(this.last);
|
|
154
|
+
if (this.hasMiddle) {
|
|
155
|
+
names.push(this.first, this.middleName().join(' ') + sep);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
names.push(this.first + sep);
|
|
159
|
+
}
|
|
132
160
|
}
|
|
133
161
|
if (this.suffix)
|
|
134
162
|
names.push(this.suffix);
|
|
135
163
|
return names.join(' ').trim();
|
|
136
164
|
}
|
|
137
165
|
birthName(orderedBy) {
|
|
138
|
-
orderedBy
|
|
166
|
+
orderedBy !== null && orderedBy !== void 0 ? orderedBy : (orderedBy = this.config.orderedBy);
|
|
139
167
|
return orderedBy === types_1.NameOrder.FIRST_NAME
|
|
140
168
|
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
141
169
|
: [this.last, this.first, ...this.middleName()].join(' ');
|
|
@@ -150,50 +178,42 @@ class Namefully {
|
|
|
150
178
|
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString(format);
|
|
151
179
|
}
|
|
152
180
|
initials(options) {
|
|
153
|
-
const
|
|
181
|
+
const { orderedBy = this.config.orderedBy, only = types_1.NameType.BIRTH_NAME, asJson } = options !== null && options !== void 0 ? options : {};
|
|
154
182
|
const firstInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials();
|
|
155
|
-
const midInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.
|
|
183
|
+
const midInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.value[0]);
|
|
156
184
|
const lastInits = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials();
|
|
157
|
-
|
|
158
|
-
|
|
185
|
+
if (asJson)
|
|
186
|
+
return { firstName: firstInits, middleName: midInits, lastName: lastInits };
|
|
159
187
|
if (only !== types_1.NameType.BIRTH_NAME) {
|
|
160
|
-
|
|
161
|
-
initials.push(...firstInits);
|
|
162
|
-
}
|
|
163
|
-
else if (only === types_1.NameType.MIDDLE_NAME) {
|
|
164
|
-
initials.push(...midInits);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
initials.push(...lastInits);
|
|
168
|
-
}
|
|
188
|
+
return only === types_1.NameType.FIRST_NAME ? firstInits : only === types_1.NameType.MIDDLE_NAME ? midInits : lastInits;
|
|
169
189
|
}
|
|
170
190
|
else if (orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
171
|
-
|
|
191
|
+
return [...firstInits, ...midInits, ...lastInits];
|
|
172
192
|
}
|
|
173
193
|
else {
|
|
174
|
-
|
|
194
|
+
return [...lastInits, ...firstInits, ...midInits];
|
|
175
195
|
}
|
|
176
|
-
return initials;
|
|
177
196
|
}
|
|
178
197
|
shorten(orderedBy) {
|
|
179
|
-
orderedBy
|
|
198
|
+
orderedBy !== null && orderedBy !== void 0 ? orderedBy : (orderedBy = this.config.orderedBy);
|
|
199
|
+
const { firstName, lastName } = __classPrivateFieldGet(this, _Namefully_fullName, "f");
|
|
180
200
|
return orderedBy === types_1.NameOrder.FIRST_NAME
|
|
181
|
-
? [
|
|
182
|
-
: [
|
|
201
|
+
? [firstName.value, lastName.toString()].join(' ')
|
|
202
|
+
: [lastName.toString(), firstName.value].join(' ');
|
|
183
203
|
}
|
|
184
204
|
flatten(options) {
|
|
185
|
-
|
|
205
|
+
const { by = types_1.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
206
|
+
if (this.length <= limit)
|
|
186
207
|
return this.full;
|
|
187
|
-
const
|
|
188
|
-
const { by, limit, recursive, withMore, withPeriod, surname } = mergedOptions;
|
|
208
|
+
const { firstName, lastName, middleName } = __classPrivateFieldGet(this, _Namefully_fullName, "f");
|
|
189
209
|
const sep = withPeriod ? '.' : '';
|
|
190
|
-
const fn = __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.toString();
|
|
191
|
-
const mn = this.middleName().join(' ');
|
|
192
|
-
const ln = __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.toString();
|
|
193
210
|
const hasMid = this.hasMiddle;
|
|
194
|
-
const
|
|
195
|
-
const
|
|
196
|
-
const
|
|
211
|
+
const fn = firstName.toString();
|
|
212
|
+
const mn = this.middleName().join(' ');
|
|
213
|
+
const ln = lastName.toString();
|
|
214
|
+
const f = firstName.initials(withMore).join(sep + ' ') + sep;
|
|
215
|
+
const l = lastName.initials(surname).join(sep + ' ') + sep;
|
|
216
|
+
const m = hasMid ? middleName.map((n) => n.value[0]).join(sep + ' ') + sep : '';
|
|
197
217
|
let name = [];
|
|
198
218
|
if (this.config.orderedBy === types_1.NameOrder.FIRST_NAME) {
|
|
199
219
|
switch (by) {
|
|
@@ -212,7 +232,7 @@ class Namefully {
|
|
|
212
232
|
case types_1.Flat.MID_LAST:
|
|
213
233
|
name = hasMid ? [fn, m, l] : [fn, l];
|
|
214
234
|
break;
|
|
215
|
-
|
|
235
|
+
default:
|
|
216
236
|
name = hasMid ? [f, m, l] : [f, l];
|
|
217
237
|
break;
|
|
218
238
|
}
|
|
@@ -234,7 +254,7 @@ class Namefully {
|
|
|
234
254
|
case types_1.Flat.MID_LAST:
|
|
235
255
|
name = hasMid ? [l, fn, m] : [l, fn];
|
|
236
256
|
break;
|
|
237
|
-
|
|
257
|
+
default:
|
|
238
258
|
name = hasMid ? [l, f, m] : [l, f];
|
|
239
259
|
break;
|
|
240
260
|
}
|
|
@@ -275,8 +295,8 @@ class Namefully {
|
|
|
275
295
|
pattern = 'o';
|
|
276
296
|
let group = '';
|
|
277
297
|
const formatted = [];
|
|
278
|
-
for (const char of pattern
|
|
279
|
-
if (constants_1.
|
|
298
|
+
for (const char of pattern) {
|
|
299
|
+
if (!constants_1.ALLOWED_FORMAT_TOKENS.includes(char)) {
|
|
280
300
|
throw new error_1.NotAllowedError({
|
|
281
301
|
source: this.full,
|
|
282
302
|
operation: 'format',
|
|
@@ -292,7 +312,8 @@ class Namefully {
|
|
|
292
312
|
return formatted.join('').trim();
|
|
293
313
|
}
|
|
294
314
|
flip() {
|
|
295
|
-
this.config.
|
|
315
|
+
const order = this.config.orderedBy === types_1.NameOrder.FIRST_NAME ? types_1.NameOrder.LAST_NAME : types_1.NameOrder.FIRST_NAME;
|
|
316
|
+
this.config.update({ orderedBy: order });
|
|
296
317
|
}
|
|
297
318
|
split(separator = /[' -]/g) {
|
|
298
319
|
return this.birth.replace(separator, ' ').split(' ');
|
|
@@ -334,27 +355,21 @@ class Namefully {
|
|
|
334
355
|
}
|
|
335
356
|
}
|
|
336
357
|
exports.Namefully = Namefully;
|
|
337
|
-
|
|
358
|
+
_Namefully_toParser = function _Namefully_toParser(raw) {
|
|
338
359
|
if (raw instanceof parser_1.Parser)
|
|
339
360
|
return raw;
|
|
340
361
|
if (typeof raw === 'string')
|
|
341
362
|
return new parser_1.StringParser(raw);
|
|
342
363
|
if ((0, utils_1.isStringArray)(raw))
|
|
343
364
|
return new parser_1.ArrayStringParser(raw);
|
|
344
|
-
if ((0,
|
|
365
|
+
if ((0, name_1.isNameArray)(raw))
|
|
345
366
|
return new parser_1.ArrayNameParser(raw);
|
|
346
367
|
if (typeof raw === 'object')
|
|
347
368
|
return new parser_1.NamaParser(raw);
|
|
348
|
-
throw new error_1.InputError({ source: raw, message: 'Cannot parse raw data
|
|
369
|
+
throw new error_1.InputError({ source: raw, message: 'Cannot parse raw data; review expected data types.' });
|
|
349
370
|
}, _Namefully_map = function _Namefully_map(char) {
|
|
350
371
|
var _a, _b;
|
|
351
372
|
switch (char) {
|
|
352
|
-
case '.':
|
|
353
|
-
case ',':
|
|
354
|
-
case ' ':
|
|
355
|
-
case '-':
|
|
356
|
-
case '_':
|
|
357
|
-
return char;
|
|
358
373
|
case 'b':
|
|
359
374
|
return this.birth;
|
|
360
375
|
case 'B':
|
|
@@ -372,21 +387,21 @@ _Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), _Name
|
|
|
372
387
|
return char === 'm' ? this.middleName().join(' ') : this.middleName().join(' ').toUpperCase();
|
|
373
388
|
case 'o':
|
|
374
389
|
case 'O':
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
names.
|
|
388
|
-
|
|
389
|
-
|
|
390
|
+
return ((character) => {
|
|
391
|
+
const sep = this.config.ending ? ',' : '';
|
|
392
|
+
const names = [];
|
|
393
|
+
if (this.prefix)
|
|
394
|
+
names.push(this.prefix);
|
|
395
|
+
names.push(`${this.last},`.toUpperCase());
|
|
396
|
+
if (this.hasMiddle)
|
|
397
|
+
names.push(this.first, this.middleName().join(' ') + sep);
|
|
398
|
+
else
|
|
399
|
+
names.push(this.first + sep);
|
|
400
|
+
if (this.suffix)
|
|
401
|
+
names.push(this.suffix);
|
|
402
|
+
const nama = names.join(' ').trim();
|
|
403
|
+
return character === 'o' ? nama : nama.toUpperCase();
|
|
404
|
+
})(char);
|
|
390
405
|
case 'p':
|
|
391
406
|
return this.prefix;
|
|
392
407
|
case 'P':
|
|
@@ -396,15 +411,21 @@ _Namefully_fullName = new WeakMap(), _Namefully_instances = new WeakSet(), _Name
|
|
|
396
411
|
case 'S':
|
|
397
412
|
return (_b = this.suffix) === null || _b === void 0 ? void 0 : _b.toUpperCase();
|
|
398
413
|
case '$f':
|
|
414
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.value[0];
|
|
399
415
|
case '$F':
|
|
400
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials()
|
|
416
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").firstName.initials(true).join('');
|
|
401
417
|
case '$l':
|
|
418
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.value[0];
|
|
402
419
|
case '$L':
|
|
403
|
-
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials()
|
|
420
|
+
return __classPrivateFieldGet(this, _Namefully_fullName, "f").lastName.initials().join('');
|
|
404
421
|
case '$m':
|
|
405
|
-
case '$M':
|
|
406
422
|
return this.hasMiddle ? this.middle[0] : undefined;
|
|
423
|
+
case '$M':
|
|
424
|
+
return this.hasMiddle ? __classPrivateFieldGet(this, _Namefully_fullName, "f").middleName.map((n) => n.value[0]).join('') : undefined;
|
|
407
425
|
default:
|
|
408
|
-
return undefined;
|
|
426
|
+
return constants_1.ALLOWED_FORMAT_TOKENS.includes(char) ? char : undefined;
|
|
409
427
|
}
|
|
410
428
|
};
|
|
429
|
+
exports.default = (names, options) => {
|
|
430
|
+
return new Namefully(names, options);
|
|
431
|
+
};
|
package/dist/lib/parser.js
CHANGED
|
@@ -1,31 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var _NamaParser_instances, _NamaParser_asNama;
|
|
8
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
3
|
exports.ArrayNameParser = exports.NamaParser = exports.ArrayStringParser = exports.StringParser = exports.Parser = void 0;
|
|
10
|
-
const full_name_1 = require("./full-name");
|
|
11
4
|
const config_1 = require("./config");
|
|
12
5
|
const utils_1 = require("./utils");
|
|
13
|
-
const validator_1 = require("./validator");
|
|
14
|
-
const name_1 = require("./name");
|
|
15
|
-
const types_1 = require("./types");
|
|
16
6
|
const error_1 = require("./error");
|
|
7
|
+
const full_name_1 = require("./full-name");
|
|
8
|
+
const types_1 = require("./types");
|
|
9
|
+
const name_1 = require("./name");
|
|
10
|
+
const validator_1 = require("./validator");
|
|
17
11
|
class Parser {
|
|
18
12
|
constructor(raw) {
|
|
19
13
|
this.raw = raw;
|
|
20
14
|
}
|
|
21
|
-
static build(text) {
|
|
15
|
+
static build(text, index) {
|
|
22
16
|
const parts = text.trim().split(types_1.Separator.SPACE.token);
|
|
23
17
|
const length = parts.length;
|
|
18
|
+
if (index instanceof utils_1.NameIndex) {
|
|
19
|
+
const names = Object.entries(index.json())
|
|
20
|
+
.filter(([, position]) => position > -1 && position < length)
|
|
21
|
+
.map(([key, position]) => new name_1.Name(parts[position], types_1.Namon.all.get(key)));
|
|
22
|
+
return new ArrayNameParser(names);
|
|
23
|
+
}
|
|
24
24
|
if (length < 2) {
|
|
25
|
-
throw new error_1.InputError({
|
|
26
|
-
source: text,
|
|
27
|
-
message: 'cannot build from invalid input',
|
|
28
|
-
});
|
|
25
|
+
throw new error_1.InputError({ source: text, message: 'expecting at least 2 name parts' });
|
|
29
26
|
}
|
|
30
27
|
else if (length === 2 || length === 3) {
|
|
31
28
|
return new StringParser(text);
|
|
@@ -36,9 +33,9 @@ class Parser {
|
|
|
36
33
|
return new ArrayStringParser([first, middles.join(' '), last]);
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
|
-
static buildAsync(text) {
|
|
36
|
+
static buildAsync(text, index) {
|
|
40
37
|
try {
|
|
41
|
-
return Promise.resolve(Parser.build(text));
|
|
38
|
+
return Promise.resolve(Parser.build(text, index));
|
|
42
39
|
}
|
|
43
40
|
catch (error) {
|
|
44
41
|
return Promise.reject(error);
|
|
@@ -50,14 +47,13 @@ class StringParser extends Parser {
|
|
|
50
47
|
parse(options) {
|
|
51
48
|
const config = config_1.Config.merge(options);
|
|
52
49
|
const names = this.raw.split(config.separator.token);
|
|
53
|
-
return new ArrayStringParser(names).parse(
|
|
50
|
+
return new ArrayStringParser(names).parse(config);
|
|
54
51
|
}
|
|
55
52
|
}
|
|
56
53
|
exports.StringParser = StringParser;
|
|
57
54
|
class ArrayStringParser extends Parser {
|
|
58
55
|
parse(options) {
|
|
59
56
|
const config = config_1.Config.merge(options);
|
|
60
|
-
const fullName = new full_name_1.FullName(config);
|
|
61
57
|
const raw = this.raw.map((n) => n.trim());
|
|
62
58
|
const index = utils_1.NameIndex.when(config.orderedBy, raw.length);
|
|
63
59
|
const validator = new validator_1.ArrayStringValidator(index);
|
|
@@ -68,8 +64,9 @@ class ArrayStringParser extends Parser {
|
|
|
68
64
|
validator.validate(raw);
|
|
69
65
|
}
|
|
70
66
|
const { firstName, lastName, middleName, prefix, suffix } = index;
|
|
71
|
-
fullName
|
|
72
|
-
|
|
67
|
+
const fullName = new full_name_1.FullName(config)
|
|
68
|
+
.setFirstName(new name_1.FirstName(raw[firstName]))
|
|
69
|
+
.setLastName(new name_1.LastName(raw[lastName]));
|
|
73
70
|
if (raw.length >= 3)
|
|
74
71
|
fullName.setMiddleName(raw[middleName].split(config.separator.token));
|
|
75
72
|
if (raw.length >= 4)
|
|
@@ -81,39 +78,32 @@ class ArrayStringParser extends Parser {
|
|
|
81
78
|
}
|
|
82
79
|
exports.ArrayStringParser = ArrayStringParser;
|
|
83
80
|
class NamaParser extends Parser {
|
|
84
|
-
constructor() {
|
|
85
|
-
super(...arguments);
|
|
86
|
-
_NamaParser_instances.add(this);
|
|
87
|
-
}
|
|
88
81
|
parse(options) {
|
|
89
82
|
const config = config_1.Config.merge(options);
|
|
83
|
+
const names = new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
84
|
+
const namon = types_1.Namon.cast(key);
|
|
85
|
+
if (!namon) {
|
|
86
|
+
throw new error_1.InputError({
|
|
87
|
+
source: Object.values(this.raw).join(' '),
|
|
88
|
+
message: `unsupported key "${key}"`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return [namon, value];
|
|
92
|
+
}));
|
|
90
93
|
if (config.bypass) {
|
|
91
|
-
validator_1.
|
|
94
|
+
validator_1.Validators.nama.validateKeys(names);
|
|
92
95
|
}
|
|
93
96
|
else {
|
|
94
|
-
validator_1.
|
|
97
|
+
validator_1.Validators.nama.validate(names);
|
|
95
98
|
}
|
|
96
99
|
return full_name_1.FullName.parse(this.raw, config);
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
102
|
exports.NamaParser = NamaParser;
|
|
100
|
-
_NamaParser_instances = new WeakSet(), _NamaParser_asNama = function _NamaParser_asNama() {
|
|
101
|
-
return new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
102
|
-
const namon = types_1.Namon.cast(key);
|
|
103
|
-
if (!namon) {
|
|
104
|
-
throw new error_1.InputError({
|
|
105
|
-
source: Object.values(this.raw).join(' '),
|
|
106
|
-
message: `unsupported key "${key}"`,
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
return [namon, value];
|
|
110
|
-
}));
|
|
111
|
-
};
|
|
112
103
|
class ArrayNameParser extends Parser {
|
|
113
104
|
parse(options) {
|
|
114
|
-
const config = config_1.Config.merge(options);
|
|
115
|
-
const fullName = new full_name_1.FullName(config);
|
|
116
105
|
validator_1.ArrayNameValidator.create().validate(this.raw);
|
|
106
|
+
const fullName = new full_name_1.FullName(options);
|
|
117
107
|
for (const name of this.raw) {
|
|
118
108
|
if (name.isPrefix) {
|
|
119
109
|
fullName.setPrefix(name);
|
|
@@ -128,8 +118,8 @@ class ArrayNameParser extends Parser {
|
|
|
128
118
|
fullName.middleName.push(name);
|
|
129
119
|
}
|
|
130
120
|
else if (name.isLastName) {
|
|
131
|
-
const
|
|
132
|
-
fullName.setLastName(
|
|
121
|
+
const mother = name instanceof name_1.LastName ? name.mother : undefined;
|
|
122
|
+
fullName.setLastName(new name_1.LastName(name.value, mother, fullName.config.surname));
|
|
133
123
|
}
|
|
134
124
|
}
|
|
135
125
|
return fullName;
|
package/dist/lib/types.js
CHANGED
|
@@ -49,7 +49,10 @@ class Namon {
|
|
|
49
49
|
return Namon.all.has(key);
|
|
50
50
|
}
|
|
51
51
|
static cast(key) {
|
|
52
|
-
|
|
52
|
+
var _a;
|
|
53
|
+
const searchValue = String(key).toLowerCase();
|
|
54
|
+
const namon = (_a = Object.entries(Namon.aliases).find(([, list]) => list.includes(searchValue))) === null || _a === void 0 ? void 0 : _a[0];
|
|
55
|
+
return Namon.has(namon !== null && namon !== void 0 ? namon : '') ? Namon.all.get(key) : undefined;
|
|
53
56
|
}
|
|
54
57
|
toString() {
|
|
55
58
|
return `Namon.${this.key}`;
|
|
@@ -72,11 +75,26 @@ Namon.all = new Map([
|
|
|
72
75
|
[Namon.LAST_NAME.key, Namon.LAST_NAME],
|
|
73
76
|
[Namon.SUFFIX.key, Namon.SUFFIX],
|
|
74
77
|
]);
|
|
78
|
+
Namon.aliases = {
|
|
79
|
+
[Namon.PREFIX.key]: ['prefix', 'px', 'p'],
|
|
80
|
+
[Namon.FIRST_NAME.key]: ['firstname', 'first', 'fn', 'f'],
|
|
81
|
+
[Namon.MIDDLE_NAME.key]: ['middlename', 'middle', 'mid', 'mn', 'm'],
|
|
82
|
+
[Namon.LAST_NAME.key]: ['lastname', 'last', 'ln', 'l'],
|
|
83
|
+
[Namon.SUFFIX.key]: ['suffix', 'sx', 's'],
|
|
84
|
+
};
|
|
75
85
|
class Separator {
|
|
76
86
|
constructor(name, token) {
|
|
77
87
|
this.name = name;
|
|
78
88
|
this.token = token;
|
|
79
89
|
}
|
|
90
|
+
static cast(key) {
|
|
91
|
+
for (const [name, separator] of Separator.all) {
|
|
92
|
+
if (separator.token === key || name.toLowerCase() === key.toLowerCase()) {
|
|
93
|
+
return separator;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
80
98
|
toString() {
|
|
81
99
|
return `Separator.${this.name}`;
|
|
82
100
|
}
|
package/dist/lib/utils.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const constants_1 = require("./constants");
|
|
5
|
-
const name_1 = require("./name");
|
|
3
|
+
exports.isStringArray = exports.toggleCase = exports.decapitalize = exports.capitalize = exports.NameIndex = void 0;
|
|
6
4
|
const types_1 = require("./types");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
7
6
|
class NameIndex {
|
|
8
7
|
static get min() {
|
|
9
8
|
return constants_1.MIN_NUMBER_OF_NAME_PARTS;
|
|
@@ -17,21 +16,22 @@ class NameIndex {
|
|
|
17
16
|
this.middleName = middleName;
|
|
18
17
|
this.lastName = lastName;
|
|
19
18
|
this.suffix = suffix;
|
|
19
|
+
this.json = this.toJson;
|
|
20
20
|
}
|
|
21
21
|
static base() {
|
|
22
|
-
return new
|
|
22
|
+
return new NameIndex(-1, 0, -1, 1, -1);
|
|
23
23
|
}
|
|
24
24
|
static when(order, count = 2) {
|
|
25
25
|
if (order === types_1.NameOrder.FIRST_NAME) {
|
|
26
26
|
switch (count) {
|
|
27
27
|
case 2:
|
|
28
|
-
return new
|
|
28
|
+
return new NameIndex(-1, 0, -1, 1, -1);
|
|
29
29
|
case 3:
|
|
30
|
-
return new
|
|
30
|
+
return new NameIndex(-1, 0, 1, 2, -1);
|
|
31
31
|
case 4:
|
|
32
|
-
return new
|
|
32
|
+
return new NameIndex(0, 1, 2, 3, -1);
|
|
33
33
|
case 5:
|
|
34
|
-
return new
|
|
34
|
+
return new NameIndex(0, 1, 2, 3, 4);
|
|
35
35
|
default:
|
|
36
36
|
return NameIndex.base();
|
|
37
37
|
}
|
|
@@ -39,54 +39,54 @@ class NameIndex {
|
|
|
39
39
|
else {
|
|
40
40
|
switch (count) {
|
|
41
41
|
case 2:
|
|
42
|
-
return new
|
|
42
|
+
return new NameIndex(-1, 1, -1, 0, -1);
|
|
43
43
|
case 3:
|
|
44
|
-
return new
|
|
44
|
+
return new NameIndex(-1, 1, 2, 0, -1);
|
|
45
45
|
case 4:
|
|
46
|
-
return new
|
|
46
|
+
return new NameIndex(0, 2, 3, 1, -1);
|
|
47
47
|
case 5:
|
|
48
|
-
return new
|
|
48
|
+
return new NameIndex(0, 2, 3, 1, 4);
|
|
49
49
|
default:
|
|
50
50
|
return NameIndex.base();
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
+
static only({ prefix = -1, firstName, middleName = -1, lastName, suffix = -1 }) {
|
|
55
|
+
return new NameIndex(prefix, firstName, middleName, lastName, suffix);
|
|
56
|
+
}
|
|
57
|
+
toJson() {
|
|
58
|
+
return {
|
|
59
|
+
prefix: this.prefix,
|
|
60
|
+
firstName: this.firstName,
|
|
61
|
+
middleName: this.middleName,
|
|
62
|
+
lastName: this.lastName,
|
|
63
|
+
suffix: this.suffix,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
54
66
|
}
|
|
55
67
|
exports.NameIndex = NameIndex;
|
|
56
68
|
function capitalize(str, range = types_1.CapsRange.INITIAL) {
|
|
57
69
|
if (!str || range === types_1.CapsRange.NONE)
|
|
58
70
|
return str;
|
|
59
|
-
const initial = str[0].toUpperCase();
|
|
60
|
-
const rest = str.slice(1).toLowerCase();
|
|
71
|
+
const [initial, rest] = [str[0].toUpperCase(), str.slice(1).toLowerCase()];
|
|
61
72
|
return range === types_1.CapsRange.INITIAL ? initial.concat(rest) : str.toUpperCase();
|
|
62
73
|
}
|
|
63
74
|
exports.capitalize = capitalize;
|
|
64
75
|
function decapitalize(str, range = types_1.CapsRange.INITIAL) {
|
|
65
76
|
if (!str || range === types_1.CapsRange.NONE)
|
|
66
77
|
return str;
|
|
67
|
-
const initial = str[0].toLowerCase();
|
|
68
|
-
const rest = str.slice(1);
|
|
78
|
+
const [initial, rest] = [str[0].toLowerCase(), str.slice(1)];
|
|
69
79
|
return range === types_1.CapsRange.INITIAL ? initial.concat(rest) : str.toLowerCase();
|
|
70
80
|
}
|
|
71
81
|
exports.decapitalize = decapitalize;
|
|
72
82
|
function toggleCase(str) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
chars.push(c.toUpperCase());
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return chars.join('');
|
|
83
|
+
return str
|
|
84
|
+
.split('')
|
|
85
|
+
.map((c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()))
|
|
86
|
+
.join('');
|
|
83
87
|
}
|
|
84
88
|
exports.toggleCase = toggleCase;
|
|
85
89
|
function isStringArray(value) {
|
|
86
90
|
return Array.isArray(value) && value.length > 0 && value.every((e) => typeof e === 'string');
|
|
87
91
|
}
|
|
88
92
|
exports.isStringArray = isStringArray;
|
|
89
|
-
function isNameArray(value) {
|
|
90
|
-
return Array.isArray(value) && value.length > 0 && value.every((e) => e instanceof name_1.Name);
|
|
91
|
-
}
|
|
92
|
-
exports.isNameArray = isNameArray;
|