Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberixae committed Sep 19, 2022
0 parents commit 2eda17c
Show file tree
Hide file tree
Showing 21 changed files with 5,273 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
lib
tsconfig.eslint.json
coverage
21 changes: 21 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["eslint-config-maasglobal-ts"],
"rules": {
"simple-import-sort/sort": [
1,
{
"groups": [["^\\u0000"], ["^maasglobal-prelude"], ["^[^.]"], ["^\\."]]
}
]
},
"overrides": [
{
"files": ["src/readme.ts"],
"rules": {
"@typescript-eslint/consistent-type-definitions": 0,
"import/no-duplicates": 0,
"simple-import-sort/sort": 0
}
}
]
}
5 changes: 5 additions & 0 deletions .extract_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


import sys; sys.stdout.write(('/*' + '\n' + '/*'.join(
'*/'.join(sys.stdin.read().split('```typescript')).split('```')
) + '*/').replace("from 'maasglobal-template-ts'", "from './index'").replace("from 'maasglobal-template-ts/lib/", "from './") + '\n' + '/* export { sum } */')
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
pull_request:
branches:
- main

jobs:
check:
runs-on: ubuntu-latest
timeout-minutes: 30
name: CI
steps:
- uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Dependencies
run: |
yarn
- name: Run Tests
run: |
yarn ci
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.swp
.DS_Store
lib
node_modules
package-lock.json
yarn-error.log
src/readme.ts
coverage
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- '10'
before_install:
- yarn
script:
- yarn ci
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 MaaS Global Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# MaaS Global List TS

Lazy list implementation based on generators

## Devops

```
npm install -g yarn # install yarn
yarn # install dependencies
yarn lint # run linters
yarn typecheck # run static type checks
yarn test # run tests
yarn prettify # auto format code base
yarn ci # perform a local CI test run
yarn build # create a production build
yarn clean # remove build artefacts
yarn readme-ts # extract readme code examples
yarn deploy-npm # deploy npm package
yarn deploy-alpha # deploy prelease npm package
```
23 changes: 23 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
globals: {
'ts-jest': {
diagnostics: {
ignoreCodes: [6133, 6196],
},
},
},
transform: {
'^.+\\.(t|j)sx?$': 'ts-jest',
},
roots: ['src'],
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts'],
coverageThreshold: {
'src/**/*.ts': {
branches: 0,
functions: 100,
lines: 100,
statements: 80,
},
},
};
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "maasglobal-list-ts",
"version": "0.0.1",
"description": "Lazy list implementation based on generators",
"license": "MIT",
"main": "lib/index.js",
"files": [
"lib/**/*"
],
"peerDependencies": {
"fp-ts": "^2.12.3"
},
"devDependencies": {
"@types/jest": "^26.0.22",
"@types/node": "^14.14.41",
"@typescript-eslint/eslint-plugin": "^4.3.0",
"@typescript-eslint/parser": "^4.3.0",
"eslint": "^6.8.0",
"eslint-config-maasglobal-ts": "^0.0.13",
"eslint-config-prettier": "^6.12.0",
"eslint-plugin-deprecation": "^1.2.0",
"eslint-plugin-fp": "^2.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-json": "^2.1.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-simple-import-sort": "^5.0.3",
"fp-ts": "^2.12.3",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.5.5",
"typescript": "^4.8.3"
},
"scripts": {
"readme-ts": "python3 .extract_code.py < README.md > src/readme.ts",
"test": "jest --no-cache",
"clean": "rm -rf lib",
"typecheck": "tsc -p src/tsconfig.json",
"build": "yarn clean && tsc",
"eslint": "eslint --max-warnings=0 '**/*.{ts,js,json}'",
"eslint-fix": "yarn eslint --fix||true",
"prettier": "prettier --write --ignore-path .gitignore --config node_modules/eslint-config-maasglobal-ts/prettierrc.js '**/*.{css,html,js,ts,json,md,yaml,yml}'",
"prettify": "yarn eslint-fix && yarn prettier",
"lint": "yarn eslint",
"ci": "yarn readme-ts && yarn typecheck && yarn test && yarn lint && yarn build",
"deploy-npm": "yarn ci && yarn publish --non-interactive",
"deploy-alpha": "yarn deploy-npm --tag alpha"
}
}
20 changes: 20 additions & 0 deletions src/InfiniteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type InfiniteList<A> = () => Generator<A, never, undefined>;

