Skip to content

Commit 64b4585

Browse files
committed
toggle selection #2
1 parent 3bfa225 commit 64b4585

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "macos-multi-select",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Given a list of ids, and an action, return a list of selected items with the same behaviour of macOS finder list view selection.",
55
"main": "dist/index.js",
66
"repository": "[email protected]:codingedgar/macos-multi-select.git",

src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
type Context = {
1+
export type Context = {
22
list: string[],
33
selected: string[],
44
}
55

66
type Command =
7-
| {type: "SINGLE SELECT", id: string}
7+
| { type: "SELECT ONE", id: string }
8+
| { type: "TOGGLE SELECTION", id: string }
89

910
export function multiselect(context: Context, command: Command): Context {
10-
if (command.type === 'SINGLE SELECT' && context.list.includes(command.id)) {
11+
if (command.type === "SELECT ONE" && context.list.includes(command.id)) {
1112
return {
1213
...context,
1314
selected: [command.id]
1415
};
16+
} else if (
17+
command.type === 'TOGGLE SELECTION' &&
18+
context.list.includes(command.id) &&
19+
context.selected.includes(command.id)
20+
) {
21+
return {
22+
...context,
23+
selected: context.selected.filter(x => x !== command.id),
24+
};
25+
} else if (
26+
command.type === 'TOGGLE SELECTION' &&
27+
context.list.includes(command.id)
28+
) {
29+
return {
30+
...context,
31+
selected: context.selected.concat([command.id])
32+
};
1533
} else {
1634
return context;
1735
}

src/spec/selectAnItem.spec.ts renamed to src/spec/selectOne.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fc from 'fast-check';
2-
import {multiselect} from '../index';
2+
import { multiselect } from '../index';
3+
34
describe('Select an Item', () => {
45
test('should be able to select one item in a non empty list', () => {
56

@@ -26,7 +27,7 @@ describe('Select an Item', () => {
2627
selected: [],
2728
},
2829
{
29-
type: 'SINGLE SELECT',
30+
type: "SELECT ONE",
3031
id,
3132
}
3233
)
@@ -69,7 +70,7 @@ describe('Select an Item', () => {
6970
selected: [selectedId],
7071
},
7172
{
72-
type: 'SINGLE SELECT',
73+
type: "SELECT ONE",
7374
id,
7475
}
7576
)

src/spec/toggleSelection.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fc from 'fast-check';
2+
import { multiselect, Context } from '../index';
3+
4+
describe('TOGGLE SELECTION', () => {
5+
test('should be able to add to selection one item in a non empty list', () => {
6+
7+
fc.assert(
8+
fc.property(
9+
fc.set(
10+
fc.string()
11+
)
12+
.filter(list => list.length > 0)
13+
.chain(list =>
14+
fc.record({
15+
list: fc.constant(list),
16+
toSelect: fc.set(
17+
fc.integer(0, list.length-1)
18+
.map(index => list[index]),
19+
),
20+
})
21+
),
22+
({
23+
list,
24+
toSelect,
25+
}) => {
26+
27+
expect(
28+
toSelect.reduce(
29+
(context: Context, id) =>
30+
multiselect(
31+
context,
32+
{
33+
type: "TOGGLE SELECTION",
34+
id,
35+
}
36+
),
37+
{
38+
list,
39+
selected: []
40+
}
41+
)
42+
)
43+
.toEqual({
44+
list,
45+
selected: toSelect
46+
})
47+
}
48+
)
49+
)
50+
51+
});
52+
})

0 commit comments

Comments
 (0)