Skip to content

Instantly share code, notes, and snippets.

@chidindu-ogbonna
Created May 8, 2019 20:20
Show Gist options
  • Save chidindu-ogbonna/570ebf8ae75ee41b309d48d90d12122d to your computer and use it in GitHub Desktop.
Save chidindu-ogbonna/570ebf8ae75ee41b309d48d90d12122d to your computer and use it in GitHub Desktop.
Helper methods
/**
* Helper helper function
* Generate a title case.
* @param {string} string
*/
const titleCase = string => {
let splitStr = string.toLowerCase().split(" ");
for (let i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] =
splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(" ");
};
/**
* Helper helper function
* Remove invalid characters
* @param {string} string
*/
const removeInvalidCharacter = string => {
return string.replace(/[^a-zA-Z0-9\- ]/g, "");
};
const makeId = length => {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result.toLowerCase();
};
const processShopName = string => {
const str = string.trim();
return titleCase(removeInvalidCharacter(str));
};
const processShopUrl = string => {
const str = string
.trim()
.toLowerCase()
.replace(/ /g, "-");
return removeInvalidCharacter(str);
};
module.exports = { processShopName, processShopUrl, makeId };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment