GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: validate that the `index` in the `.get()` methods is an Array · josdejong/mathjs@24d5ee7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/utils
    • array.js
  • test/unit-tests
    • type/matrix
      • DenseMatrix.test.js
      • SparseMatrix.test.js
    • utils
      • array.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/utils/array.js‎

Copy file name to clipboardExpand all lines: src/utils/array.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,9 @@ export function stretch (arrayToStretch, sizeToStretch, dimToStretch) {
844844
*/
845845
export function get (array, index) {
846846
if (!Array.isArray(array)) { throw new Error('Array expected') }
847+
if (!Array.isArray(index)) {
848+
throw new Error('Array expected for index')
849+
}
847850
const size = arraySize(array)
848851
if (index.length !== size.length) { throw new DimensionError(index.length, size.length) }
849852
for (let x = 0; x < index.length; x++) { validateIndex(index[x], size[x]) }
Collapse file

‎test/unit-tests/type/matrix/DenseMatrix.test.js‎

Copy file name to clipboardExpand all lines: test/unit-tests/type/matrix/DenseMatrix.test.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,16 @@ describe('DenseMatrix', function () {
440440
assert.throws(function () { m.get(math.index(1, 1)) })
441441
assert.throws(function () { m.get([[1, 1]]) })
442442
})
443+
444+
it('should throw an error when getting a value given an index that is not an array', function () {
445+
assert.throws(function () {
446+
m.get({ length: 1, reduce: () => {} })
447+
}, /Error: Array expected for index/)
448+
449+
assert.throws(function () {
450+
m.get(new Date())
451+
}, /Error: Array expected for index/)
452+
})
443453
})
444454

445455
describe('set', function () {
Collapse file

‎test/unit-tests/type/matrix/SparseMatrix.test.js‎

Copy file name to clipboardExpand all lines: test/unit-tests/type/matrix/SparseMatrix.test.js
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,21 @@ describe('SparseMatrix', function () {
730730
assert.strictEqual(m.get([4, 5]), 13)
731731
assert.strictEqual(m.get([5, 5]), -1)
732732
})
733+
734+
it('should throw an error when getting a value given an index that is not an array', function () {
735+
const m = new SparseMatrix([
736+
[1, 2],
737+
[3, 4]
738+
])
739+
740+
assert.throws(function () {
741+
m.get({ length: 1, reduce: () => {} })
742+
}, /Error: Array expected/)
743+
744+
assert.throws(function () {
745+
m.get(new Date())
746+
}, /Error: Array expected/)
747+
})
733748
})
734749

735750
describe('set', function () {
Collapse file

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

Copy file name to clipboardExpand all lines: test/unit-tests/utils/array.test.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,16 @@ describe('util.array', function () {
678678
assert.throws(function () { get(m, null) })
679679
assert.throws(function () { get(m, [[1, 1]]) })
680680
})
681+
682+
it('should throw an error when getting a value given an index that is not an array', function () {
683+
assert.throws(function () {
684+
get(m, { length: 1, reduce: () => {} })
685+
}, /Error: Array expected for index/)
686+
687+
assert.throws(function () {
688+
get(m, new Date())
689+
}, /Error: Array expected for index/)
690+
})
681691
})
682692

683693
describe('checkBroadcastingRules', function () {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.