UNPKG

diginext-utils

Version:
276 lines (275 loc) 6.67 kB
import { randInt } from "./math"; export const sumArray = (array, key) => { if (!array) { console.warn("ARRAY NOT EXITED !"); return 0; } if (key) return array.reduce((c, v) => c + v[key], 0); else return array.reduce((c, v) => c + v, 0); }; /** * * @param {Array} array * @param {string} key * @returns {Number} */ export const averageArray = (array, key) => { if (!array) { console.warn("ARRAY NOT EXITED !"); return 0; } return sumArray(array, key) / array.length || 0; }; /** * * @param {Array} array * @param {string} key * @returns {Number} */ export const minArray = (array, key) => { if (!array) { console.warn("ARRAY NOT EXITED !"); return 0; } if (array.length > 0) { if (key) return array.reduce((c, v) => (c < v[key] ? c : v[key])); else return array.reduce((c, v) => (c < v ? c : v)); } return 0; }; /** * * @param {Array} array * @param {string} key * @returns {Number} */ export const maxArray = (array, key) => { if (!array) { console.warn("ARRAY NOT EXITED !"); return 0; } if (array.length > 0) { if (key) return array.reduce((c, v) => (c > v[key] ? c : v[key])); else return array.reduce((c, v) => (c > v ? c : v)); } return 0; }; /** * * @param {Array} array * @param {string} key * @returns {Array} */ export const sortElementByString = (array, key) => { if (!Array.isArray(array)) return []; if (key) return array.sort((x, y) => { var a = x[key].toUpperCase(), b = y[key].toUpperCase(); return a == b ? 0 : a > b ? 1 : -1; }); console.log("NO KEY"); }; /** * * @param {Array} array * @param {string} key * @returns {Array} */ export const sortElementByNumber = (array, key) => { if (!Array.isArray(array)) return []; if (key) return array.sort((a, b) => { return a[key] - b[key]; }); console.log("NO KEY"); }; /** * * @param {Array} array * @returns {any} */ export const firstElement = (array) => { if (array) if (array.length || array.length > 0) return array[0]; return null; }; /** * * @param {Array} array * @returns {any} */ export const lastElement = (array) => { if (array) if (array.length || array.length > 0) return array[array.length - 1]; return null; }; /** * * @param {Array} array * @returns {any} */ export const randomIndex = (array) => { if (array) return randInt(0, array.length - 1); return -1; }; /** * * @param {Array} array * @returns {any} */ export const randomElement = (array) => { if (array) return array[randomIndex(array)]; return null; }; /** * Remove same elements from 2 arrays */ export const mergeAndMakeUniqueElement = (list1, list2, key) => { if (!list1 || !list2) return; if (key) { return list1.concat(list2).filter((item, index, self) => { return self.findIndex((x) => x[key] == item[key]) === index; }); } else { return list1.concat(list2).filter((x, index, self) => { return self.indexOf(x) === index; }); } }; /** * check target == toMatch * @param {Array} target * @param {Array} toMatch * @returns {Boolean} */ export const allMatchInArray = (target, toMatch) => { if (!target || !toMatch) return false; const found = toMatch.every((item) => { return target.includes(item); }); return found; }; export const removeItem = (item, array) => { const index = array.indexOf(item); if (index == -1) { console.warn("[ArrayExtra.removeItem] Item not found."); return array; } array.splice(index, 1); return array; }; export const removeItemByKey = (key, value, array) => { const foundIndex = array.findIndex((item) => { return item[key] == value; }); if (foundIndex < 0) { console.warn("[ArrayExtra.removeItemByKey] Item not found.", key, value); return array; } array.splice(foundIndex, 1); return array; }; export const getRandom = (array, n) => { n = n || array.length; var result = new Array(n), len = array.length, taken = new Array(len); if (n > len) throw new RangeError("getRandom: more elements taken than available"); while (n--) { var x = Math.floor(Math.random() * len); result[n] = array[x in taken ? taken[x] : x]; taken[x] = --len in taken ? taken[len] : len; } return result; }; /** * Get an array with shuffle element */ export const getHalfRandom = (array, n) => { var n = Math.ceil(array.length / 2); return getRandom(array, n); }; /** * Make array shuffle itself */ export const shuffle = (array) => { var i = array.length, j, temp; if (array.length < 1) return array; while (--i) { j = Math.floor(Math.random() * (i + 1)); temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }; /** * * @param {Array} array * @param {Number} oldIndex * @param {Number} newIndex * @returns {Array} */ export const moveIndex = (array, oldIndex, newIndex) => { if (newIndex >= array.length) { var k = newIndex - array.length + 1; while (k--) { array.push(undefined); } } array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]); return array; }; export const moveArray = (array, oldIndex, newIndex) => { while (oldIndex < 0) { oldIndex += array.length; } while (newIndex < 0) { newIndex += array.length; } if (newIndex >= array.length) { var k = newIndex - array.length; while (k-- + 1) { array.push(999); } } array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]); return array; }; const xarray = { sumArray, averageArray, minArray, maxArray, sortElementByString, sortElementByNumber, firstElement, lastElement, randomIndex, randomElement, mergeAndMakeUniqueElement, allMatchInArray, removeItem, removeItemByKey, getRandom, getHalfRandom, shuffle, moveIndex, moveArray, }; export default xarray;