Skip to content

Commit 3bfa225

Browse files
committed
select an item #1
1 parent eb1da0f commit 3bfa225

File tree

6 files changed

+2831
-0
lines changed

6 files changed

+2831
-0
lines changed

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "macos-multi-select",
3+
"version": "0.0.1",
4+
"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.",
5+
"main": "dist/index.js",
6+
"repository": "[email protected]:codingedgar/macos-multi-select.git",
7+
"author": "codingedgar <[email protected]>",
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "jest",
11+
"test-watch": "jest --watch",
12+
"build": "rm -r dist && tsc"
13+
},
14+
"bugs": {
15+
"url": "https://github.com/davidkpiano/xstate/issues"
16+
},
17+
"devDependencies": {
18+
"@types/jest": "^27.0.0",
19+
"fast-check": "^2.17.0",
20+
"jest": "^27.0.6",
21+
"ts-jest": "^27.0.4",
22+
"typescript": "^4.3.5"
23+
},
24+
"dependencies": {
25+
"fp-ts": "^2.11.1"
26+
}
27+
}

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type Context = {
2+
list: string[],
3+
selected: string[],
4+
}
5+
6+
type Command =
7+
| {type: "SINGLE SELECT", id: string}
8+
9+
export function multiselect(context: Context, command: Command): Context {
10+
if (command.type === 'SINGLE SELECT' && context.list.includes(command.id)) {
11+
return {
12+
...context,
13+
selected: [command.id]
14+
};
15+
} else {
16+
return context;
17+
}
18+
}

src/spec/selectAnItem.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import fc from 'fast-check';
2+
import {multiselect} from '../index';
3+
describe('Select an Item', () => {
4+
test('should be able to select one item in a non empty list', () => {
5+
6+
fc.assert(
7+
fc.property(
8+
fc.set(
9+
fc.string()
10+
)
11+
.filter(list => list.length > 0)
12+
.chain(list =>
13+
fc.record({
14+
list: fc.constant(list),
15+
id: fc.integer(0, list.length-1)
16+
.map(index => list[index]),
17+
})
18+
),
19+
({
20+
list,
21+
id,
22+
}) => {
23+
expect(
24+
multiselect({
25+
list,
26+
selected: [],
27+
},
28+
{
29+
type: 'SINGLE SELECT',
30+
id,
31+
}
32+
)
33+
)
34+
.toEqual({
35+
list,
36+
selected: [id]
37+
})
38+
}
39+
)
40+
)
41+
42+
});
43+
44+
test('if another single select if performed, the previous is deselected', () => {
45+
fc.assert(
46+
fc.property(
47+
fc.set(
48+
fc.string()
49+
)
50+
.filter(list => list.length > 1)
51+
.chain(list =>
52+
fc.record({
53+
list: fc.constant(list),
54+
selectedId: fc.integer(0, list.length-1)
55+
.map(index => list[index]),
56+
id: fc.integer(0, list.length-1)
57+
.map(index => list[index]),
58+
})
59+
.filter(context => context.selectedId !== context.id)
60+
),
61+
({
62+
list,
63+
selectedId,
64+
id,
65+
}) => {
66+
expect(
67+
multiselect({
68+
list,
69+
selected: [selectedId],
70+
},
71+
{
72+
type: 'SINGLE SELECT',
73+
id,
74+
}
75+
)
76+
)
77+
.toEqual({
78+
list,
79+
selected: [id]
80+
})
81+
}
82+
)
83+
)
84+
});
85+
})

tsconfig.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Basic Options */
6+
// "incremental": true, /* Enable incremental compilation */
7+
"target": "es5",
8+
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
9+
"module": "commonjs",
10+
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
11+
// "lib": [], /* Specify library files to be included in the compilation. */
12+
// "allowJs": true, /* Allow javascript files to be compiled. */
13+
// "checkJs": true, /* Report errors in .js files. */
14+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
15+
"declaration": true,
16+
/* Generates corresponding '.d.ts' file. */
17+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
18+
"sourceMap": true,
19+
/* Generates corresponding '.map' file. */
20+
// "outFile": "./", /* Concatenate and emit output to single file. */
21+
"outDir": "./dist",
22+
/* Redirect output structure to the directory. */
23+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
24+
// "composite": true, /* Enable project compilation */
25+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
26+
// "removeComments": true, /* Do not emit comments to output. */
27+
// "noEmit": true, /* Do not emit outputs. */
28+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
29+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
30+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
31+
32+
/* Strict Type-Checking Options */
33+
"strict": true,
34+
/* Enable all strict type-checking options. */
35+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
36+
// "strictNullChecks": true, /* Enable strict null checks. */
37+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
38+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
39+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
40+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
41+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
42+
43+
/* Additional Checks */
44+
// "noUnusedLocals": true, /* Report errors on unused locals. */
45+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
46+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
47+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
48+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
49+
50+
/* Module Resolution Options */
51+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
52+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
53+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
54+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
55+
// "typeRoots": [], /* List of folders to include type definitions from. */
56+
// "types": [], /* Type declaration files to be included in compilation. */
57+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
58+
"esModuleInterop": true,
59+
/* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
60+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
61+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
62+
63+
/* Source Map Options */
64+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
65+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
66+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
67+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
68+
69+
/* Experimental Options */
70+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
71+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
72+
73+
/* Advanced Options */
74+
"skipLibCheck": true,
75+
/* Skip type checking of declaration files. */
76+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
77+
},
78+
"include": [
79+
"src"
80+
],
81+
"exclude": [
82+
"src/**/*.spec.ts"
83+
]
84+
}

0 commit comments

Comments
 (0)