@foundernest/namecase
Version:
A fully typed library for fixing capitalization of people's names. Based on tamtamchik PHP library.
3 lines (2 loc) • 7.49 kB
JavaScript
function t(){return(t=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var a in n)Object.prototype.hasOwnProperty.call(n,a)&&(t[a]=n[a])}return t}).apply(this,arguments)}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,a=new Array(e);n<e;n++)a[n]=t[n];return a}function n(t,n){var a="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(a)return(a=a.call(t)).next.bind(a);if(Array.isArray(t)||(a=function(t,n){if(t){if("string"==typeof t)return e(t,n);var a=Object.prototype.toString.call(t).slice(8,-1);return"Object"===a&&t.constructor&&(a=t.constructor.name),"Map"===a||"Set"===a?Array.from(t):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?e(t,n):void 0}}(t))||n&&t&&"number"==typeof t.length){a&&(t=a);var i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=function(){function e(t){this.EXCEPTIONS=[[/\bMacEdo/,"Macedo"],[/\bMacEvicius/,"Macevicius"],[/\bMacHado/,"Machado"],[/\bMacHar/,"Machar"],[/\bMacHin/,"Machin"],[/\bMacHlin/,"Machlin"],[/\bMacIas/,"Macias"],[/\bMacIulis/,"Maciulis"],[/\bMacKie/,"Mackie"],[/\bMacKle/,"Mackle"],[/\bMacKlin/,"Macklin"],[/\bMacKmin/,"Mackmin"],[/\bMacQuarie/,"Macquarie"],[/\bMacOmber/,"Macomber"],[/\bMacIn/,"Macin"],[/\bMacKintosh/,"Mackintosh"],[/\bMacKen/,"Macken"],[/\bMacHen/,"Machen"],[/\bMacisaac/,"MacIsaac"],[/\bMacHiel/,"Machiel"],[/\bMacIol/,"Maciol"],[/\bMacKell/,"Mackell"],[/\bMacKlem/,"Macklem"],[/\bMacKrell/,"Mackrell"],[/\bMacLin/,"Maclin"],[/\bMacKey/,"Mackey"],[/\bMacKley/,"Mackley"],[/\bMacHell/,"Machell"],[/\bMacHon/,"Machon"]],this.REPLACEMENTS=[[/\b(Al)(\s+\w)/,"al$2"],[/\b(Ap)\b/,"ap"],[/\b(Bin|Binti|Binte)\b/,"bin"],[/\bDell([ae])\b/,"dell$1"],[/\bD([aeiou])\b/,"d$1"],[/\bD([ao]s)\b/,"d$1"],[/\bDe([lrn])\b/,"de$1"],[/\bL([eo])\b/,"l$1"],[/\b(El)\b/,"el"],[/\b(La)\b/,"la"],[/\b(Te)([rn])\b/,"te$2"],[/\b(Van)(\s+\w)/,"van$2"],[/\b(Von)\b/,"von"]],this.HEBREW=[[/\b(Ben)(\s+\w)/,"ben$2"],[/\b(Bat)(\s+\w)/,"bat$2"]],this.CONJUNCTIONS=["Y","E","I"],this.ROMAN_REGEX=/\b((?:[Xx]{1,3}|[Xx][Ll]|[Ll][Xx]{0,3})?(?:[Ii]{1,3}|[Ii][VvXx]|[Vv][Ii]{0,3})?)\b/g,this.POST_NOMINALS=["ACILEx","ACSM","ADC","AEPC","AFC","AFM","AICSM","AKC","AM","ARBRIBA","ARCS","ARRC","ARSM","AUH","AUS","BA","BArch","BCh","BChir","BCL","BDS","BEd","BEM","BEng","BM","BS","BSc","BSW","BVM&S","BVScBVetMed","CB","CBE","CEng","CertHE","CGC","CGM","CH","CIE","CMarEngCMarSci","CMarTech","CMG","CMILT","CML","CPhT","CPLCTP","CPM","CQSW","CSciTeach","CSI","CTL","CVO","DBE","DBEnv","DC","DCB","DCM","DCMG","DConstMgt","DCVO","DD","DEM","DFC","DFM","DIC","Dip","DipHE","DipLP","DipSW","DL","DLitt","DLP","DPhil","DProf","DPT","DREst","DSC","DSM","DSO","DSocSci","ED","EdD","EJLog","EMLog","EN","EngD","EngTech","ERD","ESLog","FADO","FAWM","FBDOFCOptom","FCEM","FCILEx","FCILT","FCSP.","FdAFdSc","FdEng","FFHOM","FFPM","FRCAFFPMRCA","FRCGP","FRCOG","FRCP","FRCPsych","FRCS","FRCVS","FSCR.","GBE","GC","GCB","GCIE","GCILEx","GCMG","GCSI","GCVO","GM","HNC","HNCert","HND","HNDip","ICTTech","IDSM","IEng","IMarEng","IOMCPM","ISO","J","JP","JrLog","KBE","KC","KCB","KCIE","KCMmG","KCSI","KCVO","KG","KP","KT","LFHOM","LG","LJ","LLB","LLD","LLM","Log","LPE","LT","LVO","MA","MAcc","MAnth","MArch","MarEngTech","MB","MBA","MBChB","MBE","MBEIOM","MBiochem","MC","MCEM","MCGI","MCh.","MChem","MChiro","MClinRes","MComp","MCOptom","MCSM","MCSP","MD","MEarthSc","MEng","MEnt","MEP","MFHOM","MFin","MFPM","MGeol","MILT","MJur","MLA","MLitt","MM","MMath","MMathStat","MMORSE","MMus","MOst","MP","MPAMEd","MPharm","MPhil","MPhys","MRCGP","MRCOG","MRCP","MRCPath","MRCPCHFRCPCH","MRCPsych","MRCS","MRCVS","MRes","MS","MSc","MScChiro","MSci","MSCR","MSM","MSocSc","MSP","MSt","MSW","MSYP","MVO","NPQH","OBE","OBI","OM","OND","PgC","PGCAP","PGCE","PgCert","PGCHE","PgCLTHE","PgD","PGDE","PgDip","PhD","PLog","PLS","QAM","QC","QFSM","QGM","QHC","QHDS","QHNS","QHP","QHS","QPM","QS","QTSCSci","RD","RFHN","RGN","RHV","RIAI","RIAS","RM","RMN","RN","RN1RNA","RN2","RN3","RN4","RN5","RN6","RN7","RN8","RN9","RNC","RNLD","RNMH","ROH","RRC","RSAW","RSci","RSciTech","RSCN","RSN","RVM","RVN","SCHM","SCJ","SCLD","SEN","SGM","SL","SPANSPMH","SPCC","SPCN","SPDN","SPHP","SPLD","SrLog","SRN","SROT","TD","UD","V100","V200","V300","VC","VD","VetMB","VN","VRD"],this.postNominalsExcluded=[],this.options={lazy:!0,irish:!0,spanish:!0,roman:!0,hebrew:!0,postnominal:!0},this.bckOptions={},this.setOptions(t)}var a=e.prototype;return a.setOptions=function(e){this.options=t({},this.options,e)},a.backupOptions=function(){this.bckOptions=t({},this.options)},a.restoreOptions=function(){this.options=t({},this.bckOptions)},a.excludePostNominals=function(t){this.postNominalsExcluded=this.postNominalsExcluded.concat(t)},a.nameCase=function(t,e){if(void 0===e&&(e={}),""===t)return t;if(this.backupOptions(),this.setOptions(e),this.options.lazy&&this.skipMixed(t))return t;t=this.capitalizeFirstLetters(t),t=this.lowercaseFinalS(t),t=this.updateIrish(t);for(var a,i=n(this.getReplacements());!(a=i()).done;){var r=a.value;t=t.replace(r[0],r[1])}return t=this.processOptions(t),this.restoreOptions(),t},a.processOptions=function(t){return this.options.roman&&(t=this.updateRoman(t)),this.options.spanish&&(t=this.updateSpanish(t)),this.options.postnominal&&(t=this.fixPostNominal(t)),t},a.capitalizeFirstLetters=function(t){return(t=t.toLowerCase()).replace(/([\s,.:;"'(-]|^)([^\s,.:;"'(-])/g,function(){var t=[].slice.call(arguments);return t[1]+t[2].toUpperCase()})},a.lowercaseFinalS=function(t){return t.replace(/'([^\s,.:;"'(-])([\s,.:;"'(-]|$)/g,function(){var t=[].slice.call(arguments);return"'"+t[1].toLowerCase()+t[2]})},a.getReplacements=function(){var t=this.REPLACEMENTS;return this.options.hebrew&&(t=t.concat(this.HEBREW)),t},a.updateIrish=function(t){return this.options.irish?((/.*?\bMac[A-Za-z]{2,}[^aciozj]\b/.test(t)||/.*?\bMc/.test(t))&&(t=this.updateMac(t)),t.replace(/\bMacmurdo/,"MacMurdo").replace(/\bMacisaac/,"MacIsaac")):t},a.updateMac=function(t){t=t.replace(/\b(Ma?c)([A-Za-z]+)/,function(){var t=[].slice.call(arguments);return t[1]+t[2].charAt(0).toUpperCase()+t[2].substring(1)});for(var e,a=n(this.EXCEPTIONS);!(e=a()).done;){var i=e.value;t=t.replace(i[0],i[1])}return t},a.updateRoman=function(t){return t.replace(this.ROMAN_REGEX,function(){return[].slice.call(arguments)[0].toUpperCase()})},a.updateSpanish=function(t){for(var e,a=function(){var n=e.value;t=t.replace(new RegExp("([\\s,.:;\"'-(]|^)"+n+"([\\s,.:;\"'-(]|$)","g"),function(){var t=[].slice.call(arguments);return t[1]+n.toLowerCase()+t[2]})},i=n(this.CONJUNCTIONS);!(e=i()).done;)a();return t},a.fixPostNominal=function(t){for(var e,a=this,i=function(){var n=e.value;t=t.replace(new RegExp("([\\s,.:;\"'-(]|^)"+a.capitalizeFirstLetters(n)+"([\\s,.:;\"'-(]|$)","g"),function(){var t=[].slice.call(arguments);return t[1]+n+t[2]})},r=n(this.POST_NOMINALS.filter(function(t){return!a.postNominalsExcluded.includes(t)}));!(e=r()).done;)i();return t},a.skipMixed=function(t){var e=t[0]===t[0].toLowerCase(),n=t.toLowerCase()===t||t.toUpperCase()===t;return!(e||n)},e}(),i=new a({});exports.default=a,exports.excludePostNominals=function(t){return i.excludePostNominals(t)},exports.nameCase=function(t,e){return i.nameCase(t,e)},exports.setOptions=function(t){return i.setOptions(t)};
//# sourceMappingURL=index.js.map