Skip to content

Commit

Permalink
fix(mapMatrix): Pass current item, indexes and source array when runn…
Browse files Browse the repository at this point in the history
…ing with array of functions
  • Loading branch information
andreidmt committed Aug 28, 2020
1 parent 69ce47d commit a432ec2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/map-matrix/map-matrix.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions src/map-matrix/map-matrix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit a432ec2

Please sign in to comment.