Skip to content

Instantly share code, notes, and snippets.

@letanure
Created May 1, 2021 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letanure/ee7db17b3b381966f61aab9812862870 to your computer and use it in GitHub Desktop.
Save letanure/ee7db17b3b381966f61aab9812862870 to your computer and use it in GitHub Desktop.
// 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',
description: 'Remove duplicated spaces',
transformation: (userString) =>
userString
.removeSpaces()
.trim()
},
{
name: 'Uppercase [AAA BBB]',
value: 'uppercase',
description: 'Converts all to uppercase and remove duplicated spaces',
transformation: (userString) =>
userString
.removeSpaces()
.toUpperCase()
.trim()
},
{
name: 'Lowercase [aaa bbb]',
value: 'lowercase',
description: 'Converts all to lowercase and remove duplicated spaces',
transformation: (userString) =>
userString
.removeSpaces()
.toLowerCase()
.trim()
},
{
name: 'Capitalize words [Aaa Bbb]',
value: 'capitalize',
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',
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',
description: 'Converts to lowercase and replace spaces by -',
transformation: (userString) =>
userString
.removeSpaces()
.toLowerCase()
.spacesToDashes()
.removeDuplicatedChar('-')
.trim()
},
{
name: 'camelCase [aaaBbb]',
value: 'camelCase',
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',
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',
description: 'Remove all digits',
transformation: (userString) =>
userString
.removeSpaces()
.replace(/[0-9]/g, '')
.trim()
},
{
name: 'Keep only numbers [aa1aBb2b => 12]',
value: 'keep-numbers',
description: 'keep only digits',
transformation: (userString) =>
userString
.replace(/\D/g,'')
.trim()
},
{
name: 'Remove HTML tags',
value: 'html-remove',
description: 'Remove all HTML tags',
transformation: (userString) =>
userString
.removeSpaces()
.replaceAll('><', '> <')
.replace(/(<([^>]+)>)/gi, '')
.trim()
},
{
name: 'Remove HTML attributes',
value: 'html-remove-attributes',
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',
description: 'Remove accents & diacritics',
transformation: (userString) =>
userString
.removeSpaces()
.removeAccents()
.trim()
},
{
name: 'URL slugfy',
value: 'slugfy',
description: 'Remove accents & diacritics, replace spaces',
transformation: (userString) =>
userString
.removeSpaces()
.removeAccents()
.toLowerCase()
.replace(/[^\w ]+/g,'')
.spacesToDashes()
.trim()
},
];
let userString = await getSelectedText()
let transformationSelectedType = await arg(
'Select transformation',
transformationTypes
)
let result = transformationTypes
.find(transformationTypes => transformationTypes.value === transformationSelectedType)
.transformation(userString)
setSelectedText(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment