microsoft-capitalize
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -5,2 +5,9 @@ # Changelog | ||
### [1.0.3](https://github.com/Kikobeats/microsoft-capitalize/compare/v1.0.2...v1.0.3) (2020-08-01) | ||
### Bug Fixes | ||
* ensure first word is capitalized ([18b9eef](https://github.com/Kikobeats/microsoft-capitalize/commit/18b9eef4eac0faa5d714d89446da27e2f2375357)) | ||
### [1.0.2](https://github.com/Kikobeats/microsoft-capitalize/compare/v1.0.1...v1.0.2) (2020-07-24) | ||
@@ -7,0 +14,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://nicedoc.io/Kikobeats/microsoft-capitalize", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "src/index.js", | ||
@@ -8,0 +8,0 @@ "author": { |
'use strict' | ||
const capitalize = str => | ||
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase() | ||
const head = str => str.charAt(0) | ||
const tail = str => str.slice(1) | ||
const upperHead = str => head(str).toUpperCase() | ||
const capitalize = str => upperHead(str) + tail(str).toLowerCase() | ||
const isUpperCaseWord = str => | ||
@@ -18,9 +23,14 @@ str.split('').every(letter => letter === letter.toUpperCase()) | ||
// respect special words | ||
exceptions.forEach(word => { | ||
const re = new RegExp(`\\b(?:${word})\\b`, 'gi') | ||
if (re.test(str)) { | ||
title = title.replace(re, word) | ||
} | ||
}) | ||
if (exceptions.length) { | ||
exceptions.forEach(word => { | ||
const re = new RegExp(`\\b(?:${word})\\b`, 'gi') | ||
if (re.test(str)) { | ||
title = title.replace(re, word) | ||
} | ||
}) | ||
// still be sure first word is capitalized even it's an exception | ||
title = upperHead(title) + tail(title) | ||
} | ||
// respect uppercase words | ||
@@ -27,0 +37,0 @@ title = title.split(' ') |
8113
40