namefully 2.0.2 → 2.2.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.
@@ -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$",s=String.fromCharCode(8203);var i,a,r,n,o,h,l,u;e.Title=void 0,(i=e.Title||(e.Title={})).US="US",i.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,(r=e.NameOrder||(e.NameOrder={})).FIRST_NAME="firstName",r.LAST_NAME="lastName",e.NameType=void 0,(n=e.NameType||(e.NameType={})).FIRST_NAME="firstName",n.MIDDLE_NAME="middleName",n.LAST_NAME="lastName",n.BIRTH_NAME="birthName",e.Flat=void 0,(o=e.Flat||(e.Flat={})).FIRST_NAME="firstName",o.MIDDLE_NAME="middleName",o.LAST_NAME="lastName",o.FIRST_MID="firstMid",o.MID_LAST="midLast",o.ALL="all",e.CapsRange=void 0,(h=e.CapsRange||(e.CapsRange={}))[h.NONE=0]="NONE",h[h.INITIAL=1]="INITIAL",h[h.ALL=2]="ALL";class m{index;key;static PREFIX=new m(0,"prefix");static FIRST_NAME=new m(1,"firstName");static MIDDLE_NAME=new m(2,"middleName");static LAST_NAME=new m(3,"lastName");static SUFFIX=new m(4,"suffix");static values=[m.PREFIX,m.FIRST_NAME,m.MIDDLE_NAME,m.LAST_NAME,m.SUFFIX];static all=new Map([[m.PREFIX.key,m.PREFIX],[m.FIRST_NAME.key,m.FIRST_NAME],[m.MIDDLE_NAME.key,m.MIDDLE_NAME],[m.LAST_NAME.key,m.LAST_NAME],[m.SUFFIX.key,m.SUFFIX]]);static aliases={[m.PREFIX.key]:["prefix","px","p"],[m.FIRST_NAME.key]:["firstname","first","fn","f"],[m.MIDDLE_NAME.key]:["middlename","middle","mid","mn","m"],[m.LAST_NAME.key]:["lastname","last","ln","l"],[m.SUFFIX.key]:["suffix","sx","s"]};constructor(e,t){this.index=e,this.key=t}static has(e){return m.all.has(e)}static cast(e){const t=String(e).toLowerCase(),s=Object.entries(m.aliases).find(([,e])=>e.includes(t))?.[0];return m.has(s??"")?m.all.get(e):void 0}toString(){return`Namon.${this.key}`}equal(e){return e instanceof m&&e.index===this.index&&e.key===this.key}}class c{name;token;static COMMA=new c("comma",",");static COLON=new c("colon",":");static DOUBLE_QUOTE=new c("doubleQuote",'"');static EMPTY=new c("empty","");static HYPHEN=new c("hyphen","-");static PERIOD=new c("period",".");static SEMI_COLON=new c("semiColon",";");static SINGLE_QUOTE=new c("singleQuote","'");static SPACE=new c("space"," ");static UNDERSCORE=new c("underscore","_");static all=new Map([[c.COMMA.name,c.COMMA],[c.COLON.name,c.COLON],[c.DOUBLE_QUOTE.name,c.DOUBLE_QUOTE],[c.EMPTY.name,c.EMPTY],[c.HYPHEN.name,c.HYPHEN],[c.PERIOD.name,c.PERIOD],[c.SEMI_COLON.name,c.SEMI_COLON],[c.SINGLE_QUOTE.name,c.SINGLE_QUOTE],[c.SPACE.name,c.SPACE],[c.UNDERSCORE.name,c.UNDERSCORE]]);static tokens=[...c.all.values()].map(e=>e.token);constructor(e,t){this.name=e,this.token=t}static cast(e){for(const[t,s]of c.all)if(s.token===e||t.toLowerCase()===e.toLowerCase())return s}toString(){return`Separator.${this.name}`}}class f{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 f(-1,0,-1,1,-1)}static when(t,s=2){if(t===e.NameOrder.FIRST_NAME)switch(s){case 2:return new f(-1,0,-1,1,-1);case 3:return new f(-1,0,1,2,-1);case 4:return new f(0,1,2,3,-1);case 5:return new f(0,1,2,3,4);default:return f.base()}else switch(s){case 2:return new f(-1,1,-1,0,-1);case 3:return new f(-1,1,2,0,-1);case 4:return new f(0,2,3,1,-1);case 5:return new f(0,2,3,1,4);default:return f.base()}}static only({prefix:e=-1,firstName:t,middleName:s=-1,lastName:i,suffix:a=-1}){return new f(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 p(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,(l=e.NameErrorType||(e.NameErrorType={}))[l.INPUT=0]="INPUT",l[l.VALIDATION=1]="VALIDATION",l[l.NOT_ALLOWED=2]="NOT_ALLOWED",l[l.UNKNOWN=3]="UNKNOWN";class g 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 y extends g{constructor(t){super(t.source,t.message,e.NameErrorType.INPUT),this.name="InputError"}}class E extends g{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 w extends g{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 M extends g{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 S{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===m.PREFIX}get isFirstName(){return this.type===m.FIRST_NAME}get isMiddleName(){return this.type===m.MIDDLE_NAME}get isLastName(){return this.type===m.LAST_NAME}get isSuffix(){return this.type===m.SUFFIX}static prefix(e){return new S(e,m.PREFIX)}static first(e){return new S(e,m.FIRST_NAME)}static middle(e){return new S(e,m.MIDDLE_NAME)}static last(e){return new S(e,m.LAST_NAME)}static suffix(e){return new S(e,m.SUFFIX)}initials(){return[this.initial]}toString(){return this.#e}equal(e){return e instanceof S&&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=p(this.#e,e??this.capsRange),this}validate(e){if("string"==typeof e&&e.trim().length<1)throw new y({source:e,message:"must be 1+ characters"})}}class A extends S{#t;constructor(e,...t){super(e,m.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=[S.first(this.value)];return this.hasMore&&e.push(...this.#t.map(S.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=p(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>p(t,e))),this}copyWith(e){return new A(e?.first??this.value,...e?.more??this.#t)}}class v extends S{format;#s;constructor(t,s,i=e.Surname.FATHER){super(t,m.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=[S.last(this.value)];return this.#s&&e.push(S.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=p(this.value,e),this.hasMother&&(this.#s=p(this.#s,e)),this}copyWith(e){return new v(e?.father??this.value,e?.mother??this.mother,e?.format??this.format)}}function I(e){return Array.isArray(e)&&e.length>0&&e.every(e=>e instanceof S)}const x="_copy";class T{#i;#a;#r;#n;#o;#h;#l;#u;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 mono(){return this.#u}get name(){return this.#i}constructor(t,s=e.NameOrder.FIRST_NAME,i=c.SPACE,a=e.Title.UK,r=!1,n=!0,o=e.Surname.FATHER,h=!1){this.#i=t,this.#a=s,this.#r=i,this.#n=a,this.#o=r,this.#h=n,this.#l=o,this.#u=h}static create(e="default"){return u.cache.has(e)||u.cache.set(e,new u(e)),u.cache.get(e)}static merge(e){if(e){const t=u.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.#u=e.mono??t.mono,t}return u.create()}copyWith(e={}){const{name:t,orderedBy:s,separator:i,title:a,ending:r,bypass:n,surname:o,mono:h}=e,l=u.create(this.#m(t??this.name+x));return l.#a=s??this.orderedBy,l.#r=i??this.separator,l.#n=a??this.title,l.#o=r??this.ending,l.#h=n??this.bypass,l.#l=o??this.surname,l.#u=h??this.mono,l}clone(){return this.copyWith()}reset(){this.#a=e.NameOrder.FIRST_NAME,this.#r=c.SPACE,this.#n=e.Title.UK,this.#o=!1,this.#h=!0,this.#l=e.Surname.FATHER,this.#u=!1,u.cache.set(this.name,this)}update({orderedBy:e,title:t,ending:s}){const i=u.cache.get(this.name);i&&(e!==this.#a&&(i.#a=e),t!==this.#n&&(i.#n=t),s!==this.#o&&(i.#o=s))}#m(e){return e===this.name||u.cache.has(e)?this.#m(e+x):e}}u=T;class F{static base=/[a-zA-Z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff\u0400-\u04FFΆ-ωΑ-ώ]/;static namon=new RegExp(`^${F.base.source}+(([' -]${F.base.source})?${F.base.source}*)*$`);static firstName=F.namon;static middleName=new RegExp(`^${F.base.source}+(([' -]${F.base.source})?${F.base.source}*)*$`);static lastName=F.namon}const L=e=>I(e)?e.map(e=>e.toString()).join(" "):"";class b{validate(e){if(0===e.length||e.length<2||e.length>5)throw new y({source:e.map(e=>e.toString()),message:"expecting a list of 2-5 elements"})}}class _{static#c;static create(){return this.#c||(this.#c=new _)}validate(e,t){if(e instanceof S)C.create().validate(e,t);else{if("string"!=typeof e)throw new y({source:typeof e,message:"expecting types of string or Name"});if(!F.namon.test(e))throw new E({source:e,nameType:"namon",message:"invalid name content failing namon regex"})}}}class R{static#c;static create(){return this.#c||(this.#c=new R)}validate(e){if(e instanceof A)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new y({source:typeof e,message:"expecting types string or FirstName"});if(!F.firstName.test(e))throw new E({source:e,nameType:"firstName",message:"invalid name content failing firstName regex"})}}}class O{static#c;static create(){return this.#c||(this.#c=new O)}validate(e){if("string"==typeof e){if(!F.middleName.test(e))throw new E({source:e,nameType:"middleName",message:"invalid name content failing middleName regex"})}else{if(!Array.isArray(e))throw new y({source:typeof e,message:"expecting types of string, string[] or Name[]"});try{const t=_.create();for(const s of e)t.validate(s,m.MIDDLE_NAME)}catch(t){throw new E({source:L(e),nameType:"middleName",message:t?.message})}}}}class D{static#c;static create(){return this.#c||(this.#c=new D)}validate(e){if(e instanceof v)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new y({source:typeof e,message:"expecting types string or LastName"});if(!F.lastName.test(e))throw new E({source:e,nameType:"lastName",message:"invalid name content failing lastName regex"})}}}class C{static#c;static create(){return this.#c||(this.#c=new C)}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(!F.namon.test(e.value))throw new E({source:e.toString(),nameType:e.type.toString(),message:"invalid name content failing namon regex"})}}class P{static#c;static create(){return this.#c||(this.#c=new P)}validate(e){this.validateKeys(e),$.firstName.validate(e.get(m.FIRST_NAME)),$.lastName.validate(e.get(m.LAST_NAME)),e.has(m.PREFIX)&&$.namon.validate(e.get(m.PREFIX)),e.has(m.SUFFIX)&&$.namon.validate(e.get(m.SUFFIX))}validateKeys(e){if(!e.size)throw new y({source:void 0,message:"Map<k,v> must not be empty"});if(e.size<2||e.size>5)throw new y({source:[...e.values()],message:"expecting 2-5 fields"});if(!e.has(m.FIRST_NAME))throw new y({source:[...e.values()],message:'"firstName" is a required key'});if(!e.has(m.LAST_NAME))throw new y({source:[...e.values()],message:'"lastName" is a required key'})}}class U extends b{index;constructor(e=f.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 j{static#c;static create(){return this.#c||(this.#c=new j)}validate(e){if(e.length<2)throw new y({source:L(e),message:"expecting at least 2 elements"});if(!this.#f(e))throw new y({source:L(e),message:"both first and last names are required"})}#f(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=P.create();static prefix=_.create();static firstName=R.create();static middleName=O.create();static lastName=D.create();static suffix=_.create()}class k{#d;#p;#N=[];#g;#y;#E;constructor(e){this.#E=T.merge(e)}get config(){return this.#E}get prefix(){return this.#d}get firstName(){return this.#p}get lastName(){return this.#g}get middleName(){return this.#N}get suffix(){return this.#y}get isMono(){return this instanceof q}static parse(e,t){try{const{prefix:s,firstName:i,middleName:a,lastName:r,suffix:n}=e;return new k(t).setPrefix(s).setFirstName("string"==typeof i?i:new A(i.value,...i.more??[])).setMiddleName("string"==typeof a?[a]:a??[]).setLastName("string"==typeof r?r:new v(r.father,r.mother)).setSuffix(n)}catch(t){if(t instanceof g)throw t;throw new M({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 S?t.value:t;return this.#d=S.prefix(this.#E.title!==e.Title.US||s.endsWith(".")?s:`${s}.`),this}setFirstName(e){return this.#E.bypass||$.firstName.validate(e),this.#p=e instanceof A?e:new A(e),this}setLastName(e){return this.#E.bypass||$.lastName.validate(e),this.#g=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 S?e:S.middle(e)),this):this}setSuffix(e){return e?(this.#E.bypass||$.suffix.validate(e),this.#y=S.suffix(e instanceof S?e.value:e),this):this}has(e){const t="string"==typeof e?m.cast(e):e;return!!t&&(t.equal(m.PREFIX)?!!this.#d:t.equal(m.SUFFIX)?!!this.#y:!t.equal(m.MIDDLE_NAME)||this.#N.length>0)}toString(){return this.isMono?this.value:Array.from(this.toIterable(!0)).join(" ")}*toIterable(e=!1){this.#d&&(yield this.#d),e?(yield*this.#p.asNames,yield*this.#N,yield*this.#g.asNames):(yield this.#p,yield*this.#N,yield this.#g),this.#y&&(yield this.#y)}*[Symbol.iterator](){yield*this.toIterable(!0)}}class q extends k{#e;#w;constructor(e,t){super(t??{name:"mononym",mono:!0}),this.#e=e.toString(),this.type=e instanceof S?e.type:m.FIRST_NAME}set type(e){this.#w="string"==typeof e?m.cast(e)??m.FIRST_NAME:e,this.#M(this.#e)}get type(){return this.#w}get value(){return this.#e}#M(e){if(this.setFirstName(s).setLastName(s).setMiddleName([]).setPrefix(null).setSuffix(null),this.#w.equal(m.FIRST_NAME))this.setFirstName(e);else if(this.#w.equal(m.LAST_NAME))this.setLastName(e);else if(this.#w.equal(m.MIDDLE_NAME))this.setMiddleName([e]);else if(this.#w.equal(m.PREFIX))this.setPrefix(e);else{if(!this.#w.equal(m.SUFFIX))throw new g(e,"invalid mononym type");this.setSuffix(e)}}}class B{raw;constructor(e){this.raw=e}static build(e,t){const s=e.trim().split(c.SPACE.token),i=s.length;if(t instanceof f){const e=Object.entries(t.json()).filter(([,e])=>e>-1&&e<i).map(([e,t])=>new S(s[t],m.all.get(e)));return new z(e)}if(i<2)throw new y({source:e,message:"expecting at least 2 name parts"});if(2===i||3===i)return new H(e);{const e=s.pop(),[t,...i]=s;return new X([t,i.join(" "),e])}}static buildAsync(e,t){try{return Promise.resolve(B.build(e,t))}catch(e){return Promise.reject(e)}}}class H extends B{parse(e){const t=T.merge(e),s=this.raw.split(t.separator.token);return new X(s).parse(t)}}class X extends B{parse(e){const t=T.merge(e);if(1===this.raw.length&&t.mono)return new K(this.raw[0]).parse(t);const s=this.raw.map(e=>e.trim()),i=f.when(t.orderedBy,s.length),a=new U(i);t.bypass?a.validateIndex(s):a.validate(s);const{firstName:r,lastName:n,middleName:o,prefix:h,suffix:l}=i,u=new k(t).setFirstName(new A(s[r])).setLastName(new v(s[n]));return s.length>=3&&u.setMiddleName(s[o].split(t.separator.token)),s.length>=4&&u.setPrefix(S.prefix(s[h])),5===s.length&&u.setSuffix(S.suffix(s[l])),u}}class W extends B{parse(e){const t=T.merge(e),s=new Map(Object.entries(this.raw).map(([e,t])=>{const s=m.cast(e);if(!s)throw new y({source:Object.values(this.raw).join(" "),message:`unsupported key "${e}"`});return[s,t]}));return t.bypass?$.nama.validateKeys(s):$.nama.validate(s),k.parse(this.raw,t)}}class z extends B{parse(e){const t=T.merge(e);if(1===this.raw.length&&t.mono)return new K(this.raw[0]).parse(e);j.create().validate(this.raw);const s=new k(t);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 A?e:new A(e.value));else if(e.isMiddleName)s.middleName.push(e);else if(e.isLastName){const i=e instanceof v?e.mother:void 0;s.setLastName(new v(e.value,i,t.surname))}return s}}class K extends B{parse(e){const t=T.merge(e);t.bypass&&$.namon.validate(this.raw);const s=t.mono instanceof m?t.mono:m.FIRST_NAME,i=this.raw instanceof S?this.raw:new S(this.raw.trim(),s);return new q(i,t)}}class Y{#S;constructor(e,t){this.#S=this.#A(e).parse(t)}static tryParse(e,t){try{return new Y(B.build(e,t))}catch{return}}static async parse(e,t){return B.buildAsync(e,t).then(e=>new Y(e))}get config(){return this.#S.config}get isMono(){return this.#S.isMono}get length(){return this.isMono?this.#S.toString().length:this.birth.length}get prefix(){return this.#S.prefix?.toString()}get first(){return this.firstName()}get middle(){return this.hasMiddle?this.middleName()[0]:void 0}get hasMiddle(){return this.#S.has(m.MIDDLE_NAME)}get last(){return this.lastName()}get suffix(){return this.#S.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.#S.toIterable()}get size(){return Array.from(this.parts).length}*[Symbol.iterator](){yield*this.#S.toIterable(!0)}toString(){return this.full}get(e){const t="string"==typeof e?m.cast(e):e;return t?.equal(m.PREFIX)?this.#S.prefix:t?.equal(m.FIRST_NAME)?this.#S.firstName:t?.equal(m.MIDDLE_NAME)?this.#S.middleName:t?.equal(m.LAST_NAME)?this.#S.lastName:t?.equal(m.SUFFIX)?this.#S.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.#S.has(e)}fullName(t){if(this.isMono)return this.#S.toString();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.hasMiddle?i.push(this.first,this.middleName().join(" ")+s):i.push(this.first+s)),this.suffix&&i.push(this.suffix),i.join(" ").trim()}birthName(t){return this.isMono?this.#S.toString():(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.#S.firstName.toString(e)}middleName(){return this.#S.middleName.map(e=>e.value)}lastName(e){return this.#S.lastName.toString(e)}initials(t){if(this.isMono)return[this.#S.toString()[0]];const{orderedBy:s=this.config.orderedBy,only:i=e.NameType.BIRTH_NAME,asJson:a}=t??{},r=this.#S.firstName.initials(),n=this.#S.middleName.map(e=>e.value[0]),o=this.#S.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){if(this.isMono)return this.#S.toString();t??=this.config.orderedBy;const{firstName:s,lastName:i}=this.#S;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;if(this.isMono)return`${this.initials()}${n?".":""}`;const{firstName:h,lastName:l,middleName:u}=this.#S,m=n?".":"",c=this.hasMiddle,f=h.toString(),d=this.middleName().join(" "),p=l.toString(),N=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?[N,d,p]:[N,p];break;case e.Flat.LAST_NAME:E=c?[f,d,g]:[f,g];break;case e.Flat.MIDDLE_NAME:E=c?[f,y,p]:[f,p];break;case e.Flat.FIRST_MID:E=c?[N,y,p]:[N,p];break;case e.Flat.MID_LAST:E=c?[f,y,g]:[f,g];break;default:E=c?[N,y,g]:[N,g]}else switch(s){case e.Flat.FIRST_NAME:E=c?[p,N,d]:[p,N];break;case e.Flat.LAST_NAME:E=c?[g,f,d]:[g,f];break;case e.Flat.MIDDLE_NAME:E=c?[p,f,y]:[p,f];break;case e.Flat.FIRST_MID:E=c?[p,N,y]:[p,N];break;case e.Flat.MID_LAST:E=c?[g,f,y]:[g,f];break;default:E=c?[g,N,y]:[g,N]}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 w({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 p(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("")}serialize(){const{config:e,firstName:t,lastName:s}=this.#S;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,mono:e.mono instanceof m?e.mono.key:e.mono}}}#A(e){if(e instanceof B)return e;if("string"==typeof e)return new H(e);if(N(e))return new X(e);if(I(e))return new z(e);if("object"==typeof e)return new W(e);throw new y({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.#S.firstName.value[0];case"$F":return this.#S.firstName.initials(!0).join("");case"$l":return this.#S.lastName.value[0];case"$L":return this.#S.lastName.initials().join("");case"$m":return this.hasMiddle?this.middle[0]:void 0;case"$M":return this.hasMiddle?this.#S.middleName.map(e=>e.value[0]).join(""):void 0;default:return t.includes(e)?e:void 0}}}class Q{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 J extends Q{constructor(e,t,s,i,a){super(t,s,i,a),this.add(...e)}static create(e){return new J(e?[e]:[])}static of(...e){return new J(e)}static use({names:e,prebuild:t,postbuild:s,preclear:i,postclear:a}){return new J(e??[],t,s,i,a)}build(e){this.prebuild?.();const t=[...this.queue],s=T.merge(e);return s.mono||j.create().validate(t),this.instance=new Y(t,s),this.postbuild?.(this.instance),this.instance}}e.Config=T,e.FirstName=A,e.FullName=k,e.InputError=y,e.LastName=v,e.Mononym=q,e.Name=S,e.NameBuilder=J,e.NameError=g,e.NameIndex=f,e.Namefully=Y,e.Namon=m,e.NotAllowedError=w,e.Parser=B,e.Separator=c,e.UnknownError=M,e.ValidationError=E,e.default=(e,t)=>new Y(e,t),e.deserialize=function(e){try{const t="string"==typeof e?JSON.parse(e):e;if(!t||"object"!=typeof t)throw new y({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=J.of();o&&l.add(S.prefix(o)),h&&l.add(S.suffix(h)),n&&l.add(..."string"==typeof n?[S.middle(n)]:n.map(e=>S.middle(e))),l.add("string"==typeof a?S.first(a):new A(a.value,...a.more??[])),l.add("string"==typeof r?S.last(r):new v(r.father,r.mother));const u=c.cast(i.separator),f="string"==typeof i.mono?m.cast(i.mono)??!1:i.mono;return l.build({...i,separator:u,mono:f})}catch(t){if(t instanceof g)throw t;throw new M({source:String(e),message:"could not deserialize data",origin:t instanceof Error?t:new Error(String(t))})}},e.isNameArray=I,e.version="2.2.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.2.0",
4
4
  "description": "Handle personal names in a particular order, way, or shape.",
5
5
  "author": "Ralph Florent",
6
6
  "license": "MIT",
@@ -30,7 +30,7 @@
30
30
  "format": "prettier --write src example",
31
31
  "lint": "eslint src",
32
32
  "test": "jest",
33
- "test:cov": "jest --collectCoverage",
33
+ "test:cov": "jest --coverage",
34
34
  "prebuild": "shx rm -rf dist",
35
35
  "build:esm": "tsc -p tsconfig.json",
36
36
  "build:cjs": "tsc -p tsconfig.cjs.json",
package/readme.md CHANGED
@@ -4,6 +4,7 @@
4
4
  [![JSR Version][jsr-version]][jsr-url]
5
5
  [![CI build][ci-img]][ci-url]
6
6
  [![MIT License][license-img]][license-url]
7
+ [![DeepWiki][deepwiki-img]][deepwiki-url]
7
8
 
8
9
  Human name handling made easy.
9
10
  [Try it live](https://stackblitz.com/edit/namefully).
@@ -204,6 +205,24 @@ const name = new Namefully(
204
205
  );
205
206
  ```
206
207
 
208
+ ### mono
209
+
210
+ `boolean | Namon` - default: `false`
211
+
212
+ Enables support for mononyms (i.e., single word names). You may also use `Namon`
213
+ to assign which name type is being used to represent the mononym.
214
+
215
+ > You should know that this goes against the original design philosophy of the library,
216
+ > which is intentionally opinionated around shaping and organizing multiple name components.
217
+ > Treating a single token as a "full" name makes a lot of the existing API semantics
218
+ > somewhat meaningless.
219
+
220
+ ```ts
221
+ const name = new Namefully('Plato', { mono: true }); // throws an exception without this flag.
222
+ console.log(name.full); // Plato
223
+ console.log(name.initials()); // ['P']
224
+ ```
225
+
207
226
  To sum it all up, the default values are:
208
227
 
209
228
  ```ts
@@ -213,7 +232,8 @@ To sum it all up, the default values are:
213
232
  title: Title.UK,
214
233
  ending: false,
215
234
  bypass: true,
216
- surname: Surname.FATHER
235
+ surname: Surname.FATHER,
236
+ mono: false
217
237
  }
218
238
  ```
219
239
 
@@ -227,8 +247,10 @@ import { Config, FullName, Namefully, Parser } from 'namefully';
227
247
  // Suppose you want to cover this '#' separator
228
248
  class SimpleParser extends Parser<string> {
229
249
  parse(options: Partial<Config>): FullName {
230
- const [firstName, lastName] = this.raw.split('#');
231
- return FullName.parse({ firstName, lastName }, Config.merge(options));
250
+ const [fn, ln] = this.raw.split('#', 2);
251
+ return new FullName(options)
252
+ .setFirstName(fn.trim())
253
+ .setLastName(ln.trim());
232
254
  }
233
255
  }
234
256
 
@@ -303,7 +325,7 @@ So, this utility understands the name parts as follows:
303
325
 
304
326
  `namefully` does not support certain use cases:
305
327
 
306
- - mononame: `Plato` - a workaround is to set the mononame as both first and last name;
328
+ - mononame: `Plato` - enable the mononym flag `Config.mono` to support this.
307
329
  - nickname: `Dwayne "The Rock" Johnson` - use custom parser instead.
308
330
  - multiple prefixes or suffixes: `Prof. Dr. Einstein`.
309
331
 
@@ -326,6 +348,8 @@ The underlying content of this utility is licensed under [MIT License][license-u
326
348
  [ci-url]: https://github.com/ralflorent/namefully/actions/workflows/ci.yml
327
349
  [license-img]: https://img.shields.io/npm/l/namefully
328
350
  [license-url]: https://opensource.org/licenses/MIT
351
+ [deepwiki-img]: https://deepwiki.com/badge.svg
352
+ [deepwiki-url]: https://deepwiki.com/ralflorent/namefully
329
353
 
330
354
  [contributing-url]: https://github.com/ralflorent/namefully/blob/main/CONTRIBUTING.md
331
355
  [examples]: https://github.com/ralflorent/namefully/tree/main/example