diff --git a/src/map-matrix/map-matrix.js b/src/map-matrix/map-matrix.js index cfddc68..be64687 100644 --- a/src/map-matrix/map-matrix.js +++ b/src/map-matrix/map-matrix.js @@ -1,17 +1,14 @@ import { pipe } from "../pipe/pipe" -const _mapMatrix = (fn, source) => { +const _mapMatrix = (_fn, source) => { const result = [] + const fn = Array.isArray(_fn) ? pipe(..._fn) : _fn for (let i = 0, rowCount = source.length; i < rowCount; ++i) { const row = [] for (let j = 0, columnCount = source[i].length; j < columnCount; ++j) { - row.push( - Array.isArray(fn) - ? pipe(...fn)(source[i][j]) - : fn(source[i][j], i, j, source) - ) + row.push(fn(source[i][j], i, j, source)) } result.push(row) @@ -22,16 +19,18 @@ const _mapMatrix = (fn, source) => { /** * Matrix version of "map". Iterates over a two-dimensional array and applies - * a function on each element, returning a new matrix with the transformed elements. + * a function on each element, returning a new matrix with the transformed + * elements. * - * @param {Fn|Fn[]} fn Transform function called on all elements - * @param {[][]} source Two-dimensional array to iterate over + * @param {Fn|Fn[]} fn Transform function called on all elements + * @param {[][]} source Two-dimensional array to iterate over * + * @name mapMatrix * @tag Array * @signature (fn: Fn|Fn[]) => (source: [][]) => [][] * @signature (fn: Fn|Fn[], source: [][]) => [][] * - * @return {[][]} Returns new instance + * @return {[][]} New array instance * * @example * const inc = x => x + 1 @@ -43,21 +42,11 @@ const _mapMatrix = (fn, source) => { * // => [[3, 4], [5, 6]] */ export const mapMatrix = (...params) => { - /* - * @signature (fn:Fn|Fn[]) => (source: [][]): [][] - * - * mapMatrix(inc)([[1, 2], [3, 4]]) - * // => [[2, 3], [4, 5]] - */ - if (params.length === 1) { + // @signature (fn) => (source) + if (params.length <= 1) { return source => _mapMatrix(params[0], source) } - /* - * @signature (fn: Fn|Fn[], source: [][]): [][] - * - * mapMatrix(inc, [[1, 2], [3, 4]]) - * // => [[2, 3], [4, 5]] - */ + // @signature (fn, source) return _mapMatrix(...params) } diff --git a/src/map-matrix/map-matrix.test.js b/src/map-matrix/map-matrix.test.js index 34d5ff7..bce4c01 100644 --- a/src/map-matrix/map-matrix.test.js +++ b/src/map-matrix/map-matrix.test.js @@ -44,5 +44,16 @@ test("mapMatrix", t => { ], "Multiple transform functions ([square, inc], [[1, 2], [3, 4]]) // => [[2, 5], [10, 17]]" ) + + mapMatrix((currentValue, rowIndex, columIndex, array) => { + t.equal( + currentValue, + array[rowIndex][columIndex], + `callback element "${currentValue}" should equal [${array}][${rowIndex}][${columIndex}]` + ) + + return currentValue * currentValue + })([[1, 2]]) + t.end() })