Last active
February 6, 2024 13:57
-
-
Save pablogiralt/f52d27428e7501909616d1dea04edfeb to your computer and use it in GitHub Desktop.
Shopify handlelize function in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// handlelize in liquid: https://github.com/Shopify/liquid/blob/63eb1aac69a31d97e343822b973b3a51941c8ac2/performance/shopify/shop_filter.rb#L100 | |
// how to handlelize in js: https://ricardometring.com/javascript-replace-special-characters | |
function handlelize (str) { | |
str = str.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // Remove accents | |
.replace(/([^\w]+|\s+)/g, '-') // Replace space and other characters by hyphen | |
.replace(/\-\-+/g, '-') // Replaces multiple hyphens by one hyphen | |
.replace(/(^-+|-+$)/g, '') // Remove extra hyphens from beginning or end of the string | |
.toLowerCase(); // To lowercase | |
return str | |
}; | |
export default handlelize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pablogiralt based on the liquid handleize I have adjusted it to the following
The only addition is line 7 to reflect https://github.com/Shopify/liquid/blob/main/performance/shopify/shop_filter.rb#L100