MCPcopy
hub / github.com/josdejong/mathjs / flatten

Function flatten

src/utils/array.js:471–511  ·  view source on GitHub ↗
(array, isRectangular = false)

Source from the content-addressed store, hash-verified

469 * @return {Array} The flattened array (1 dimensional)
470 */
471export function flatten (array, isRectangular = false) {
472 if (!Array.isArray(array)) {
473 // if not an array, return as is
474 return array
475 }
476 if (typeof isRectangular !== 'boolean') {
477 throw new TypeError('Boolean expected for second argument of flatten')
478 }
479 const flat = []
480
481 if (isRectangular) {
482 _flattenRectangular(array)
483 } else {
484 _flatten(array)
485 }
486
487 return flat
488
489 function _flatten (array) {
490 for (let i = 0; i < array.length; i++) {
491 const item = array[i]
492 if (Array.isArray(item)) {
493 _flatten(item)
494 } else {
495 flat.push(item)
496 }
497 }
498 }
499
500 function _flattenRectangular (array) {
501 if (Array.isArray(array[0])) {
502 for (let i = 0; i < array.length; i++) {
503 _flattenRectangular(array[i])
504 }
505 } else {
506 for (let i = 0; i < array.length; i++) {
507 flat.push(array[i])
508 }
509 }
510 }
511}
512
513/**
514 * A safe map

Callers 5

pickRandom.test.jsFile · 0.90
array.test.jsFile · 0.90
flatten.jsFile · 0.90
reshapeFunction · 0.70
flatten.test.jsFile · 0.50

Calls 2

_flattenRectangularFunction · 0.85
_flattenFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…