string-title-case
Advanced tools
Comparing version 1.0.0 to 2.0.0
47
index.js
/** | ||
* Title Case a String whilst Excluding Certain Words | ||
* @param {string} title String to be "title cased" | ||
* @param {array} exclusions Array of strings to be excluded from title casing [optional] | ||
* @return {string} Title-cased string | ||
* @param {string} title String to be "title cased" | ||
* @param {array} exclusions Array of strings to be excluded from title casing [optional] | ||
* @return {string} Title-cased string | ||
*/ | ||
const titleCase = (title, exclusions = []) => { | ||
return title | ||
.trim() | ||
.toLowerCase() | ||
.split(' ') | ||
.map(function(str, i) { | ||
if (exclusions.indexOf(str) === -1) | ||
return capitalizeFirstChar(str); | ||
else | ||
return str; | ||
}) | ||
.join(' '); | ||
} | ||
return title | ||
.trim() | ||
.toLowerCase() | ||
.split(' ') | ||
.map(function filterWords(word) { | ||
if (exclusions.includes(word)) | ||
return word; | ||
else | ||
return capitaliseFirstChar(word); | ||
}) | ||
.join(' '); | ||
}; | ||
const capitalizeFirstChar = word => { | ||
return word.split('').map(function(w,i) { | ||
return i == 0 ? w.toUpperCase() : w; | ||
}).join(''); | ||
} | ||
/** | ||
* Capitalise the first character in a string | ||
* @param word | ||
* @returns {string} | ||
*/ | ||
const capitaliseFirstChar = (word) => { | ||
return word.split('').map(function(w,i) { | ||
return i == 0 ? w.toUpperCase() : w; | ||
}).join(''); | ||
}; | ||
exports.titleCase = titleCase; | ||
module.exports = titleCase; |
{ | ||
"name": "string-title-case", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Capitalises first character of each word in string except words from an exclusion array.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,19 +6,14 @@ # StringTitleCase - JavaScript | ||
## Installation | ||
`npm install string-title-case` | ||
```javascript | ||
npm install string-title-case | ||
``` | ||
## Usage | ||
Node.JS: | ||
```javascript | ||
const {titleCase} = require('string-title-case'); | ||
const titleCase = require('string-title-case'); | ||
console.log( titleCase("hello TO The world", ['to','the']) ); // Hello to the World | ||
console.log( titleCase("g'day mate") ); // G'day Mate | ||
``` | ||
const myString = 'This messAge IS the best!'; | ||
const myTitleCasedString = titleCase(myString, ['is', 'the']); | ||
ES6: | ||
```javascript | ||
import {titleCase} from 'string-title-case'; | ||
console.log( titleCase("hello TO The world", ['to','the']) ); // Hello to the World | ||
console.log( titleCase("g'day mate") ); // G'day Mate | ||
console.log(myTitleCasedString); // 'This Message is the Best!' | ||
``` |
@@ -1,2 +0,2 @@ | ||
const {titleCase} = require('./index.js'); | ||
const titleCase = require('./index.js'); | ||
@@ -9,2 +9,5 @@ if (titleCase("hello To The world!", ['to','the']) !== "Hello to the World!") | ||
if (titleCase("This messAge IS thE best!", ['is', 'the']) !== "This Message is the Best!") | ||
process.exit(1); | ||
process.exit(0); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17037
9
38
19