Created
February 21, 2022 13:30
-
-
Save stephenscaff/00e32032278784118d8764b5523b13fa to your computer and use it in GitHub Desktop.
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
/** | |
* isEven | |
* @param {number} | |
*/ | |
export function isEven(num) { | |
return num % 2 === 0; | |
} | |
/** | |
* Count Items | |
* @param {array} | |
*/ | |
export function countItems(items) { | |
if (items) { | |
return items.length; | |
} | |
} | |
/** | |
* Is Last Item | |
* @param {array} | |
*/ | |
export function isLastItem(index, arry) { | |
return index === arry.length - 1; | |
} |
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
/** | |
* Format Currency | |
* @param {string} priceStr - price string | |
* @param {string} locale - locale string | |
* @param {string} currency - currency type string | |
*/ | |
export function formatCurrency(priceStr, locale = 'en-US', currency = 'USD') { | |
if (!priceStr) return; | |
const price = parseInt(priceStr); | |
return price.toLocaleString(locale, { | |
style: 'currency', | |
currency: currency | |
}); | |
} |
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
/** | |
* Get a date, x number of days from Today | |
* @param {number} days - days to add | |
* @param {string} weekdayStyle - ex:'short', 'long' | |
* @param {string} monthStyle - ex: 'short', 'long' | |
* @param {bool} hasYear - output year | |
*/ | |
export function getFutureDate( | |
days, | |
weekdayStyle = 'long', | |
monthStyle = 'long' | |
) { | |
const today = new Date(); | |
today.setDate(today.getDate() + days); | |
const options = { | |
...(weekdayStyle && { weekday: weekdayStyle }), | |
...(monthStyle && { month: monthStyle }), | |
day: 'numeric' | |
}; | |
return today.toLocaleString('en-us', options); | |
} |
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
/** | |
* Capitalize String | |
* @param {string} | |
*/ | |
export function capitalize(str) { | |
return str.replace(/^\w/, c => c.toUpperCase()); | |
} | |
/** | |
* Convert string to Sentence Case | |
* @param {string} | |
*/ | |
export function sentenceCase(str) { | |
const caps = str.replace(/^\w/, c => c.toUpperCase()); | |
return caps.replace(/_/g, ' ').replace(/-/g, ''); | |
} | |
/** | |
* dashify | |
* Formats string to an ID friendly version, | |
* replacing whitespace with dashes and lowercasing | |
*/ | |
export function dashify(str) { | |
const dashes = str.replace(/\s/g, '-'); | |
return dashes.toLowerCase(); | |
} | |
/** | |
* Text Elippser | |
* @param {string} - content string | |
* @param {number} - number of chars before ellipse | |
*/ | |
export function ellipseText(str, maxChars) { | |
return str.length > maxChars ? `${str.substr(0, maxChars)}...` : str; | |
} | |
/** | |
* String Decoder | |
* @param {String} | |
*/ | |
export function decode(str) { | |
return str.replace(/&#(\d+);/g, (match, dec) => { | |
String.fromCharCode(dec); | |
}); | |
} | |
/** | |
* Count number of words | |
* @param {String} | |
*/ | |
export function countWords(str) { | |
return str.split(/\s+/).length; | |
} |
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
/** | |
* Is Object | |
*/ | |
export function isObject(value) { | |
return value && typeof value === "object" && value.constructor === Object; | |
} | |
/** | |
* Is Array | |
*/ | |
export function isArray(value) { | |
// https://webbjocke.com/javascript-check-data-types/#javascript-array | |
return value && typeof value === "object" && value.constructor === Array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment