@foundernest/namecase
Version:
A fully typed library for fixing capitalization of people's names. Based on tamtamchik PHP library.
3 lines (2 loc) • 7.65 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).namecase={})}(this,function(t){function e(){return(e=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t}).apply(this,arguments)}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}function i(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(i)return(i=i.call(t)).next.bind(i);if(Array.isArray(t)||(i=function(t,e){if(t){if("string"==typeof t)return n(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?n(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var a=0;return function(){return a>=t.length?{done:!0}:{done:!1,value:t[a++]}}}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 t(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 n=t.prototype;return n.setOptions=function(t){this.options=e({},this.options,t)},n.backupOptions=function(){this.bckOptions=e({},this.options)},n.restoreOptions=function(){this.options=e({},this.bckOptions)},n.excludePostNominals=function(t){this.postNominalsExcluded=this.postNominalsExcluded.concat(t)},n.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 n,a=i(this.getReplacements());!(n=a()).done;){var o=n.value;t=t.replace(o[0],o[1])}return t=this.processOptions(t),this.restoreOptions(),t},n.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},n.capitalizeFirstLetters=function(t){return(t=t.toLowerCase()).replace(/([\s,.:;"'(-]|^)([^\s,.:;"'(-])/g,function(){var t=[].slice.call(arguments);return t[1]+t[2].toUpperCase()})},n.lowercaseFinalS=function(t){return t.replace(/'([^\s,.:;"'(-])([\s,.:;"'(-]|$)/g,function(){var t=[].slice.call(arguments);return"'"+t[1].toLowerCase()+t[2]})},n.getReplacements=function(){var t=this.REPLACEMENTS;return this.options.hebrew&&(t=t.concat(this.HEBREW)),t},n.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},n.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,n=i(this.EXCEPTIONS);!(e=n()).done;){var a=e.value;t=t.replace(a[0],a[1])}return t},n.updateRoman=function(t){return t.replace(this.ROMAN_REGEX,function(){return[].slice.call(arguments)[0].toUpperCase()})},n.updateSpanish=function(t){for(var e,n=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]})},a=i(this.CONJUNCTIONS);!(e=a()).done;)n();return t},n.fixPostNominal=function(t){for(var e,n=this,a=function(){var i=e.value;t=t.replace(new RegExp("([\\s,.:;\"'-(]|^)"+n.capitalizeFirstLetters(i)+"([\\s,.:;\"'-(]|$)","g"),function(){var t=[].slice.call(arguments);return t[1]+i+t[2]})},o=i(this.POST_NOMINALS.filter(function(t){return!n.postNominalsExcluded.includes(t)}));!(e=o()).done;)a();return t},n.skipMixed=function(t){var e=t[0]===t[0].toLowerCase(),n=t.toLowerCase()===t||t.toUpperCase()===t;return!(e||n)},t}(),o=new a({});t.default=a,t.excludePostNominals=function(t){return o.excludePostNominals(t)},t.nameCase=function(t,e){return o.nameCase(t,e)},t.setOptions=function(t){return o.setOptions(t)}});
//# sourceMappingURL=index.umd.js.map