From e4179f505cfef762afef9d1e138dae1ea1816eb9 Mon Sep 17 00:00:00 2001 From: Neef Rehman Date: Tue, 26 Jul 2022 14:49:01 +0100 Subject: [PATCH 1/4] perf: move checks out of recursive function call restructures responsibilities to the reduce count of some checks improves performance and reduce bundle size --- .github/workflows/setup-node-and-test.yml | 12 +++---- benchmark/run-benchmark.ts | 5 ++- src/index.ts | 40 +++++++++-------------- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/.github/workflows/setup-node-and-test.yml b/.github/workflows/setup-node-and-test.yml index 1c29ed1..72aad9f 100644 --- a/.github/workflows/setup-node-and-test.yml +++ b/.github/workflows/setup-node-and-test.yml @@ -48,11 +48,11 @@ jobs: run: npm run build - name: validate build run: | - FILE=lib/index.js - FILE2=lib/index.d.ts - if [ -f $FILE ] && [ -f $FILE2 ]; then echo "Files exist" - else echo "Files do not exist" && exit 1 - fi + FILE=lib/index.js + FILE2=lib/index.d.ts + if [ -f $FILE ] && [ -f $FILE2 ]; then echo "Files exist" + else echo "Files do not exist" && exit 1 + fi - name: lint run: npm run lint - name: test @@ -68,4 +68,4 @@ jobs: id: benchmark run: npm run benchmark:diff:ci env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/benchmark/run-benchmark.ts b/benchmark/run-benchmark.ts index a9e8b82..2917a8c 100644 --- a/benchmark/run-benchmark.ts +++ b/benchmark/run-benchmark.ts @@ -78,7 +78,7 @@ benny ["new", "old", "delta"] ); - if (!isInCI) { + if (!isInCI || !process.env.GITHUB_TOKEN) { return; } @@ -89,8 +89,7 @@ benny body += `| ${diff.name} | ${diff.new}| ${diff.old} | ${diff.delta} |\n`; }); - const octokit = getOctokit(process.env.GITHUB_TOKEN ?? ""); - await octokit.rest.issues.createComment({ + await getOctokit(process.env.GITHUB_TOKEN).rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, diff --git a/src/index.ts b/src/index.ts index 2c621e9..19385f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,9 +23,17 @@ const makeMatrix = ( dimensions: D | Vector, initialValues: ValueOrFunction = null ): Matrix => { - const dimensionCount = typeof dimensions === "number" ? dimensions : dimensions.length; - const initialPosition = Array(dimensionCount).fill(0) as Vector; + if (typeof dimensions === "number") { + const initialPosition = Array(dimensions).fill(0) as Vector; + const vecDimensions = [...Array(dimensions)].map((_, i) => dimensions - i); + return _makeMatrix(vecDimensions as Vector, initialValues, initialPosition); + } + + if (dimensions.some(d => !Number.isInteger(d))) { + throw new TypeError("Dimensions must be integers"); + } + const initialPosition = Array(dimensions.length).fill(0) as Vector; return _makeMatrix(dimensions, initialValues, initialPosition); }; @@ -33,32 +41,16 @@ const makeMatrix = ( * Recursively creates a matrix, and keeps track of the current vector. */ function _makeMatrix( - dimensions: D | Vector, + dimensions: Vector, initialValues: ValueOrFunction, currentPosition: Vector ): Matrix { - let currentDimensionLength: number; - let remainingDimensions: number | Vector; - let remainingDimensionCount: number; - - if (typeof dimensions === "number") { - currentDimensionLength = dimensions; - remainingDimensions = dimensions - 1; - remainingDimensionCount = remainingDimensions; - } else { - currentDimensionLength = dimensions[0] ?? 0; - remainingDimensions = dimensions.slice(1); - remainingDimensionCount = remainingDimensions.length; - } - - if (!Number.isInteger(currentDimensionLength)) { - throw new TypeError(`Dimensions must be integers`); - } - + const [currentDimensionLength, ...remainingDimensions] = dimensions; + const remainingDimensionCount = remainingDimensions.length; const currentDimension = currentPosition.length - 1 - remainingDimensionCount; const needsRecursion = remainingDimensionCount > 0; - const matrix = [...Array(currentDimensionLength)].map((_, i) => { + return [...Array(currentDimensionLength)].map((_, i) => { currentPosition[currentDimension] = i; return needsRecursion ? _makeMatrix( @@ -69,9 +61,7 @@ function _makeMatrix( : initialValues instanceof Function ? initialValues(currentPosition.slice() as Vector) : initialValues; - }); - - return matrix as Matrix; + }) as Matrix; } export default makeMatrix; From c506817da25e36518684adce115acc0bc2408367 Mon Sep 17 00:00:00 2001 From: Neef Rehman Date: Tue, 26 Jul 2022 14:54:19 +0100 Subject: [PATCH 2/4] perf(validation): drop custom integer validation drops integer validation in favour of inbuilt js array length checks --- .../__snapshots__/index.test.ts.snap | 140 +++++++++--------- src/__tests__/index.test.ts | 22 +-- src/index.ts | 4 - 3 files changed, 78 insertions(+), 88 deletions(-) diff --git a/src/__tests__/__snapshots__/index.test.ts.snap b/src/__tests__/__snapshots__/index.test.ts.snap index a5ae636..3d5d6df 100644 --- a/src/__tests__/__snapshots__/index.test.ts.snap +++ b/src/__tests__/__snapshots__/index.test.ts.snap @@ -1,12 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`arrays of N dimensions — No initial values: 1 1`] = ` +exports[`arrays of N dimensions No initial values 1 1`] = ` Array [ null, ] `; -exports[`arrays of N dimensions — No initial values: 2 1`] = ` +exports[`arrays of N dimensions No initial values 2 1`] = ` Array [ Array [ null, @@ -17,7 +17,7 @@ Array [ ] `; -exports[`arrays of N dimensions — No initial values: 3 1`] = ` +exports[`arrays of N dimensions No initial values 3 1`] = ` Array [ Array [ Array [ @@ -46,7 +46,7 @@ Array [ ] `; -exports[`arrays of N dimensions — No initial values: 4 1`] = ` +exports[`arrays of N dimensions No initial values 4 1`] = ` Array [ Array [ Array [ @@ -155,7 +155,7 @@ Array [ ] `; -exports[`arrays of N dimensions — No initial values: 5 1`] = ` +exports[`arrays of N dimensions No initial values 5 1`] = ` Array [ Array [ Array [ @@ -690,13 +690,13 @@ Array [ ] `; -exports[`arrays of N dimensions — callback initial values — math: 1 1`] = ` +exports[`arrays of N dimensions callback initial values — math 1 1`] = ` Array [ 8, ] `; -exports[`arrays of N dimensions — callback initial values — math: 2 1`] = ` +exports[`arrays of N dimensions callback initial values — math 2 1`] = ` Array [ Array [ 8, @@ -707,7 +707,7 @@ Array [ ] `; -exports[`arrays of N dimensions — callback initial values — math: 3 1`] = ` +exports[`arrays of N dimensions callback initial values — math 3 1`] = ` Array [ Array [ Array [ @@ -736,7 +736,7 @@ Array [ ] `; -exports[`arrays of N dimensions — callback initial values — math: 4 1`] = ` +exports[`arrays of N dimensions callback initial values — math 4 1`] = ` Array [ Array [ Array [ @@ -845,7 +845,7 @@ Array [ ] `; -exports[`arrays of N dimensions — callback initial values — math: 5 1`] = ` +exports[`arrays of N dimensions callback initial values — math 5 1`] = ` Array [ Array [ Array [ @@ -1380,13 +1380,13 @@ Array [ ] `; -exports[`arrays of N dimensions — number initial values: 1 1`] = ` +exports[`arrays of N dimensions number initial values 1 1`] = ` Array [ 0, ] `; -exports[`arrays of N dimensions — number initial values: 2 1`] = ` +exports[`arrays of N dimensions number initial values 2 1`] = ` Array [ Array [ 0, @@ -1397,7 +1397,7 @@ Array [ ] `; -exports[`arrays of N dimensions — number initial values: 3 1`] = ` +exports[`arrays of N dimensions number initial values 3 1`] = ` Array [ Array [ Array [ @@ -1426,7 +1426,7 @@ Array [ ] `; -exports[`arrays of N dimensions — number initial values: 4 1`] = ` +exports[`arrays of N dimensions number initial values 4 1`] = ` Array [ Array [ Array [ @@ -1535,7 +1535,7 @@ Array [ ] `; -exports[`arrays of N dimensions — number initial values: 5 1`] = ` +exports[`arrays of N dimensions number initial values 5 1`] = ` Array [ Array [ Array [ @@ -2070,13 +2070,13 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — joined: 1 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — joined 1 1`] = ` Array [ "my position is 0", ] `; -exports[`arrays of N dimensions — self aware callback initial values — joined: 2 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — joined 2 1`] = ` Array [ Array [ "my position is 0,0", @@ -2087,7 +2087,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — joined: 3 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — joined 3 1`] = ` Array [ Array [ Array [ @@ -2116,7 +2116,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — joined: 4 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — joined 4 1`] = ` Array [ Array [ Array [ @@ -2225,7 +2225,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — joined: 5 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — joined 5 1`] = ` Array [ Array [ Array [ @@ -2760,7 +2760,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — object conversion: 1 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — object conversion 1 1`] = ` Array [ Object { "a": undefined, @@ -2773,7 +2773,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — object conversion: 2 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — object conversion 2 1`] = ` Array [ Array [ Object { @@ -2798,7 +2798,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — object conversion: 3 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — object conversion 3 1`] = ` Array [ Array [ Array [ @@ -2869,7 +2869,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — object conversion: 4 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — object conversion 4 1`] = ` Array [ Array [ Array [ @@ -3146,7 +3146,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — object conversion: 5 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — object conversion 5 1`] = ` Array [ Array [ Array [ @@ -4521,7 +4521,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — vector: 1 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — vector 1 1`] = ` Array [ Array [ 0, @@ -4529,7 +4529,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — vector: 2 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — vector 2 1`] = ` Array [ Array [ Array [ @@ -4546,7 +4546,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — vector: 3 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — vector 3 1`] = ` Array [ Array [ Array [ @@ -4599,7 +4599,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — vector: 4 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — vector 4 1`] = ` Array [ Array [ Array [ @@ -4828,7 +4828,7 @@ Array [ ] `; -exports[`arrays of N dimensions — self aware callback initial values — vector: 5 1`] = ` +exports[`arrays of N dimensions self aware callback initial values — vector 5 1`] = ` Array [ Array [ Array [ @@ -6083,13 +6083,13 @@ Array [ ] `; -exports[`arrays of N dimensions — string initial values: 1 1`] = ` +exports[`arrays of N dimensions string initial values 1 1`] = ` Array [ "string", ] `; -exports[`arrays of N dimensions — string initial values: 2 1`] = ` +exports[`arrays of N dimensions string initial values 2 1`] = ` Array [ Array [ "string", @@ -6100,7 +6100,7 @@ Array [ ] `; -exports[`arrays of N dimensions — string initial values: 3 1`] = ` +exports[`arrays of N dimensions string initial values 3 1`] = ` Array [ Array [ Array [ @@ -6129,7 +6129,7 @@ Array [ ] `; -exports[`arrays of N dimensions — string initial values: 4 1`] = ` +exports[`arrays of N dimensions string initial values 4 1`] = ` Array [ Array [ Array [ @@ -6238,7 +6238,7 @@ Array [ ] `; -exports[`arrays of N dimensions — string initial values: 5 1`] = ` +exports[`arrays of N dimensions string initial values 5 1`] = ` Array [ Array [ Array [ @@ -6773,7 +6773,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — No initial values: [1, 1] 1`] = ` +exports[`arrays of specific dimensions No initial values [1, 1] 1`] = ` Array [ Array [ null, @@ -6781,7 +6781,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — No initial values: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions No initial values [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -6876,7 +6876,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — No initial values: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions No initial values [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -7189,7 +7189,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — No initial values: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions No initial values [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -7230,7 +7230,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — No initial values: [6] 1`] = ` +exports[`arrays of specific dimensions No initial values [6] 1`] = ` Array [ null, null, @@ -7241,7 +7241,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — callback initial values — math: [1, 1] 1`] = ` +exports[`arrays of specific dimensions callback initial values — math [1, 1] 1`] = ` Array [ Array [ 8, @@ -7249,7 +7249,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — callback initial values — math: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions callback initial values — math [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -7344,7 +7344,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — callback initial values — math: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions callback initial values — math [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -7657,7 +7657,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — callback initial values — math: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions callback initial values — math [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -7698,7 +7698,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — callback initial values — math: [6] 1`] = ` +exports[`arrays of specific dimensions callback initial values — math [6] 1`] = ` Array [ 8, 8, @@ -7709,7 +7709,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — number initial values: [1, 1] 1`] = ` +exports[`arrays of specific dimensions number initial values [1, 1] 1`] = ` Array [ Array [ 0, @@ -7717,7 +7717,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — number initial values: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions number initial values [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -7812,7 +7812,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — number initial values: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions number initial values [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -8125,7 +8125,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — number initial values: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions number initial values [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -8166,7 +8166,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — number initial values: [6] 1`] = ` +exports[`arrays of specific dimensions number initial values [6] 1`] = ` Array [ 0, 0, @@ -8177,7 +8177,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — joined: [1, 1] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — joined [1, 1] 1`] = ` Array [ Array [ "my position is 0,0", @@ -8185,7 +8185,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — joined: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — joined [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -8280,7 +8280,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — joined: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — joined [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -8593,7 +8593,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — joined: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — joined [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -8634,7 +8634,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — joined: [6] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — joined [6] 1`] = ` Array [ "my position is 0", "my position is 1", @@ -8645,7 +8645,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — object conversion: [1, 1] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — object conversion [1, 1] 1`] = ` Array [ Array [ Object { @@ -8660,7 +8660,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — object conversion: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — object conversion [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -9035,7 +9035,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — object conversion: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — object conversion [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -10244,7 +10244,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — object conversion: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — object conversion [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -10411,7 +10411,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — object conversion: [6] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — object conversion [6] 1`] = ` Array [ Object { "a": undefined, @@ -10464,7 +10464,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — vector: [1, 1] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — vector [1, 1] 1`] = ` Array [ Array [ Array [ @@ -10475,7 +10475,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — vector: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — vector [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -10770,7 +10770,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — vector: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — vector [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -11851,7 +11851,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — vector: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — vector [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -11964,7 +11964,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — self aware callback initial values — vector: [6] 1`] = ` +exports[`arrays of specific dimensions self aware callback initial values — vector [6] 1`] = ` Array [ Array [ 0, @@ -11987,7 +11987,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — string initial values: [1, 1] 1`] = ` +exports[`arrays of specific dimensions string initial values [1, 1] 1`] = ` Array [ Array [ "string", @@ -11995,7 +11995,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — string initial values: [1, 4, 5, 2] 1`] = ` +exports[`arrays of specific dimensions string initial values [1, 4, 5, 2] 1`] = ` Array [ Array [ Array [ @@ -12090,7 +12090,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — string initial values: [2, 4, 2, 4, 2] 1`] = ` +exports[`arrays of specific dimensions string initial values [2, 4, 2, 4, 2] 1`] = ` Array [ Array [ Array [ @@ -12403,7 +12403,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — string initial values: [3, 2, 3] 1`] = ` +exports[`arrays of specific dimensions string initial values [3, 2, 3] 1`] = ` Array [ Array [ Array [ @@ -12444,7 +12444,7 @@ Array [ ] `; -exports[`arrays of specific dimensions — string initial values: [6] 1`] = ` +exports[`arrays of specific dimensions string initial values [6] 1`] = ` Array [ "string", "string", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 5159009..59f838c 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -1,11 +1,5 @@ import makeMatrix from "../index"; -describe("validation", () => { - test("should error when a float is used as a dimension", () => { - expect(() => makeMatrix([1, 0.2, 3])).toThrow(TypeError); - }); -}); - const nDimensions = [1, 2, 3, 4, 5]; const specificDimensions = [[6], [1, 1], [3, 2, 3], [1, 4, 5, 2], [2, 4, 2, 4, 2]]; @@ -14,38 +8,38 @@ const describeCases = [ { argType: "specific", testCases: specificDimensions.map(n => [n, n]) }, ]; -describe.each(describeCases)("arrays of $argType dimensions —", ({ testCases }) => { - describe("No initial values:", () => { +describe.each(describeCases)("arrays of $argType dimensions", ({ testCases }) => { + describe("No initial values", () => { test.each(testCases)("%p", dimensions => { expect(makeMatrix(dimensions)).toMatchSnapshot(); }); }); - describe("string initial values:", () => { + describe("string initial values", () => { test.each(testCases)("%p", dimensions => { expect(makeMatrix(dimensions, "string")).toMatchSnapshot(); }); }); - describe("number initial values:", () => { + describe("number initial values", () => { test.each(testCases)("%p", dimensions => { expect(makeMatrix(dimensions, 0)).toMatchSnapshot(); }); }); - describe("callback initial values — math:", () => { + describe("callback initial values — math", () => { test.each(testCases)("%p", dimensions => { expect(makeMatrix(dimensions, () => 10 - 2)).toMatchSnapshot(); }); }); - describe("self aware callback initial values — vector:", () => { + describe("self aware callback initial values — vector", () => { test.each(testCases)("%p", dimensions => { expect(makeMatrix(dimensions, vector => vector)).toMatchSnapshot(); }); }); - describe("self aware callback initial values — joined:", () => { + describe("self aware callback initial values — joined", () => { test.each(testCases)("%p", dimensions => { expect( makeMatrix(dimensions, vector => `my position is ${vector.join()}`) @@ -53,7 +47,7 @@ describe.each(describeCases)("arrays of $argType dimensions —", ({ testCases } }); }); - describe("self aware callback initial values — object conversion:", () => { + describe("self aware callback initial values — object conversion", () => { test.each(testCases)("%p", dimensions => { expect( makeMatrix(dimensions, ([x, y, z, a, b, c]) => ({ x, y, z, a, b, c })) diff --git a/src/index.ts b/src/index.ts index 19385f7..5c66f11 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,10 +29,6 @@ const makeMatrix = ( return _makeMatrix(vecDimensions as Vector, initialValues, initialPosition); } - if (dimensions.some(d => !Number.isInteger(d))) { - throw new TypeError("Dimensions must be integers"); - } - const initialPosition = Array(dimensions.length).fill(0) as Vector; return _makeMatrix(dimensions, initialValues, initialPosition); }; From 93cd53dcf96958a60ee860e4ed950f45365d0eb8 Mon Sep 17 00:00:00 2001 From: Neef Rehman Date: Tue, 26 Jul 2022 14:58:28 +0100 Subject: [PATCH 3/4] refactor: rename variable for clarity --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5c66f11..f1e8a13 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,8 +25,8 @@ const makeMatrix = ( ): Matrix => { if (typeof dimensions === "number") { const initialPosition = Array(dimensions).fill(0) as Vector; - const vecDimensions = [...Array(dimensions)].map((_, i) => dimensions - i); - return _makeMatrix(vecDimensions as Vector, initialValues, initialPosition); + const arrayDimensions = [...Array(dimensions)].map((_, i) => dimensions - i); + return _makeMatrix(arrayDimensions as Vector, initialValues, initialPosition); } const initialPosition = Array(dimensions.length).fill(0) as Vector; From 75c369afcd3b431f91f93001554de564752e36ee Mon Sep 17 00:00:00 2001 From: Neef Rehman Date: Tue, 26 Jul 2022 15:12:12 +0100 Subject: [PATCH 4/4] refactor: refactor top-level conditional --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1e8a13..32c2410 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,13 +23,14 @@ const makeMatrix = ( dimensions: D | Vector, initialValues: ValueOrFunction = null ): Matrix => { + const dimensionCount = typeof dimensions === "number" ? dimensions : dimensions.length; + const initialPosition = Array(dimensionCount).fill(0) as Vector; + if (typeof dimensions === "number") { - const initialPosition = Array(dimensions).fill(0) as Vector; const arrayDimensions = [...Array(dimensions)].map((_, i) => dimensions - i); return _makeMatrix(arrayDimensions as Vector, initialValues, initialPosition); } - const initialPosition = Array(dimensions.length).fill(0) as Vector; return _makeMatrix(dimensions, initialValues, initialPosition); }; @@ -42,9 +43,8 @@ function _makeMatrix( currentPosition: Vector ): Matrix { const [currentDimensionLength, ...remainingDimensions] = dimensions; - const remainingDimensionCount = remainingDimensions.length; - const currentDimension = currentPosition.length - 1 - remainingDimensionCount; - const needsRecursion = remainingDimensionCount > 0; + const currentDimension = currentPosition.length - 1 - remainingDimensions.length; + const needsRecursion = remainingDimensions.length > 0; return [...Array(currentDimensionLength)].map((_, i) => { currentPosition[currentDimension] = i;