Skip to content

Commit

Permalink
Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
philipahlberg committed Aug 8, 2020
1 parent cdf9fef commit e6776fd
Show file tree
Hide file tree
Showing 11 changed files with 716 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: publish

on:
release:
types: [created]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- run: yarn install
- run: yarn run build
- run: yarn run test
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- run: yarn install
- run: yarn run build
- run: yarn run test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src
tests
rollup.config.js
tsconfig.json
.github
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) 2020 Philip Ahlberg

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.
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "conveyorbelt",
"version": "0.1.0",
"author": "Philip Ahlberg <[email protected]>",
"description": "An iterator utility library",
"license": "MIT",
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"keywords": [],
"homepage": "https://github.com/philipahlberg/conveyorbelt#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/philipahlberg/conveyorbelt.git"
},
"bugs": {
"url": "https://github.com/philipahlberg/conveyorbelt/issues"
},
"scripts": {
"build": "rollup -c rollup.config.js",
"test": "windtunnel test tests/index.mjs"
},
"dependencies": {},
"devDependencies": {
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-typescript": "^5.0.2",
"@types/node": "^14.0.27",
"@windtunnel/assert": "^0.4.0",
"@windtunnel/cli": "^0.4.0",
"rollup": "^2.23.0",
"tslib": "^2.0.0",
"typescript": "^3.9.7"
},
"peerDependencies": {},
"optionalDependencies": {}
}
27 changes: 27 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import module from 'module';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import pkg from './package.json';

const dependencies = Object.keys(pkg.dependencies);
const peerDependencies = Object.keys(pkg.peerDependencies);

export default {
input: 'src/index.ts',
output: {
dir: 'dist',
format: 'esm',
entryFileNames: '[name].mjs',
},
plugins: [
typescript(),
resolve({
preferBuiltins: true,
}),
],
external: [
...module.builtinModules,
...dependencies,
...peerDependencies,
],
};
240 changes: 240 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
class Iter<T> implements Iterable<T> {
private source: Iterable<T>;

constructor(source: Iterable<T>) {
this.source = source;
}

*[Symbol.iterator](): Iterator<T> {
for (const item of this.source) {
yield item;
}
}

all(fn: (item: T) => boolean): boolean {
return all(fn, this.source);
}

any(fn: (item: T) => boolean): boolean {
return any(fn, this.source);
}

chain(iterable: Iterable<T>): Iterable<T> {
return new Iter(chain(this.source, iterable));
}

count(): bigint {
return count(this.source);
}

enumerate(): Iterable<[bigint, T]> {
return new Iter(enumerate(this.source));
}

filter(fn: (item: T) => boolean): Iterable<T> {
return new Iter(filter(fn, this.source));
}

find(fn: (item: T) => boolean): T | null {
return find(fn, this.source);
}

fold<S>(fn: (state: S, item: T) => S, initial: S): S {
return fold(fn, initial, this.source);
}

first(): T | null {
return first(this.source);
}

last(): T | null {
return last(this.source);
}

map<B>(fn: (item: T) => B): Iterable<B> {
return new Iter(map(fn, this.source));
}

nth(n: number): T | null {
return nth(n, this.source);
}

skip(n: number): Iterable<T> {
return new Iter(skip(n, this.source));
}

skipWhile(fn: (item: T) => boolean): Iterable<T> {
return new Iter(skipWhile(fn, this.source));
}

take(n: number): Iterable<T> {
return new Iter(take(n, this.source));
}

takeWhile(fn: (item: T) => boolean): Iterable<T> {
return new Iter(takeWhile(fn, this.source));
}

collect(): T[] {
return [...this.source];
}
}

class Wrap<T> implements Iterable<T> {
private source: Iterator<T>;

constructor(source: Iterator<T>) {
this.source = source;
}

[Symbol.iterator]() {
return this.source;
}
}

const wrap = <T>(iterator: Iterator<T>): Iterable<T> => {
return new Wrap(iterator);
};

function all<T>(fn: (item: T) => boolean, iterable: Iterable<T>): boolean {
for (const item of iterable) {
if (!fn(item)) {
return false;
}
}
return true;
}

function any<T>(fn: (item: T) => boolean, iterable: Iterable<T>): boolean {
for (const item of iterable) {
if (fn(item)) {
return true;
}
}
return false;
}

function* chain<T>(a: Iterable<T>, b: Iterable<T>): Iterable<T> {
yield* a;
yield* b;
}

function count<T>(iterable: Iterable<T>): bigint {
let n = 0n;
for (const _ of iterable) {
n = n + 1n;
}
return n;
}

function* enumerate<T>(iterable: Iterable<T>): Iterable<[bigint, T]> {
let i = 0n;
for (const item of iterable) {
yield [i, item];
i = i + 1n;
}
}

function* filter<T>(fn: (item: T) => boolean, iterable: Iterable<T>): Iterable<T> {
for (const item of iterable) {
if (fn(item)) {
yield item;
}
}
}

function find<T>(fn: (item: T) => boolean, iterable: Iterable<T>): T | null {
for (const item of iterable) {
if (fn(item)) {
return item;
}
}
return null;
}

function fold<T, S>(fn: (state: S, item: T) => S, initial: S, iterable: Iterable<T>): S {
let state: S = initial;
for (const item of iterable) {
state = fn(state, item);
}
return state;
}

function first<T>(iterable: Iterable<T>): T | null {
for (const item of iterable) {
return item;
}
return null;
}

function last<T>(iterable: Iterable<T>): T | null {
let last: T | null = null;
for (const item of iterable) {
last = item;
}
return last;
}

function* map<T, B>(fn: (item: T) => B, iterable: Iterable<T>): Iterable<B> {
for (const item of iterable) {
yield fn(item);
}
}

function nth<T>(n: number, iterable: Iterable<T>): T | null {
for (const item of iterable) {
if (n === 0) {
return item;
}
n = n - 1;
}
return null;
}

function* skip<T>(n: number, iterable: Iterable<T>): Iterable<T> {
const iterator = iterable[Symbol.iterator]();
while (n > 0) {
iterator.next();
n = n - 1;
}
yield* wrap(iterator);
}

function* skipWhile<T>(fn: (item: T) => boolean, iterable: Iterable<T>): Iterable<T> {
const iterator = iterable[Symbol.iterator]();
let value: T;
while (true) {
const next = iterator.next();
value = next.value;
if (next.done || !fn(next.value)) {
break;
}
}
yield value;
yield* wrap(iterator);
}

function* take<T>(n: number, iterable: Iterable<T>): Iterable<T> {
for (const item of iterable) {
if (n > 0) {
yield item;
} else {
break;
}
n = n - 1;
}
}

function* takeWhile<T>(fn: (item: T) => boolean, iterable: Iterable<T>): Iterable<T> {
for (const item of iterable) {
if (fn(item)) {
yield item;
} else {
break;
}
}
}

export const iter = <T>(iterable: Iterable<T>): Iterable<T> => {
return new Iter(iterable);
};
Loading

0 comments on commit e6776fd

Please sign in to comment.