namefully 2.0.2 → 2.1.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/namefully.js CHANGED
@@ -4,33 +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.2';
7
+ const VERSION = '2.1.0';
8
8
  const MIN_NUMBER_OF_NAME_PARTS = 2;
9
9
  const MAX_NUMBER_OF_NAME_PARTS = 5;
10
- const ALLOWED_FORMAT_TOKENS = [
11
- '.',
12
- ',',
13
- ' ',
14
- '-',
15
- '_',
16
- 'b',
17
- 'B',
18
- 'f',
19
- 'F',
20
- 'l',
21
- 'L',
22
- 'm',
23
- 'M',
24
- 'n',
25
- 'N',
26
- 'o',
27
- 'O',
28
- 'p',
29
- 'P',
30
- 's',
31
- 'S',
32
- '$',
33
- ];
10
+ const ALLOWED_FORMAT_TOKENS = ` .,_-()[]<>'"bBfFlLmMnNoOpPsS$`;
34
11
 
35
12
  exports.Title = void 0;
36
13
  (function (Title) {
@@ -87,6 +64,13 @@
87
64
  [Namon.LAST_NAME.key, Namon.LAST_NAME],
88
65
  [Namon.SUFFIX.key, Namon.SUFFIX],
89
66
  ]);
67
+ static aliases = {
68
+ [Namon.PREFIX.key]: ['prefix', 'px', 'p'],
69
+ [Namon.FIRST_NAME.key]: ['firstname', 'first', 'fn', 'f'],
70
+ [Namon.MIDDLE_NAME.key]: ['middlename', 'middle', 'mid', 'mn', 'm'],
71
+ [Namon.LAST_NAME.key]: ['lastname', 'last', 'ln', 'l'],
72
+ [Namon.SUFFIX.key]: ['suffix', 'sx', 's'],
73
+ };
90
74
  constructor(index, key) {
91
75
  this.index = index;
92
76
  this.key = key;
@@ -95,7 +79,9 @@
95
79
  return Namon.all.has(key);
96
80
  }
97
81
  static cast(key) {
98
- return Namon.has(key) ? Namon.all.get(key) : undefined;
82
+ const searchValue = String(key).toLowerCase();
83
+ const namon = Object.entries(Namon.aliases).find(([, list]) => list.includes(searchValue))?.[0];
84
+ return Namon.has(namon ?? '') ? Namon.all.get(key) : undefined;
99
85
  }
100
86
  toString() {
101
87
  return `Namon.${this.key}`;
@@ -134,6 +120,14 @@
134
120
  this.name = name;
135
121
  this.token = token;
136
122
  }
123
+ static cast(key) {
124
+ for (const [name, separator] of Separator.all) {
125
+ if (separator.token === key || name.toLowerCase() === key.toLowerCase()) {
126
+ return separator;
127
+ }
128
+ }
129
+ return undefined;
130
+ }
137
131
  toString() {
138
132
  return `Separator.${this.name}`;
139
133
  }
@@ -472,7 +466,7 @@
472
466
  return this.mother ?? '';
473
467
  case exports.Surname.HYPHENATED:
474
468
  return this.hasMother ? `${this.value}-${this.#mother}` : this.value;
475
- case exports.Surname.ALL:
469
+ default:
476
470
  return this.hasMother ? `${this.value} ${this.#mother}` : this.value;
477
471
  }
478
472
  }
@@ -945,13 +939,35 @@
945
939
  this.#suffix = Name.suffix(name instanceof Name ? name.value : name);
946
940
  return this;
947
941
  }
