Skip to content

Commit e6776fd

Browse files
committed
Setup
1 parent cdf9fef commit e6776fd

File tree

11 files changed

+716
-0
lines changed

11 files changed

+716
-0
lines changed

.github/workflows/publish.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: publish
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v1
13+
with:
14+
node-version: 14
15+
- run: yarn install
16+
- run: yarn run build
17+
- run: yarn run test
18+
- run: yarn publish
19+
env:
20+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: 14
19+
- run: yarn install
20+
- run: yarn run build
21+
- run: yarn run test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
src
2+
tests
3+
rollup.config.js
4+
tsconfig.json
5+
.github

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Philip Ahlberg
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.

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "conveyorbelt",
3+
"version": "0.1.0",
4+
"author": "Philip Ahlberg <philipahlberg@users.noreply.github.com>",
5+
"description": "An iterator utility library",
6+
"license": "MIT",
7+
"main": "dist/index.mjs",
8+
"module": "dist/index.mjs",
9+
"types": "dist/index.d.ts",
10+
"keywords": [],
11+
"homepage": "https://github.com/philipahlberg/conveyorbelt#readme",
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/philipahlberg/conveyorbelt.git"
15+
},
16+
"bugs": {
17+
"url": "https://github.com/philipahlberg/conveyorbelt/issues"
18+
},
19+
"scripts": {
20+
"build": "rollup -c rollup.config.js",
21+
"test": "windtunnel test tests/index.mjs"
22+
},
23+
"dependencies": {},
24+
"devDependencies": {
25+
"@rollup/plugin-node-resolve": "^8.4.0",
26+
"@rollup/plugin-typescript": "^5.0.2",
27+
"@types/node": "^14.0.27",
28+
"@windtunnel/assert": "^0.4.0",
29+
"@windtunnel/cli": "^0.4.0",
30+
"rollup": "^2.23.0",
31+
"tslib": "^2.0.0",
32+
"typescript": "^3.9.7"
33+
},
34+
"peerDependencies": {},
35+
"optionalDependencies": {}
36+
}

rollup.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import module from 'module';
2+
import resolve from '@rollup/plugin-node-resolve';
3+
import typescript from '@rollup/plugin-typescript';
4+
import pkg from './package.json';
5+
6+
const dependencies = Object.keys(pkg.dependencies);
7+
const peerDependencies = Object.keys(pkg.peerDependencies);
8+
9+
export default {
10+
input: 'src/index.ts',
11+
output: {
12+
dir: 'dist',
13+
format: 'esm',
14+
entryFileNames: '[name].mjs',
15+
},
16+
plugins: [
17+
typescript(),
18+
resolve({
19+
preferBuiltins: true,
20+
}),
21+
],
22+
external: [
23+
...module.builtinModules,
24+
...dependencies,
25+
...peerDependencies,
26+
],
27+
};

