Skip to content

Commit 40c09c5

Browse files
dvd101xjosdejong
andauthored
refactor: simplify flatten function using Array.prototype.flat (#3354)
* refactor: simplify flatten function using Array.prototype.flat * Added fallback * added option to flatten arrays with homogeneous size * Typos in array.js and array.test.js * fix types in jsdocs * Renamed variable * Reverted to original form --------- Co-authored-by: Jos de Jong <[email protected]>
1 parent 4c473eb commit 40c09c5

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

src/function/matrix/flatten.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createFlatten = /* #__PURE__ */ factory(name, dependencies, ({ type
3232
Matrix: function (x) {
3333
// Return the same matrix type as x (Dense or Sparse Matrix)
3434
// Return the same data type as x
35-
return x.create(flattenArray(x.toArray()), x.datatype())
35+
return x.create(flattenArray(x.valueOf()), x.datatype())
3636
}
3737
})
3838
})

src/utils/array.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { deepStrictEqual } from './object.js'
1010
* This function checks the size of the first entry, it does not validate
1111
* whether all dimensions match. (use function `validate` for that)
1212
* @param {Array} x
13-
* @Return {Number[]} size
13+
* @return {number[]} size
1414
*/
1515
export function arraySize (x) {
1616
const s = []
@@ -28,7 +28,7 @@ export function arraySize (x) {
2828
* has a size corresponding to the provided size array.
2929
* @param {Array} array Array to be validated
3030
* @param {number[]} size Array with the size of each dimension
31-
* @param {number} dim Current dimension
31+
* @param {number} dim Current dimension
3232
* @throws DimensionError
3333
* @private
3434
*/
@@ -51,7 +51,7 @@ function _validate (array, size, dim) {
5151
_validate(array[i], size, dimNext)
5252
}
5353
} else {
54-
// last dimension. none of the childs may be an array
54+
// last dimension. none of the children may be an array
5555
for (i = 0; i < len; i++) {
5656
if (Array.isArray(array[i])) {
5757
throw new DimensionError(size.length + 1, size.length, '>')
@@ -82,7 +82,7 @@ export function validate (array, size) {
8282

8383
/**
8484
* Validate whether the source of the index matches the size of the Array
85-
* @param {Array | Matrix} array Array to be validated
85+
* @param {Array | Matrix} value Array to be validated
8686
* @param {Index} index Index with the source information to validate
8787
* @throws DimensionError
8888
*/
@@ -113,8 +113,8 @@ export function validateIndex (index, length) {
113113
}
114114

115115
/**
116-
* Test if and index has empty values
117-
* @param {number} index Zero-based index
116+
* Test if an index has empty values
117+
* @param {Index} index Zero-based index
118118
*/
119119
export function isEmptyIndex (index) {
120120
for (let i = 0; i < index._dimensions.length; ++i) {
@@ -140,7 +140,7 @@ export function isEmptyIndex (index) {
140140
* Resize a multi dimensional array. The resized array is returned.
141141
* @param {Array | number} array Array to be resized
142142
* @param {number[]} size Array with the size of each dimension
143-
* @param {*} [defaultValue=0] Value to be filled in in new entries,
143+
* @param {*} [defaultValue=0] Value to be filled in new entries,
144144
* zero by default. Specify for example `null`,
145145
* to clearly see entries that are not explicitly
146146
* set.
@@ -180,7 +180,7 @@ export function resize (array, size, defaultValue) {
180180
* @param {Array} array Array to be resized
181181
* @param {number[]} size Array with the size of each dimension
182182
* @param {number} dim Current dimension
183-
* @param {*} [defaultValue] Value to be filled in in new entries,
183+
* @param {*} [defaultValue] Value to be filled in new entries,
184184
* undefined by default.
185185
* @private
186186
*/
@@ -283,7 +283,7 @@ export function reshape (array, sizes) {
283283

284284
/**
285285
* Replaces the wildcard -1 in the sizes array.
286-
* @param {number[]} sizes List of sizes for each dimension. At most on wildcard.
286+
* @param {number[]} sizes List of sizes for each dimension. At most one wildcard.
287287
* @param {number} currentLength Number of elements in the array.
288288
* @throws {Error} If more than one wildcard or unable to replace it.
289289
* @returns {number[]} The sizes array with wildcard replaced.
@@ -333,7 +333,7 @@ function _reshape (array, sizes) {
333333
// testing if there are enough elements for the requested shape
334334
let tmpArray = array
335335
let tmpArray2
336-
// for each dimensions starting by the last one and ignoring the first one
336+
// for each dimension starting by the last one and ignoring the first one
337337
for (let sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) {
338338
const size = sizes[sizeIndex]
339339
tmpArray2 = []
@@ -408,7 +408,7 @@ function _squeeze (array, dims, dim) {
408408
/**
409409
* Unsqueeze a multi dimensional array: add dimensions when missing
410410
*
411-
* Paramter `size` will be mutated to match the new, unqueezed matrix size.
411+
* Parameter `size` will be mutated to match the new, unsqueezed matrix size.
412412
*
413413
* @param {Array} array
414414
* @param {number} dims Desired number of dimensions of the array
@@ -442,7 +442,7 @@ export function unsqueeze (array, dims, outer, size) {
442442
* @param {Array} array
443443
* @param {number} dims Required number of dimensions
444444
* @param {number} dim Current dimension
445-
* @returns {Array | *} Returns the squeezed array
445+
* @returns {Array | *} Returns the unsqueezed array
446446
* @private
447447
*/
448448
function _unsqueeze (array, dims, dim) {
@@ -517,7 +517,7 @@ export function filter (array, callback) {
517517
}
518518

519519
/**
520-
* Filter values in a callback given a regular expression
520+
* Filter values in an array given a regular expression
521521
* @param {Array} array
522522
* @param {RegExp} regexp
523523
* @return {Array} Returns the filtered array
@@ -634,7 +634,7 @@ export function getArrayDataType (array, typeOf) {
634634

635635
/**
636636
* Return the last item from an array
637-
* @param {array}
637+
* @param {Array} array
638638
* @returns {*}
639639
*/
640640
export function last (array) {
@@ -643,16 +643,16 @@ export function last (array) {
643643

644644
/**
645645
* Get all but the last element of array.
646-
* @param {array}
647-
* @returns {*}
646+
* @param {Array} array
647+
* @returns {Array}
648648
*/
649649
export function initial (array) {
650650
return array.slice(0, array.length - 1)
651651
}
652652

653653
/**
654654
* Recursively concatenate two matrices.
655-
* The contents of the matrices is not cloned.
655+
* The contents of the matrices are not cloned.
656656
* @param {Array} a Multi dimensional array
657657
* @param {Array} b Multi dimensional array
658658
* @param {number} concatDim The dimension on which to concatenate (zero-based)
@@ -682,8 +682,8 @@ function concatRecursive (a, b, concatDim, dim) {
682682
* Concatenates many arrays in the specified direction
683683
* @param {...Array} arrays All the arrays to concatenate
684684
* @param {number} concatDim The dimension on which to concatenate (zero-based)
685-
* @returns
686-
*/
685+
* @returns {Array}
686+
*/
687687
export function concat () {
688688
const arrays = Array.prototype.slice.call(arguments, 0, -1)
689689
const concatDim = Array.prototype.slice.call(arguments, -1)
@@ -699,9 +699,9 @@ export function concat () {
699699
}
700700

701701
/**
702-
* Receives two or more sizes and get's the broadcasted size for both.
702+
* Receives two or more sizes and gets the broadcasted size for both.
703703
* @param {...number[]} sizes Sizes to broadcast together
704-
* @returns
704+
* @returns {number[]} The broadcasted size
705705
*/
706706
export function broadcastSizes (...sizes) {
707707
const dimensions = sizes.map((s) => s.length)
@@ -736,17 +736,17 @@ export function checkBroadcastingRules (size, toSize) {
736736
const n = N - dim + j
737737
if ((size[j] < toSize[n] && size[j] > 1) || (size[j] > toSize[n])) {
738738
throw new Error(
739-
`shape missmatch: missmatch is found in arg with shape (${size}) not possible to broadcast dimension ${dim} with size ${size[j]} to size ${toSize[n]}`
739+
`shape mismatch: mismatch is found in arg with shape (${size}) not possible to broadcast dimension ${dim} with size ${size[j]} to size ${toSize[n]}`
740740
)
741741
}
742742
}
743743
}
744744

745745
/**
746746
* Broadcasts a single array to a certain size
747-
* @param {array} array Array to be broadcasted
747+
* @param {Array} array Array to be broadcasted
748748
* @param {number[]} toSize Size to broadcast the array
749-
* @returns The broadcasted array
749+
* @returns {Array} The broadcasted array
750750
*/
751751
export function broadcastTo (array, toSize) {
752752
let Asize = arraySize(array)
@@ -778,11 +778,11 @@ export function broadcastTo (array, toSize) {
778778
/**
779779
* Broadcasts arrays and returns the broadcasted arrays in an array
780780
* @param {...Array | any} arrays
781-
* @returns
781+
* @returns {Array[]} The broadcasted arrays
782782
*/
783783
export function broadcastArrays (...arrays) {
784784
if (arrays.length === 0) {
785-
throw new Error('Insuficient number of argumnets in function broadcastArrays')
785+
throw new Error('Insufficient number of arguments in function broadcastArrays')
786786
}
787787
if (arrays.length === 1) {
788788
return arrays[0]
@@ -795,11 +795,11 @@ export function broadcastArrays (...arrays) {
795795
}
796796

797797
/**
798-
* stretches a matrix up to a certain size in a certain dimension
798+
* Stretches a matrix up to a certain size in a certain dimension
799799
* @param {Array} arrayToStretch
800800
* @param {number[]} sizeToStretch
801801
* @param {number} dimToStretch
802-
* @returns
802+
* @returns {Array} The stretched array
803803
*/
804804
export function stretch (arrayToStretch, sizeToStretch, dimToStretch) {
805805
return concat(...Array(sizeToStretch).fill(arrayToStretch), dimToStretch)
@@ -809,13 +809,13 @@ export function stretch (arrayToStretch, sizeToStretch, dimToStretch) {
809809
* Retrieves a single element from an array given an index.
810810
*
811811
* @param {Array} array - The array from which to retrieve the value.
812-
* @param {Array<number>} idx - An array of indices specifying the position of the desired element in each dimension.
812+
* @param {Array<number>} index - An array of indices specifying the position of the desired element in each dimension.
813813
* @returns {*} - The value at the specified position in the array.
814814
*
815815
* @example
816816
* const arr = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
817817
* const index = [1, 0, 1];
818-
* console.log(getValue(arr, index)); // 6
818+
* console.log(get(arr, index)); // 6
819819
*/
820820
export function get (array, index) {
821821
if (!Array.isArray(array)) { throw new Error('Array expected') }
@@ -849,7 +849,7 @@ export function recurse (value, index, array, callback) {
849849
/**
850850
* Deep clones a multidimensional array
851851
* @param {Array} array
852-
* @returns cloned array
852+
* @returns {Array} cloned array
853853
*/
854854
export function clone (array) {
855855
return Object.assign([], array)

test/unit-tests/utils/array.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe('util.array', function () {
195195
[0, 0],
196196
[0, 0]
197197
])
198-
// TODO: would be nicer if this returns uninit everwhere and not undefined on some places
198+
// TODO: would be nicer if this returns uninit everywhere and not undefined in some places
199199
})
200200

201201
it('should resize a 2 dimensional array to 1 dimensional', function () {
@@ -604,7 +604,7 @@ describe('util.array', function () {
604604
})
605605

606606
it('should throw an error when the broadcasting rules are not followed', function () {
607-
assert.throws(function () { broadcastSizes([2, 2], [3, 2]) }, /Error: shape missmatch: missmatch is found in arg with shape.*/)
607+
assert.throws(function () { broadcastSizes([2, 2], [3, 2]) }, /Error: shape mismatch: mismatch is found in arg with shape.*/)
608608
})
609609
})
610610

0 commit comments

Comments
 (0)