83 lines
7.9 KiB
JavaScript
83 lines
7.9 KiB
JavaScript
|
|
/**
|
|||
|
|
* Capitalize a string, transforming it's first character to a capital letter.
|
|||
|
|
* @returns {string}
|
|||
|
|
*/
|
|||
|
|
export function capitalize() {
|
|||
|
|
if ( !this.length ) return this;
|
|||
|
|
return this.charAt(0).toUpperCase() + this.slice(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Compare this string (x) with the other string (y) by comparing each character's Unicode code point value.
|
|||
|
|
* Returns a negative Number if x < y, a positive Number if x > y, or a zero otherwise.
|
|||
|
|
* This is the same comparision function that used by Array#sort if the compare function argument is omitted.
|
|||
|
|
* The result is host/locale-independent.
|
|||
|
|
* @param {string} other The other string to compare this string to.
|
|||
|
|
* @returns {number}
|
|||
|
|
*/
|
|||
|
|
export function compare(other) {
|
|||
|
|
return this < other ? -1 : this > other ? 1 : 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Convert a string to Title Case where the first letter of each word is capitalized.
|
|||
|
|
* @returns {string}
|
|||
|
|
*/
|
|||
|
|
export function titleCase() {
|
|||
|
|
if (!this.length) return this;
|
|||
|
|
return this.toLowerCase().split(' ').reduce((parts, word) => {
|
|||
|
|
if ( !word ) return parts;
|
|||
|
|
const title = word.replace(word[0], word[0].toUpperCase());
|
|||
|
|
parts.push(title);
|
|||
|
|
return parts;
|
|||
|
|
}, []).join(' ');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Strip any script tags which were included within a provided string.
|
|||
|
|
* @returns {string}
|
|||
|
|
*/
|
|||
|
|
export function stripScripts() {
|
|||
|
|
let el = document.createElement("div");
|
|||
|
|
el.innerHTML = this;
|
|||
|
|
for ( let s of el.getElementsByTagName("script") ) {
|
|||
|
|
s.parentNode.removeChild(s);
|
|||
|
|
}
|
|||
|
|
return el.innerHTML;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Map characters to lower case ASCII
|
|||
|
|
* @type {Record<string, string>}
|
|||
|
|
*/
|
|||
|
|
const CHAR_MAP = JSON.parse('{"$":"dollar","%":"percent","&":"and","<":"less",">":"greater","|":"or","¢":"cent","£":"pound","¤":"currency","¥":"yen","©":"(c)","ª":"a","®":"(r)","º":"o","À":"A","Á":"A","Â":"A","Ã":"A","Ä":"A","Å":"A","Æ":"AE","Ç":"C","È":"E","É":"E","Ê":"E","Ë":"E","Ì":"I","Í":"I","Î":"I","Ï":"I","Ð":"D","Ñ":"N","Ò":"O","Ó":"O","Ô":"O","Õ":"O","Ö":"O","Ø":"O","Ù":"U","Ú":"U","Û":"U","Ü":"U","Ý":"Y","Þ":"TH","ß":"ss","à":"a","á":"a","â":"a","ã":"a","ä":"a","å":"a","æ":"ae","ç":"c","è":"e","é":"e","ê":"e","ë":"e","ì":"i","í":"i","î":"i","ï":"i","ð":"d","ñ":"n","ò":"o","ó":"o","ô":"o","õ":"o","ö":"o","ø":"o","ù":"u","ú":"u","û":"u","ü":"u","ý":"y","þ":"th","ÿ":"y","Ā":"A","ā":"a","Ă":"A","ă":"a","Ą":"A","ą":"a","Ć":"C","ć":"c","Č":"C","č":"c","Ď":"D","ď":"d","Đ":"DJ","đ":"dj","Ē":"E","ē":"e","Ė":"E","ė":"e","Ę":"e","ę":"e","Ě":"E","ě":"e","Ğ":"G","ğ":"g","Ģ":"G","ģ":"g","Ĩ":"I","ĩ":"i","Ī":"i","ī":"i","Į":"I","į":"i","İ":"I","ı":"i","Ķ":"k","ķ":"k","Ļ":"L","ļ":"l","Ľ":"L","ľ":"l","Ł":"L","ł":"l","Ń":"N","ń":"n","Ņ":"N","ņ":"n","Ň":"N","ň":"n","Ő":"O","ő":"o","Œ":"OE","œ":"oe","Ŕ":"R","ŕ":"r","Ř":"R","ř":"r","Ś":"S","ś":"s","Ş":"S","ş":"s","Š":"S","š":"s","Ţ":"T","ţ":"t","Ť":"T","ť":"t","Ũ":"U","ũ":"u","Ū":"u","ū":"u","Ů":"U","ů":"u","Ű":"U","ű":"u","Ų":"U","ų":"u","Ŵ":"W","ŵ":"w","Ŷ":"Y","ŷ":"y","Ÿ":"Y","Ź":"Z","ź":"z","Ż":"Z","ż":"z","Ž":"Z","ž":"z","ƒ":"f","Ơ":"O","ơ":"o","Ư":"U","ư":"u","Lj":"LJ","lj":"lj","Nj":"NJ","nj":"nj","Ș":"S","ș":"s","Ț":"T","ț":"t","˚":"o","Ά":"A","Έ":"E","Ή":"H","Ί":"I","Ό":"O","Ύ":"Y","Ώ":"W","ΐ":"i","Α":"A","Β":"B","Γ":"G","Δ":"D","Ε":"E","Ζ":"Z","Η":"H","Θ":"8","Ι":"I","Κ":"K","Λ":"L","Μ":"M","Ν":"N","Ξ":"3","Ο":"O","Π":"P","Ρ":"R","Σ":"S","Τ":"T","Υ":"Y","Φ":"F","Χ":"X","Ψ":"PS","Ω":"W","Ϊ":"I","Ϋ":"Y","ά":"a","έ":"e","ή":"h","ί":"i","ΰ":"y","α":"a","β":"b","γ":"g","δ":"d","ε":"e","ζ":"z","η":"h","θ":"8","ι":"i","κ":"k","λ":"l","μ":"m","ν":"n","ξ":"3","ο":"o","π":"p","ρ":"r","ς":"s","σ":"s","τ":"t","υ":"y","φ":"f","χ":"x","ψ":"ps","ω":"w","ϊ":"i","ϋ":"y","ό":"o","ύ":"y","ώ":"w","Ё":"Yo","Ђ":"DJ","Є":"Ye","І":"I","Ї":"Yi","Ј":"J","Љ":"LJ","Њ":"NJ","Ћ":"C","Џ":"DZ","А":"A","Б":"B","В":"V","Г":"G","Д":"D","Е":"E","Ж":"Zh","З":"Z","И":"I","Й":"J","К":"K","Л":"L","М":"M","Н":"N","О":"O","П":"P","Р":"R","С":"S","Т":"T","У":"U","Ф":"F","Х":"H","Ц":"C","Ч":"Ch","Ш":"Sh","Щ":"Sh","Ъ":"U","Ы":"Y","Ь":"","Э":"E","Ю":"Yu","Я":"Ya","а":"a","б":"b","в":"v","г":"g","д":"d","е":"e","ж":"zh","з":"z","и":"i","й":"j","к":"k","л":"l","м":"m","н":"n","о":"o","п":"p","р":"r","с":"s","т":"t","у":"u","ф":"f","х":"h","ц":"c","ч":"ch","ш":"sh","щ":"sh","ъ":"u","ы":"y","ь":"","э":"e","ю":"yu","я":"ya","ё":"yo","ђ":"dj","є":"ye","і":"i","ї":"yi","ј":"j","љ":"lj","њ":"nj","ћ":"c","ѝ":"u","џ":"dz","Ґ":"G","ґ":"g","Ғ":"GH","ғ":"gh","Қ":"KH","қ":"kh","Ң":"NG","ң":"ng","Ү":"UE","ү":"ue","Ұ":"U","ұ":"u","Һ":"H","һ":"h","Ә":"AE","ә":"ae","Ө":"OE","ө":"oe","฿":"baht","ა":"a","ბ":"b","გ":"g","დ":"d","ე":"e","ვ":"v","ზ":"z","თ":"t","ი":"i","კ":"k","ლ":"l","მ":"m","ნ":"n","ო":"o","პ":"p","ჟ":"zh","რ":"r","ს":"s","ტ":"t","უ":"u","ფ":"f","ქ":"k","ღ":"gh","ყ":"q","შ":"sh","ჩ":"ch","ც":"ts","ძ":"dz","წ":"ts","ჭ":"ch","ხ":"kh","ჯ":"j","ჰ":"h","Ẁ":"W","ẁ":"w","Ẃ":"W","ẃ":"w","Ẅ":"W","ẅ":"w","ẞ":"SS","Ạ":"A","ạ":"a","Ả":"A","ả":"a","Ấ":"A","ấ":"a","Ầ":"A","ầ":"a","Ẩ":"A","ẩ":"a","Ẫ":"A","ẫ":"a","Ậ":"A","ậ":"a","Ắ":"A","ắ":"a","Ằ":"A","ằ":"a","Ẳ":"A","ẳ":"a","Ẵ":"A","ẵ":"a","Ặ":"A","ặ":"a","Ẹ":"E","ẹ":"e","Ẻ":"E","ẻ":"e","Ẽ":"E","ẽ":"e","Ế":"E","ế":"e","Ề":"E","ề":"e","Ể":"E","ể":"e","Ễ":"E","ễ":"e","Ệ":"E","ệ":"e","Ỉ":"I","<EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Transform any string into an url-viable slug string
|
|||
|
|
* @param {object} [options] Optional arguments which customize how the slugify operation is performed
|
|||
|
|
* @param {string} [options.replacement="-"] The replacement character to separate terms, default is '-'
|
|||
|
|
* @param {boolean} [options.strict=false] Replace all non-alphanumeric characters, or allow them? Default false
|
|||
|
|
* @param {boolean} [options.lowercase=true] Lowercase the string.
|
|||
|
|
* @returns {string} The slugified input string
|
|||
|
|
*/
|
|||
|
|
export function slugify({replacement='-', strict=false, lowercase=true}={}) {
|
|||
|
|
let slug = this.split("").reduce((result, char) => result + (CHAR_MAP[char] || char), "").trim();
|
|||
|
|
if ( lowercase ) slug = slug.toLowerCase();
|
|||
|
|
|
|||
|
|
// Convert any spaces to the replacement character and de-dupe
|
|||
|
|
slug = slug.replace(new RegExp('[\\s' + replacement + ']+', 'g'), replacement);
|
|||
|
|
|
|||
|
|
// If we're being strict, replace anything that is not alphanumeric
|
|||
|
|
if ( strict ) slug = slug.replace(new RegExp('[^a-zA-Z0-9' + replacement + ']', 'g'), '');
|
|||
|
|
return slug;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Define properties on the String environment
|
|||
|
|
Object.defineProperties(String.prototype, {
|
|||
|
|
capitalize: {value: capitalize},
|
|||
|
|
compare: {value: compare},
|
|||
|
|
titleCase: {value: titleCase},
|
|||
|
|
stripScripts: {value: stripScripts},
|
|||
|
|
slugify: {value: slugify}
|
|||
|
|
});
|