Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace concat with spread: items can be arrays #527

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/array-permutations/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function permutations(arr) {
for (var j = 0, len2 = partial.length; j <= len2; j++) {
var start = partial.slice(0, j);
var end = partial.slice(j);
var merged = start.concat(first, end);
var merged = start.concat([first], end);

output.push(merged);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/array-permutations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function permutations(arr) {
for (var j = 0, len2 = partial.length; j <= len2; j++) {
var start = partial.slice(0, j);
var end = partial.slice(j);
var merged = start.concat(first, end);
var merged = start.concat([first], end);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"first" is an item of the array being permuted. So if arr has type T[], then:

arr: T[]
first: T
start: T[]
end: T[]

The arguments to concat should both be of type T[].

The fact that this was (mostly) working before is due to JS's concat behaving somewhat like push: it can accept non-array values. But if T itself is an array type, then this did not work as intended.


output.push(merged);
}
Expand Down
4 changes: 3 additions & 1 deletion test/array-permutations/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var test = require('../util/test')(__filename);
var permutations = require('../../packages/array-permutations');

test('array with all permutations', function(t) {
t.plan(4);
t.plan(5);

t.deepEqual(
permutations(['javascript', 'typescript']),
Expand All @@ -17,6 +17,8 @@ test('array with all permutations', function(t) {
t.deepEqual(permutations([]), []);
t.deepEqual(permutations([1]), [[1]]);

t.deepEqual(permutations([[1], [2]]), [[[1], [2]], [[2], [1]]]);

t.end();
});

Expand Down