UNPKG

si-funciona

Version:

Funciones de utilidad para uso general. [General usage utility functions.]

98 lines (97 loc) 4.21 kB
import 'core-js/stable'; /** * Store the comparison result for an element that may exist in either of compared arrays. * - value stores the element value from the arrays being compared * - results has the comparison results where first index (0) is result for first compared array * and the second index (1) will be the result for the second compared array * @example * // example of input and resulting output * * const arr1 = ['match1', 'firstMismatch1', 'match2', 'firstMismatch2', 'badMatch1'] * const arr2 = ['match1', 'match2', 'secondMismatch1', 'badMatch1', 'badMatch1'] * * // Taking the first element in both, then the value: 'match1' exists in both arrays * // compareArrayResult will be { value: 'match1', result: [0, 0] } * // First index of 0 indicates this value in the first array exists in the second array * // Second index of 0 indicates this value in the second array exists in the first array * * // Taking the second element in the first array, then the value: 'firstMismatch1' exists in only the first array * // compareArrayResult will be { value: 'firstMismatch1', result: [1, -1] } * // First index of 1 indicates this value in the first array might need to be added to the second array * // Second index of -1 indicates this value only exists in the first array * * // Taking the third element in the second array, then the value: 'secondMismatch1' exists in only the second array * // compareArrayResult will be { value: 'secondMismatch1', result: [-1, 1] } * // First index of -1 indicates this value only exists in the second array * // Second index of 1 indicates this value in the second array might need to be added to the first array * @typedef {Object.<string, string|Array.<number>>} module:arrayHelpers~compareArrayResult * @memberOf module:arrayHelpers * @property {string} value - The element value being compared * @property {Array.<Array.<string|number>>} keys - Keys in arrays associated with this value * @property {Array.<number>} result - The results in the order of the compared arrays */ export type compareArrayResult = { value: string; keys: Array<Array<number | string>>; result: Array<number>; }; export type compareArrayResultMap = Array<compareArrayResult>; /** * Compare two Arrays and return the Object where the value for each property is as follows: * -1 to indicate val1 is less than val2 * 0 to indicate both values are the equal * 1 to indicate val1 is greater than val2 * The returned Object uses the element values as the property names * This functions works by first creating a concatenated array of all unique values. Then for each unique values, * convert to a string and use it as a new property name. Array filter each array checking if it has the unique value. * Use the lengths of these filtered arrays to compare. So if the first array has the value and the second one doesn't * the first length will be one or more and the second will be zero, if the both have the value then both will be one * or more. * @example * // example of input and resulting output * compareArrays( * ['match1', 'firstMismatch1', 'match2', 'firstMismatch2', 'badMatch1'], * ['match1', 'match2', 'secondMismatch1', 'badMatch1', 'badMatch1'] * ) * // unique array * ['match1', 'firstMismatch1', 'match2', 'firstMismatch2', 'badMatch1', 'secondMismatch1'] * // result object * [ * { * value: 'match1', * keys: [[0], [0]], * result: [0, 0] * }, * { * value: 'firstMismatch1', * keys: [[1], []], * result: [1, -1] * }, * { * value: 'match2', * keys: [[2], [1]], * result: [0, 0] * }, * { * value: 'firstMismatch2', * keys: [[3], []], * result: [1, -1] * }, * { * value: 'badMatch1', * keys: [[4], [3, 4]], * result: [0, 0] * }, * { * value: 'secondMismatch1', * keys: [[], [2]], * result: [-1, 1] * } * ] * * @memberOf module:arrayHelpers * @param {...Array} arrays - The arrays to compare * @returns {Array.<module:arrayHelpers~compareArrayResult>} */ declare const compareArrays: (...arrays: Array<Array<any>>) => compareArrayResultMap; export default compareArrays;