GeezerVison is showing github.com — fix: validate that the `index` in the `.get()` methods is an Array · josdejong/mathjs@24d5ee7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/utils/array.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -844,6 +844,9 @@ export function stretch (arrayToStretch, sizeToStretch, dimToStretch) { | 844 | 844 | */ | 845 | 845 | export function get (array, index) { | 846 | 846 | if (!Array.isArray(array)) { throw new Error('Array expected') } | | 847 | + if (!Array.isArray(index)) { | | 848 | + throw new Error('Array expected for index') | | 849 | + } | 847 | 850 | const size = arraySize(array) | 848 | 851 | if (index.length !== size.length) { throw new DimensionError(index.length, size.length) } | 849 | 852 | for (let x = 0; x < index.length; x++) { validateIndex(index[x], size[x]) } | |
Collapse file Copy file name to clipboardExpand all lines: test/unit-tests/type/matrix/DenseMatrix.test.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -440,6 +440,16 @@ describe('DenseMatrix', function () { | 440 | 440 | assert.throws(function () { m.get(math.index(1, 1)) }) | 441 | 441 | assert.throws(function () { m.get([[1, 1]]) }) | 442 | 442 | }) | | 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 | + }) | 443 | 453 | }) | 444 | 454 |
| 445 | 455 | describe('set', function () { | |
Collapse file Copy file name to clipboardExpand all lines: test/unit-tests/type/matrix/SparseMatrix.test.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -730,6 +730,21 @@ describe('SparseMatrix', function () { | 730 | 730 | assert.strictEqual(m.get([4, 5]), 13) | 731 | 731 | assert.strictEqual(m.get([5, 5]), -1) | 732 | 732 | }) | | 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 | + }) | 733 | 748 | }) | 734 | 749 |
| 735 | 750 | describe('set', function () { | |
Collapse file Copy file name to clipboardExpand all lines: test/unit-tests/utils/array.test.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -678,6 +678,16 @@ describe('util.array', function () { | 678 | 678 | assert.throws(function () { get(m, null) }) | 679 | 679 | assert.throws(function () { get(m, [[1, 1]]) }) | 680 | 680 | }) | | 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 | + }) | 681 | 691 | }) | 682 | 692 |
| 683 | 693 | describe('checkBroadcastingRules', function () { | |
|