// Menu: Text / String transformations // Description: spaces, accents, cases, variables names // Author: Luiz Tanure // Twitter: @tanure let { getSelectedText, setSelectedText } = await kit("text") // let userString = await arg("Type or paste the text to transform:") String.prototype.upperCaseFirstLetter = function () { return this.charAt(0).toUpperCase() + this.slice(1); }; String.prototype.lowerCaseFirstLetter = function () { return this.charAt(0).toLowerCase() + this.slice(1); }; String.prototype.removeSpaces = function () { return this.trim().replace(/\s\s+/g, ' '); }; String.prototype.spacesToDashes = function () { return this.trim().replace(/\s+/g, '-'); }; String.prototype.spacesToUnderscores = function () { return this.trim().replace(/\s+/g, '_'); }; String.prototype.dashesToUnderscores = function () { return this.trim().replace(/-+/g, '_'); }; String.prototype.removeDuplicatedChar = function (char) { const patternString = `${char}${char}+`; const patternRegex = new RegExp(patternString,'gi'); return this.trim().replace(patternRegex, char) }; String.prototype.removeAccents = function (char) { return this.normalize('NFD').replace(/[\u0300-\u036f]/g, '') }; const transformationTypes = [ { name: 'Remove duplicated spaces', value: 'remove-duplicated-spaces', shortcode: 's', description: 'Remove duplicated spaces', transformation: (userString) => userString .removeSpaces() .trim() }, { name: 'Uppercase [AAA BBB]', value: 'uppercase', shortcode: 'U', description: 'Converts all to uppercase and remove duplicated spaces', transformation: (userString) => userString .removeSpaces() .toUpperCase() .trim() }, { name: 'Lowercase [aaa bbb]', value: 'lowercase', shortcode: 'l', description: 'Converts all to lowercase and remove duplicated spaces', transformation: (userString) => userString .removeSpaces() .toLowerCase() .trim() }, { name: 'Capitalize words [Aaa Bbb]', value: 'capitalize', shortcode: 'c', description: 'Capitalizes first letter of each word', transformation: (userString) => userString .removeSpaces() .toLowerCase() .split(' ') .map((word) => word.upperCaseFirstLetter()) .join(' ') .trim() }, { name: 'Snake case [aaa_bbb]', value: 'snake_case', shortcode: '_', description: 'Converts to lowercase and replace spaces by _\n aaa ajnkwdqwdioqwadiomawods dawsndxilw', transformation: (userString) => userString .removeSpaces() .toLowerCase() .spacesToUnderscores() .dashesToUnderscores() .removeDuplicatedChar('_') .trim() }, { name: 'kebab-case [aaa-bbb]', value: 'kebab-case', shortcode: '-', description: 'Converts to lowercase and replace spaces by -', transformation: (userString) => userString .removeSpaces() .toLowerCase() .spacesToDashes() .removeDuplicatedChar('-') .trim() }, { name: 'camelCase [aaaBbb]', value: 'camelCase', shortcode: 'C', description: 'Capitalize all words after the first word and removes spaces', transformation: (userString) => userString .removeSpaces() .toLowerCase() .split(' ') .map((word) => word.upperCaseFirstLetter()) .join('') .lowerCaseFirstLetter() }, { name: 'PascalCase [AaaBbb]', value: 'PascalCase', shortcode: 'P', description: 'Capitalize all words and removes spaces', transformation: (userString) => userString .removeSpaces() .toLowerCase() .split(' ') .map((word) => word.upperCaseFirstLetter()) .join('') }, { name: 'Remove numbers [aa1aBb2b => aaaBbb]', value: 'remove-numbers', shortcode: '0', description: 'Remove all digits', transformation: (userString) => userString .removeSpaces() .replace(/[0-9]/g, '') .trim() }, { name: 'Keep only numbers [aa1aBb2b => 12]', value: 'keep-numbers', shortcode: '1', description: 'keep only digits', transformation: (userString) => userString .replace(/\D/g,'') .trim() }, { name: 'Remove HTML tags', value: 'html-remove', shortcode: 'h', description: 'Remove all HTML tags', transformation: (userString) => userString .removeSpaces() .replaceAll('><', '> <') .replace(/(<([^>]+)>)/gi, '') .trim() }, { name: 'Remove HTML attributes', value: 'html-remove-attributes', shortcode: 'a', description: 'Remove attributes from HTML tags', transformation: (userString) => userString .removeSpaces() .replace(/<\s*(\w+).*?>/, '<$1>') .replace(/(\s<)/gi, '<') .replace(/(\s<)/gi, '---') .trim() }, { name: 'Remove accents & diacritics [âêì => aei]', value: 'html-remove-accents', shortcode: '\'', description: 'Remove accents & diacritics', transformation: (userString) => userString .removeSpaces() .removeAccents() .trim() }, { name: 'URL slugfy', value: 'slugfy', shortcode: 'u', description: 'Remove accents & diacritics, replace spaces', transformation: (userString) => userString .removeSpaces() .removeAccents() .toLowerCase() .replace(/[^\w ]+/g,'') .spacesToDashes() .trim() }, ]; const transformationTypesWithShortcode = transformationTypes.map((transformationType) => ({ ...transformationType, name: `[${transformationType.shortcode}] ${transformationType.name}` }) ) let userString = await getSelectedText() let transformationSelectedType = await arg( 'Select transformation', transformationTypesWithShortcode ) let result = transformationTypes .find(transformationTypes => transformationTypes.value === transformationSelectedType) .transformation(userString) setSelectedText(result)