Skip to content

Commit

Permalink
feat: add russian language support (@lordoys)
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Jun 5, 2024
1 parent 921f5b4 commit baae8b6
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
// lower case ranges
// [à-öø-ÿ]

export const magicSplit = /^[a-zà-öø-ÿ]+|[A-ZÀ-ÖØ-ß][a-zà-öø-ÿ]+|[a-zà-öø-ÿ]+|[0-9]+|[A-ZÀ-ÖØ-ß]+(?![a-zà-öø-ÿ])/g
export const magicSplit =
/^[a-zà-öø-ÿа-я]+|[A-ZÀ-ÖØ-ßА-Я][a-zà-öø-ÿа-я]+|[a-zà-öø-ÿа-я]+|[0-9]+|[A-ZÀ-ÖØ-ßА-Я]+(?![a-zà-öø-ÿа-я])/g
export const spaceSplit = /\S+/g

/**
* A string.matchAll function that will return an array of "string parts" and the indexes at which it split each part
*/
export function getPartsAndIndexes(
string: string,
splitRegex: RegExp
splitRegex: RegExp,
): {
parts: string[]
prefixes: string[]
Expand Down Expand Up @@ -48,7 +49,7 @@ export function getPartsAndIndexes(
*/
export function splitAndPrefix(
string: string,
options?: { keepSpecialCharacters?: boolean; keep?: string[]; prefix?: string }
options?: { keepSpecialCharacters?: boolean; keep?: string[]; prefix?: string },
): string[] {
const { keepSpecialCharacters = false, keep, prefix = '' } = options || {}
const normalString = string.trim().normalize('NFC')
Expand All @@ -63,7 +64,9 @@ export function splitAndPrefix(

if (keepSpecialCharacters === false) {
if (keep) {
part = part.normalize('NFD').replace(new RegExp(`[^a-zA-ZØßø0-9${keep.join('')}]`, 'g'), '')
part = part
.normalize('NFD')
.replace(new RegExp(`[^a-zA-ZØßø0-9${keep.join('')}]`, 'g'), '')
}
if (!keep) {
part = part.normalize('NFD').replace(/[^a-zA-ZØßø0-9]/g, '')
Expand Down Expand Up @@ -106,5 +109,8 @@ export function splitAndPrefix(
export function capitaliseWord(string: string): string {
const match = string.matchAll(magicSplit).next().value
const firstLetterIndex = match ? match.index : 0
return string.slice(0, firstLetterIndex + 1).toUpperCase() + string.slice(firstLetterIndex + 1).toLowerCase()
return (
string.slice(0, firstLetterIndex + 1).toUpperCase() +
string.slice(firstLetterIndex + 1).toLowerCase()
)
}

0 comments on commit baae8b6

Please sign in to comment.