export function of<A>(a: A): InfiniteList<A> {
return function* () {
while (true) {
yield a;
}
};
}

export function map<A, B>(f: (a: A) => B): (ila: InfiniteList<A>) => InfiniteList<B> {
return (ila) =>
function* () {
const ha = ila();
while (true) {
const { value } = ha.next();
yield f(value);
}
};
}
42 changes: 42 additions & 0 deletions src/List.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export type List<A> = () => Generator<A, unknown, undefined>;

export function of<A>(a: A): List<A> {
return function* () {
yield a;
};
}

export function fromArray<A>(aa: Array<A>): List<A> {
return function* () {
yield* aa;
};
}

export function toArray<A>(la: List<A>): Array<A> {
const ha = la();
return [...ha];
}

export function map<A, B>(f: (a: A) => B): (la: List<A>) => List<B> {
return (la) =>
function* () {
const ha = la();
for (const a of ha) {
yield f(a);
}
};
}

export function take(n: number): <A>(la: List<A>) => List<A> {
return (la) =>
function* () {
const ha = la();
for (let i = 0; i < n; i += 1) {
const { done, value } = ha.next();
if (done) {
return;
}
yield value;
}
};
}
39 changes: 39 additions & 0 deletions src/NonEmptyList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { NonEmptyArray } from 'fp-ts/NonEmptyArray';

export type NonEmptyList<A> = () => Generator<A, A, undefined>;

export function of<A>(a: A): NonEmptyList<A> {
return function* () {
const sample = a;
yield a;
return sample;
};
}

export function fromNonEmptyArray<A>(nea: NonEmptyArray<A>): NonEmptyList<A> {
return function* () {
const [sample] = nea;
yield* nea;
return sample;
};
}

export function toNonEmptyArray<A>(la: NonEmptyList<A>): NonEmptyArray<A> {
const ha = la();
return [...ha] as NonEmptyArray<A>;
}

export function map<A, B>(f: (a: A) => B): (nela: NonEmptyList<A>) => NonEmptyList<B> {
return (nela) =>
function* () {
const ha = nela();
while (true) {
const { done, value } = ha.next();
if (done) {
const sample = value;
return f(sample);
}
yield f(value);
}
};
}
54 changes: 54 additions & 0 deletions src/__tests__/InfiniteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { pipe } from 'fp-ts/function';

import type { InfiniteList } from '../InfiniteList';
import * as InfiniteList_ from '../InfiniteList';
import type { List } from '../List';
import * as List_ from '../List';
import type { NonEmptyList } from '../NonEmptyList';

describe('InfiniteList', () => {
describe('type', () => {
it('should be assignable to List', async () => {
const _test: List<number> = InfiniteList_.of(123);
});
it('should be assignable to NonEmptyList', async () => {
const _test: NonEmptyList<number> = InfiniteList_.of(123);
});
it('should be assignable to InfiniteList', async () => {
const _test: InfiniteList<number> = InfiniteList_.of(123);
});
});

it('should be compatible with List take', async () => {
const list: InfiniteList<number> = function* () {
while (true) {
yield 123;
}
};
const result = pipe(list, List_.take(3), List_.toArray);
expect(result).toStrictEqual([123, 123, 123]);
});

describe('of function', () => {
it('should construct a InfiniteList', async () => {
const result = pipe(InfiniteList_.of(123), List_.take(3), List_.toArray);
expect(result).toStrictEqual([123, 123, 123]);
});
});

describe('map function', () => {
it('should map a NonEmptyList', async () => {
const double = (x: number) => 2 * x;
const input = 123;
const count = 3;
const result = pipe(
InfiniteList_.of(input),
InfiniteList_.map(double),
List_.take(count),
List_.toArray,
);
const expected = Array(count).fill(input).map(double);
expect(result).toStrictEqual(expected);
});
});
});
Loading

0 comments on commit 2eda17c

Please sign in to comment.