namefully 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,i,a,r,n,h,o,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,(h=e.CapsRange||(e.CapsRange={}))[h.NONE=0]="NONE",h[h.INITIAL=1]="INITIAL",h[h.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,i,a){this.prefix=e,this.firstName=t,this.middleName=s,this.lastName=i,this.suffix=a}static base(){return new this(-1,0,-1,1,-1)}static when(t,s=2){if(t===e.NameOrder.FIRST_NAME)switch(s){case 2:return new this(-1,0,-1,1,-1);case 3:return new this(-1,0,1,2,-1);case 4:return new this(0,1,2,3,-1);case 5:return new this(0,1,2,3,4);default:return c.base()}else switch(s){case 2:return new this(-1,1,-1,0,-1);case 3:return new this(-1,1,2,0,-1);case 4:return new this(0,2,3,1,-1);case 5:return new this(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 this(e,t,s,i,a)}toJson(){return{prefix:this.prefix,firstName:this.firstName,middleName:this.middleName,lastName:this.lastName,suffix:this.suffix}}}function f(t,s=e.CapsRange.INITIAL){if(!t||s===e.CapsRange.NONE)return t;const i=t[0].toUpperCase(),a=t.slice(1).toLowerCase();return s===e.CapsRange.INITIAL?i.concat(a):t.toUpperCase()}function d(t,s=e.CapsRange.INITIAL){if(!t||s===e.CapsRange.NONE)return t;const i=t[0].toLowerCase(),a=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,(o=e.NameErrorType||(e.NameErrorType={}))[o.INPUT=0]="INPUT",o[o.VALIDATION=1]="VALIDATION",o[o.NOT_ALLOWED=2]="NOT_ALLOWED",o[o.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(){let e="";return this.source||(e="<undefined>"),"string"==typeof this.source&&(e=this.source),N(this.source)&&(e=this.source.join(" ")),e}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 w 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 y extends p{origin;constructor(t){super(t.source,t.message,e.NameErrorType.UNKNOWN),this.origin=t.error,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,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 this(e,u.PREFIX)}static first(e){return new this(e,u.FIRST_NAME)}static middle(e){return new this(e,u.MIDDLE_NAME)}static last(e){return new this(e,u.LAST_NAME)}static suffix(e){return new this(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=f(this.#e,e??this.capsRange),this}decaps(e){return this.value=d(this.#e,e??this.capsRange),this}validate(e){if(e&&e?.trim()?.length<2)throw new g({source:e,message:"must be 2+ characters"})}}class M extends A{#t;constructor(e,...t){super(e,u.FIRST_NAME),t.forEach(e=>this.validate(e)),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(e=>A.first(e))),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=f(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>f(t,e))),this}decaps(e){return e=e||this.capsRange,this.value=d(this.value,e),this.hasMore&&(this.#t=this.#t.map(t=>d(t,e))),this}copyWith(e){return new M(e?.first??this.value,...e?.more??this.#t)}}class S extends A{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=[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=t||this.format){case e.Surname.MOTHER:this.#s&&s.push(this.#s[0]);break;case e.Surname.HYPHENATED:case e.Surname.ALL:s.push(this.initial),this.#s&&s.push(this.#s[0]);break;case e.Surname.FATHER:default:s.push(this.initial)}return s}caps(e){return e=e||this.capsRange,this.value=f(this.value,e),this.hasMother&&(this.#s=f(this.#s,e)),this}decaps(e){return e=e||this.capsRange,this.value=d(this.value,e),this.hasMother&&(this.#s=d(this.#s,e)),this}copyWith(e){return new S(e?.father??this.value,e?.mother??this.mother,e?.format??this.format)}}function v(e){return Array.isArray(e)&&e.length>0&&e.every(e=>e instanceof A)}const T="_copy";class I{#i;#a;#r;#n;#h;#o;#l;static cache=new Map;get orderedBy(){return this.#a}get separator(){return this.#r}get title(){return this.#n}get ending(){return this.#h}get bypass(){return this.#o}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,h=e.Surname.FATHER){this.#i=t,this.#a=s,this.#r=i,this.#n=a,this.#h=r,this.#o=n,this.#l=h}static create(e="default"){return l.cache.has(e)||l.cache.set(e,new this(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.#h=e.ending??t.ending,t.#o=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:h}=e,o=l.create(this.#u(t??this.name+T));return o.#a=s??this.orderedBy,o.#r=i??this.separator,o.#n=a??this.title,o.#h=r??this.ending,o.#o=n??this.bypass,o.#l=h??this.surname,o}clone(){return this.copyWith()}reset(){this.#a=e.NameOrder.FIRST_NAME,this.#r=m.SPACE,this.#n=e.Title.UK,this.#h=!1,this.#o=!0,this.#l=e.Surname.FATHER,l.cache.set(this.name,this)}updateOrder(e){e&&e!==this.#a&&(l.cache.get(this.name).#a=e)}#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=>v(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-2 elements"})}}class _{static#m;static create(){return this.#m||(this.#m=new this)}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 | Name"});if(!x.namon.test(e))throw new E({source:e,nameType:"namon",message:"invalid content"})}}}class b{static#m;static create(){return this.#m||(this.#m=new this)}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 | FirstName"});if(!x.firstName.test(e))throw new E({source:e,nameType:"firstName",message:"invalid content"})}}}class R{static#m;static create(){return this.#m||(this.#m=new this)}validate(e){if("string"==typeof e){if(!x.middleName.test(e))throw new E({source:e,nameType:"middleName",message:"invalid content"})}else{if(!Array.isArray(e))throw new g({source:typeof e,message:"expecting types of string | string[] | 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 this)}validate(e){if(e instanceof S)e.asNames.forEach(e=>this.validate(e.value));else{if("string"!=typeof e)throw new g({source:typeof e,message:"expecting types string | LastName"});if(!x.lastName.test(e))throw new E({source:e,nameType:"lastName",message:"invalid content"})}}}class D{static#m;static create(){return this.#m||(this.#m=new this)}validate(e,t){if(t&&e.type!==t)throw new E({source:e.toString(),nameType:e.type.toString(),message:"wrong type"});if(!x.namon.test(e.value))throw new E({source:e.toString(),nameType:e.type.toString(),message:"invalid content"})}}class C{static#m;static create(){return this.#m||(this.#m=new this)}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-2 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 this)}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{#f;#d;#N=[];#p;#g;#E;constructor(e){this.#E=I.merge(e)}get config(){return this.#E}get prefix(){return this.#f}get firstName(){return this.#d}get lastName(){return this.#p}get middleName(){return this.#N}get suffix(){return this.#g}static parse(e,t){try{const s=new j(t);return s.setPrefix(e.prefix),s.setFirstName(e.firstName),s.setMiddleName(e.middleName??[]),s.setLastName(e.lastName),s.setSuffix(e.suffix),s}catch(t){if(t instanceof p)throw t;throw new y({source:Object.values(e).join(" "),message:"could not parse JSON content",error: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.#f=A.prefix(this.#E.title===e.Title.US?`${s}.`:s),this}setFirstName(e){return this.#E.bypass||$.firstName.validate(e),this.#d=e instanceof M?e:new M(e),this}setLastName(e){return this.#E.bypass||$.lastName.validate(e),this.#p=e instanceof S?e:new S(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.#f: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),i=s.length;if(t instanceof c){const e=Object.entries(t.toJson()).filter(([,e])=>e>-1&&e<i).map(([e,t])=>new A(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=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),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:h,middleName:o,prefix:l,suffix:u}=a;return s.setFirstName(new M(i[n])),s.setLastName(new S(i[h])),i.length>=3&&s.setMiddleName(i[o].split(t.separator.token)),i.length>=4&&s.setPrefix(A.prefix(i[l])),5===i.length&&s.setSuffix(A.suffix(i[u])),s}}class H extends k{parse(e){const t=I.merge(e);return t.bypass?C.create().validateKeys(this.#w()):C.create().validate(this.#w()),j.parse(this.raw,t)}#w(){return 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]}))}}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 i=new S(e.value,e instanceof S?e.mother:void 0,t.surname);s.setLastName(i)}return s}}class W{#y;constructor(e,t){this.#y=this.#A(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}}has(e){return this.#y.has(e)}fullName(t){const s=this.config.ending?",":"",i=[];return t=t||this.config.orderedBy,this.prefix&&i.push(this.prefix),t===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=t||this.config.orderedBy)===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 s=this.#y.firstName.initials(),i=this.#y.middleName.map(e=>e.initials()[0]),a=this.#y.lastName.initials();if(t?.asJson)return{firstName:s,middleName:i,lastName:a};const r=[],{orderedBy:n=this.config.orderedBy,only:h=e.NameType.BIRTH_NAME}=t??{};return h!==e.NameType.BIRTH_NAME?h===e.NameType.FIRST_NAME?r.push(...s):h===e.NameType.MIDDLE_NAME?r.push(...i):r.push(...a):n===e.NameOrder.FIRST_NAME?r.push(...s,...i,...a):r.push(...a,...s,...i),r}shorten(t){return(t=t||this.config.orderedBy)===e.NameOrder.FIRST_NAME?[this.#y.firstName.value,this.#y.lastName.toString()].join(" "):[this.#y.lastName.toString(),this.#y.firstName.value].join(" ")}flatten(t){const{by:s=e.Flat.MIDDLE_NAME,limit:i=20,recursive:a=!1,withMore:r=!1,withPeriod:n=!0,surname:h}=t;if(this.length<=i)return this.full;const o=n?".":"",l=this.#y.firstName.toString(),u=this.middleName().join(" "),m=this.#y.lastName.toString(),c=this.hasMiddle,f=this.#y.firstName.initials(r).join(o+" ")+o,d=this.#y.lastName.initials(h).join(o+" ")+o,N=c?this.#y.middleName.map(e=>e.initials()[0]).join(o+" ")+o:"";let p=[];if(this.config.orderedBy===e.NameOrder.FIRST_NAME)switch(s){case e.Flat.FIRST_NAME:p=c?[f,u,m]:[f,m];break;case e.Flat.LAST_NAME:p=c?[l,u,d]:[l,d];break;case e.Flat.MIDDLE_NAME:p=c?[l,N,m]:[l,m];break;case e.Flat.FIRST_MID:p=c?[f,N,m]:[f,m];break;case e.Flat.MID_LAST:p=c?[l,N,d]:[l,d];break;case e.Flat.ALL:p=c?[f,N,d]:[f,d]}else switch(s){case e.Flat.FIRST_NAME:p=c?[m,f,u]:[m,f];break;case e.Flat.LAST_NAME:p=c?[d,l,u]:[d,l];break;case e.Flat.MIDDLE_NAME:p=c?[m,l,N]:[m,l];break;case e.Flat.FIRST_MID:p=c?[m,f,N]:[m,f];break;case e.Flat.MID_LAST:p=c?[d,l,N]:[d,l];break;case e.Flat.ALL:p=c?[d,f,N]:[d,f]}const g=p.join(" ");if(a&&g.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?g:this.flatten({...t,by:i})}return g}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.split("")){if(-1===t.indexOf(a))throw new w({source:this.full,operation:"format",message:`unsupported character <${a}> from ${e}.`});s+=a,"$"!==a&&(i.push(this.#M(s)??""),s="")}return i.join("").trim()}flip(){this.config.updateOrder(this.config.orderedBy===e.NameOrder.FIRST_NAME?e.NameOrder.LAST_NAME:e.NameOrder.FIRST_NAME)}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 d(this.toPascalCase())}toPascalCase(){return this.split().map(e=>f(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 function(e){const t=[];for(const s of e)s===s.toUpperCase()?t.push(s.toLowerCase()):t.push(s.toUpperCase());return t.join("")}(this.birth)}#A(e){if(e instanceof k)return e;if("string"==typeof e)return new B(e);if(N(e))return new q(e);if(v(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."})}#M(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 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":case"$F":return this.#y.firstName.initials()[0];case"$l":case"$L":return this.#y.lastName.initials()[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,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 Y extends K{constructor(e,t,s,i,a){super(t,s,i,a),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:i,postclear:a}){return new Y(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=I,e.FirstName=M,e.FullName=j,e.InputError=g,e.LastName=S,e.Name=A,e.NameBuilder=Y,e.NameError=p,e.NameIndex=c,e.Namefully=W,e.Namon=u,e.NotAllowedError=w,e.Parser=k,e.Separator=m,e.UnknownError=y,e.ValidationError=E,e.default=(e,t)=>new W(e,t),e.isNameArray=v,e.version="2.0.0",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=[".",","," ","-","_","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})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "namefully",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Handle personal names in a particular order, way, or shape.",
5
5
  "author": "Ralph Florent",
6
6
  "license": "MIT",
@@ -10,33 +10,33 @@
10
10
  "type": "module",
11
11
  "main": "dist/cjs/index.js",
12
12
  "module": "dist/esm/index.js",
13
- "types": "dist/esm/index.d.js",
13
+ "types": "dist/esm/index.d.ts",
14
14
  "browser": "dist/namefully.min.js",
15
15
  "files": ["dist"],
16
16
  "exports": {
17
17
  ".": {
18
18
  "import": "./dist/esm/index.js",
19
19
  "require": "./dist/cjs/index.js",
20
- "types": "./dist/esm/index.d.js"
20
+ "types": "./dist/esm/index.d.ts"
21
21
  },
22
22
  "./*": {
23
23
  "import": "./dist/esm/*.js",
24
24
  "require": "./dist/cjs/*.js",
25
- "types": "./dist/esm/*.d.js"
25
+ "types": "./dist/esm/*.d.ts"
26
26
  }
27
27
  },
28
28
  "scripts": {
29
29
  "start": "tsx example/main.ts",
30
- "format": "prettier --write src",
30
+ "format": "prettier --write src example",
31
31
  "lint": "eslint src",
32
32
  "test": "jest",
33
33
  "test:cov": "jest --collectCoverage",
34
34
  "prebuild": "shx rm -rf dist",
35
- "build:esm": "tsc",
35
+ "build:esm": "tsc -p tsconfig.json",
36
36
  "build:cjs": "tsc -p tsconfig.cjs.json",
37
37
  "build:dts": "tsc -p tsconfig.dts.json",
38
38
  "build": "npm run build:esm && npm run build:cjs && npm run build:dts && rollup -c",
39
- "prepublish": "npm run format && npm run build"
39
+ "prepublish": "npm run format && npm run lint && npm run build"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@eslint/js": "^9.29.0",
package/readme.md CHANGED
@@ -3,7 +3,6 @@
3
3
  [![npm version][version-img]][version-url]
4
4
  [![JSR Version][jsr-version]][jsr-url]
5
5
  [![CI build][ci-img]][ci-url]
6
- [![Coverage Status][codecov-img]][codecov-url]
7
6
  [![MIT License][license-img]][license-url]
8
7
 
9
8
  Human name handling made easy.
@@ -54,7 +53,7 @@ npm i namefully
54
53
 
55
54
  ## Dependencies
56
55
 
57
- None
56
+ None.
58
57
 
59
58
  ## Related packages
60
59
 
@@ -66,7 +65,8 @@ This package is also available in [Angular](https://angular.io/) and
66
65
 
67
66
  ## Usage
68
67
 
69
- See [examples] or [test cases][test-cases] for more details.
68
+ See [examples] or [test cases][test-cases] for more details. Here's a glimpse at
69
+ what this utility does:
70
70
 
71
71
  ```ts
72
72
  import { Namefully } from 'namefully';
@@ -80,16 +80,17 @@ console.log(name.zip()); // Thomas A. E.
80
80
  ```
81
81
 
82
82
  > **Note** that if you intend to use this utility for non-standard name cases such as
83
- > many middle names or last names, use `Namefully.parse()` or `NameBuilder` instead.
83
+ > multiple first, middle or last names, use `Namefully.parse()` or `NameBuilder` instead.
84
84
 
85
85
  ## `Config` and default values
86
86
 
87
- `Config` is a single configuration to use across the other components.
87
+ `Config` represents a series of configurable parameters used to control and customize
88
+ how `Namefully` objects get created.
88
89
 
89
- The multiton pattern is used to keep one configuration across the `Namefully`
90
- setup. This is useful for avoiding confusion when building other components such
91
- as `FirstName`, `LastName`, or `Name` of distinct types (or `Namon`) that may
92
- be of particular shapes.
90
+ The multiton pattern is used to allow unique settings across `Namefully` objects.
91
+ This is quite useful for avoiding confusion when building other name components such
92
+ as `FirstName`, `LastName`, or `Name` that may not often follow the same pattern,
93
+ rule or behavior in your application.
93
94
 
94
95
  Below are enlisted the options supported by `namefully`.
95
96
 
@@ -269,17 +270,17 @@ The opening `[` and closing `]` brackets mean that these parts are optional. In
269
270
  other words, the most basic/typical case is a name that looks like this:
270
271
  `John Smith`, where `John` is the _firstName_ and `Smith`, the _lastName_.
271
272
 
272
- > NOTE: Do notice that the order of appearance matters and (as shown in
273
- > [orderedBy](#orderedby)) can be altered through configured parameters. By default,
274
- > the order of appearance is as shown above and will be used as a basis for
275
- > future examples and use cases.
273
+ > Do notice that the order of appearance matters and (as shown in [orderedBy](#orderedby))
274
+ > can be altered through configured parameters. By default, the order of appearance
275
+ > is as shown above and will be used as a basis for future examples and use cases.
276
276
 
277
277
  Once imported, all that is required to do is to create an instance of
278
- `Namefully` and the rest will follow.
278
+ `Namefully` and the rest will follow. Keep in mind that all name parts must have
279
+ at least one (1) character to proceed.
279
280
 
280
281
  ### Basic cases
281
282
 
282
- Let us take a common example with all the parts:
283
+ Let us take this example with all the parts:
283
284
 
284
285
  `Mr John Joe Smith PhD`
285
286
 
@@ -323,8 +324,6 @@ The underlying content of this utility is licensed under [MIT License][license-u
323
324
  [jsr-url]: https://jsr.io/@ralflorent/namefully
324
325
  [ci-img]: https://github.com/ralflorent/namefully/workflows/build/badge.svg
325
326
  [ci-url]: https://github.com/ralflorent/namefully/actions/workflows/ci.yml
326
- [codecov-img]: https://codecov.io/gh/ralflorent/namefully/branch/main/graph/badge.svg
327
- [codecov-url]: https://codecov.io/gh/ralflorent/namefully
328
327
  [license-img]: https://img.shields.io/npm/l/namefully
329
328
  [license-url]: https://opensource.org/licenses/MIT
330
329