Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Shine18/fa302a4f75e9ecd76294ac16be38e2e3 to your computer and use it in GitHub Desktop.

Select an option

Save Shine18/fa302a4f75e9ecd76294ac16be38e2e3 to your computer and use it in GitHub Desktop.
Shopify handleize function in JavaScript
// from https://github.com/Shopify/liquid/blob/63eb1aac69a31d97e343822b973b3a51941c8ac2/performance/shopify/shop_filter.rb#L100
Shopify.handleize = function (str) {
str = str.toLowerCase();
var toReplace = ['"', "'", "\\", "(", ")", "[", "]"];
// For the old browsers
for (var i = 0; i < toReplace.length; ++i) {
str = str.replace(toReplace[i], "");
}
str = str.replace(/\W+/g, "-");
if (str.charAt(str.length - 1) == "-") {
str = str.replace(/-+\z/, "");
}
if (str.charAt(0) == "-") {
str = str.replace(/\A-+/, "");
}
return str
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment