2024-01-10 10:18:30 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2024-07-22 11:45:05 +00:00
|
|
|
const CAPITALIZED_WORD_REGEXP = /\p{Lu}\p{Ll}+/u; // e.g. Apple
|
|
|
|
const ACRONYM_REGEXP = /\p{Lu}+(?=(\p{Lu}\p{Ll})|\P{L}|\b)/u; // e.g. ID, URL, handles an acronym followed by a capitalized word e.g. HTMLElement
|
|
|
|
const LOWERCASED_WORD_REGEXP = /(\p{Ll}+)/u; // e.g. apple
|
|
|
|
const ANY_LETTERS = /\p{L}+/u; // will match any sequence of letters, including in languages without a concept of upper/lower case
|
|
|
|
const DIGITS_REGEXP = /\p{N}+/u; // e.g. 123
|
|
|
|
|
|
|
|
const WORD_OR_NUMBER_REGEXP = new RegExp(
|
|
|
|
`${CAPITALIZED_WORD_REGEXP.source}|${ACRONYM_REGEXP.source}|${LOWERCASED_WORD_REGEXP.source}|${ANY_LETTERS.source}|${DIGITS_REGEXP.source}`,
|
|
|
|
"gu",
|
|
|
|
);
|
|
|
|
|
2024-01-10 10:18:30 +00:00
|
|
|
export function splitToWords(input: string) {
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
return input.match(WORD_OR_NUMBER_REGEXP) ?? [];
|
2024-01-10 10:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function capitalizeWord(word: string): string {
|
|
|
|
return word
|
2024-01-10 20:33:57 +00:00
|
|
|
? word?.[0]?.toLocaleUpperCase() + word.slice(1).toLocaleLowerCase()
|
2024-01-10 10:18:30 +00:00
|
|
|
: word;
|
|
|
|
}
|