| import memoize from "lodash.memoize"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export const slugify = memoize( |
| function (text: string, lowerCase = true): string { |
| if (lowerCase) { |
| text = text.toLowerCase(); |
| } |
| return text |
| .trim() |
| .replace(/[:.]/g, "-") |
| .replace(/[^\w -]+/g, "") |
| .replace(/\s+/g, "-"); |
| }, |
| (...args) => JSON.stringify(args) |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| export function capitalize(s: string): string { |
| return s.charAt(0).toUpperCase() + s.slice(1); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function lowerize(s: string): string { |
| return s.charAt(0).toLowerCase() + s.slice(1); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export const capitalizeEachWord = memoize(function (s: string): string { |
| if (!s) { |
| return ""; |
| } |
| const wrds = s |
| .trim() |
| .split(/\s+/) |
| .map((wrd) => capitalize(wrd)); |
| return wrds.join(" "); |
| }); |
|
|
| |
| |
| |
| |
| |
| |
| const _shortWords = new Set([ |
| "a", |
| "an", |
| "the", |
| "and", |
| "but", |
| "or", |
| "for", |
| "nor", |
| "on", |
| "at", |
| "to", |
| "from", |
| "by", |
| "with", |
| "in", |
| "of", |
| "over", |
| "under", |
| "not", |
| ]); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export const toTitleCase = memoize(function (s: string): string { |
| if (!s) { |
| return ""; |
| } |
| const words = s.trim().split(/\s+/); |
| const titleCasedWords = words.map((word, index) => { |
| const lowerWord = word.toLowerCase(); |
| if ( |
| index === 0 || |
| index === words.length - 1 || |
| !_shortWords.has(lowerWord) |
| ) { |
| return capitalize(word); |
| } |
| return lowerWord; |
| }); |
| return titleCasedWords.join(" "); |
| }); |
|
|
| |
| |
| |
| |
| |
| |
| |
| function _hasInternalCapital(word: string): boolean { |
| if (word.length < 2) { |
| return false; |
| } |
| |
| for (let i = 1; i < word.length; i++) { |
| const char = word[i]; |
| |
| if (char >= "A" && char <= "Z") { |
| return true; |
| } |
| } |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export const toSentenceCase = memoize(function (s: string): string { |
| if (!s) { |
| return ""; |
| } |
| const trimmed = s.trim(); |
| if (trimmed.length === 0) { |
| return ""; |
| } |
| const words = trimmed.split(/\s+/); |
| const firstWord = capitalize(words[0]); |
| const otherWords = words.slice(1).map((word) => { |
| return _hasInternalCapital(word) ? word : word.toLowerCase(); |
| }); |
| return [firstWord, ...otherWords].join(" "); |
| }); |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function removeTerminalPunctuation(s: string): string { |
| |
| while (s.length > 0 && !s[s.length - 1].match(/[a-zA-Z\d]/i)) { |
| s = s.slice(0, -1); |
| } |
| return s; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function isSentence(txt: string): boolean { |
| if (txt === undefined) { |
| return true; |
| } |
|
|
| if (txt.length === 0) { |
| return true; |
| } |
|
|
| |
| txt = txt.replace(/(<([^>]+)>)/g, ""); |
|
|
| |
| txt = txt.trim(); |
|
|
| |
| if (txt[0] !== txt[0].toUpperCase()) { |
| return false; |
| } |
|
|
| |
| const lastChar = txt[txt.length - 1]; |
| return ( |
| lastChar === "." || |
| lastChar === "?" || |
| lastChar === "!" || |
| lastChar === '"' || |
| lastChar === ":" || |
| lastChar === ")" |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function naturalSort<T>( |
| a: T, |
| b: T, |
| keyAccessor: (item: T) => string = (item) => String(item) |
| ): number { |
| |
| const splitIntoChunks = (str: string): (string | number)[] => { |
| |
| const matches = str.match(/(\d+|\D+)/g) || []; |
|
|
| |
| return matches.map((chunk) => { |
| const numVal = Number(chunk); |
| return isNaN(numVal) ? chunk : numVal; |
| }); |
| }; |
|
|
| |
| const strA = keyAccessor(a); |
| const strB = keyAccessor(b); |
|
|
| const chunksA = splitIntoChunks(strA); |
| const chunksB = splitIntoChunks(strB); |
|
|
| |
| const minLength = Math.min(chunksA.length, chunksB.length); |
|
|
| for (let i = 0; i < minLength; i++) { |
| const chunkA = chunksA[i]; |
| const chunkB = chunksB[i]; |
|
|
| |
| if (typeof chunkA === typeof chunkB) { |
| |
| if (typeof chunkA === "number" && typeof chunkB === "number") { |
| if (chunkA !== chunkB) { |
| return chunkA - chunkB; |
| } |
| } |
| |
| else if (chunkA !== chunkB) { |
| return String(chunkA) < String(chunkB) ? -1 : 1; |
| } |
| } |
| |
| else { |
| return typeof chunkA === "number" ? -1 : 1; |
| } |
| } |
|
|
| |
| |
| return chunksA.length - chunksB.length; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function createNaturalSortFunc<T>( |
| keyAccessor?: (item: T) => string |
| ): (a: T, b: T) => number { |
| return (a: T, b: T) => naturalSort(a, b, keyAccessor); |
| } |
|
|