Last active
July 28, 2022 11:48
-
-
Save louicoder/d7f777975aa8ed398ddff7b3e9f85654 to your computer and use it in GitHub Desktop.
Helper Functions in Javascript
This file contains hidden or 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
/** | |
* FUNCTION shuffles an array passed and return a shuffled array with | |
* new positions for all array elements. | |
* More in this shuffle techniques can be found here on link below | |
* https://bost.ocks.org/mike/shuffle/ | |
* | |
* @param {Array} arr - array to be shuffled | |
* @returns {Array} array - new shuffled array. | |
*/ | |
function shuffle (array) { | |
var currentIndex = array.length, | |
randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex--; | |
// And swap it with the current element. | |
[ array[currentIndex], array[randomIndex] ] = [ array[randomIndex], array[currentIndex] ]; | |
} | |
return array; | |
} | |
/** | |
* const picks random number of specified elements in an array and will return | |
* random (n) numbers from the array length | |
* | |
* @param {Array} arr - array of elements to pick from | |
* @param {Number} [count=7] - count of elements to be returned | |
* | |
* @returns {Array} - array containing number of elements with length of count | |
*/ | |
function pickRandomArrayElements (arr, count = 7) { | |
let _arr = [ ...arr ]; | |
return [ ...Array(count) ].map(() => _arr.splice(Math.floor(Math.random() * _arr.length), 1)[0]); | |
} | |
/** | |
* function validates email addresses. | |
* | |
* @param {String} email | |
* @returns {Boolen} | |
*/ | |
function ValidateEmail (email) { | |
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) return true; | |
return false; | |
} | |
/** | |
* Function returns iso date strig with Time Offset of region. | |
* @param {date} d | |
* @returns {date} | |
*/ | |
function local1 (d = new Date()) { | |
// const tzoffset = d.getTimezoneOffset() * 60000;//offset in milliseconds. | |
// return (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1); | |
return new Date(Date.now() - d.getTimezoneOffset() * 60000).toISOString().slice(0, -1); | |
} | |
/** | |
* Function formats number by thousands comma separator e.g '1020303' will return '1,020,303' | |
* @param {String} x | |
* @returns {String} | |
*/ | |
function numberFormattedWithCommas (x) { | |
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); | |
} | |
/** | |
* | |
* @param {String | Number} number | |
* @param {Number} fix | |
* @param {Boolean} caps | |
* @returns String | |
*/ | |
export const currencyFormatter = (number, fix = 1, caps = false) => { | |
const num = parseInt(number); | |
// if (typeof num !== Number) | |
// return Alert.alert('Not a number', 'the value parse to be converted is not a number, try again'); | |
const SYM = caps ? [ '', 'K', 'M', 'B', 'T', 'P', 'E' ] : [ '', 'k', 'm', 'b', 't', 'p', 'e' ]; | |
// what tier? (determines SI symbol) | |
var tier = (Math.log10(Math.abs(num)) / 3) | 0; | |
// if zero, we don't need a suffix | |
if (tier == 0) return number; | |
// get suffix and determine scale | |
var suffix = SYM[tier]; | |
var scale = Math.pow(10, tier * 3); | |
// scale the number | |
var scaled = number / scale; | |
// format number and add suffix | |
return scaled.toFixed(fix) + suffix; | |
}; | |
export const isoStringWithoutOffset = () => | |
new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString().slice(0, -1); | |
// Splits array of n-size into size of 10 since its the limit for firestore in operator in the above function | |
export const getArraychunks = (array, size) => { | |
var results = []; | |
while (array.length) { | |
results.push(array.splice(0, size)); | |
} | |
// console.log('CHUNK', results); | |
return results; | |
}; | |
export const getDatesBetweenRange = function (startDate, endDate) { | |
// Start Date format should be YYYY-MM-DD | |
// End Date format should be YYYY-MM-DD | |
var dates = []; | |
// Get the day to start from. | |
var currDate = moment(startDate).startOf('day'); | |
// Add one day to the end date to return the end date as well | |
var lastDate = moment(endDate).startOf('day'); | |
// Get the difference in days | |
// < 0 to exclude the end date, but use < 1 to include last day. | |
while (currDate.add(1, 'days').diff(lastDate) < 1) { | |
// console.log(moment(currDate).format('DD-MM-YYYY')); | |
dates.push(moment(currDate).format('DD-MM-YYYY')); | |
} | |
return dates; | |
}; | |
/** Function groups an array of objects by a passed key | |
* | |
* @param {x} x Array | |
* @param {f} f key | |
* @returns Object | |
*/ | |
export const groupBy = (x, f) => x.reduce((a, b) => ((a[f(b)] || []).push(b), a), {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment