namefully 1.2.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/builder.js +82 -0
- package/dist/cjs/config.js +100 -0
- package/dist/{lib → cjs}/constants.js +1 -1
- package/dist/{lib → cjs}/error.js +8 -5
- package/dist/cjs/fullname.js +102 -0
- package/dist/{lib → cjs}/index.js +18 -12
- package/dist/cjs/name.js +218 -0
- package/dist/cjs/namefully.js +391 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/parser.js +135 -0
- package/dist/{lib → cjs}/types.js +40 -36
- package/dist/{lib → cjs}/utils.js +29 -17
- package/dist/cjs/validator.js +266 -0
- package/dist/esm/builder.d.ts +73 -0
- package/dist/esm/builder.js +78 -0
- package/dist/{types → esm}/config.d.ts +1 -1
- package/dist/esm/config.js +96 -0
- package/dist/{types → esm}/constants.d.ts +1 -1
- package/dist/esm/constants.js +27 -0
- package/dist/{types → esm}/error.d.ts +2 -3
- package/dist/esm/error.js +87 -0
- package/dist/{types/full-name.d.ts → esm/fullname.d.ts} +3 -3
- package/dist/esm/fullname.js +98 -0
- package/dist/esm/index.d.ts +25 -0
- package/dist/esm/index.js +12 -0
- package/dist/{types → esm}/name.d.ts +2 -1
- package/dist/esm/name.js +211 -0
- package/dist/{types → esm}/namefully.d.ts +23 -13
- package/dist/esm/namefully.js +387 -0
- package/dist/esm/package.json +1 -0
- package/dist/{types → esm}/parser.d.ts +7 -6
- package/dist/esm/parser.js +127 -0
- package/dist/esm/types.js +106 -0
- package/dist/{types → esm}/utils.d.ts +4 -3
- package/dist/esm/utils.js +96 -0
- package/dist/{types → esm}/validator.d.ts +3 -3
- package/dist/esm/validator.js +259 -0
- package/dist/namefully.js +1580 -0
- package/dist/namefully.min.js +1 -0
- package/package.json +52 -32
- package/readme.md +63 -35
- package/dist/lib/config.js +0 -112
- package/dist/lib/full-name.js +0 -115
- package/dist/lib/name.js +0 -230
- package/dist/lib/namefully.js +0 -410
- package/dist/lib/parser.js +0 -138
- package/dist/lib/validator.js +0 -301
- package/dist/types/index.d.ts +0 -21
- package/dist/umd/namefully.js +0 -1832
- package/dist/umd/namefully.min.js +0 -1
- /package/dist/{types → esm}/types.d.ts +0 -0
|
@@ -0,0 +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})});
|
package/package.json
CHANGED
|
@@ -1,46 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "namefully",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Handle personal names in a particular order, way, or shape.",
|
|
5
5
|
"author": "Ralph Florent",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ralflorent/namefully",
|
|
8
8
|
"homepage": "https://namefully.netlify.app",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
"keywords": ["format", "parse", "human", "personal", "family", "name"],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "dist/cjs/index.js",
|
|
12
|
+
"module": "dist/esm/index.js",
|
|
13
|
+
"types": "dist/esm/index.d.js",
|
|
14
|
+
"browser": "dist/namefully.min.js",
|
|
15
|
+
"files": ["dist"],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/esm/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js",
|
|
20
|
+
"types": "./dist/esm/index.d.js"
|
|
21
|
+
},
|
|
22
|
+
"./*": {
|
|
23
|
+
"import": "./dist/esm/*.js",
|
|
24
|
+
"require": "./dist/cjs/*.js",
|
|
25
|
+
"types": "./dist/esm/*.d.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
23
28
|
"scripts": {
|
|
24
|
-
"
|
|
25
|
-
"format": "prettier --write
|
|
29
|
+
"start": "tsx example/main.ts",
|
|
30
|
+
"format": "prettier --write src",
|
|
31
|
+
"lint": "eslint src",
|
|
32
|
+
"test": "jest",
|
|
33
|
+
"test:cov": "jest --collectCoverage",
|
|
26
34
|
"prebuild": "shx rm -rf dist",
|
|
27
|
-
"build": "tsc
|
|
28
|
-
"
|
|
29
|
-
"
|
|
35
|
+
"build:esm": "tsc",
|
|
36
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
37
|
+
"build:dts": "tsc -p tsconfig.dts.json",
|
|
38
|
+
"build": "npm run build:esm && npm run build:cjs && npm run build:dts && rollup -c",
|
|
39
|
+
"prepublish": "npm run format && npm run build"
|
|
30
40
|
},
|
|
31
41
|
"devDependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"
|
|
35
|
-
"jest": "^29.5.
|
|
36
|
-
"
|
|
42
|
+
"@eslint/js": "^9.29.0",
|
|
43
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
44
|
+
"@rollup/plugin-typescript": "^12.1.3",
|
|
45
|
+
"@types/jest": "^29.5.14",
|
|
46
|
+
"@types/node": "^24.0.3",
|
|
47
|
+
"jest": "^29.7.0",
|
|
48
|
+
"prettier": "^3.6.0",
|
|
49
|
+
"rollup": "^4.44.0",
|
|
37
50
|
"shx": "^0.4.0",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"typescript": "
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
"ts-jest": "^29.4.0",
|
|
52
|
+
"tslib": "^2.8.1",
|
|
53
|
+
"tsx": "^4.20.3",
|
|
54
|
+
"typescript": "~5.3.3",
|
|
55
|
+
"typescript-eslint": "^8.35.0"
|
|
56
|
+
},
|
|
57
|
+
"prettier": {
|
|
58
|
+
"singleQuote": true,
|
|
59
|
+
"trailingComma": "all",
|
|
60
|
+
"printWidth": 120
|
|
61
|
+
},
|
|
62
|
+
"jest": {
|
|
63
|
+
"moduleNameMapper": {"^(\\.{1,2}/.*)\\.js$": "$1"},
|
|
64
|
+
"transform": {"^.+\\.ts$": "ts-jest"}
|
|
45
65
|
}
|
|
46
66
|
}
|
package/readme.md
CHANGED
|
@@ -44,6 +44,7 @@ You may want to use this library if:
|
|
|
44
44
|
3. Use tokens (separators) to reshape prefixes and suffixes
|
|
45
45
|
4. Accept customized parsers (do it yourself)
|
|
46
46
|
5. Parse non-standard name cases
|
|
47
|
+
6. Build a name on the fly (via a builder)
|
|
47
48
|
|
|
48
49
|
## Installation
|
|
49
50
|
|
|
@@ -65,10 +66,10 @@ This package is also available in [Angular](https://angular.io/) and
|
|
|
65
66
|
|
|
66
67
|
## Usage
|
|
67
68
|
|
|
68
|
-
See
|
|
69
|
+
See [examples] or [test cases][test-cases] for more details.
|
|
69
70
|
|
|
70
71
|
```ts
|
|
71
|
-
import { Namefully } from 'namefully'
|
|
72
|
+
import { Namefully } from 'namefully';
|
|
72
73
|
|
|
73
74
|
const name = new Namefully('Thomas Alva Edison');
|
|
74
75
|
console.log(name.short); // Thomas Edison
|
|
@@ -78,10 +79,8 @@ console.log(name.format('L, f m')); // EDISON, Thomas Alva
|
|
|
78
79
|
console.log(name.zip()); // Thomas A. E.
|
|
79
80
|
```
|
|
80
81
|
|
|
81
|
-
>
|
|
82
|
-
> many middle names or last names,
|
|
83
|
-
> using `Namefully.parse()` lets you parse names containing many middle names
|
|
84
|
-
> with the risk of throwing a `NameError` when the parsing is not possible.
|
|
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.
|
|
85
84
|
|
|
86
85
|
## `Config` and default values
|
|
87
86
|
|
|
@@ -112,7 +111,7 @@ const name2 = new Namefully(['Edison', 'Thomas'], { orderedBy: NameOrder.LAST_NA
|
|
|
112
111
|
console.log(name2.first); // Thomas
|
|
113
112
|
```
|
|
114
113
|
|
|
115
|
-
>
|
|
114
|
+
> **Note** that this option also affects all the other results of the API. In other
|
|
116
115
|
> words, the results will prioritize the order of appearance set in the first
|
|
117
116
|
> place for the other operations. Keep in mind that in some cases, it can be
|
|
118
117
|
> altered on the go. See the example below.
|
|
@@ -165,11 +164,10 @@ console.log(name.prefix); // Mr.
|
|
|
165
164
|
Sets an ending character after the full name (a comma before the suffix actually).
|
|
166
165
|
|
|
167
166
|
```ts
|
|
168
|
-
const name = new Namefully(
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}, { ending: true })
|
|
167
|
+
const name = new Namefully(
|
|
168
|
+
{ firstName: 'John', lastName: 'Smith', suffix: 'Ph.D'},
|
|
169
|
+
{ ending: true },
|
|
170
|
+
)
|
|
173
171
|
console.log(name.full) // John Smith, Ph.D
|
|
174
172
|
console.log(name.suffix) // Ph.D
|
|
175
173
|
```
|
|
@@ -182,8 +180,8 @@ Defines the distinct formats to output a compound surname (e.g., Hispanic surnam
|
|
|
182
180
|
|
|
183
181
|
```ts
|
|
184
182
|
const name = new Namefully(
|
|
185
|
-
|
|
186
|
-
|
|
183
|
+
[new FirstName('John'), new LastName('Doe', 'Smith')],
|
|
184
|
+
{ surname: Surname.HYPHENATED },
|
|
187
185
|
);
|
|
188
186
|
console.log(name.full); // John Doe-Smith
|
|
189
187
|
```
|
|
@@ -209,12 +207,12 @@ To sum it all up, the default values are:
|
|
|
209
207
|
|
|
210
208
|
```ts
|
|
211
209
|
{
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
210
|
+
orderedBy: NameOrder.FIRST_NAME,
|
|
211
|
+
separator: Separator.SPACE,
|
|
212
|
+
title: Title.UK,
|
|
213
|
+
ending: false,
|
|
214
|
+
bypass: true,
|
|
215
|
+
surname: Surname.FATHER
|
|
218
216
|
}
|
|
219
217
|
```
|
|
220
218
|
|
|
@@ -223,23 +221,47 @@ To sum it all up, the default values are:
|
|
|
223
221
|
Customize your own parser to indicate the full name yourself.
|
|
224
222
|
|
|
225
223
|
```ts
|
|
226
|
-
import { Config, FullName, Namefully, Parser } from 'namefully'
|
|
224
|
+
import { Config, FullName, Namefully, Parser } from 'namefully';
|
|
227
225
|
|
|
228
226
|
// Suppose you want to cover this '#' separator
|
|
229
227
|
class SimpleParser extends Parser<string> {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
228
|
+
parse(options: Partial<Config>): FullName {
|
|
229
|
+
const [firstName, lastName] = this.raw.split('#');
|
|
230
|
+
return FullName.parse({ firstName, lastName }, Config.merge(options));
|
|
231
|
+
}
|
|
234
232
|
}
|
|
235
233
|
|
|
236
234
|
const name = new Namefully(new SimpleParser('Juan#Garcia'));
|
|
237
235
|
console.log(name.full); // Juan Garcia
|
|
238
236
|
```
|
|
239
237
|
|
|
238
|
+
**Or** use `NameIndex` to specify where the name parts are located in a text.
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
import { Namefully, NameIndex } from 'namefully';
|
|
242
|
+
|
|
243
|
+
const indexing = NameIndex.only({ firstName: 0, lastName: 3 });
|
|
244
|
+
const name = Namefully.tryParse('Dwayne "The Rock" Johnson', indexing);
|
|
245
|
+
console.log(name.full); // Dwayne Johnson
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**Or** simply use `NameBuilder` to build a name on the fly (with lifecycle hooks if needed).
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
import { Name, NameBuilder } from 'namefully';
|
|
252
|
+
|
|
253
|
+
const builder = NameBuilder.of(Name.first('Nikola')); // can be more than one name
|
|
254
|
+
builder.add(Name.last('Tesla'));
|
|
255
|
+
builder.add(Name.prefix('Mr'));
|
|
256
|
+
|
|
257
|
+
const name = builder.build() // with options if needed
|
|
258
|
+
console.log(name.full); // Mr Nikola Tesla
|
|
259
|
+
```
|
|
260
|
+
|
|
240
261
|
## Concepts and examples
|
|
241
262
|
|
|
242
|
-
The name standards
|
|
263
|
+
The name standards (inspired by this [UK name guide][name-standards]) used for
|
|
264
|
+
the current version of this library are as follows:
|
|
243
265
|
|
|
244
266
|
`[prefix] firstName [middleName] lastName [suffix]`
|
|
245
267
|
|
|
@@ -257,7 +279,7 @@ Once imported, all that is required to do is to create an instance of
|
|
|
257
279
|
|
|
258
280
|
### Basic cases
|
|
259
281
|
|
|
260
|
-
Let us take a common example:
|
|
282
|
+
Let us take a common example with all the parts:
|
|
261
283
|
|
|
262
284
|
`Mr John Joe Smith PhD`
|
|
263
285
|
|
|
@@ -274,23 +296,24 @@ So, this utility understands the name parts as follows:
|
|
|
274
296
|
- flattened: `John J. S.`
|
|
275
297
|
- initials: `J J S`
|
|
276
298
|
- public: `John S`
|
|
299
|
+
- salutation: `Mr Smith`
|
|
277
300
|
|
|
278
301
|
### Limitations
|
|
279
302
|
|
|
280
|
-
`namefully` does not
|
|
303
|
+
`namefully` does not support certain use cases:
|
|
281
304
|
|
|
282
|
-
- mononame: `Plato
|
|
283
|
-
-
|
|
305
|
+
- mononame: `Plato` - a workaround is to set the mononame as both first and last name;
|
|
306
|
+
- nickname: `Dwayne "The Rock" Johnson` - use custom parser instead.
|
|
307
|
+
- multiple prefixes or suffixes: `Prof. Dr. Einstein`.
|
|
284
308
|
|
|
285
|
-
|
|
309
|
+
## Contributing
|
|
286
310
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
Developed by [Ralph Florent](https://github.com/ralflorent).
|
|
311
|
+
Visit [CONTRIBUTING.md][contributing-url] for details on the contribution guidelines,
|
|
312
|
+
the code of conduct, and the process for submitting pull requests.
|
|
290
313
|
|
|
291
314
|
## License
|
|
292
315
|
|
|
293
|
-
The underlying content of this utility is licensed under [MIT License]
|
|
316
|
+
The underlying content of this utility is licensed under [MIT License][license-url].
|
|
294
317
|
|
|
295
318
|
<!-- References -->
|
|
296
319
|
|
|
@@ -304,3 +327,8 @@ The underlying content of this utility is licensed under [MIT License](LICENSE).
|
|
|
304
327
|
[codecov-url]: https://codecov.io/gh/ralflorent/namefully
|
|
305
328
|
[license-img]: https://img.shields.io/npm/l/namefully
|
|
306
329
|
[license-url]: https://opensource.org/licenses/MIT
|
|
330
|
+
|
|
331
|
+
[contributing-url]: https://github.com/ralflorent/namefully/blob/main/CONTRIBUTING.md
|
|
332
|
+
[examples]: https://github.com/ralflorent/namefully/tree/main/example
|
|
333
|
+
[test-cases]: https://github.com/ralflorent/namefully/tree/main/src/namefully.spec.ts
|
|
334
|
+
[name-standards]: https://www.fbiic.gov/public/2008/nov/Naming_practice_guide_UK_2006.pdf
|
package/dist/lib/config.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
|
-
var _Config_instances, _a, _Config_name, _Config_orderedBy, _Config_separator, _Config_title, _Config_ending, _Config_bypass, _Config_surname, _Config_genNewName;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.Config = void 0;
|
|
16
|
-
const types_1 = require("./types");
|
|
17
|
-
const defaultName = 'default';
|
|
18
|
-
const copyAlias = '_copy';
|
|
19
|
-
class Config {
|
|
20
|
-
get orderedBy() {
|
|
21
|
-
return __classPrivateFieldGet(this, _Config_orderedBy, "f");
|
|
22
|
-
}
|
|
23
|
-
get separator() {
|
|
24
|
-
return __classPrivateFieldGet(this, _Config_separator, "f");
|
|
25
|
-
}
|
|
26
|
-
get title() {
|
|
27
|
-
return __classPrivateFieldGet(this, _Config_title, "f");
|
|
28
|
-
}
|
|
29
|
-
get ending() {
|
|
30
|
-
return __classPrivateFieldGet(this, _Config_ending, "f");
|
|
31
|
-
}
|
|
32
|
-
get bypass() {
|
|
33
|
-
return __classPrivateFieldGet(this, _Config_bypass, "f");
|
|
34
|
-
}
|
|
35
|
-
get surname() {
|
|
36
|
-
return __classPrivateFieldGet(this, _Config_surname, "f");
|
|
37
|
-
}
|
|
38
|
-
get name() {
|
|
39
|
-
return __classPrivateFieldGet(this, _Config_name, "f");
|
|
40
|
-
}
|
|
41
|
-
constructor(name, orderedBy = types_1.NameOrder.FIRST_NAME, separator = types_1.Separator.SPACE, title = types_1.Title.UK, ending = false, bypass = true, surname = types_1.Surname.FATHER) {
|
|
42
|
-
_Config_instances.add(this);
|
|
43
|
-
_Config_name.set(this, void 0);
|
|
44
|
-
_Config_orderedBy.set(this, void 0);
|
|
45
|
-
_Config_separator.set(this, void 0);
|
|
46
|
-
_Config_title.set(this, void 0);
|
|
47
|
-
_Config_ending.set(this, void 0);
|
|
48
|
-
_Config_bypass.set(this, void 0);
|
|
49
|
-
_Config_surname.set(this, void 0);
|
|
50
|
-
__classPrivateFieldSet(this, _Config_name, name, "f");
|
|
51
|
-
__classPrivateFieldSet(this, _Config_orderedBy, orderedBy, "f");
|
|
52
|
-
__classPrivateFieldSet(this, _Config_separator, separator, "f");
|
|
53
|
-
__classPrivateFieldSet(this, _Config_title, title, "f");
|
|
54
|
-
__classPrivateFieldSet(this, _Config_ending, ending, "f");
|
|
55
|
-
__classPrivateFieldSet(this, _Config_bypass, bypass, "f");
|
|
56
|
-
__classPrivateFieldSet(this, _Config_surname, surname, "f");
|
|
57
|
-
}
|
|
58
|
-
static create(name = defaultName) {
|
|
59
|
-
if (!_a.cache.has(name))
|
|
60
|
-
_a.cache.set(name, new this(name));
|
|
61
|
-
return _a.cache.get(name);
|
|
62
|
-
}
|
|
63
|
-
static merge(other) {
|
|
64
|
-
var _b, _c, _d, _e, _f, _g;
|
|
65
|
-
if (!other) {
|
|
66
|
-
return _a.create();
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
const config = _a.create(other.name);
|
|
70
|
-
__classPrivateFieldSet(config, _Config_orderedBy, (_b = other.orderedBy) !== null && _b !== void 0 ? _b : config.orderedBy, "f");
|
|
71
|
-
__classPrivateFieldSet(config, _Config_separator, (_c = other.separator) !== null && _c !== void 0 ? _c : config.separator, "f");
|
|
72
|
-
__classPrivateFieldSet(config, _Config_title, (_d = other.title) !== null && _d !== void 0 ? _d : config.title, "f");
|
|
73
|
-
__classPrivateFieldSet(config, _Config_ending, (_e = other.ending) !== null && _e !== void 0 ? _e : config.ending, "f");
|
|
74
|
-
__classPrivateFieldSet(config, _Config_bypass, (_f = other.bypass) !== null && _f !== void 0 ? _f : config.bypass, "f");
|
|
75
|
-
__classPrivateFieldSet(config, _Config_surname, (_g = other.surname) !== null && _g !== void 0 ? _g : config.surname, "f");
|
|
76
|
-
return config;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
copyWith(options = {}) {
|
|
80
|
-
const { name, orderedBy, separator, title, ending, bypass, surname } = options;
|
|
81
|
-
const config = _a.create(__classPrivateFieldGet(this, _Config_instances, "m", _Config_genNewName).call(this, name !== null && name !== void 0 ? name : this.name + copyAlias));
|
|
82
|
-
__classPrivateFieldSet(config, _Config_orderedBy, orderedBy !== null && orderedBy !== void 0 ? orderedBy : this.orderedBy, "f");
|
|
83
|
-
__classPrivateFieldSet(config, _Config_separator, separator !== null && separator !== void 0 ? separator : this.separator, "f");
|
|
84
|
-
__classPrivateFieldSet(config, _Config_title, title !== null && title !== void 0 ? title : this.title, "f");
|
|
85
|
-
__classPrivateFieldSet(config, _Config_ending, ending !== null && ending !== void 0 ? ending : this.ending, "f");
|
|
86
|
-
__classPrivateFieldSet(config, _Config_bypass, bypass !== null && bypass !== void 0 ? bypass : this.bypass, "f");
|
|
87
|
-
__classPrivateFieldSet(config, _Config_surname, surname !== null && surname !== void 0 ? surname : this.surname, "f");
|
|
88
|
-
return config;
|
|
89
|
-
}
|
|
90
|
-
clone() {
|
|
91
|
-
return this.copyWith();
|
|
92
|
-
}
|
|
93
|
-
reset() {
|
|
94
|
-
__classPrivateFieldSet(this, _Config_orderedBy, types_1.NameOrder.FIRST_NAME, "f");
|
|
95
|
-
__classPrivateFieldSet(this, _Config_separator, types_1.Separator.SPACE, "f");
|
|
96
|
-
__classPrivateFieldSet(this, _Config_title, types_1.Title.UK, "f");
|
|
97
|
-
__classPrivateFieldSet(this, _Config_ending, false, "f");
|
|
98
|
-
__classPrivateFieldSet(this, _Config_bypass, true, "f");
|
|
99
|
-
__classPrivateFieldSet(this, _Config_surname, types_1.Surname.FATHER, "f");
|
|
100
|
-
_a.cache.set(this.name, this);
|
|
101
|
-
}
|
|
102
|
-
updateOrder(order) {
|
|
103
|
-
if (order && order !== __classPrivateFieldGet(this, _Config_orderedBy, "f")) {
|
|
104
|
-
__classPrivateFieldSet(_a.cache.get(this.name), _Config_orderedBy, order, "f");
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.Config = Config;
|
|
109
|
-
_a = Config, _Config_name = new WeakMap(), _Config_orderedBy = new WeakMap(), _Config_separator = new WeakMap(), _Config_title = new WeakMap(), _Config_ending = new WeakMap(), _Config_bypass = new WeakMap(), _Config_surname = new WeakMap(), _Config_instances = new WeakSet(), _Config_genNewName = function _Config_genNewName(name) {
|
|
110
|
-
return name === this.name || _a.cache.has(name) ? __classPrivateFieldGet(this, _Config_instances, "m", _Config_genNewName).call(this, name + copyAlias) : name;
|
|
111
|
-
};
|
|
112
|
-
Config.cache = new Map();
|
package/dist/lib/full-name.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _FullName_prefix, _FullName_firstName, _FullName_middleName, _FullName_lastName, _FullName_suffix, _FullName_config;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.FullName = void 0;
|
|
16
|
-
const config_1 = require("./config");
|
|
17
|
-
const error_1 = require("./error");
|
|
18
|
-
const name_1 = require("./name");
|
|
19
|
-
const types_1 = require("./types");
|
|
20
|
-
const validator_1 = require("./validator");
|
|
21
|
-
class FullName {
|
|
22
|
-
constructor(options) {
|
|
23
|
-
_FullName_prefix.set(this, void 0);
|
|
24
|
-
_FullName_firstName.set(this, void 0);
|
|
25
|
-
_FullName_middleName.set(this, []);
|
|
26
|
-
_FullName_lastName.set(this, void 0);
|
|
27
|
-
_FullName_suffix.set(this, void 0);
|
|
28
|
-
_FullName_config.set(this, void 0);
|
|
29
|
-
__classPrivateFieldSet(this, _FullName_config, config_1.Config.merge(options), "f");
|
|
30
|
-
}
|
|
31
|
-
get config() {
|
|
32
|
-
return __classPrivateFieldGet(this, _FullName_config, "f");
|
|
33
|
-
}
|
|
34
|
-
get prefix() {
|
|
35
|
-
return __classPrivateFieldGet(this, _FullName_prefix, "f");
|
|
36
|
-
}
|
|
37
|
-
get firstName() {
|
|
38
|
-
return __classPrivateFieldGet(this, _FullName_firstName, "f");
|
|
39
|
-
}
|
|
40
|
-
get lastName() {
|
|
41
|
-
return __classPrivateFieldGet(this, _FullName_lastName, "f");
|
|
42
|
-
}
|
|
43
|
-
get middleName() {
|
|
44
|
-
return __classPrivateFieldGet(this, _FullName_middleName, "f");
|
|
45
|
-
}
|
|
46
|
-
get suffix() {
|
|
47
|
-
return __classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
48
|
-
}
|
|
49
|
-
static parse(json, config) {
|
|
50
|
-
try {
|
|
51
|
-
const fullName = new FullName(config);
|
|
52
|
-
fullName.setPrefix(json.prefix);
|
|
53
|
-
fullName.setFirstName(json.firstName);
|
|
54
|
-
fullName.setMiddleName(json.middleName);
|
|
55
|
-
fullName.setLastName(json.lastName);
|
|
56
|
-
fullName.setSuffix(json.suffix);
|
|
57
|
-
return fullName;
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
if (error instanceof error_1.NameError)
|
|
61
|
-
throw error;
|
|
62
|
-
throw new error_1.UnknownError({
|
|
63
|
-
source: Object.values(json).join(' '),
|
|
64
|
-
message: 'could not parse JSON content',
|
|
65
|
-
error,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
setPrefix(name) {
|
|
70
|
-
if (!name)
|
|
71
|
-
return this;
|
|
72
|
-
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
73
|
-
validator_1.Validators.prefix.validate(name);
|
|
74
|
-
const prefix = name instanceof name_1.Name ? name.value : name;
|
|
75
|
-
__classPrivateFieldSet(this, _FullName_prefix, name_1.Name.prefix(__classPrivateFieldGet(this, _FullName_config, "f").title === types_1.Title.US ? `${prefix}.` : prefix), "f");
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
setFirstName(name) {
|
|
79
|
-
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
80
|
-
validator_1.Validators.firstName.validate(name);
|
|
81
|
-
__classPrivateFieldSet(this, _FullName_firstName, name instanceof name_1.FirstName ? name : new name_1.FirstName(name), "f");
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
setLastName(name) {
|
|
85
|
-
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
86
|
-
validator_1.Validators.lastName.validate(name);
|
|
87
|
-
__classPrivateFieldSet(this, _FullName_lastName, name instanceof name_1.LastName ? name : new name_1.LastName(name), "f");
|
|
88
|
-
return this;
|
|
89
|
-
}
|
|
90
|
-
setMiddleName(names) {
|
|
91
|
-
if (!Array.isArray(names))
|
|
92
|
-
return this;
|
|
93
|
-
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
94
|
-
validator_1.Validators.middleName.validate(names);
|
|
95
|
-
__classPrivateFieldSet(this, _FullName_middleName, names.map((name) => (name instanceof name_1.Name ? name : name_1.Name.middle(name))), "f");
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
setSuffix(name) {
|
|
99
|
-
if (!name)
|
|
100
|
-
return this;
|
|
101
|
-
if (!__classPrivateFieldGet(this, _FullName_config, "f").bypass)
|
|
102
|
-
validator_1.Validators.suffix.validate(name);
|
|
103
|
-
__classPrivateFieldSet(this, _FullName_suffix, name_1.Name.suffix(name instanceof name_1.Name ? name.value : name), "f");
|
|
104
|
-
return this;
|
|
105
|
-
}
|
|
106
|
-
has(namon) {
|
|
107
|
-
if (namon.equal(types_1.Namon.PREFIX))
|
|
108
|
-
return !!__classPrivateFieldGet(this, _FullName_prefix, "f");
|
|
109
|
-
if (namon.equal(types_1.Namon.SUFFIX))
|
|
110
|
-
return !!__classPrivateFieldGet(this, _FullName_suffix, "f");
|
|
111
|
-
return namon.equal(types_1.Namon.MIDDLE_NAME) ? __classPrivateFieldGet(this, _FullName_middleName, "f").length > 0 : true;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
exports.FullName = FullName;
|
|
115
|
-
_FullName_prefix = new WeakMap(), _FullName_firstName = new WeakMap(), _FullName_middleName = new WeakMap(), _FullName_lastName = new WeakMap(), _FullName_suffix = new WeakMap(), _FullName_config = new WeakMap();
|