ckhatton
12/9/2014 - 12:15 AM

Shopify JS helpers; for adding 'error' class; formatting a string to an URL or from an URL, or to a Shopify URL.

Shopify JS helpers; for adding 'error' class; formatting a string to an URL or from an URL, or to a Shopify URL.

function addError(element) { $(element).addClass('error'); }

function removeError(element) { $(element).removeClass('error'); }

function formatForUrl(str) {
  return str
    .replace(/ /g, '%20')
    .replace(/:/g, '%20')
    .replace(/\\/g, '-')
    .replace(/\//g, '-')
    .replace(/-{2,}/g, '-');
}

function formatFromUrl(str) {
  return str.replace(/%20/g, ' ');
}

function formatForShopify(str) {
  return str.replace(/%20/g, '+');
}