948
- has(namon) {
942
+ has(key) {
943
+ const namon = typeof key === 'string' ? Namon.cast(key) : key;
944
+ if (!namon)
945
+ return false;
949
946
  if (namon.equal(Namon.PREFIX))
950
947
  return !!this.#prefix;
951
948
  if (namon.equal(Namon.SUFFIX))
952
949
  return !!this.#suffix;
953
950
  return namon.equal(Namon.MIDDLE_NAME) ? this.#middleName.length > 0 : true;
954
951
  }
952
+ *toIterable(flat = false) {
953
+ if (this.#prefix)
954
+ yield this.#prefix;
955
+ if (flat) {
956
+ yield* this.#firstName.asNames;
957
+ yield* this.#middleName;
958
+ yield* this.#lastName.asNames;
959
+ }
960
+ else {
961
+ yield this.#firstName;
962
+ yield* this.#middleName;
963
+ yield this.#lastName;
964
+ }
965
+ if (this.#suffix)
966
+ yield this.#suffix;
967
+ }
968
+ *[Symbol.iterator]() {
969
+ yield* this.toIterable(true);
970
+ }
955
971
  }
956
972
 
957
973
  class Parser {
@@ -1065,8 +1081,8 @@
1065
1081
  fullName.middleName.push(name);
1066
1082
  }
1067
1083
  else if (name.isLastName) {
1068
- const lastName = new LastName(name.value, name instanceof LastName ? name.mother : undefined, config.surname);
1069
- fullName.setLastName(lastName);
1084
+ const mother = name instanceof LastName ? name.mother : undefined;
1085
+ fullName.setLastName(new LastName(name.value, mother, config.surname));
1070
1086
  }
1071
1087
  }
1072
1088
  return fullName;
@@ -1131,25 +1147,43 @@
1131
1147
  get salutation() {
1132
1148
  return this.format('p l');
1133
1149
  }
1150
+ get parts() {
1151
+ return this.#fullName.toIterable();
1152
+ }
1153
+ get size() {
1154
+ return Array.from(this.parts).length;
1155
+ }
1156
+ *[Symbol.iterator]() {
1157
+ yield* this.#fullName.toIterable(true);
1158
+ }
1134
1159
  toString() {
1135
1160
  return this.full;
1136
1161
  }
1137
- get(namon) {
1138
- if (namon.equal(Namon.PREFIX))
1162
+ get(key) {
1163
+ const namon = typeof key === 'string' ? Namon.cast(key) : key;
1164
+ if (namon?.equal(Namon.PREFIX))
1139
1165
  return this.#fullName.prefix;
1140
- if (namon.equal(Namon.FIRST_NAME))
1166
+ if (namon?.equal(Namon.FIRST_NAME))
1141
1167
  return this.#fullName.firstName;
1142
- if (namon.equal(Namon.MIDDLE_NAME))
1168
+ if (namon?.equal(Namon.MIDDLE_NAME))
1143
1169
  return this.#fullName.middleName;
1144
- if (namon.equal(Namon.LAST_NAME))
1170
+ if (namon?.equal(Namon.LAST_NAME))
1145
1171
  return this.#fullName.lastName;
1146
- if (namon.equal(Namon.SUFFIX))
1172
+ if (namon?.equal(Namon.SUFFIX))
1147
1173
  return this.#fullName.suffix;
1148
1174
  return undefined;
1149
1175
  }
1150
1176
  equal(other) {
1151
1177
  return this.toString() === other.toString();
1152
1178
  }
1179
+ deepEqual(other) {
1180
+ const others = Array.from(other.parts);
1181
+ for (const part of this.parts) {
1182
+ if (!others.some((name) => name.equal(part)))
1183
+ return false;
1184
+ }
1185
+ return true;
1186
+ }
1153
1187
  toJson() {
1154
1188
  return {
1155
1189
  prefix: this.prefix,
@@ -1248,7 +1282,7 @@
1248
1282
  case exports.Flat.MID_LAST:
1249
1283
  name = hasMid ? [fn, m, l] : [fn, l];
1250
1284
  break;
1251
- case exports.Flat.ALL:
1285
+ default:
1252
1286
  name = hasMid ? [f, m, l] : [f, l];
1253
1287
  break;
1254
1288
  }
@@ -1270,7 +1304,7 @@
1270
1304
  case exports.Flat.MID_LAST:
1271
1305
  name = hasMid ? [l, fn, m] : [l, fn];
1272
1306
  break;
1273
- case exports.Flat.ALL:
1307
+ default:
1274
1308
  name = hasMid ? [l, f, m] : [l, f];
1275
1309
  break;
1276
1310
  }
@@ -1311,7 +1345,7 @@
1311
1345
  let group = '';
1312
1346
  const formatted = [];
1313
1347
  for (const char of pattern) {
1314
- if (ALLOWED_FORMAT_TOKENS.indexOf(char) === -1) {
1348
+ if (!ALLOWED_FORMAT_TOKENS.includes(char)) {
1315
1349
  throw new NotAllowedError({
1316
1350
  source: this.full,
1317
1351
  operation: 'format',
@@ -1383,12 +1417,6 @@
1383
1417
  }
1384
1418
  #map(char) {
1385
1419
  switch (char) {
1386
- case '.':
1387
- case ',':
1388
- case ' ':
1389
- case '-':
1390
- case '_':
1391
- return char;
1392
1420
  case 'b':
1393
1421
  return this.birth;
1394
1422
  case 'B':
@@ -1430,18 +1458,42 @@
1430
1458
  case 'S':
1431
1459
  return this.suffix?.toUpperCase();
1432
1460
  case '$f':
1433
- case '$F':
1434
1461
  return this.#fullName.firstName.value[0];
1462
+ case '$F':
1463
+ return this.#fullName.firstName.initials(true).join('');
1435
1464
  case '$l':
1436
- case '$L':
1437
1465
  return this.#fullName.lastName.value[0];
1466
+ case '$L':
1467
+ return this.#fullName.lastName.initials().join('');
1438
1468
  case '$m':
1439
- case '$M':
1440
1469
  return this.hasMiddle ? this.middle[0] : undefined;
1470
+ case '$M':
1471
+ return this.hasMiddle ? this.#fullName.middleName.map((n) => n.value[0]).join('') : undefined;
1441
1472
  default:
1442
- return undefined;
1473
+ return ALLOWED_FORMAT_TOKENS.includes(char) ? char : undefined;
1443
1474
  }
1444
1475
  }
1476
+ serialize() {
1477
+ const { config, firstName: fn, lastName: ln } = this.#fullName;
1478
+ return {
1479
+ names: {
1480
+ prefix: this.prefix,
1481
+ firstName: fn.hasMore ? { value: fn.value, more: fn.more } : fn.value,
1482
+ middleName: this.hasMiddle ? this.middleName() : undefined,
1483
+ lastName: ln.hasMother ? { father: ln.father, mother: ln.mother } : ln.value,
1484
+ suffix: this.suffix,
1485
+ },
1486
+ config: {
1487
+ name: config.name,
1488
+ orderedBy: config.orderedBy,
1489
+ separator: config.separator.token,
1490
+ title: config.title,
1491
+ ending: config.ending,
1492
+ bypass: config.bypass,
1493
+ surname: config.surname,
1494
+ },
1495
+ };
1496
+ }
1445
1497
  }
1446
1498
  var namefully = (names, options) => {
1447
1499
  return new Namefully(names, options);
@@ -1514,16 +1566,49 @@
1514
1566
  static use({ names, prebuild, postbuild, preclear, postclear, }) {
1515
1567
  return new NameBuilder(names ?? [], prebuild, postbuild, preclear, postclear);
1516
1568
  }
1517
- build(config) {
1569
+ build(options) {
1518
1570
  this.prebuild?.();
1519
1571
  const names = [...this.queue];
1520
1572
  ArrayNameValidator.create().validate(names);
1521
- this.instance = new Namefully(names, config);
1573
+ this.instance = new Namefully(names, options);
1522
1574
  this.postbuild?.(this.instance);
1523
1575
  return this.instance;
1524
1576
  }
1525
1577
  }
1526
1578
 
1579
+ function deserialize(data) {
1580
+ try {
1581
+ const parsed = typeof data === 'string' ? JSON.parse(data) : data;
1582
+ if (!parsed || typeof parsed !== 'object') {
1583
+ throw new InputError({
1584
+ source: String(data),
1585
+ message: 'invalid serialized data; must be an object or a string',
1586
+ });
1587
+ }
1588
+ const { names, config } = parsed;
1589
+ const { firstName: fn, lastName: ln, middleName: mn, prefix: px, suffix: sx } = names;
1590
+ const builder = NameBuilder.of();
1591
+ if (px)
1592
+ builder.add(Name.prefix(px));
1593
+ if (sx)
1594
+ builder.add(Name.suffix(sx));
1595
+ if (mn)
1596
+ builder.add(...mn.map((n) => Name.middle(n)));
1597
+ builder.add(typeof fn === 'string' ? Name.first(fn) : new FirstName(fn.value, ...(fn.more ?? [])));
1598
+ builder.add(typeof ln === 'string' ? Name.last(ln) : new LastName(ln.father, ln.mother));
1599
+ return builder.build(config);
1600
+ }
1601
+ catch (error) {
1602
+ if (error instanceof NameError)
1603
+ throw error;
1604
+ throw new UnknownError({
1605
+ source: String(data),
1606
+ message: 'could not deserialize data',
1607
+ origin: error instanceof Error ? error : new Error(String(error)),
1608
+ });
1609
+ }
1610
+ }
1611
+
1527
1612
  exports.Config = Config;
1528
1613
  exports.FirstName = FirstName;
1529
1614
  exports.FullName = FullName;
@@ -1541,6 +1626,7 @@
1541
1626
  exports.UnknownError = UnknownError;
1542
1627
  exports.ValidationError = ValidationError;
1543
1628
  exports.default = namefully;
1629
+ exports.deserialize = deserialize;
1544
1630
  exports.isNameArray = isNameArray;
1545
1631
  exports.version = VERSION;
1546
1632
 
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).namefully={})}(this,function(e){"use strict";const t=[".",","," ","-","_","b","B","f","F","l","L","m","M","n","N","o","O","p","P","s","S","$"];var s,a,i,r,n,o,h,l;e.Title=void 0,(s=e.Title||(e.Title={})).US="US",s.UK="UK",e.Surname=void 0,(a=e.Surname||(e.Surname={})).FATHER="father",a.MOTHER="mother",a.HYPHENATED="hyphenated",a.ALL="all",e.NameOrder=void 0,(i=e.NameOrder||(e.NameOrder={})).FIRST_NAME="firstName",i.LAST_NAME="lastName",e.NameType=void 0,(r=e.NameType||(e.NameType={})).FIRST_NAME="firstName",r.MIDDLE_NAME="middleName",r.LAST_NAME="lastName",r.BIRTH_NAME="birthName",e.Flat=void 0,(n=e.Flat||(e.Flat={})).FIRST_NAME="firstName",n.MIDDLE_NAME="middleName",n.LAST_NAME="lastName",n.FIRST_MID="firstMid",n.MID_LAST="midLast",n.ALL="all",e.CapsRange=void 0,(o=e.CapsRange||(e.CapsRange={}))[o.NONE=0]="NONE",o[o.INITIAL=1]="INITIAL",o[o.ALL=2]="ALL";class u{index;key;static PREFIX=new u(0,"prefix");static FIRST_NAME=new u(1,"firstName");static MIDDLE_NAME=new u(2,"middleName");static LAST_NAME=new u(3,"lastName");static SUFFIX=new u(4,"suffix");static values=[u.PREFIX,u.FIRST_NAME,u.MIDDLE_NAME,u.LAST_NAME,u.SUFFIX];static all=new Map([[u.PREFIX.key,u.PREFIX],[u.FIRST_NAME.key,u.FIRST_NAME],[u.MIDDLE_NAME.key,u.MIDDLE_NAME],[u.LAST_NAME.key,u.LAST_NAME],[u.SUFFIX.key,u.SUFFIX]]);constructor(e,t){this.index=e,this.key=t}static has(e){return u.all.has(e)}static cast(e){return u.has(e)?u.all.get(e):void 0}toString(){return`Namon.${this.key}`}equal(e){return e instanceof u&&e.index===this.index&&e.key===this.key}}class m{name;token;static COMMA=new m("comma",",");static COLON=new m("colon",":");static DOUBLE_QUOTE=new m("doubleQuote",'"');static EMPTY=new m("empty","");static HYPHEN=new m("hyphen","-");static PERIOD=new m("period",".");static SEMI_COLON=new m("semiColon",";");static SINGLE_QUOTE=new m("singleQuote","'");static SPACE=new m("space"," ");static UNDERSCORE=new m("underscore","_");static all=new Map([[m.COMMA.name,m.COMMA],[m.COLON.name,m.COLON],[m.DOUBLE_QUOTE.name,m.DOUBLE_QUOTE],[m.EMPTY.name,m.EMPTY],[m.HYPHEN.name,m.HYPHEN],[m.PERIOD.name,m.PERIOD],[m.SEMI_COLON.name,m.SEMI_COLON],[m.SINGLE_QUOTE.name,m.SINGLE_QUOTE],[m.SPACE.name,m.SPACE],[m.UNDERSCORE.name,m.UNDERSCORE]]);static tokens=[...m.all.values()].map(e=>e.token);constructor(e,t){this.name=e,this.token=t}toString(){return`Separator.${this.name}`}}class c{prefix;firstName;middleName;lastName;suffix;static get min(){return 2}static get max(){return 5}constructor(e,t,s,a,i){this.prefix=e,this.firstName=t,this.middleName=s,this.lastName=a,this.suffix=i}static base(){return new c(-1,0,-1,1,-1)}static when(t,s=2){if(t===e.NameOrder.FIRST_NAME)switch(s){case 2:return new c(-1,0,-1,1,-1);case 3:return new c(-1,0,1,2,-1);case 4:return new c(0,1,2,3,-1);case 5:return new c(0,1,2,3,4);default:return c.base()}else switch(s){case 2:return new c(-1,1,-1,0,-1);case 3:return new c(-1,1,2,0,-1);case 4:return new c(0,2,3,1,-1);case 5:return new c(0,2,3,1,4);default:return c.base()}}static only({prefix:e=-1,firstName:t,middleName:s=-1,lastName:a,suffix:i=-1}){return new c(e,t,s,a,i)}toJson(){return{prefix:this.prefix,firstName:this.firstName,middleName:this.middleName,lastName:this.lastName,suffix:this.suffix}}json=this.toJson}function d(t,s=e.CapsRange.INITIAL){if(!t||s===e.CapsRange.NONE)return t;const[a,i]=[t[0].toUpperCase(),t.slice(1).toLowerCase()];return s===e.CapsRange.INITIAL?a.concat(i):t.toUpperCase()}function f(t,s=e.CapsRange.INITIAL){if(!t||s===e.CapsRange.NONE)return t;const[a,i]=[t[0].toLowerCase(),t.slice(1)];return s===e.CapsRange.INITIAL?a.concat(i):t.toLowerCase()}function N(e){return Array.isArray(e)&&e.length>0&&e.every(e=>"string"==typeof e)}e.NameErrorType=void 0,(h=e.NameErrorType||(e.NameErrorType={}))[h.INPUT=0]="INPUT",h[h.VALIDATION=1]="VALIDATION",h[h.NOT_ALLOWED=2]="NOT_ALLOWED",h[h.UNKNOWN=3]="UNKNOWN";class p extends Error{source;type;constructor(t,s,a=e.NameErrorType.UNKNOWN){super(s),this.source=t,this.type=a,this.name="NameError"}get sourceAsString(){return"string"==typeof this.source?this.source:N(this.source)?this.source.join(" "):"<undefined>"}get hasMessage(){return this.message.trim().length>0}toString(){let e=`${this.name} (${this.sourceAsString})`;return this.hasMessage&&(e=`${e}: ${this.message}`),e}}class g extends p{constructor(t){super(t.source,t.message,e.NameErrorType.INPUT),this.name="InputError"}}class E extends p{nameType;constructor(t){super(t.source,t.message,e.NameErrorType.VALIDATION),this.nameType=t.nameType,this.name="ValidationError"}toString(){let e=`${this.name} (${this.nameType}='${this.sourceAsString}')`;return this.hasMessage&&(e=`${e}: ${this.message}`),e}}class y extends p{operation;constructor(t){super(t.source,t.message,e.NameErrorType.NOT_ALLOWED),this.operation=t.operation,this.name="NotAllowedError"}toString(){let e=`${this.name} (${this.sourceAsString})`;return this.operation&&this.operation.trim().length>0&&(e=`${e} - ${this.operation}`),this.hasMessage&&(e=`${e}: ${this.message}`),e}}class w extends p{origin;constructor(t){super(t.source,t.message,e.NameErrorType.UNKNOWN),this.origin=t.origin,this.name="UnknownError"}toString(){let e=super.toString();return this.origin&&(e+=`\n${this.origin.toString()}`),e}}class A{type;#e;initial;capsRange;constructor(t,s,a){this.type=s,this.capsRange=a??e.CapsRange.INITIAL,this.value=t,a&&this.caps(a)}set value(e){this.validate(e),this.#e=e,this.initial=e[0]}get value(){return this.#e}get length(){return this.#e.length}get isPrefix(){return this.type===u.PREFIX}get isFirstName(){return this.type===u.FIRST_NAME}get isMiddleName(){return this.type===u.MIDDLE_NAME}get isLastName(){return this.type===u.LAST_NAME}get isSuffix(){return this.type===u.SUFFIX}static prefix(e){return new A(e,u.PREFIX)}static first(e){return new A(e,u.FIRST_NAME)}static middle(e){return new A(e,u.MIDDLE_NAME)}static last(e){return new A(e,u.LAST_NAME)}static suffix(e){return new A(e,u.SUFFIX)}initials(){return[this.initial]}toString(){return this.#e}equal(e){return e instanceof A&&e.value===this.value&&e.type===this.type}caps(e){return this.value=d(this.#e,e??this.capsRange),this}decaps(e){return this.value=f(this.#e,e??this.capsRange),this}validate(e){if("string"==typeof e&&e.trim().length<1)throw new g({source:e,message:"must be 1+ characters"})}}class M extends A{#t;constructor(e,...t){super(e,u.FIRST_NAME),t.forEach(this.validate),this.#t=t}get hasMore(){return this.#t.length>0}get length(){return super.length+(this.hasMore?this.#t.reduce((e,t)=>e+t).length:0)}get asNames(){const e=[A.first(this.value)];return this.hasMore&&e.push(...this.#t.map(A.first)),e}get more(){return this.#t}toString(e=!1){return e&&this.hasMore?`${this.value} ${this.#t.join(" ")}`.trim():this.value}initials(e=!1){const t=[this.initial];return e&&this.hasMore&&t.push(...this.#t.map(e=>e[0])),t}caps(e){return e=e||this.capsRange,this.value=d(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>d(t,e))),this}decaps(e){return e=e||this.capsRange,this.value=f(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>f(t,e))),this}copyWith(e){return new M(e?.first??this.value,...e?.more??this.#t)}}class v extends A{format;#s;constructor(t,s,a=e.Surname.FATHER){super(t,u.LAST_NAME),this.format=a,this.validate(s),this.#s=s}get father(){return this.value}get mother(){return this.#s}get hasMother(){return!!this.#s}get length(){return super.length+(this.#s?.length??0)}get asNames(){const e=[A.last(this.value)];return this.#s&&e.push(A.last(this.#s)),e}toString(t){switch(t=t??this.format){case e.Surname.FATHER:return this.value;case e.Surname.MOTHER:return this.mother??"";case e.Surname.HYPHENATED:return this.hasMother?`${this.value}-${this.#s}`:this.value;case e.Surname.ALL:return this.hasMother?`${this.value} ${this.#s}`:this.value}}initials(t){const s=[];switch(t??this.format){case e.Surname.HYPHENATED:case e.Surname.ALL:s.push(this.initial),this.#s&&s.push(this.#s[0]);break;case e.Surname.MOTHER:this.#s&&s.push(this.#s[0]);break;default:s.push(this.initial)}return s}caps(e){return e??=this.capsRange,this.value=d(this.value,e),this.hasMother&&(this.#s=d(this.#s,e)),this}decaps(e){return e??=this.capsRange,this.value=f(this.value,e),this.hasMother&&(this.#s=f(this.#s,e)),this}copyWith(e){return new v(e?.father??this.value,e?.mother??this.mother,e?.format??this.format)}}function S(e){return Array.isArray(e)&&e.length>0&&e.every(e=>e instanceof A)}const T="_copy";class I{#a;#i;#r;#n;#o;#h;#l;static cache=new Map;get orderedBy(){return this.#i}get separator(){return this.#r}get title(){return this.#n}get ending(){return this.#o}get bypass(){return this.#h}get surname(){return this.#l}get name(){return this.#a}constructor(t,s=e.NameOrder.FIRST_NAME,a=m.SPACE,i=e.Title.UK,r=!1,n=!0,o=e.Surname.FATHER){this.#a=t,this.#i=s,this.#r=a,this.#n=i,this.#o=r,this.#h=n,this.#l=o}static create(e="default"){return l.cache.has(e)||l.cache.set(e,new l(e)),l.cache.get(e)}static merge(e){if(e){const t=l.create(e.name);return t.#i=e.orderedBy??t.orderedBy,t.#r=e.separator??t.separator,t.#n=e.title??t.title,t.#o=e.ending??t.ending,t.#h=e.bypass??t.bypass,t.#l=e.surname??t.surname,t}return l.create()}copyWith(e={}){const{name:t,orderedBy:s,separator:a,title:i,ending:r,bypass:n,surname:o}=e,h=l.create(this.#u(t??this.name+T));return h.#i=s??this.orderedBy,h.#r=a??this.separator,h.#n=i??this.title,h.#o=r??this.ending,h.#h=n??this.bypass,h.#l=o??this.surname,h}clone(){return this.copyWith()}reset(){this.#i=e.NameOrder.FIRST_NAME,this.#r=m.SPACE,this.#n=e.Title.UK,this.#o=!1,this.#h=!0,this.#l=e.Surname.FATHER,l.cache.set(this.name,this)}updateOrder(e){this.update({orderedBy:e})}update({orderedBy:e,title:t,ending:s}){const a=l.cache.get(this.name);a&&(e!==this.#i&&(a.#i=e),t!==this.#n&&(a.#n=t),s!==this.#o&&(a.#o=s))}#u(e){return e===this.name||l.cache.has(e)?this.#u(e+T):e}}l=I;class x{static base=/[a-zA-Z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\u0400-\u04FFΆ-ωΑ-ώ]/;static namon=new RegExp(`^${x.base.source}+(([' -]${x.base.source})?${x.base.source}*)*$`);static firstName=x.namon;static middleName=new RegExp(`^${x.base.source}+(([' -]${x.base.source})?${x.base.source}*)*$`);static lastName=x.namon}const L=e=>S(e)?e.map(e=>e.toString()).join(" "):"";class F{validate(e){if(0===e.length||e.length<2||e.length>5)throw new g({source:e.map(e=>e.toString()),message:"expecting a list of 2-5 elements"})}}class _{static#m;static create(){return this.#m||(this.#m=new _)}validate(e,t){if(e instanceof A)D.create().validate(e,t);else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types of string or Name"});if(!x.namon.test(e))throw new E({source:e,nameType:"namon",message:"invalid name content failing namon regex"})}}}class b{static#m;static create(){return this.#m||(this.#m=new b)}validate(e){if(e instanceof M)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types string or FirstName"});if(!x.firstName.test(e))throw new E({source:e,nameType:"firstName",message:"invalid name content failing firstName regex"})}}}class R{static#m;static create(){return this.#m||(this.#m=new R)}validate(e){if("string"==typeof e){if(!x.middleName.test(e))throw new E({source:e,nameType:"middleName",message:"invalid name content failing middleName regex"})}else{if(!Array.isArray(e))throw new g({source:typeof e,message:"expecting types of string, string[] or Name[]"});try{const t=_.create();for(const s of e)t.validate(s,u.MIDDLE_NAME)}catch(t){throw new E({source:L(e),nameType:"middleName",message:t?.message})}}}}class O{static#m;static create(){return this.#m||(this.#m=new O)}validate(e){if(e instanceof v)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types string or LastName"});if(!x.lastName.test(e))throw new E({source:e,nameType:"lastName",message:"invalid name content failing lastName regex"})}}}class D{static#m;static create(){return this.#m||(this.#m=new D)}validate(e,t){if(t&&e.type!==t)throw new E({source:e.toString(),nameType:e.type.toString(),message:"wrong name type; only Namon types are supported"});if(!x.namon.test(e.value))throw new E({source:e.toString(),nameType:e.type.toString(),message:"invalid name content failing namon regex"})}}class C{static#m;static create(){return this.#m||(this.#m=new C)}validate(e){this.validateKeys(e),$.firstName.validate(e.get(u.FIRST_NAME)),$.lastName.validate(e.get(u.LAST_NAME)),e.has(u.PREFIX)&&$.namon.validate(e.get(u.PREFIX)),e.has(u.SUFFIX)&&$.namon.validate(e.get(u.SUFFIX))}validateKeys(e){if(!e.size)throw new g({source:void 0,message:"Map<k,v> must not be empty"});if(e.size<2||e.size>5)throw new g({source:[...e.values()],message:"expecting 2-5 fields"});if(!e.has(u.FIRST_NAME))throw new g({source:[...e.values()],message:'"firstName" is a required key'});if(!e.has(u.LAST_NAME))throw new g({source:[...e.values()],message:'"lastName" is a required key'})}}class U extends F{index;constructor(e=c.base()){super(),this.index=e}validate(e){this.validateIndex(e),$.firstName.validate(e[this.index.firstName]),$.lastName.validate(e[this.index.lastName]),e.length>=3&&$.middleName.validate(e[this.index.middleName]),e.length>=4&&$.namon.validate(e[this.index.prefix]),5===e.length&&$.namon.validate(e[this.index.suffix])}validateIndex(e){super.validate(e)}}class P{static#m;static create(){return this.#m||(this.#m=new P)}validate(e){if(e.length<2)throw new g({source:L(e),message:"expecting at least 2 elements"});if(!this.#c(e))throw new g({source:L(e),message:"both first and last names are required"})}#c(e){const t={};for(const s of e)(s.isFirstName||s.isLastName)&&(t[s.type.key]=s.toString());return 2===Object.keys(t).length}}class ${static namon=_.create();static nama=C.create();static prefix=_.create();static firstName=b.create();static middleName=R.create();static lastName=O.create();static suffix=_.create()}class j{#d;#f;#N=[];#p;#g;#E;constructor(e){this.#E=I.merge(e)}get config(){return this.#E}get prefix(){return this.#d}get firstName(){return this.#f}get lastName(){return this.#p}get middleName(){return this.#N}get suffix(){return this.#g}static parse(e,t){try{return new j(t).setPrefix(e.prefix).setFirstName(e.firstName).setMiddleName(e.middleName??[]).setLastName(e.lastName).setSuffix(e.suffix)}catch(t){if(t instanceof p)throw t;throw new w({source:Object.values(e).join(" "),message:"could not parse JSON content",origin:t instanceof Error?t:new Error(String(t))})}}setPrefix(t){if(!t)return this;this.#E.bypass||$.prefix.validate(t);const s=t instanceof A?t.value:t;return this.#d=A.prefix(this.#E.title===e.Title.US?`${s}.`:s),this}setFirstName(e){return this.#E.bypass||$.firstName.validate(e),this.#f=e instanceof M?e:new M(e),this}setLastName(e){return this.#E.bypass||$.lastName.validate(e),this.#p=e instanceof v?e:new v(e),this}setMiddleName(e){return Array.isArray(e)?(this.#E.bypass||$.middleName.validate(e),this.#N=e.map(e=>e instanceof A?e:A.middle(e)),this):this}setSuffix(e){return e?(this.#E.bypass||$.suffix.validate(e),this.#g=A.suffix(e instanceof A?e.value:e),this):this}has(e){return e.equal(u.PREFIX)?!!this.#d:e.equal(u.SUFFIX)?!!this.#g:!e.equal(u.MIDDLE_NAME)||this.#N.length>0}}class k{raw;constructor(e){this.raw=e}static build(e,t){const s=e.trim().split(m.SPACE.token),a=s.length;if(t instanceof c){const e=Object.entries(t.json()).filter(([,e])=>e>-1&&e<a).map(([e,t])=>new A(s[t],u.all.get(e)));return new X(e)}if(a<2)throw new g({source:e,message:"cannot build from invalid input"});if(2===a||3===a)return new B(e);{const e=s.pop(),[t,...a]=s;return new q([t,a.join(" "),e])}}static buildAsync(e,t){try{return Promise.resolve(k.build(e,t))}catch(e){return Promise.reject(e)}}}class B extends k{parse(e){const t=I.merge(e),s=this.raw.split(t.separator.token);return new q(s).parse(e)}}class q extends k{parse(e){const t=I.merge(e),s=new j(t),a=this.raw.map(e=>e.trim()),i=c.when(t.orderedBy,a.length),r=new U(i);t.bypass?r.validateIndex(a):r.validate(a);const{firstName:n,lastName:o,middleName:h,prefix:l,suffix:u}=i;return s.setFirstName(new M(a[n])),s.setLastName(new v(a[o])),a.length>=3&&s.setMiddleName(a[h].split(t.separator.token)),a.length>=4&&s.setPrefix(A.prefix(a[l])),5===a.length&&s.setSuffix(A.suffix(a[u])),s}}class H extends k{parse(e){const t=I.merge(e),s=new Map(Object.entries(this.raw).map(([e,t])=>{const s=u.cast(e);if(!s)throw new g({source:Object.values(this.raw).join(" "),message:`unsupported key "${e}"`});return[s,t]}));return t.bypass?C.create().validateKeys(s):C.create().validate(s),j.parse(this.raw,t)}}class X extends k{parse(e){const t=I.merge(e),s=new j(t);P.create().validate(this.raw);for(const e of this.raw)if(e.isPrefix)s.setPrefix(e);else if(e.isSuffix)s.setSuffix(e);else if(e.isFirstName)s.setFirstName(e instanceof M?e:new M(e.value));else if(e.isMiddleName)s.middleName.push(e);else if(e.isLastName){const a=new v(e.value,e instanceof v?e.mother:void 0,t.surname);s.setLastName(a)}return s}}class W{#y;constructor(e,t){this.#y=this.#w(e).parse(t)}static tryParse(e,t){try{return new W(k.build(e,t))}catch{return}}static async parse(e,t){return k.buildAsync(e,t).then(e=>new W(e))}get config(){return this.#y.config}get length(){return this.birth.length}get prefix(){return this.#y.prefix?.toString()}get first(){return this.firstName()}get middle(){return this.hasMiddle?this.middleName()[0]:void 0}get hasMiddle(){return this.#y.has(u.MIDDLE_NAME)}get last(){return this.lastName()}get suffix(){return this.#y.suffix?.toString()}get birth(){return this.birthName()}get short(){return this.shorten()}get long(){return this.birth}get full(){return this.fullName()}get public(){return this.format("f $l")}get salutation(){return this.format("p l")}toString(){return this.full}get(e){return e.equal(u.PREFIX)?this.#y.prefix:e.equal(u.FIRST_NAME)?this.#y.firstName:e.equal(u.MIDDLE_NAME)?this.#y.middleName:e.equal(u.LAST_NAME)?this.#y.lastName:e.equal(u.SUFFIX)?this.#y.suffix:void 0}equal(e){return this.toString()===e.toString()}toJson(){return{prefix:this.prefix,firstName:this.first,middleName:this.middleName(),lastName:this.last,suffix:this.suffix}}json=this.toJson;has(e){return this.#y.has(e)}fullName(t){const s=this.config.ending?",":"",a=[];return this.prefix&&a.push(this.prefix),(t??this.config.orderedBy)===e.NameOrder.FIRST_NAME?a.push(this.first,...this.middleName(),this.last+s):a.push(this.last,this.first,this.middleName().join(" ")+s),this.suffix&&a.push(this.suffix),a.join(" ").trim()}birthName(t){return t??=this.config.orderedBy,t===e.NameOrder.FIRST_NAME?[this.first,...this.middleName(),this.last].join(" "):[this.last,this.first,...this.middleName()].join(" ")}firstName(e=!0){return this.#y.firstName.toString(e)}middleName(){return this.#y.middleName.map(e=>e.value)}lastName(e){return this.#y.lastName.toString(e)}initials(t){const{orderedBy:s=this.config.orderedBy,only:a=e.NameType.BIRTH_NAME,asJson:i}=t??{},r=this.#y.firstName.initials(),n=this.#y.middleName.map(e=>e.value[0]),o=this.#y.lastName.initials();return i?{firstName:r,middleName:n,lastName:o}:a!==e.NameType.BIRTH_NAME?a===e.NameType.FIRST_NAME?r:a===e.NameType.MIDDLE_NAME?n:o:s===e.NameOrder.FIRST_NAME?[...r,...n,...o]:[...o,...r,...n]}shorten(t){t??=this.config.orderedBy;const{firstName:s,lastName:a}=this.#y;return t===e.NameOrder.FIRST_NAME?[s.value,a.toString()].join(" "):[a.toString(),s.value].join(" ")}flatten(t){const{by:s=e.Flat.MIDDLE_NAME,limit:a=20,recursive:i=!1,withMore:r=!1,withPeriod:n=!0,surname:o}=t;if(this.length<=a)return this.full;const{firstName:h,lastName:l,middleName:u}=this.#y,m=n?".":"",c=this.hasMiddle,d=h.toString(),f=this.middleName().join(" "),N=l.toString(),p=h.initials(r).join(m+" ")+m,g=l.initials(o).join(m+" ")+m,E=c?u.map(e=>e.value[0]).join(m+" ")+m:"";let y=[];if(this.config.orderedBy===e.NameOrder.FIRST_NAME)switch(s){case e.Flat.FIRST_NAME:y=c?[p,f,N]:[p,N];break;case e.Flat.LAST_NAME:y=c?[d,f,g]:[d,g];break;case e.Flat.MIDDLE_NAME:y=c?[d,E,N]:[d,N];break;case e.Flat.FIRST_MID:y=c?[p,E,N]:[p,N];break;case e.Flat.MID_LAST:y=c?[d,E,g]:[d,g];break;case e.Flat.ALL:y=c?[p,E,g]:[p,g]}else switch(s){case e.Flat.FIRST_NAME:y=c?[N,p,f]:[N,p];break;case e.Flat.LAST_NAME:y=c?[g,d,f]:[g,d];break;case e.Flat.MIDDLE_NAME:y=c?[N,d,E]:[N,d];break;case e.Flat.FIRST_MID:y=c?[N,p,E]:[N,p];break;case e.Flat.MID_LAST:y=c?[g,d,E]:[g,d];break;case e.Flat.ALL:y=c?[g,p,E]:[g,p]}const w=y.join(" ");if(i&&w.length>a){const a=s===e.Flat.FIRST_NAME?e.Flat.MIDDLE_NAME:s===e.Flat.MIDDLE_NAME?e.Flat.LAST_NAME:s===e.Flat.LAST_NAME?e.Flat.FIRST_MID:s===e.Flat.FIRST_MID?e.Flat.MID_LAST:s===e.Flat.MID_LAST||s===e.Flat.ALL?e.Flat.ALL:s;return a===s?w:this.flatten({...t,by:a})}return w}zip(t=e.Flat.MID_LAST,s=!0){return this.flatten({limit:0,by:t,withPeriod:s})}format(e){if("short"===e)return this.short;if("long"===e)return this.long;if("public"===e)return this.public;"official"===e&&(e="o");let s="";const a=[];for(const i of e){if(-1===t.indexOf(i))throw new y({source:this.full,operation:"format",message:`unsupported character <${i}> from ${e}.`});s+=i,"$"!==i&&(a.push(this.#A(s)??""),s="")}return a.join("").trim()}flip(){const t=this.config.orderedBy===e.NameOrder.FIRST_NAME?e.NameOrder.LAST_NAME:e.NameOrder.FIRST_NAME;this.config.update({orderedBy:t})}split(e=/[' -]/g){return this.birth.replace(e," ").split(" ")}join(e=""){return this.split().join(e)}toUpperCase(){return this.birth.toUpperCase()}toLowerCase(){return this.birth.toLowerCase()}toCamelCase(){return f(this.toPascalCase())}toPascalCase(){return this.split().map(e=>d(e)).join("")}toSnakeCase(){return this.split().map(e=>e.toLowerCase()).join("_")}toHyphenCase(){return this.split().map(e=>e.toLowerCase()).join("-")}toDotCase(){return this.split().map(e=>e.toLowerCase()).join(".")}toToggleCase(){return this.birth.split("").map(e=>e===e.toUpperCase()?e.toLowerCase():e.toUpperCase()).join("")}#w(e){if(e instanceof k)return e;if("string"==typeof e)return new B(e);if(N(e))return new q(e);if(S(e))return new X(e);if("object"==typeof e)return new H(e);throw new g({source:e,message:"Cannot parse raw data; review expected data types."})}#A(e){switch(e){case".":case",":case" ":case"-":case"_":return e;case"b":return this.birth;case"B":return this.birth.toUpperCase();case"f":return this.first;case"F":return this.first.toUpperCase();case"l":return this.last;case"L":return this.last.toUpperCase();case"m":case"M":return"m"===e?this.middleName().join(" "):this.middleName().join(" ").toUpperCase();case"o":case"O":return(e=>{const t=this.config.ending?",":"",s=[];this.prefix&&s.push(this.prefix),s.push(`${this.last},`.toUpperCase()),this.hasMiddle?s.push(this.first,this.middleName().join(" ")+t):s.push(this.first+t),this.suffix&&s.push(this.suffix);const a=s.join(" ").trim();return"o"===e?a:a.toUpperCase()})(e);case"p":return this.prefix;case"P":return this.prefix?.toUpperCase();case"s":return this.suffix;case"S":return this.suffix?.toUpperCase();case"$f":case"$F":return this.#y.firstName.value[0];case"$l":case"$L":return this.#y.lastName.value[0];case"$m":case"$M":return this.hasMiddle?this.middle[0]:void 0;default:return}}}class K{prebuild;postbuild;preclear;postclear;queue=[];instance=null;constructor(e,t,s,a){this.prebuild=e,this.postbuild=t,this.preclear=s,this.postclear=a}get size(){return this.queue.length}removeFirst(){return this.queue.length>0?this.queue.shift():void 0}removeLast(){return this.queue.length>0?this.queue.pop():void 0}addFirst(e){this.queue.unshift(e)}addLast(e){this.queue.push(e)}add(...e){this.queue.push(...e)}remove(e){const t=this.queue.indexOf(e);return-1!==t&&(this.queue.splice(t,1),!0)}removeWhere(e){this.queue=this.queue.filter(t=>!e(t))}retainWhere(e){this.queue=this.queue.filter(e)}clear(){null!==this.instance&&this.preclear?.(this.instance),this.queue=[],this.postclear?.(),this.instance=null}}class Y extends K{constructor(e,t,s,a,i){super(t,s,a,i),this.add(...e)}static create(e){return new Y(e?[e]:[])}static of(...e){return new Y(e)}static use({names:e,prebuild:t,postbuild:s,preclear:a,postclear:i}){return new Y(e??[],t,s,a,i)}build(e){this.prebuild?.();const t=[...this.queue];return P.create().validate(t),this.instance=new W(t,e),this.postbuild?.(this.instance),this.instance}}e.Config=I,e.FirstName=M,e.FullName=j,e.InputError=g,e.LastName=v,e.Name=A,e.NameBuilder=Y,e.NameError=p,e.NameIndex=c,e.Namefully=W,e.Namon=u,e.NotAllowedError=y,e.Parser=k,e.Separator=m,e.UnknownError=w,e.ValidationError=E,e.default=(e,t)=>new W(e,t),e.isNameArray=S,e.version="2.0.2",Object.defineProperty(e,"__esModule",{value:!0})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).namefully={})}(this,function(e){"use strict";const t=" .,_-()[]<>'\"bBfFlLmMnNoOpPsS$";var s,i,a,r,n,o,h,l;e.Title=void 0,(s=e.Title||(e.Title={})).US="US",s.UK="UK",e.Surname=void 0,(i=e.Surname||(e.Surname={})).FATHER="father",i.MOTHER="mother",i.HYPHENATED="hyphenated",i.ALL="all",e.NameOrder=void 0,(a=e.NameOrder||(e.NameOrder={})).FIRST_NAME="firstName",a.LAST_NAME="lastName",e.NameType=void 0,(r=e.NameType||(e.NameType={})).FIRST_NAME="firstName",r.MIDDLE_NAME="middleName",r.LAST_NAME="lastName",r.BIRTH_NAME="birthName",e.Flat=void 0,(n=e.Flat||(e.Flat={})).FIRST_NAME="firstName",n.MIDDLE_NAME="middleName",n.LAST_NAME="lastName",n.FIRST_MID="firstMid",n.MID_LAST="midLast",n.ALL="all",e.CapsRange=void 0,(o=e.CapsRange||(e.CapsRange={}))[o.NONE=0]="NONE",o[o.INITIAL=1]="INITIAL",o[o.ALL=2]="ALL";class u{index;key;static PREFIX=new u(0,"prefix");static FIRST_NAME=new u(1,"firstName");static MIDDLE_NAME=new u(2,"middleName");static LAST_NAME=new u(3,"lastName");static SUFFIX=new u(4,"suffix");static values=[u.PREFIX,u.FIRST_NAME,u.MIDDLE_NAME,u.LAST_NAME,u.SUFFIX];static all=new Map([[u.PREFIX.key,u.PREFIX],[u.FIRST_NAME.key,u.FIRST_NAME],[u.MIDDLE_NAME.key,u.MIDDLE_NAME],[u.LAST_NAME.key,u.LAST_NAME],[u.SUFFIX.key,u.SUFFIX]]);static aliases={[u.PREFIX.key]:["prefix","px","p"],[u.FIRST_NAME.key]:["firstname","first","fn","f"],[u.MIDDLE_NAME.key]:["middlename","middle","mid","mn","m"],[u.LAST_NAME.key]:["lastname","last","ln","l"],[u.SUFFIX.key]:["suffix","sx","s"]};constructor(e,t){this.index=e,this.key=t}static has(e){return u.all.has(e)}static cast(e){const t=String(e).toLowerCase(),s=Object.entries(u.aliases).find(([,e])=>e.includes(t))?.[0];return u.has(s??"")?u.all.get(e):void 0}toString(){return`Namon.${this.key}`}equal(e){return e instanceof u&&e.index===this.index&&e.key===this.key}}class m{name;token;static COMMA=new m("comma",",");static COLON=new m("colon",":");static DOUBLE_QUOTE=new m("doubleQuote",'"');static EMPTY=new m("empty","");static HYPHEN=new m("hyphen","-");static PERIOD=new m("period",".");static SEMI_COLON=new m("semiColon",";");static SINGLE_QUOTE=new m("singleQuote","'");static SPACE=new m("space"," ");static UNDERSCORE=new m("underscore","_");static all=new Map([[m.COMMA.name,m.COMMA],[m.COLON.name,m.COLON],[m.DOUBLE_QUOTE.name,m.DOUBLE_QUOTE],[m.EMPTY.name,m.EMPTY],[m.HYPHEN.name,m.HYPHEN],[m.PERIOD.name,m.PERIOD],[m.SEMI_COLON.name,m.SEMI_COLON],[m.SINGLE_QUOTE.name,m.SINGLE_QUOTE],[m.SPACE.name,m.SPACE],[m.UNDERSCORE.name,m.UNDERSCORE]]);static tokens=[...m.all.values()].map(e=>e.token);constructor(e,t){this.name=e,this.token=t}static cast(e){for(const[t,s]of m.all)if(s.token===e||t.toLowerCase()===e.toLowerCase())return s}toString(){return`Separator.${this.name}`}}class c{prefix;firstName;middleName;lastName;suffix;static get min(){return 2}static get max(){return 5}constructor(e,t,s,i,a){this.prefix=e,this.firstName=t,this.middleName=s,this.lastName=i,this.suffix=a}static base(){return new c(-1,0,-1,1,-1)}static when(t,s=2){if(t===e.NameOrder.FIRST_NAME)switch(s){case 2:return new c(-1,0,-1,1,-1);case 3:return new c(-1,0,1,2,-1);case 4:return new c(0,1,2,3,-1);case 5:return new c(0,1,2,3,4);default:return c.base()}else switch(s){case 2:return new c(-1,1,-1,0,-1);case 3:return new c(-1,1,2,0,-1);case 4:return new c(0,2,3,1,-1);case 5:return new c(0,2,3,1,4);default:return c.base()}}static only({prefix:e=-1,firstName:t,middleName:s=-1,lastName:i,suffix:a=-1}){return new c(e,t,s,i,a)}toJson(){return{prefix:this.prefix,firstName:this.firstName,middleName:this.middleName,lastName:this.lastName,suffix:this.suffix}}json=this.toJson}function d(t,s=e.CapsRange.INITIAL){if(!t||s===e.CapsRange.NONE)return t;const[i,a]=[t[0].toUpperCase(),t.slice(1).toLowerCase()];return s===e.CapsRange.INITIAL?i.concat(a):t.toUpperCase()}function f(t,s=e.CapsRange.INITIAL){if(!t||s===e.CapsRange.NONE)return t;const[i,a]=[t[0].toLowerCase(),t.slice(1)];return s===e.CapsRange.INITIAL?i.concat(a):t.toLowerCase()}function N(e){return Array.isArray(e)&&e.length>0&&e.every(e=>"string"==typeof e)}e.NameErrorType=void 0,(h=e.NameErrorType||(e.NameErrorType={}))[h.INPUT=0]="INPUT",h[h.VALIDATION=1]="VALIDATION",h[h.NOT_ALLOWED=2]="NOT_ALLOWED",h[h.UNKNOWN=3]="UNKNOWN";class p extends Error{source;type;constructor(t,s,i=e.NameErrorType.UNKNOWN){super(s),this.source=t,this.type=i,this.name="NameError"}get sourceAsString(){return"string"==typeof this.source?this.source:N(this.source)?this.source.join(" "):"<undefined>"}get hasMessage(){return this.message.trim().length>0}toString(){let e=`${this.name} (${this.sourceAsString})`;return this.hasMessage&&(e=`${e}: ${this.message}`),e}}class g extends p{constructor(t){super(t.source,t.message,e.NameErrorType.INPUT),this.name="InputError"}}class y extends p{nameType;constructor(t){super(t.source,t.message,e.NameErrorType.VALIDATION),this.nameType=t.nameType,this.name="ValidationError"}toString(){let e=`${this.name} (${this.nameType}='${this.sourceAsString}')`;return this.hasMessage&&(e=`${e}: ${this.message}`),e}}class E extends p{operation;constructor(t){super(t.source,t.message,e.NameErrorType.NOT_ALLOWED),this.operation=t.operation,this.name="NotAllowedError"}toString(){let e=`${this.name} (${this.sourceAsString})`;return this.operation&&this.operation.trim().length>0&&(e=`${e} - ${this.operation}`),this.hasMessage&&(e=`${e}: ${this.message}`),e}}class w extends p{origin;constructor(t){super(t.source,t.message,e.NameErrorType.UNKNOWN),this.origin=t.origin,this.name="UnknownError"}toString(){let e=super.toString();return this.origin&&(e+=`\n${this.origin.toString()}`),e}}class v{type;#e;initial;capsRange;constructor(t,s,i){this.type=s,this.capsRange=i??e.CapsRange.INITIAL,this.value=t,i&&this.caps(i)}set value(e){this.validate(e),this.#e=e,this.initial=e[0]}get value(){return this.#e}get length(){return this.#e.length}get isPrefix(){return this.type===u.PREFIX}get isFirstName(){return this.type===u.FIRST_NAME}get isMiddleName(){return this.type===u.MIDDLE_NAME}get isLastName(){return this.type===u.LAST_NAME}get isSuffix(){return this.type===u.SUFFIX}static prefix(e){return new v(e,u.PREFIX)}static first(e){return new v(e,u.FIRST_NAME)}static middle(e){return new v(e,u.MIDDLE_NAME)}static last(e){return new v(e,u.LAST_NAME)}static suffix(e){return new v(e,u.SUFFIX)}initials(){return[this.initial]}toString(){return this.#e}equal(e){return e instanceof v&&e.value===this.value&&e.type===this.type}caps(e){return this.value=d(this.#e,e??this.capsRange),this}decaps(e){return this.value=f(this.#e,e??this.capsRange),this}validate(e){if("string"==typeof e&&e.trim().length<1)throw new g({source:e,message:"must be 1+ characters"})}}class M extends v{#t;constructor(e,...t){super(e,u.FIRST_NAME),t.forEach(this.validate),this.#t=t}get hasMore(){return this.#t.length>0}get length(){return super.length+(this.hasMore?this.#t.reduce((e,t)=>e+t).length:0)}get asNames(){const e=[v.first(this.value)];return this.hasMore&&e.push(...this.#t.map(v.first)),e}get more(){return this.#t}toString(e=!1){return e&&this.hasMore?`${this.value} ${this.#t.join(" ")}`.trim():this.value}initials(e=!1){const t=[this.initial];return e&&this.hasMore&&t.push(...this.#t.map(e=>e[0])),t}caps(e){return e=e||this.capsRange,this.value=d(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>d(t,e))),this}decaps(e){return e=e||this.capsRange,this.value=f(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>f(t,e))),this}copyWith(e){return new M(e?.first??this.value,...e?.more??this.#t)}}class A extends v{format;#s;constructor(t,s,i=e.Surname.FATHER){super(t,u.LAST_NAME),this.format=i,this.validate(s),this.#s=s}get father(){return this.value}get mother(){return this.#s}get hasMother(){return!!this.#s}get length(){return super.length+(this.#s?.length??0)}get asNames(){const e=[v.last(this.value)];return this.#s&&e.push(v.last(this.#s)),e}toString(t){switch(t=t??this.format){case e.Surname.FATHER:return this.value;case e.Surname.MOTHER:return this.mother??"";case e.Surname.HYPHENATED:return this.hasMother?`${this.value}-${this.#s}`:this.value;default:return this.hasMother?`${this.value} ${this.#s}`:this.value}}initials(t){const s=[];switch(t??this.format){case e.Surname.HYPHENATED:case e.Surname.ALL:s.push(this.initial),this.#s&&s.push(this.#s[0]);break;case e.Surname.MOTHER:this.#s&&s.push(this.#s[0]);break;default:s.push(this.initial)}return s}caps(e){return e??=this.capsRange,this.value=d(this.value,e),this.hasMother&&(this.#s=d(this.#s,e)),this}decaps(e){return e??=this.capsRange,this.value=f(this.value,e),this.hasMother&&(this.#s=f(this.#s,e)),this}copyWith(e){return new A(e?.father??this.value,e?.mother??this.mother,e?.format??this.format)}}function S(e){return Array.isArray(e)&&e.length>0&&e.every(e=>e instanceof v)}const I="_copy";class x{#i;#a;#r;#n;#o;#h;#l;static cache=new Map;get orderedBy(){return this.#a}get separator(){return this.#r}get title(){return this.#n}get ending(){return this.#o}get bypass(){return this.#h}get surname(){return this.#l}get name(){return this.#i}constructor(t,s=e.NameOrder.FIRST_NAME,i=m.SPACE,a=e.Title.UK,r=!1,n=!0,o=e.Surname.FATHER){this.#i=t,this.#a=s,this.#r=i,this.#n=a,this.#o=r,this.#h=n,this.#l=o}static create(e="default"){return l.cache.has(e)||l.cache.set(e,new l(e)),l.cache.get(e)}static merge(e){if(e){const t=l.create(e.name);return t.#a=e.orderedBy??t.orderedBy,t.#r=e.separator??t.separator,t.#n=e.title??t.title,t.#o=e.ending??t.ending,t.#h=e.bypass??t.bypass,t.#l=e.surname??t.surname,t}return l.create()}copyWith(e={}){const{name:t,orderedBy:s,separator:i,title:a,ending:r,bypass:n,surname:o}=e,h=l.create(this.#u(t??this.name+I));return h.#a=s??this.orderedBy,h.#r=i??this.separator,h.#n=a??this.title,h.#o=r??this.ending,h.#h=n??this.bypass,h.#l=o??this.surname,h}clone(){return this.copyWith()}reset(){this.#a=e.NameOrder.FIRST_NAME,this.#r=m.SPACE,this.#n=e.Title.UK,this.#o=!1,this.#h=!0,this.#l=e.Surname.FATHER,l.cache.set(this.name,this)}updateOrder(e){this.update({orderedBy:e})}update({orderedBy:e,title:t,ending:s}){const i=l.cache.get(this.name);i&&(e!==this.#a&&(i.#a=e),t!==this.#n&&(i.#n=t),s!==this.#o&&(i.#o=s))}#u(e){return e===this.name||l.cache.has(e)?this.#u(e+I):e}}l=x;class T{static base=/[a-zA-Z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\u0400-\u04FFΆ-ωΑ-ώ]/;static namon=new RegExp(`^${T.base.source}+(([' -]${T.base.source})?${T.base.source}*)*$`);static firstName=T.namon;static middleName=new RegExp(`^${T.base.source}+(([' -]${T.base.source})?${T.base.source}*)*$`);static lastName=T.namon}const F=e=>S(e)?e.map(e=>e.toString()).join(" "):"";class L{validate(e){if(0===e.length||e.length<2||e.length>5)throw new g({source:e.map(e=>e.toString()),message:"expecting a list of 2-5 elements"})}}class b{static#m;static create(){return this.#m||(this.#m=new b)}validate(e,t){if(e instanceof v)D.create().validate(e,t);else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types of string or Name"});if(!T.namon.test(e))throw new y({source:e,nameType:"namon",message:"invalid name content failing namon regex"})}}}class _{static#m;static create(){return this.#m||(this.#m=new _)}validate(e){if(e instanceof M)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types string or FirstName"});if(!T.firstName.test(e))throw new y({source:e,nameType:"firstName",message:"invalid name content failing firstName regex"})}}}class R{static#m;static create(){return this.#m||(this.#m=new R)}validate(e){if("string"==typeof e){if(!T.middleName.test(e))throw new y({source:e,nameType:"middleName",message:"invalid name content failing middleName regex"})}else{if(!Array.isArray(e))throw new g({source:typeof e,message:"expecting types of string, string[] or Name[]"});try{const t=b.create();for(const s of e)t.validate(s,u.MIDDLE_NAME)}catch(t){throw new y({source:F(e),nameType:"middleName",message:t?.message})}}}}class O{static#m;static create(){return this.#m||(this.#m=new O)}validate(e){if(e instanceof A)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types string or LastName"});if(!T.lastName.test(e))throw new y({source:e,nameType:"lastName",message:"invalid name content failing lastName regex"})}}}class D{static#m;static create(){return this.#m||(this.#m=new D)}validate(e,t){if(t&&e.type!==t)throw new y({source:e.toString(),nameType:e.type.toString(),message:"wrong name type; only Namon types are supported"});if(!T.namon.test(e.value))throw new y({source:e.toString(),nameType:e.type.toString(),message:"invalid name content failing namon regex"})}}class C{static#m;static create(){return this.#m||(this.#m=new C)}validate(e){this.validateKeys(e),j.firstName.validate(e.get(u.FIRST_NAME)),j.lastName.validate(e.get(u.LAST_NAME)),e.has(u.PREFIX)&&j.namon.validate(e.get(u.PREFIX)),e.has(u.SUFFIX)&&j.namon.validate(e.get(u.SUFFIX))}validateKeys(e){if(!e.size)throw new g({source:void 0,message:"Map<k,v> must not be empty"});if(e.size<2||e.size>5)throw new g({source:[...e.values()],message:"expecting 2-5 fields"});if(!e.has(u.FIRST_NAME))throw new g({source:[...e.values()],message:'"firstName" is a required key'});if(!e.has(u.LAST_NAME))throw new g({source:[...e.values()],message:'"lastName" is a required key'})}}class U extends L{index;constructor(e=c.base()){super(),this.index=e}validate(e){this.validateIndex(e),j.firstName.validate(e[this.index.firstName]),j.lastName.validate(e[this.index.lastName]),e.length>=3&&j.middleName.validate(e[this.index.middleName]),e.length>=4&&j.namon.validate(e[this.index.prefix]),5===e.length&&j.namon.validate(e[this.index.suffix])}validateIndex(e){super.validate(e)}}class P{static#m;static create(){return this.#m||(this.#m=new P)}validate(e){if(e.length<2)throw new g({source:F(e),message:"expecting at least 2 elements"});if(!this.#c(e))throw new g({source:F(e),message:"both first and last names are required"})}#c(e){const t={};for(const s of e)(s.isFirstName||s.isLastName)&&(t[s.type.key]=s.toString());return 2===Object.keys(t).length}}class j{static namon=b.create();static nama=C.create();static prefix=b.create();static firstName=_.create();static middleName=R.create();static lastName=O.create();static suffix=b.create()}class ${#d;#f;#N=[];#p;#g;#y;constructor(e){this.#y=x.merge(e)}get config(){return this.#y}get prefix(){return this.#d}get firstName(){return this.#f}get lastName(){return this.#p}get middleName(){return this.#N}get suffix(){return this.#g}static parse(e,t){try{return new $(t).setPrefix(e.prefix).setFirstName(e.firstName).setMiddleName(e.middleName??[]).setLastName(e.lastName).setSuffix(e.suffix)}catch(t){if(t instanceof p)throw t;throw new w({source:Object.values(e).join(" "),message:"could not parse JSON content",origin:t instanceof Error?t:new Error(String(t))})}}setPrefix(t){if(!t)return this;this.#y.bypass||j.prefix.validate(t);const s=t instanceof v?t.value:t;return this.#d=v.prefix(this.#y.title===e.Title.US?`${s}.`:s),this}setFirstName(e){return this.#y.bypass||j.firstName.validate(e),this.#f=e instanceof M?e:new M(e),this}setLastName(e){return this.#y.bypass||j.lastName.validate(e),this.#p=e instanceof A?e:new A(e),this}setMiddleName(e){return Array.isArray(e)?(this.#y.bypass||j.middleName.validate(e),this.#N=e.map(e=>e instanceof v?e:v.middle(e)),this):this}setSuffix(e){return e?(this.#y.bypass||j.suffix.validate(e),this.#g=v.suffix(e instanceof v?e.value:e),this):this}has(e){const t="string"==typeof e?u.cast(e):e;return!!t&&(t.equal(u.PREFIX)?!!this.#d:t.equal(u.SUFFIX)?!!this.#g:!t.equal(u.MIDDLE_NAME)||this.#N.length>0)}*toIterable(e=!1){this.#d&&(yield this.#d),e?(yield*this.#f.asNames,yield*this.#N,yield*this.#p.asNames):(yield this.#f,yield*this.#N,yield this.#p),this.#g&&(yield this.#g)}*[Symbol.iterator](){yield*this.toIterable(!0)}}class k{raw;constructor(e){this.raw=e}static build(e,t){const s=e.trim().split(m.SPACE.token),i=s.length;if(t instanceof c){const e=Object.entries(t.json()).filter(([,e])=>e>-1&&e<i).map(([e,t])=>new v(s[t],u.all.get(e)));return new X(e)}if(i<2)throw new g({source:e,message:"cannot build from invalid input"});if(2===i||3===i)return new B(e);{const e=s.pop(),[t,...i]=s;return new q([t,i.join(" "),e])}}static buildAsync(e,t){try{return Promise.resolve(k.build(e,t))}catch(e){return Promise.reject(e)}}}class B extends k{parse(e){const t=x.merge(e),s=this.raw.split(t.separator.token);return new q(s).parse(e)}}class q extends k{parse(e){const t=x.merge(e),s=new $(t),i=this.raw.map(e=>e.trim()),a=c.when(t.orderedBy,i.length),r=new U(a);t.bypass?r.validateIndex(i):r.validate(i);const{firstName:n,lastName:o,middleName:h,prefix:l,suffix:u}=a;return s.setFirstName(new M(i[n])),s.setLastName(new A(i[o])),i.length>=3&&s.setMiddleName(i[h].split(t.separator.token)),i.length>=4&&s.setPrefix(v.prefix(i[l])),5===i.length&&s.setSuffix(v.suffix(i[u])),s}}class H extends k{parse(e){const t=x.merge(e),s=new Map(Object.entries(this.raw).map(([e,t])=>{const s=u.cast(e);if(!s)throw new g({source:Object.values(this.raw).join(" "),message:`unsupported key "${e}"`});return[s,t]}));return t.bypass?C.create().validateKeys(s):C.create().validate(s),$.parse(this.raw,t)}}class X extends k{parse(e){const t=x.merge(e),s=new $(t);P.create().validate(this.raw);for(const e of this.raw)if(e.isPrefix)s.setPrefix(e);else if(e.isSuffix)s.setSuffix(e);else if(e.isFirstName)s.setFirstName(e instanceof M?e:new M(e.value));else if(e.isMiddleName)s.middleName.push(e);else if(e.isLastName){const i=e instanceof A?e.mother:void 0;s.setLastName(new A(e.value,i,t.surname))}return s}}class W{#E;constructor(e,t){this.#E=this.#w(e).parse(t)}static tryParse(e,t){try{return new W(k.build(e,t))}catch{return}}static async parse(e,t){return k.buildAsync(e,t).then(e=>new W(e))}get config(){return this.#E.config}get length(){return this.birth.length}get prefix(){return this.#E.prefix?.toString()}get first(){return this.firstName()}get middle(){return this.hasMiddle?this.middleName()[0]:void 0}get hasMiddle(){return this.#E.has(u.MIDDLE_NAME)}get last(){return this.lastName()}get suffix(){return this.#E.suffix?.toString()}get birth(){return this.birthName()}get short(){return this.shorten()}get long(){return this.birth}get full(){return this.fullName()}get public(){return this.format("f $l")}get salutation(){return this.format("p l")}get parts(){return this.#E.toIterable()}get size(){return Array.from(this.parts).length}*[Symbol.iterator](){yield*this.#E.toIterable(!0)}toString(){return this.full}get(e){const t="string"==typeof e?u.cast(e):e;return t?.equal(u.PREFIX)?this.#E.prefix:t?.equal(u.FIRST_NAME)?this.#E.firstName:t?.equal(u.MIDDLE_NAME)?this.#E.middleName:t?.equal(u.LAST_NAME)?this.#E.lastName:t?.equal(u.SUFFIX)?this.#E.suffix:void 0}equal(e){return this.toString()===e.toString()}deepEqual(e){const t=Array.from(e.parts);for(const e of this.parts)if(!t.some(t=>t.equal(e)))return!1;return!0}toJson(){return{prefix:this.prefix,firstName:this.first,middleName:this.middleName(),lastName:this.last,suffix:this.suffix}}json=this.toJson;has(e){return this.#E.has(e)}fullName(t){const s=this.config.ending?",":"",i=[];return this.prefix&&i.push(this.prefix),(t??this.config.orderedBy)===e.NameOrder.FIRST_NAME?i.push(this.first,...this.middleName(),this.last+s):i.push(this.last,this.first,this.middleName().join(" ")+s),this.suffix&&i.push(this.suffix),i.join(" ").trim()}birthName(t){return t??=this.config.orderedBy,t===e.NameOrder.FIRST_NAME?[this.first,...this.middleName(),this.last].join(" "):[this.last,this.first,...this.middleName()].join(" ")}firstName(e=!0){return this.#E.firstName.toString(e)}middleName(){return this.#E.middleName.map(e=>e.value)}lastName(e){return this.#E.lastName.toString(e)}initials(t){const{orderedBy:s=this.config.orderedBy,only:i=e.NameType.BIRTH_NAME,asJson:a}=t??{},r=this.#E.firstName.initials(),n=this.#E.middleName.map(e=>e.value[0]),o=this.#E.lastName.initials();return a?{firstName:r,middleName:n,lastName:o}:i!==e.NameType.BIRTH_NAME?i===e.NameType.FIRST_NAME?r:i===e.NameType.MIDDLE_NAME?n:o:s===e.NameOrder.FIRST_NAME?[...r,...n,...o]:[...o,...r,...n]}shorten(t){t??=this.config.orderedBy;const{firstName:s,lastName:i}=this.#E;return t===e.NameOrder.FIRST_NAME?[s.value,i.toString()].join(" "):[i.toString(),s.value].join(" ")}flatten(t){const{by:s=e.Flat.MIDDLE_NAME,limit:i=20,recursive:a=!1,withMore:r=!1,withPeriod:n=!0,surname:o}=t;if(this.length<=i)return this.full;const{firstName:h,lastName:l,middleName:u}=this.#E,m=n?".":"",c=this.hasMiddle,d=h.toString(),f=this.middleName().join(" "),N=l.toString(),p=h.initials(r).join(m+" ")+m,g=l.initials(o).join(m+" ")+m,y=c?u.map(e=>e.value[0]).join(m+" ")+m:"";let E=[];if(this.config.orderedBy===e.NameOrder.FIRST_NAME)switch(s){case e.Flat.FIRST_NAME:E=c?[p,f,N]:[p,N];break;case e.Flat.LAST_NAME:E=c?[d,f,g]:[d,g];break;case e.Flat.MIDDLE_NAME:E=c?[d,y,N]:[d,N];break;case e.Flat.FIRST_MID:E=c?[p,y,N]:[p,N];break;case e.Flat.MID_LAST:E=c?[d,y,g]:[d,g];break;default:E=c?[p,y,g]:[p,g]}else switch(s){case e.Flat.FIRST_NAME:E=c?[N,p,f]:[N,p];break;case e.Flat.LAST_NAME:E=c?[g,d,f]:[g,d];break;case e.Flat.MIDDLE_NAME:E=c?[N,d,y]:[N,d];break;case e.Flat.FIRST_MID:E=c?[N,p,y]:[N,p];break;case e.Flat.MID_LAST:E=c?[g,d,y]:[g,d];break;default:E=c?[g,p,y]:[g,p]}const w=E.join(" ");if(a&&w.length>i){const i=s===e.Flat.FIRST_NAME?e.Flat.MIDDLE_NAME:s===e.Flat.MIDDLE_NAME?e.Flat.LAST_NAME:s===e.Flat.LAST_NAME?e.Flat.FIRST_MID:s===e.Flat.FIRST_MID?e.Flat.MID_LAST:s===e.Flat.MID_LAST||s===e.Flat.ALL?e.Flat.ALL:s;return i===s?w:this.flatten({...t,by:i})}return w}zip(t=e.Flat.MID_LAST,s=!0){return this.flatten({limit:0,by:t,withPeriod:s})}format(e){if("short"===e)return this.short;if("long"===e)return this.long;if("public"===e)return this.public;"official"===e&&(e="o");let s="";const i=[];for(const a of e){if(!t.includes(a))throw new E({source:this.full,operation:"format",message:`unsupported character <${a}> from ${e}.`});s+=a,"$"!==a&&(i.push(this.#v(s)??""),s="")}return i.join("").trim()}flip(){const t=this.config.orderedBy===e.NameOrder.FIRST_NAME?e.NameOrder.LAST_NAME:e.NameOrder.FIRST_NAME;this.config.update({orderedBy:t})}split(e=/[' -]/g){return this.birth.replace(e," ").split(" ")}join(e=""){return this.split().join(e)}toUpperCase(){return this.birth.toUpperCase()}toLowerCase(){return this.birth.toLowerCase()}toCamelCase(){return f(this.toPascalCase())}toPascalCase(){return this.split().map(e=>d(e)).join("")}toSnakeCase(){return this.split().map(e=>e.toLowerCase()).join("_")}toHyphenCase(){return this.split().map(e=>e.toLowerCase()).join("-")}toDotCase(){return this.split().map(e=>e.toLowerCase()).join(".")}toToggleCase(){return this.birth.split("").map(e=>e===e.toUpperCase()?e.toLowerCase():e.toUpperCase()).join("")}#w(e){if(e instanceof k)return e;if("string"==typeof e)return new B(e);if(N(e))return new q(e);if(S(e))return new X(e);if("object"==typeof e)return new H(e);throw new g({source:e,message:"Cannot parse raw data; review expected data types."})}#v(e){switch(e){case"b":return this.birth;case"B":return this.birth.toUpperCase();case"f":return this.first;case"F":return this.first.toUpperCase();case"l":return this.last;case"L":return this.last.toUpperCase();case"m":case"M":return"m"===e?this.middleName().join(" "):this.middleName().join(" ").toUpperCase();case"o":case"O":return(e=>{const t=this.config.ending?",":"",s=[];this.prefix&&s.push(this.prefix),s.push(`${this.last},`.toUpperCase()),this.hasMiddle?s.push(this.first,this.middleName().join(" ")+t):s.push(this.first+t),this.suffix&&s.push(this.suffix);const i=s.join(" ").trim();return"o"===e?i:i.toUpperCase()})(e);case"p":return this.prefix;case"P":return this.prefix?.toUpperCase();case"s":return this.suffix;case"S":return this.suffix?.toUpperCase();case"$f":return this.#E.firstName.value[0];case"$F":return this.#E.firstName.initials(!0).join("");case"$l":return this.#E.lastName.value[0];case"$L":return this.#E.lastName.initials().join("");case"$m":return this.hasMiddle?this.middle[0]:void 0;case"$M":return this.hasMiddle?this.#E.middleName.map(e=>e.value[0]).join(""):void 0;default:return t.includes(e)?e:void 0}}serialize(){const{config:e,firstName:t,lastName:s}=this.#E;return{names:{prefix:this.prefix,firstName:t.hasMore?{value:t.value,more:t.more}:t.value,middleName:this.hasMiddle?this.middleName():void 0,lastName:s.hasMother?{father:s.father,mother:s.mother}:s.value,suffix:this.suffix},config:{name:e.name,orderedBy:e.orderedBy,separator:e.separator.token,title:e.title,ending:e.ending,bypass:e.bypass,surname:e.surname}}}}class z{prebuild;postbuild;preclear;postclear;queue=[];instance=null;constructor(e,t,s,i){this.prebuild=e,this.postbuild=t,this.preclear=s,this.postclear=i}get size(){return this.queue.length}removeFirst(){return this.queue.length>0?this.queue.shift():void 0}removeLast(){return this.queue.length>0?this.queue.pop():void 0}addFirst(e){this.queue.unshift(e)}addLast(e){this.queue.push(e)}add(...e){this.queue.push(...e)}remove(e){const t=this.queue.indexOf(e);return-1!==t&&(this.queue.splice(t,1),!0)}removeWhere(e){this.queue=this.queue.filter(t=>!e(t))}retainWhere(e){this.queue=this.queue.filter(e)}clear(){null!==this.instance&&this.preclear?.(this.instance),this.queue=[],this.postclear?.(),this.instance=null}}class K extends z{constructor(e,t,s,i,a){super(t,s,i,a),this.add(...e)}static create(e){return new K(e?[e]:[])}static of(...e){return new K(e)}static use({names:e,prebuild:t,postbuild:s,preclear:i,postclear:a}){return new K(e??[],t,s,i,a)}build(e){this.prebuild?.();const t=[...this.queue];return P.create().validate(t),this.instance=new W(t,e),this.postbuild?.(this.instance),this.instance}}e.Config=x,e.FirstName=M,e.FullName=$,e.InputError=g,e.LastName=A,e.Name=v,e.NameBuilder=K,e.NameError=p,e.NameIndex=c,e.Namefully=W,e.Namon=u,e.NotAllowedError=E,e.Parser=k,e.Separator=m,e.UnknownError=w,e.ValidationError=y,e.default=(e,t)=>new W(e,t),e.deserialize=function(e){try{const t="string"==typeof e?JSON.parse(e):e;if(!t||"object"!=typeof t)throw new g({source:String(e),message:"invalid serialized data; must be an object or a string"});const{names:s,config:i}=t,{firstName:a,lastName:r,middleName:n,prefix:o,suffix:h}=s,l=K.of();return o&&l.add(v.prefix(o)),h&&l.add(v.suffix(h)),n&&l.add(...n.map(e=>v.middle(e))),l.add("string"==typeof a?v.first(a):new M(a.value,...a.more??[])),l.add("string"==typeof r?v.last(r):new A(r.father,r.mother)),l.build(i)}catch(t){if(t instanceof p)throw t;throw new w({source:String(e),message:"could not deserialize data",origin:t instanceof Error?t:new Error(String(t))})}},e.isNameArray=S,e.version="2.1.0",Object.defineProperty(e,"__esModule",{value:!0})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "namefully",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "Handle personal names in a particular order, way, or shape.",
5
5
  "author": "Ralph Florent",
6
6
  "license": "MIT",