src/index.ts

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
class Iter<T> implements Iterable<T> {
2+
private source: Iterable<T>;
3+
4+
constructor(source: Iterable<T>) {
5+
this.source = source;
6+
}
7+
8+
*[Symbol.iterator](): Iterator<T> {
9+
for (const item of this.source) {
10+
yield item;
11+
}
12+
}
13+
14+
all(fn: (item: T) => boolean): boolean {
15+
return all(fn, this.source);
16+
}
17+
18+
any(fn: (item: T) => boolean): boolean {
19+
return any(fn, this.source);
20+
}
21+
22+
chain(iterable: Iterable<T>): Iterable<T> {
23+
return new Iter(chain(this.source, iterable));
24+
}
25+
26+
count(): bigint {
27+
return count(this.source);
28+
}
29+
30+
enumerate(): Iterable<[bigint, T]> {
31+
return new Iter(enumerate(this.source));
32+
}
33+
34+
filter(fn: (item: T) => boolean): Iterable<T> {
35+
return new Iter(filter(fn, this.source));
36+
}
37+
38+
find(fn: (item: T) => boolean): T | null {
39+
return find(fn, this.source);
40+
}
41+
42+
fold<S>(fn: (state: S, item: T) => S, initial: S): S {
43+
return fold(fn, initial, this.source);
44+
}
45+
46+
first(): T | null {
47+
return first(this.source);
48+
}
49+
50+
last(): T | null {
51+
return last(this.source);
52+
}
53+
54+
map<B>(fn: (item: T) => B): Iterable<B> {
55+
return new Iter(map(fn, this.source));
56+
}
57+
58+
nth(n: number): T | null {
59+
return nth(n, this.source);
60+
}
61+
62+
skip(n: number): Iterable<T> {
63+
return new Iter(skip(n, this.source));
64+
}
65+
66+
skipWhile(fn: (item: T) => boolean): Iterable<T> {
67+
return new Iter(skipWhile(fn, this.source));
68+
}
69+
70+
take(n: number): Iterable<T> {
71+
return new Iter(take(n, this.source));
72+
}
73+
74+
takeWhile(fn: (item: T) => boolean): Iterable<T> {
75+
return new Iter(takeWhile(fn, this.source));
76+
}
77+
78+
collect(): T[] {
79+
return [...this.source];
80+
}
81+
}
82+
83+
class Wrap<T> implements Iterable<T> {
84+
private source: Iterator<T>;
85+
86+
constructor(source: Iterator<T>) {
87+
this.source = source;
88+
}
89+
90+
[Symbol.iterator]() {
91+
return this.source;
92+
}
93+
}
94+
95+
const wrap = <T>(iterator: Iterator<T>): Iterable<T> => {
96+
return new Wrap(iterator);
97+
};
98+
99+
function all<T>(fn: (item: T) => boolean, iterable: Iterable<T>): boolean {
100+
for (const item of iterable) {
101+
if (!fn(item)) {
102+
return false;
103+
}
104+
}
105+
return true;
106+
}
107+
108+
function any<T>(fn: (item: T) => boolean, iterable: Iterable<T>): boolean {
109+
for (const item of iterable) {
110+
if (fn(item)) {
111+
return true;
112+
}
113+
}
114+
return false;
115+
}
116+
117+
function* chain<T>(a: Iterable<T>, b: Iterable<T>): Iterable<T> {
118+
yield* a;
119+
yield* b;
120+
}
121+
122+
function count<T>(iterable: Iterable<T>): bigint {
123+
let n = 0n;
124+
for (const _ of iterable) {
125+
n = n + 1n;
126+
}
127+
return n;
128+
}
129+
130+
function* enumerate<T>(iterable: Iterable<T>): Iterable<[bigint, T]> {
131+
let i = 0n;
132+
for (const item of iterable) {
133+
yield [i, item];
134+
i = i + 1n;
135+
}
136+
}
137+
138+
function* filter<T>(fn: (item: T) => boolean, iterable: Iterable<T>): Iterable<T> {
139+
for (const item of iterable) {
140+
if (fn(item)) {
141+
yield item;
142+
}
143+
}
144+
}
145+
146+
function find<T>(fn: (item: T) => boolean, iterable: Iterable<T>): T | null {
147+
for (const item of iterable) {
148+
if (fn(item)) {
149+
return item;
150+
}
151+
}
152+
return null;
153+
}
154+
155+
function fold<T, S>(fn: (state: S, item: T) => S, initial: S, iterable: Iterable<T>): S {
156+
let state: S = initial;
157+
for (const item of iterable) {
158+
state = fn(state, item);
159+
}
160+
return state;
161+
}
162+
163+
function first<T>(iterable: Iterable<T>): T | null {
164+
for (const item of iterable) {
165+
return item;
166+
}
167+
return null;
168+
}
169+
170+
function last<T>(iterable: Iterable<T>): T | null {
171+
let last: T | null = null;
172+
for (const item of iterable) {
173+
last = item;
174+
}
175+
return last;
176+
}
177+
178+
function* map<T, B>(fn: (item: T) => B, iterable: Iterable<T>): Iterable<B> {
179+
for (const item of iterable) {
180+
yield fn(item);
181+
}
182+
}
183+
184+
function nth<T>(n: number, iterable: Iterable<T>): T | null {
185+
for (const item of iterable) {
186+
if (n === 0) {
187+
return item;
188+
}
189+
n = n - 1;
190+
}
191+
return null;
192+
}
193+
194+
function* skip<T>(n: number, iterable: Iterable<T>): Iterable<T> {
195+
const iterator = iterable[Symbol.iterator]();
196+
while (n > 0) {
197+
iterator.next();
198+
n = n - 1;
199+
}
200+
yield* wrap(iterator);
201+
}
202+
203+
function* skipWhile<T>(fn: (item: T) => boolean, iterable: Iterable<T>): Iterable<T> {
204+
const iterator = iterable[Symbol.iterator]();
205+
let value: T;
206+
while (true) {
207+
const next = iterator.next();
208+
value = next.value;
209+
if (next.done || !fn(next.value)) {
210+
break;
211+
}
212+
}
213+
yield value;
214+
yield* wrap(iterator);
215+
}
216+
217+
function* take<T>(n: number, iterable: Iterable<T>): Iterable<T> {
218+
for (const item of iterable) {
219+
if (n > 0) {
220+
yield item;
221+
} else {
222+
break;
223+
}
224+
n = n - 1;
225+
}
226+
}
227+
228+
function* takeWhile<T>(fn: (item: T) => boolean, iterable: Iterable<T>): Iterable<T> {
229+
for (const item of iterable) {
230+
if (fn(item)) {
231+
yield item;
232+
} else {
233+
break;
234+
}
235+
}
236+
}
237+
238+
export const iter = <T>(iterable: Iterable<T>): Iterable<T> => {
239+
return new Iter(iterable);
240+
};

0 commit comments

Comments
 (0)