Created
June 27, 2019 03:09
-
-
Save dexterbrylle/3650193cf64702237f91b9b0dc4f2b2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JS String Utils | |
* @author Dexter Brylle Matos <[email protected]> | |
*/ | |
'use strict' | |
function toString (value) { | |
return value === null ? '' : value.toString() | |
} | |
function replaceAccents (str) { | |
str = str.toString() | |
// verify if string has accents and replace them | |
// https://stackoverflow.com/questions/23476532/check-if-string-contains-only-letters-in-javascript | |
if (str.search(/[\xC0-\xFF]/g) > -1) { | |
str = str | |
.replace(/[\xC0-\xC5]/g, 'A') | |
.replace(/[\xC6]/g, 'AE') | |
.replace(/[\xC7]/g, 'C') | |
.replace(/[\xC8-\xCB]/g, 'E') | |
.replace(/[\xCC-\xCF]/g, 'I') | |
.replace(/[\xD0]/g, 'D') | |
.replace(/[\xD1]/g, 'N') | |
.replace(/[\xD2-\xD6\xD8]/g, 'O') | |
.replace(/[\xD9-\xDC]/g, 'U') | |
.replace(/[\xDD]/g, 'Y') | |
.replace(/[\xDE]/g, 'P') | |
.replace(/[\xE0-\xE5]/g, 'a') | |
.replace(/[\xE6]/g, 'ae') | |
.replace(/[\xE7]/g, 'c') | |
.replace(/[\xE8-\xEB]/g, 'e') | |
.replace(/[\xEC-\xEF]/g, 'i') | |
.replace(/[\xF1]/g, 'n') | |
.replace(/[\xF2-\xF6\xF8]/g, 'o') | |
.replace(/[\xF9-\xFC]/g, 'u') | |
.replace(/[\xFE]/g, 'p') | |
.replace(/[\xFD\xFF]/g, 'y') | |
} | |
return str | |
} | |
function removeNonWord (str) { | |
let PATTERN = /[^\x20\x2D0-9A-Z\x5Fa-z\xC0-\xD6\xD8-\xF6\xF8-\xFF]/g | |
str = this.toString(str) | |
return str.replace(PATTERN, '') | |
} | |
// https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case | |
function camelCase (str) { | |
str = this.toString(str) | |
str = this.replaceAccents(str) | |
str = this.removeNonWord(str).replace(/[-_]/g, ' ') | |
if (/[a-z]/.test(str) && /^|\s[A-Z]+\s|$/.test(str)) { | |
str = str.replace(/\s(\w+)/g, function (word, m) { | |
return /^[A-Z]+$/.test(m) ? word : this.lowerCase(word) | |
}) | |
} else if (/\s/.test(str)) { | |
str = this.lowerCase(str) | |
} | |
return str | |
.replace(/\s[a-z]/g, this.upperCase) | |
.replace(/^\s*[A-Z]+/g, this.lowerCase) | |
.replace(/\s+/g, '') | |
} | |
// Transform first letter of word to uppercase | |
// https://stackoverflow.com/questions/43785788/convert-string-to-title-case-after-dash-or-slash | |
function properCase (str) { | |
str = this.toString(str) | |
return this.lowerCase(str).replace(/^\w|\s\w/g, this.upperCase) | |
} | |
// Subset of camelCase | |
// https://stackoverflow.com/questions/30521224/javascript-convert-pascalcase-to-underscore-case | |
function pascalCase (str) { | |
str = this.toString(str) | |
return this.camelCase(str).replace(/^[a-z]/, this.upperCase) | |
} | |
// Transform string to uppercase | |
function upperCase (str) { | |
str = this.toString(str) | |
return str.toUpperCase() | |
} | |
// Transform string to lowercase | |
function lowerCase (str) { | |
str = str.toString() | |
return str.toLowerCase() | |
} | |
function slugify (s, d) { | |
s = this.toString(s) | |
if (d === null) { | |
d = '-' | |
} | |
s = this.replaceAccents(s) | |
s = this.removeNonWord(s) | |
s = this.trim(s) | |
.replace(/ +g/, d) | |
.toLowerCase() | |
return s | |
} | |
function contains (s, ss, fi) { | |
s = this.toString(s) | |
ss = this.toString(ss) | |
return s.indexOf(ss, fi) !== -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment