namefully 2.0.0 → 2.0.2
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/cjs/config.js +14 -5
- package/dist/cjs/constants.js +3 -3
- package/dist/cjs/error.js +4 -7
- package/dist/cjs/fullname.js +10 -11
- package/dist/cjs/name.js +18 -22
- package/dist/cjs/namefully.js +32 -40
- package/dist/cjs/parser.js +13 -15
- package/dist/cjs/utils.js +18 -25
- package/dist/cjs/validator.js +24 -39
- package/dist/esm/builder.d.ts +4 -2
- package/dist/esm/config.d.ts +6 -1
- package/dist/esm/config.js +14 -5
- package/dist/esm/constants.d.ts +2 -2
- package/dist/esm/constants.js +2 -2
- package/dist/esm/error.d.ts +10 -14
- package/dist/esm/error.js +4 -7
- package/dist/esm/fullname.d.ts +10 -12
- package/dist/esm/fullname.js +10 -11
- package/dist/esm/name.d.ts +12 -20
- package/dist/esm/name.js +18 -22
- package/dist/esm/namefully.d.ts +64 -65
- package/dist/esm/namefully.js +33 -41
- package/dist/esm/parser.d.ts +3 -6
- package/dist/esm/parser.js +13 -15
- package/dist/esm/types.d.ts +16 -48
- package/dist/esm/utils.d.ts +4 -5
- package/dist/esm/utils.js +18 -25
- package/dist/esm/validator.d.ts +1 -1
- package/dist/esm/validator.js +29 -44
- package/dist/namefully.js +124 -155
- package/dist/namefully.min.js +1 -1
- package/package.json +7 -7
- package/readme.md +16 -17
package/dist/namefully.js
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.namefully = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
-
const VERSION = '2.0.
|
|
7
|
+
const VERSION = '2.0.2';
|
|
8
8
|
const MIN_NUMBER_OF_NAME_PARTS = 2;
|
|
9
9
|
const MAX_NUMBER_OF_NAME_PARTS = 5;
|
|
10
|
-
const
|
|
10
|
+
const ALLOWED_FORMAT_TOKENS = [
|
|
11
11
|
'.',
|
|
12
12
|
',',
|
|
13
13
|
' ',
|
|
@@ -159,19 +159,19 @@
|
|
|
159
159
|
this.suffix = suffix;
|
|
160
160
|
}
|
|
161
161
|
static base() {
|
|
162
|
-
return new
|
|
162
|
+
return new NameIndex(-1, 0, -1, 1, -1);
|
|
163
163
|
}
|
|
164
164
|
static when(order, count = 2) {
|
|
165
165
|
if (order === exports.NameOrder.FIRST_NAME) {
|
|
166
166
|
switch (count) {
|
|
167
167
|
case 2:
|
|
168
|
-
return new
|
|
168
|
+
return new NameIndex(-1, 0, -1, 1, -1);
|
|
169
169
|
case 3:
|
|
170
|
-
return new
|
|
170
|
+
return new NameIndex(-1, 0, 1, 2, -1);
|
|
171
171
|
case 4:
|
|
172
|
-
return new
|
|
172
|
+
return new NameIndex(0, 1, 2, 3, -1);
|
|
173
173
|
case 5:
|
|
174
|
-
return new
|
|
174
|
+
return new NameIndex(0, 1, 2, 3, 4);
|
|
175
175
|
default:
|
|
176
176
|
return NameIndex.base();
|
|
177
177
|
}
|
|
@@ -179,20 +179,20 @@
|
|
|
179
179
|
else {
|
|
180
180
|
switch (count) {
|
|
181
181
|
case 2:
|
|
182
|
-
return new
|
|
182
|
+
return new NameIndex(-1, 1, -1, 0, -1);
|
|
183
183
|
case 3:
|
|
184
|
-
return new
|
|
184
|
+
return new NameIndex(-1, 1, 2, 0, -1);
|
|
185
185
|
case 4:
|
|
186
|
-
return new
|
|
186
|
+
return new NameIndex(0, 2, 3, 1, -1);
|
|
187
187
|
case 5:
|
|
188
|
-
return new
|
|
188
|
+
return new NameIndex(0, 2, 3, 1, 4);
|
|
189
189
|
default:
|
|
190
190
|
return NameIndex.base();
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
static only({ prefix = -1, firstName, middleName = -1, lastName, suffix = -1 }) {
|
|
195
|
-
return new
|
|
195
|
+
return new NameIndex(prefix, firstName, middleName, lastName, suffix);
|
|
196
196
|
}
|
|
197
197
|
toJson() {
|
|
198
198
|
return {
|
|
@@ -203,32 +203,25 @@
|
|
|
203
203
|
suffix: this.suffix,
|
|
204
204
|
};
|
|
205
205
|
}
|
|
206
|
+
json = this.toJson;
|
|
206
207
|
}
|
|
207
208
|
function capitalize(str, range = exports.CapsRange.INITIAL) {
|
|
208
209
|
if (!str || range === exports.CapsRange.NONE)
|
|
209
210
|
return str;
|
|
210
|
-
const initial = str[0].toUpperCase();
|
|
211
|
-
const rest = str.slice(1).toLowerCase();
|
|
211
|
+
const [initial, rest] = [str[0].toUpperCase(), str.slice(1).toLowerCase()];
|
|
212
212
|
return range === exports.CapsRange.INITIAL ? initial.concat(rest) : str.toUpperCase();
|
|
213
213
|
}
|
|
214
214
|
function decapitalize(str, range = exports.CapsRange.INITIAL) {
|
|
215
215
|
if (!str || range === exports.CapsRange.NONE)
|
|
216
216
|
return str;
|
|
217
|
-
const initial = str[0].toLowerCase();
|
|
218
|
-
const rest = str.slice(1);
|
|
217
|
+
const [initial, rest] = [str[0].toLowerCase(), str.slice(1)];
|
|
219
218
|
return range === exports.CapsRange.INITIAL ? initial.concat(rest) : str.toLowerCase();
|
|
220
219
|
}
|
|
221
220
|
function toggleCase(str) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
chars.push(c.toUpperCase());
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
return chars.join('');
|
|
221
|
+
return str
|
|
222
|
+
.split('')
|
|
223
|
+
.map((c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()))
|
|
224
|
+
.join('');
|
|
232
225
|
}
|
|
233
226
|
function isStringArray(value) {
|
|
234
227
|
return Array.isArray(value) && value.length > 0 && value.every((e) => typeof e === 'string');
|
|
@@ -251,14 +244,11 @@
|
|
|
251
244
|
this.name = 'NameError';
|
|
252
245
|
}
|
|
253
246
|
get sourceAsString() {
|
|
254
|
-
let input = '';
|
|
255
|
-
if (!this.source)
|
|
256
|
-
input = '<undefined>';
|
|
257
247
|
if (typeof this.source === 'string')
|
|
258
|
-
|
|
248
|
+
return this.source;
|
|
259
249
|
if (isStringArray(this.source))
|
|
260
|
-
|
|
261
|
-
return
|
|
250
|
+
return this.source.join(' ');
|
|
251
|
+
return '<undefined>';
|
|
262
252
|
}
|
|
263
253
|
get hasMessage() {
|
|
264
254
|
return this.message.trim().length > 0;
|
|
@@ -310,7 +300,7 @@
|
|
|
310
300
|
origin;
|
|
311
301
|
constructor(error) {
|
|
312
302
|
super(error.source, error.message, exports.NameErrorType.UNKNOWN);
|
|
313
|
-
this.origin = error.
|
|
303
|
+
this.origin = error.origin;
|
|
314
304
|
this.name = 'UnknownError';
|
|
315
305
|
}
|
|
316
306
|
toString() {
|
|
@@ -360,19 +350,19 @@
|
|
|
360
350
|
return this.type === Namon.SUFFIX;
|
|
361
351
|
}
|
|
362
352
|
static prefix(value) {
|
|
363
|
-
return new
|
|
353
|
+
return new Name(value, Namon.PREFIX);
|
|
364
354
|
}
|
|
365
355
|
static first(value) {
|
|
366
|
-
return new
|
|
356
|
+
return new Name(value, Namon.FIRST_NAME);
|
|
367
357
|
}
|
|
368
358
|
static middle(value) {
|
|
369
|
-
return new
|
|
359
|
+
return new Name(value, Namon.MIDDLE_NAME);
|
|
370
360
|
}
|
|
371
361
|
static last(value) {
|
|
372
|
-
return new
|
|
362
|
+
return new Name(value, Namon.LAST_NAME);
|
|
373
363
|
}
|
|
374
364
|
static suffix(value) {
|
|
375
|
-
return new
|
|
365
|
+
return new Name(value, Namon.SUFFIX);
|
|
376
366
|
}
|
|
377
367
|
initials() {
|
|
378
368
|
return [this.initial];
|
|
@@ -392,8 +382,8 @@
|
|
|
392
382
|
return this;
|
|
393
383
|
}
|
|
394
384
|
validate(name) {
|
|
395
|
-
if (name && name
|
|
396
|
-
throw new InputError({ source: name, message: 'must be
|
|
385
|
+
if (typeof name === 'string' && name.trim().length < 1) {
|
|
386
|
+
throw new InputError({ source: name, message: 'must be 1+ characters' });
|
|
397
387
|
}
|
|
398
388
|
}
|
|
399
389
|
}
|
|
@@ -401,7 +391,7 @@
|
|
|
401
391
|
#more;
|
|
402
392
|
constructor(value, ...more) {
|
|
403
393
|
super(value, Namon.FIRST_NAME);
|
|
404
|
-
more.forEach(
|
|
394
|
+
more.forEach(this.validate);
|
|
405
395
|
this.#more = more;
|
|
406
396
|
}
|
|
407
397
|
get hasMore() {
|
|
@@ -412,9 +402,8 @@
|
|
|
412
402
|
}
|
|
413
403
|
get asNames() {
|
|
414
404
|
const names = [Name.first(this.value)];
|
|
415
|
-
if (this.hasMore)
|
|
416
|
-
names.push(...this.#more.map(
|
|
417
|
-
}
|
|
405
|
+
if (this.hasMore)
|
|
406
|
+
names.push(...this.#more.map(Name.first));
|
|
418
407
|
return names;
|
|
419
408
|
}
|
|
420
409
|
get more() {
|
|
@@ -425,9 +414,8 @@
|
|
|
425
414
|
}
|
|
426
415
|
initials(withMore = false) {
|
|
427
416
|
const inits = [this.initial];
|
|
428
|
-
if (withMore && this.hasMore)
|
|
417
|
+
if (withMore && this.hasMore)
|
|
429
418
|
inits.push(...this.#more.map((n) => n[0]));
|
|
430
|
-
}
|
|
431
419
|
return inits;
|
|
432
420
|
}
|
|
433
421
|
caps(range) {
|
|
@@ -489,34 +477,32 @@
|
|
|
489
477
|
}
|
|
490
478
|
}
|
|
491
479
|
initials(format) {
|
|
492
|
-
format = format || this.format;
|
|
493
480
|
const inits = [];
|
|
494
|
-
switch (format) {
|
|
495
|
-
case exports.Surname.MOTHER:
|
|
496
|
-
if (this.#mother)
|
|
497
|
-
inits.push(this.#mother[0]);
|
|
498
|
-
break;
|
|
481
|
+
switch (format ?? this.format) {
|
|
499
482
|
case exports.Surname.HYPHENATED:
|
|
500
483
|
case exports.Surname.ALL:
|
|
501
484
|
inits.push(this.initial);
|
|
502
485
|
if (this.#mother)
|
|
503
486
|
inits.push(this.#mother[0]);
|
|
504
487
|
break;
|
|
505
|
-
case exports.Surname.
|
|
488
|
+
case exports.Surname.MOTHER:
|
|
489
|
+
if (this.#mother)
|
|
490
|
+
inits.push(this.#mother[0]);
|
|
491
|
+
break;
|
|
506
492
|
default:
|
|
507
493
|
inits.push(this.initial);
|
|
508
494
|
}
|
|
509
495
|
return inits;
|
|
510
496
|
}
|
|
511
497
|
caps(range) {
|
|
512
|
-
range
|
|
498
|
+
range ??= this.capsRange;
|
|
513
499
|
this.value = capitalize(this.value, range);
|
|
514
500
|
if (this.hasMother)
|
|
515
501
|
this.#mother = capitalize(this.#mother, range);
|
|
516
502
|
return this;
|
|
517
503
|
}
|
|
518
504
|
decaps(range) {
|
|
519
|
-
range
|
|
505
|
+
range ??= this.capsRange;
|
|
520
506
|
this.value = decapitalize(this.value, range);
|
|
521
507
|
if (this.hasMother)
|
|
522
508
|
this.#mother = decapitalize(this.#mother, range);
|
|
@@ -574,7 +560,7 @@
|
|
|
574
560
|
}
|
|
575
561
|
static create(name = defaultName) {
|
|
576
562
|
if (!_a.cache.has(name))
|
|
577
|
-
_a.cache.set(name, new
|
|
563
|
+
_a.cache.set(name, new _a(name));
|
|
578
564
|
return _a.cache.get(name);
|
|
579
565
|
}
|
|
580
566
|
static merge(other) {
|
|
@@ -615,10 +601,19 @@
|
|
|
615
601
|
this.#surname = exports.Surname.FATHER;
|
|
616
602
|
_a.cache.set(this.name, this);
|
|
617
603
|
}
|
|
618
|
-
updateOrder(
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
604
|
+
updateOrder(orderedBy) {
|
|
605
|
+
this.update({ orderedBy });
|
|
606
|
+
}
|
|
607
|
+
update({ orderedBy, title, ending }) {
|
|
608
|
+
const config = _a.cache.get(this.name);
|
|
609
|
+
if (!config)
|
|
610
|
+
return;
|
|
611
|
+
if (orderedBy !== this.#orderedBy)
|
|
612
|
+
config.#orderedBy = orderedBy;
|
|
613
|
+
if (title !== this.#title)
|
|
614
|
+
config.#title = title;
|
|
615
|
+
if (ending !== this.#ending)
|
|
616
|
+
config.#ending = ending;
|
|
622
617
|
}
|
|
623
618
|
#genNewName(name) {
|
|
624
619
|
return name === this.name || _a.cache.has(name) ? this.#genNewName(name + copyAlias) : name;
|
|
@@ -641,7 +636,7 @@
|
|
|
641
636
|
if (values.length === 0 || values.length < MIN_NUMBER_OF_NAME_PARTS || values.length > MAX_NUMBER_OF_NAME_PARTS) {
|
|
642
637
|
throw new InputError({
|
|
643
638
|
source: values.map((n) => n.toString()),
|
|
644
|
-
message: `expecting a list of ${MIN_NUMBER_OF_NAME_PARTS}-${
|
|
639
|
+
message: `expecting a list of ${MIN_NUMBER_OF_NAME_PARTS}-${MAX_NUMBER_OF_NAME_PARTS} elements`,
|
|
645
640
|
});
|
|
646
641
|
}
|
|
647
642
|
}
|
|
@@ -649,7 +644,7 @@
|
|
|
649
644
|
class NamonValidator {
|
|
650
645
|
static #validator;
|
|
651
646
|
static create() {
|
|
652
|
-
return this.#validator || (this.#validator = new
|
|
647
|
+
return this.#validator || (this.#validator = new NamonValidator());
|
|
653
648
|
}
|
|
654
649
|
validate(value, type) {
|
|
655
650
|
if (value instanceof Name) {
|
|
@@ -660,22 +655,19 @@
|
|
|
660
655
|
throw new ValidationError({
|
|
661
656
|
source: value,
|
|
662
657
|
nameType: 'namon',
|
|
663
|
-
message: 'invalid content',
|
|
658
|
+
message: 'invalid name content failing namon regex',
|
|
664
659
|
});
|
|
665
660
|
}
|
|
666
661
|
}
|
|
667
662
|
else {
|
|
668
|
-
throw new InputError({
|
|
669
|
-
source: typeof value,
|
|
670
|
-
message: 'expecting types of string | Name',
|
|
671
|
-
});
|
|
663
|
+
throw new InputError({ source: typeof value, message: 'expecting types of string or Name' });
|
|
672
664
|
}
|
|
673
665
|
}
|
|
674
666
|
}
|
|
675
667
|
class FirstNameValidator {
|
|
676
668
|
static #validator;
|
|
677
669
|
static create() {
|
|
678
|
-
return this.#validator || (this.#validator = new
|
|
670
|
+
return this.#validator || (this.#validator = new FirstNameValidator());
|
|
679
671
|
}
|
|
680
672
|
validate(value) {
|
|
681
673
|
if (value instanceof FirstName) {
|
|
@@ -686,22 +678,19 @@
|
|
|
686
678
|
throw new ValidationError({
|
|
687
679
|
source: value,
|
|
688
680
|
nameType: 'firstName',
|
|
689
|
-
message: 'invalid content',
|
|
681
|
+
message: 'invalid name content failing firstName regex',
|
|
690
682
|
});
|
|
691
683
|
}
|
|
692
684
|
}
|
|
693
685
|
else {
|
|
694
|
-
throw new InputError({
|
|
695
|
-
source: typeof value,
|
|
696
|
-
message: 'expecting types string | FirstName',
|
|
697
|
-
});
|
|
686
|
+
throw new InputError({ source: typeof value, message: 'expecting types string or FirstName' });
|
|
698
687
|
}
|
|
699
688
|
}
|
|
700
689
|
}
|
|
701
690
|
class MiddleNameValidator {
|
|
702
691
|
static #validator;
|
|
703
692
|
static create() {
|
|
704
|
-
return this.#validator || (this.#validator = new
|
|
693
|
+
return this.#validator || (this.#validator = new MiddleNameValidator());
|
|
705
694
|
}
|
|
706
695
|
validate(value) {
|
|
707
696
|
if (typeof value === 'string') {
|
|
@@ -709,7 +698,7 @@
|
|
|
709
698
|
throw new ValidationError({
|
|
710
699
|
source: value,
|
|
711
700
|
nameType: 'middleName',
|
|
712
|
-
message: 'invalid content',
|
|
701
|
+
message: 'invalid name content failing middleName regex',
|
|
713
702
|
});
|
|
714
703
|
}
|
|
715
704
|
}
|
|
@@ -730,7 +719,7 @@
|
|
|
730
719
|
else {
|
|
731
720
|
throw new InputError({
|
|
732
721
|
source: typeof value,
|
|
733
|
-
message: 'expecting types of string
|
|
722
|
+
message: 'expecting types of string, string[] or Name[]',
|
|
734
723
|
});
|
|
735
724
|
}
|
|
736
725
|
}
|
|
@@ -738,7 +727,7 @@
|
|
|
738
727
|
class LastNameValidator {
|
|
739
728
|
static #validator;
|
|
740
729
|
static create() {
|
|
741
|
-
return this.#validator || (this.#validator = new
|
|
730
|
+
return this.#validator || (this.#validator = new LastNameValidator());
|
|
742
731
|
}
|
|
743
732
|
validate(value) {
|
|
744
733
|
if (value instanceof LastName) {
|
|
@@ -749,36 +738,33 @@
|
|
|
749
738
|
throw new ValidationError({
|
|
750
739
|
source: value,
|
|
751
740
|
nameType: 'lastName',
|
|
752
|
-
message: 'invalid content',
|
|
741
|
+
message: 'invalid name content failing lastName regex',
|
|
753
742
|
});
|
|
754
743
|
}
|
|
755
744
|
}
|
|
756
745
|
else {
|
|
757
|
-
throw new InputError({
|
|
758
|
-
source: typeof value,
|
|
759
|
-
message: 'expecting types string | LastName',
|
|
760
|
-
});
|
|
746
|
+
throw new InputError({ source: typeof value, message: 'expecting types string or LastName' });
|
|
761
747
|
}
|
|
762
748
|
}
|
|
763
749
|
}
|
|
764
750
|
class NameValidator {
|
|
765
751
|
static #validator;
|
|
766
752
|
static create() {
|
|
767
|
-
return this.#validator || (this.#validator = new
|
|
753
|
+
return this.#validator || (this.#validator = new NameValidator());
|
|
768
754
|
}
|
|
769
755
|
validate(name, type) {
|
|
770
756
|
if (type && name.type !== type) {
|
|
771
757
|
throw new ValidationError({
|
|
772
758
|
source: name.toString(),
|
|
773
759
|
nameType: name.type.toString(),
|
|
774
|
-
message: 'wrong type',
|
|
760
|
+
message: 'wrong name type; only Namon types are supported',
|
|
775
761
|
});
|
|
776
762
|
}
|
|
777
763
|
if (!ValidationRule.namon.test(name.value)) {
|
|
778
764
|
throw new ValidationError({
|
|
779
765
|
source: name.toString(),
|
|
780
766
|
nameType: name.type.toString(),
|
|
781
|
-
message: 'invalid content',
|
|
767
|
+
message: 'invalid name content failing namon regex',
|
|
782
768
|
});
|
|
783
769
|
}
|
|
784
770
|
}
|
|
@@ -786,7 +772,7 @@
|
|
|
786
772
|
class NamaValidator {
|
|
787
773
|
static #validator;
|
|
788
774
|
static create() {
|
|
789
|
-
return this.#validator || (this.#validator = new
|
|
775
|
+
return this.#validator || (this.#validator = new NamaValidator());
|
|
790
776
|
}
|
|
791
777
|
validate(value) {
|
|
792
778
|
this.validateKeys(value);
|
|
@@ -804,20 +790,14 @@
|
|
|
804
790
|
else if (nama.size < MIN_NUMBER_OF_NAME_PARTS || nama.size > MAX_NUMBER_OF_NAME_PARTS) {
|
|
805
791
|
throw new InputError({
|
|
806
792
|
source: [...nama.values()],
|
|
807
|
-
message: `expecting ${MIN_NUMBER_OF_NAME_PARTS}-${
|
|
793
|
+
message: `expecting ${MIN_NUMBER_OF_NAME_PARTS}-${MAX_NUMBER_OF_NAME_PARTS} fields`,
|
|
808
794
|
});
|
|
809
795
|
}
|
|
810
796
|
if (!nama.has(Namon.FIRST_NAME)) {
|
|
811
|
-
throw new InputError({
|
|
812
|
-
source: [...nama.values()],
|
|
813
|
-
message: '"firstName" is a required key',
|
|
814
|
-
});
|
|
797
|
+
throw new InputError({ source: [...nama.values()], message: '"firstName" is a required key' });
|
|
815
798
|
}
|
|
816
799
|
if (!nama.has(Namon.LAST_NAME)) {
|
|
817
|
-
throw new InputError({
|
|
818
|
-
source: [...nama.values()],
|
|
819
|
-
message: '"lastName" is a required key',
|
|
820
|
-
});
|
|
800
|
+
throw new InputError({ source: [...nama.values()], message: '"lastName" is a required key' });
|
|
821
801
|
}
|
|
822
802
|
}
|
|
823
803
|
}
|
|
@@ -845,7 +825,7 @@
|
|
|
845
825
|
class ArrayNameValidator {
|
|
846
826
|
static #validator;
|
|
847
827
|
static create() {
|
|
848
|
-
return this.#validator || (this.#validator = new
|
|
828
|
+
return this.#validator || (this.#validator = new ArrayNameValidator());
|
|
849
829
|
}
|
|
850
830
|
validate(value) {
|
|
851
831
|
if (value.length < MIN_NUMBER_OF_NAME_PARTS) {
|
|
@@ -911,13 +891,12 @@
|
|
|
911
891
|
}
|
|
912
892
|
static parse(json, config) {
|
|
913
893
|
try {
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
return fullName;
|
|
894
|
+
return new FullName(config)
|
|
895
|
+
.setPrefix(json.prefix)
|
|
896
|
+
.setFirstName(json.firstName)
|
|
897
|
+
.setMiddleName(json.middleName ?? [])
|
|
898
|
+
.setLastName(json.lastName)
|
|
899
|
+
.setSuffix(json.suffix);
|
|
921
900
|
}
|
|
922
901
|
catch (error) {
|
|
923
902
|
if (error instanceof NameError)
|
|
@@ -925,7 +904,7 @@
|
|
|
925
904
|
throw new UnknownError({
|
|
926
905
|
source: Object.values(json).join(' '),
|
|
927
906
|
message: 'could not parse JSON content',
|
|
928
|
-
|
|
907
|
+
origin: error instanceof Error ? error : new Error(String(error)),
|
|
929
908
|
});
|
|
930
909
|
}
|
|
931
910
|
}
|
|
@@ -955,7 +934,7 @@
|
|
|
955
934
|
return this;
|
|
956
935
|
if (!this.#config.bypass)
|
|
957
936
|
Validators.middleName.validate(names);
|
|
958
|
-
this.#middleName = names.map((
|
|
937
|
+
this.#middleName = names.map((n) => (n instanceof Name ? n : Name.middle(n)));
|
|
959
938
|
return this;
|
|
960
939
|
}
|
|
961
940
|
setSuffix(name) {
|
|
@@ -984,7 +963,7 @@
|
|
|
984
963
|
const parts = text.trim().split(Separator.SPACE.token);
|
|
985
964
|
const length = parts.length;
|
|
986
965
|
if (index instanceof NameIndex) {
|
|
987
|
-
const names = Object.entries(index.
|
|
966
|
+
const names = Object.entries(index.json())
|
|
988
967
|
.filter(([, position]) => position > -1 && position < length)
|
|
989
968
|
.map(([key, position]) => new Name(parts[position], Namon.all.get(key)));
|
|
990
969
|
return new ArrayNameParser(names);
|
|
@@ -1048,16 +1027,7 @@
|
|
|
1048
1027
|
class NamaParser extends Parser {
|
|
1049
1028
|
parse(options) {
|
|
1050
1029
|
const config = Config.merge(options);
|
|
1051
|
-
|
|
1052
|
-
NamaValidator.create().validateKeys(this.#asNama());
|
|
1053
|
-
}
|
|
1054
|
-
else {
|
|
1055
|
-
NamaValidator.create().validate(this.#asNama());
|
|
1056
|
-
}
|
|
1057
|
-
return FullName.parse(this.raw, config);
|
|
1058
|
-
}
|
|
1059
|
-
#asNama() {
|
|
1060
|
-
return new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
1030
|
+
const names = new Map(Object.entries(this.raw).map(([key, value]) => {
|
|
1061
1031
|
const namon = Namon.cast(key);
|
|
1062
1032
|
if (!namon) {
|
|
1063
1033
|
throw new InputError({
|
|
@@ -1067,6 +1037,13 @@
|
|
|
1067
1037
|
}
|
|
1068
1038
|
return [namon, value];
|
|
1069
1039
|
}));
|
|
1040
|
+
if (config.bypass) {
|
|
1041
|
+
NamaValidator.create().validateKeys(names);
|
|
1042
|
+
}
|
|
1043
|
+
else {
|
|
1044
|
+
NamaValidator.create().validate(names);
|
|
1045
|
+
}
|
|
1046
|
+
return FullName.parse(this.raw, config);
|
|
1070
1047
|
}
|
|
1071
1048
|
}
|
|
1072
1049
|
class ArrayNameParser extends Parser {
|
|
@@ -1182,16 +1159,16 @@
|
|
|
1182
1159
|
suffix: this.suffix,
|
|
1183
1160
|
};
|
|
1184
1161
|
}
|
|
1162
|
+
json = this.toJson;
|
|
1185
1163
|
has(namon) {
|
|
1186
1164
|
return this.#fullName.has(namon);
|
|
1187
1165
|
}
|
|
1188
1166
|
fullName(orderedBy) {
|
|
1189
1167
|
const sep = this.config.ending ? ',' : '';
|
|
1190
1168
|
const names = [];
|
|
1191
|
-
orderedBy = orderedBy || this.config.orderedBy;
|
|
1192
1169
|
if (this.prefix)
|
|
1193
1170
|
names.push(this.prefix);
|
|
1194
|
-
if (orderedBy === exports.NameOrder.FIRST_NAME) {
|
|
1171
|
+
if ((orderedBy ?? this.config.orderedBy) === exports.NameOrder.FIRST_NAME) {
|
|
1195
1172
|
names.push(this.first, ...this.middleName(), this.last + sep);
|
|
1196
1173
|
}
|
|
1197
1174
|
else {
|
|
@@ -1202,7 +1179,7 @@
|
|
|
1202
1179
|
return names.join(' ').trim();
|
|
1203
1180
|
}
|
|
1204
1181
|
birthName(orderedBy) {
|
|
1205
|
-
orderedBy
|
|
1182
|
+
orderedBy ??= this.config.orderedBy;
|
|
1206
1183
|
return orderedBy === exports.NameOrder.FIRST_NAME
|
|
1207
1184
|
? [this.first, ...this.middleName(), this.last].join(' ')
|
|
1208
1185
|
: [this.last, this.first, ...this.middleName()].join(' ');
|
|
@@ -1217,50 +1194,42 @@
|
|
|
1217
1194
|
return this.#fullName.lastName.toString(format);
|
|
1218
1195
|
}
|
|
1219
1196
|
initials(options) {
|
|
1197
|
+
const { orderedBy = this.config.orderedBy, only = exports.NameType.BIRTH_NAME, asJson } = options ?? {};
|
|
1220
1198
|
const firstInits = this.#fullName.firstName.initials();
|
|
1221
|
-
const midInits = this.#fullName.middleName.map((n) => n.
|
|
1199
|
+
const midInits = this.#fullName.middleName.map((n) => n.value[0]);
|
|
1222
1200
|
const lastInits = this.#fullName.lastName.initials();
|
|
1223
|
-
if (
|
|
1201
|
+
if (asJson)
|
|
1224
1202
|
return { firstName: firstInits, middleName: midInits, lastName: lastInits };
|
|
1225
|
-
const initials = [];
|
|
1226
|
-
const { orderedBy = this.config.orderedBy, only = exports.NameType.BIRTH_NAME } = options ?? {};
|
|
1227
1203
|
if (only !== exports.NameType.BIRTH_NAME) {
|
|
1228
|
-
|
|
1229
|
-
initials.push(...firstInits);
|
|
1230
|
-
}
|
|
1231
|
-
else if (only === exports.NameType.MIDDLE_NAME) {
|
|
1232
|
-
initials.push(...midInits);
|
|
1233
|
-
}
|
|
1234
|
-
else {
|
|
1235
|
-
initials.push(...lastInits);
|
|
1236
|
-
}
|
|
1204
|
+
return only === exports.NameType.FIRST_NAME ? firstInits : only === exports.NameType.MIDDLE_NAME ? midInits : lastInits;
|
|
1237
1205
|
}
|
|
1238
1206
|
else if (orderedBy === exports.NameOrder.FIRST_NAME) {
|
|
1239
|
-
|
|
1207
|
+
return [...firstInits, ...midInits, ...lastInits];
|
|
1240
1208
|
}
|
|
1241
1209
|
else {
|
|
1242
|
-
|
|
1210
|
+
return [...lastInits, ...firstInits, ...midInits];
|
|
1243
1211
|
}
|
|
1244
|
-
return initials;
|
|
1245
1212
|
}
|
|
1246
1213
|
shorten(orderedBy) {
|
|
1247
|
-
orderedBy
|
|
1214
|
+
orderedBy ??= this.config.orderedBy;
|
|
1215
|
+
const { firstName, lastName } = this.#fullName;
|
|
1248
1216
|
return orderedBy === exports.NameOrder.FIRST_NAME
|
|
1249
|
-
? [
|
|
1250
|
-
: [
|
|
1217
|
+
? [firstName.value, lastName.toString()].join(' ')
|
|
1218
|
+
: [lastName.toString(), firstName.value].join(' ');
|
|
1251
1219
|
}
|
|
1252
1220
|
flatten(options) {
|
|
1253
1221
|
const { by = exports.Flat.MIDDLE_NAME, limit = 20, recursive = false, withMore = false, withPeriod = true, surname, } = options;
|
|
1254
1222
|
if (this.length <= limit)
|
|
1255
1223
|
return this.full;
|
|
1224
|
+
const { firstName, lastName, middleName } = this.#fullName;
|
|
1256
1225
|
const sep = withPeriod ? '.' : '';
|
|
1257
|
-
const fn = this.#fullName.firstName.toString();
|
|
1258
|
-
const mn = this.middleName().join(' ');
|
|
1259
|
-
const ln = this.#fullName.lastName.toString();
|
|
1260
1226
|
const hasMid = this.hasMiddle;
|
|
1261
|
-
const
|
|
1262
|
-
const
|
|
1263
|
-
const
|
|
1227
|
+
const fn = firstName.toString();
|
|
1228
|
+
const mn = this.middleName().join(' ');
|
|
1229
|
+
const ln = lastName.toString();
|
|
1230
|
+
const f = firstName.initials(withMore).join(sep + ' ') + sep;
|
|
1231
|
+
const l = lastName.initials(surname).join(sep + ' ') + sep;
|
|
1232
|
+
const m = hasMid ? middleName.map((n) => n.value[0]).join(sep + ' ') + sep : '';
|
|
1264
1233
|
let name = [];
|
|
1265
1234
|
if (this.config.orderedBy === exports.NameOrder.FIRST_NAME) {
|
|
1266
1235
|
switch (by) {
|
|
@@ -1341,8 +1310,8 @@
|
|
|
1341
1310
|
pattern = 'o';
|
|
1342
1311
|
let group = '';
|
|
1343
1312
|
const formatted = [];
|
|
1344
|
-
for (const char of pattern
|
|
1345
|
-
if (
|
|
1313
|
+
for (const char of pattern) {
|
|
1314
|
+
if (ALLOWED_FORMAT_TOKENS.indexOf(char) === -1) {
|
|
1346
1315
|
throw new NotAllowedError({
|
|
1347
1316
|
source: this.full,
|
|
1348
1317
|
operation: 'format',
|
|
@@ -1358,7 +1327,8 @@
|
|
|
1358
1327
|
return formatted.join('').trim();
|
|
1359
1328
|
}
|
|
1360
1329
|
flip() {
|
|
1361
|
-
this.config.
|
|
1330
|
+
const order = this.config.orderedBy === exports.NameOrder.FIRST_NAME ? exports.NameOrder.LAST_NAME : exports.NameOrder.FIRST_NAME;
|
|
1331
|
+
this.config.update({ orderedBy: order });
|
|
1362
1332
|
}
|
|
1363
1333
|
split(separator = /[' -]/g) {
|
|
1364
1334
|
return this.birth.replace(separator, ' ').split(' ');
|
|
@@ -1409,7 +1379,7 @@
|
|
|
1409
1379
|
return new ArrayNameParser(raw);
|
|
1410
1380
|
if (typeof raw === 'object')
|
|
1411
1381
|
return new NamaParser(raw);
|
|
1412
|
-
throw new InputError({ source: raw, message: 'Cannot parse raw data
|
|
1382
|
+
throw new InputError({ source: raw, message: 'Cannot parse raw data; review expected data types.' });
|
|
1413
1383
|
}
|
|
1414
1384
|
#map(char) {
|
|
1415
1385
|
switch (char) {
|
|
@@ -1437,16 +1407,15 @@
|
|
|
1437
1407
|
case 'o':
|
|
1438
1408
|
case 'O':
|
|
1439
1409
|
return ((character) => {
|
|
1440
|
-
const sep = this.config.ending ? ',' : ''
|
|
1410
|
+
const sep = this.config.ending ? ',' : '';
|
|
1411
|
+
const names = [];
|
|
1441
1412
|
if (this.prefix)
|
|
1442
1413
|
names.push(this.prefix);
|
|
1443
1414
|
names.push(`${this.last},`.toUpperCase());
|
|
1444
|
-
if (this.hasMiddle)
|
|
1415
|
+
if (this.hasMiddle)
|
|
1445
1416
|
names.push(this.first, this.middleName().join(' ') + sep);
|
|
1446
|
-
|
|
1447
|
-
else {
|
|
1417
|
+
else
|
|
1448
1418
|
names.push(this.first + sep);
|
|
1449
|
-
}
|
|
1450
1419
|
if (this.suffix)
|
|
1451
1420
|
names.push(this.suffix);
|
|
1452
1421
|
const nama = names.join(' ').trim();
|
|
@@ -1462,10 +1431,10 @@
|
|
|
1462
1431
|
return this.suffix?.toUpperCase();
|
|
1463
1432
|
case '$f':
|
|
1464
1433
|
case '$F':
|
|
1465
|
-
return this.#fullName.firstName.
|
|
1434
|
+
return this.#fullName.firstName.value[0];
|
|
1466
1435
|
case '$l':
|
|
1467
1436
|
case '$L':
|
|
1468
|
-
return this.#fullName.lastName.
|
|
1437
|
+
return this.#fullName.lastName.value[0];
|
|
1469
1438
|
case '$m':
|
|
1470
1439
|
case '$M':
|
|
1471
1440
|
return this.hasMiddle ? this.middle[0] : undefined;
|