maccounter
46 строк · 1.2 Кб
1import { space } from '@assets/symbols'2
3type StringParser = string | undefined4
5export const htmlStringParser = (strToDecode: string): StringParser => {6const decodedStr = new DOMParser()7.parseFromString(8`<!doctype html><body>${strToDecode}`,9'text/html',10)11.body12.textContent13return decodedStr ?? undefined14}
15
16export const phoneParser = (phoneToParse: string): StringParser => {17const regExp = /(\+\d)(\d{3})(\d{3})(\d{2})(\d{2})$/s18const match = phoneToParse.match(regExp)19
20if (!match || !match[0]) {21return undefined22}23match.shift()24
25return match.join(space)26}
27
28export const getFirstLetters = (strToDecode: string, join?: string | boolean): StringParser | Array<string> => {29const regExp = /\b[A-Z]|(?<=^|\s)[А-ЯЁ]/gu30const match = strToDecode.match(regExp)31
32if (!match) { return undefined }33
34if (join === true) {35return match.join('')36}37if (join) {38return match.join(join)39}40return match41}
42
43export const nameParser = (firstName: string, secondName: string): StringParser => {44const initials = getFirstLetters(`${firstName} ${secondName}`, true)45return typeof initials === 'string' ? initials : initials?.join('')46}
47