Skip to content

Commit fbe0cb1

Browse files
committed
Initial
0 parents  commit fbe0cb1

12 files changed

+259
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_size = 2
7+
indent_style = space
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true

.github/workflows/test_master.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
- dev
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [14.x, 16.x, 17.x, 18.x, 19.x, 20.x, 21.x]
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
27+
- name: Run tests
28+
run: |
29+
npm install
30+
npm run test
31+
env:
32+
CI: true
33+
34+
- name: Upload coverage report to Coveralls
35+
uses: coverallsapp/github-action@master
36+
with:
37+
github-token: ${{ secrets.GITHUB_TOKEN }}
38+
parallel: true
39+
40+
coveralls:
41+
needs: build
42+
runs-on: ubuntu-latest
43+
44+
steps:
45+
- name: Coveralls finished
46+
uses: coverallsapp/github-action@master
47+
with:
48+
github-token: ${{ secrets.GITHUB_TOKEN }}
49+
parallel-finished: true

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.idea
2+
node_modules
3+
dist
4+
package-lock.json
5+
coverage
6+
es
7+
lib

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Collections View Change Log
2+
3+
## v0.1.0 - TBD

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Smoren
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Collection View for TypeScript and JavaScript
2+

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').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
};

package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "collections-view",
3+
"version": "0.1.0",
4+
"description": "TODO",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/Smoren/collections-view-ts.git"
9+
},
10+
"author": {
11+
"name": "Smoren",
12+
"email": "ofigate@gmail.com",
13+
"url": "https://github.com/Smoren"
14+
},
15+
"homepage": "https://github.com/Smoren/collections-view-ts#readme",
16+
"bugs": {
17+
"url": "https://github.com/Smoren/collections-view-ts/issues"
18+
},
19+
"main": "./lib/index.js",
20+
"module": "./es/index.js",
21+
"scripts": {
22+
"build": "npm run build:commonjs && npm run build:es",
23+
"build:commonjs": "rimraf lib && tsc --downlevelIteration --target es5 --outDir lib --module commonjs --declaration",
24+
"build:es": "rimraf es && tsc --target es6 --outDir es --module es6",
25+
"test": "jest --coverage",
26+
"jest": "jest",
27+
"format": "ts-scripts format",
28+
"lint": "ts-scripts lint",
29+
"prepublishOnly": "npm run build"
30+
},
31+
"files": [
32+
"es",
33+
"lib",
34+
"src",
35+
"tests/examples",
36+
"tests/fixture.ts",
37+
"tests/tools"
38+
],
39+
"keywords": [
40+
"collection",
41+
"view"
42+
],
43+
"devDependencies": {
44+
"@borderless/ts-scripts": "^0.13.6",
45+
"@types/jest": "^29.2.4",
46+
"jest": "^29.3.1",
47+
"rimraf": "^4.4.0",
48+
"ts-expect": "^1.1.0",
49+
"ts-jest": "^29.0.3",
50+
"typescript": "^4.9.4"
51+
},
52+
"types": "./lib/index.d.ts",
53+
"contributors": [],
54+
"jsnext:main": "./es/index.js",
55+
"sideEffects": false
56+
}

src/array-view.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { ArrayViewConfig } from "./types";
2+
3+
class ExtendableProxy<T extends Object> {
4+
public readonly target: T;
5+
6+
constructor(target: T, handler: ProxyHandler<T>) {
7+
this.target = target;
8+
return new Proxy(this, {
9+
set(_, key, value, proxy) {
10+
if (handler.set === undefined) {
11+
return false;
12+
}
13+
14+
return handler.set(target, key, value, proxy);
15+
},
16+
get(object, key, proxy) {
17+
if (handler.get === undefined) {
18+
return undefined;
19+
}
20+
21+
return handler.get(target, key, proxy);
22+
}
23+
});
24+
}
25+
}
26+
27+
export class ArrayView<T> extends ExtendableProxy<Array<T>> {
28+
constructor(target: Array<T>, config: ArrayViewConfig<T>) {
29+
const {
30+
index,
31+
readonly = false,
32+
fixed = false,
33+
onOutOfRange = () => undefined,
34+
} = config;
35+
36+
super(target, {
37+
get(target, prop) {
38+
const i = index(Number(prop), target);
39+
40+
if (i in target) {
41+
return target[i];
42+
}
43+
44+
return onOutOfRange(Number(prop));
45+
},
46+
set(target, prop, value): boolean {
47+
const i = index(Number(prop), target);
48+
49+
if (readonly) {
50+
return false;
51+
}
52+
53+
if (fixed && !(i in target)) {
54+
return false;
55+
}
56+
57+
target[i] = value;
58+
return true;
59+
}
60+
});
61+
}
62+
}

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type RecordKey = string | number | symbol;
2+
3+
export type ArrayViewConfig<T> = {
4+
index: (i: number, target: Array<T>) => number;
5+
readonly?: boolean;
6+
fixed?: boolean;
7+
onOutOfRange?: (i: number) => T | undefined;
8+
};

tests/examples/examples.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from "@jest/globals";
2+
import { ArrayView } from "../../src/array-view";
3+
4+
it("First example", () => {
5+
const input = [1, 2, 3];
6+
7+
const view = new ArrayView(input, {
8+
index: (i, target) => target.length - 1 - i,
9+
});
10+
11+
const actual = [];
12+
13+
14+
expect(true).toBe(true);
15+
});

tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "commonjs",
5+
"moduleResolution": "Node",
6+
"lib": ["es6", "es2018.asynciterable"],
7+
"rootDir": "./src",
8+
"allowJs": false,
9+
"outDir": "./dist",
10+
"esModuleInterop": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"strict": true,
13+
"skipLibCheck": true,
14+
"sourceMap": true,
15+
"inlineSources": true,
16+
"declaration": true,
17+
"isolatedModules": true
18+
},
19+
"exclude": ["tests"]
20+
}

0 commit comments

Comments
 (0)