Last active
January 19, 2021 19:14
-
-
Save lookitskris/fa7852a46dd73e38b837052b233476ad to your computer and use it in GitHub Desktop.
some javascript helper methods i've used in various projects
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
| export const nestArray = (items, parent = '#') => { | |
| const nested = []; | |
| Object.values(items).forEach(item => { | |
| if (item.parent == parent) { | |
| const fetchedChildren = nestArray(items, item.id); | |
| if (fetchedChildren.length) { | |
| item.fetchedChildren = fetchedChildren; | |
| } | |
| nested.push(item); | |
| } | |
| }); | |
| return nested; | |
| }; | |
| export const flattenArray = (items) => { | |
| var result = []; | |
| for (let item of items) { | |
| if (item) { | |
| result.push(item) | |
| if (item.fetchedChildren) { | |
| result = result.concat(flattenArray(item.fetchedChildren)); | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| export function deepCopy(obj) { | |
| if(typeof obj !== 'object' || obj === null) { | |
| return obj; | |
| } | |
| if(obj instanceof Date) { | |
| return new Date(obj.getTime()); | |
| } | |
| if(obj instanceof Array) { | |
| return obj.reduce((arr, item, i) => { | |
| arr[i] = deepCopy(item); | |
| return arr; | |
| }, []); | |
| } | |
| if(obj instanceof Object) { | |
| return Object.keys(obj).reduce((newObj, key) => { | |
| newObj[key] = deepCopy(obj[key]); | |
| return newObj; | |
| }, {}) | |
| } | |
| } | |
| export function getContrastYIQ(hexcolor) { | |
| const replacedColor = hexcolor.replace('#', ''); | |
| const r = parseInt(replacedColor.substr(0, 2), 16); | |
| const g = parseInt(replacedColor.substr(2, 2), 16); | |
| const b = parseInt(replacedColor.substr(4, 2), 16); | |
| const yiq = (r * 299 + g * 587 + b * 114) / 1000; | |
| return yiq >= 128 ? 'black' : 'white'; | |
| } | |
| export function multiplesOf(numbers, number) { | |
| var multiples = []; | |
| for (var i = 0; i < numbers.length; i++) { | |
| if (numbers[i] % number === 0) { | |
| multiples.push(numbers[i]); | |
| } | |
| } | |
| return multiples; | |
| } | |
| export function convertNumber(number, format) { | |
| let tail = format.lastIndexOf('.'); | |
| let convertedNumber = number.toString(); | |
| let formatter = format; | |
| tail = tail > -1 ? format.substr(tail) : ''; | |
| if (tail.length > 0) { | |
| if (tail.charAt(1) === '#') { | |
| tail = convertedNumber.substr(convertedNumber.lastIndexOf('.'), tail.length); | |
| } | |
| } | |
| convertedNumber = convertedNumber.replace(/\..*|[^0-9]/g, '').split(''); | |
| formatter = formatter.replace(/\..*/g, '').split(''); | |
| for (let i = format.length - 1; i > -1; i += 1) { | |
| if (format[i] === '#') { | |
| formatter[i] = convertedNumber.pop(); | |
| } | |
| } | |
| return convertedNumber.join('') + formatter.join('') + tail; | |
| } | |
| function getListOfValues() { | |
| return new Array(20).fill().map((_, i) => ({ label: i, value: i })); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment