Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 iterators - enable ForOf for collections #47

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## Fork Info

This is fork of [linq-collections](https://github.com/isc30/linq-collections) library by [isc30](https://github.com/isc30). Only support for ES6 iterators were added. It allows direct usage of `ForOf`. Example:

```
const list = new List([1, 2, 3]);
for (const item of list) {
console.log(item);
}
```

# Linq-Collections: (IEnumerable, ...) + (List, Dictionary, ...)

[![npm version](https://img.shields.io/npm/v/linq-collections.svg)](https://npmjs.org/package/linq-collections)
Expand Down
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "linq-collections",
"name": "linq-collections-es6",
"version": "1.0.255",
"description": "Linq-Collections (ES5): [IEnumerable, IQueryable, ...] + [List, Dictionary, Stack, ... + readonly]",
"description": "Linq-Collections (ES6): [IEnumerable, IQueryable, ...] + [List, Dictionary, Stack, ... + readonly]. Compatible with ES6 iterators.",
"main": "./build/src/Linq.js",
"files": [
"build/src",
Expand All @@ -14,18 +14,15 @@
"typings": "./build/src/Linq.d.ts",
"types": "./build/src/Linq.d.ts",
"author": {
"name": "Ivan Sanz",
"email": "ivansanzcarasa@gmail.com",
"url": "https://github.com/isc30"
},
"bugs": {
"url": "https://github.com/isc30/linq-collections/issues"
"name": "Martin Volek",
"email": "martin@vdolek.cz",
"url": "https://github.com/vdolek"
},
"repository": {
"type": "git",
"url": "https://github.com/isc30/linq-collections"
"url": "https://github.com/vdolek/linq-collections-es6"
},
"homepage": "https://github.com/isc30/linq-collections#readme",
"homepage": "https://github.com/vdolek/linq-collections-es6#readme",
"scripts": {
"node-compile": "tsc",
"web-tests-compile": "browserify ./test/TestSuite.ts -p [tsify] > linq-tests.web.js",
Expand Down
4 changes: 4 additions & 0 deletions src/Collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export abstract class EnumerableCollection<TElement>
public abstract asEnumerable(): IEnumerable<TElement>;
public abstract toArray(): TElement[];

[Symbol.iterator](): Iterator<TElement> {
return this.asEnumerable()[Symbol.iterator]();
}

public toList(): IList<TElement>
{
return new List<TElement>(this.toArray());
Expand Down
9 changes: 9 additions & 0 deletions src/Enumerables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export type IGrouping<TKey, TElement> = IKeyValue<TKey, IQueryable<TElement>>;

export interface IQueryable<TOut>
{
[Symbol.iterator](): Iterator<TOut>;

copy(): IQueryable<TOut>;

asEnumerable(): IEnumerable<TOut>;
Expand Down Expand Up @@ -179,6 +181,13 @@ export abstract class EnumerableBase<TElement, TOut> implements IEnumerable<TOut
public abstract copy(): IEnumerable<TOut>;
public abstract value(): TOut;

*[Symbol.iterator](): Iterator<TOut> {
const iterator = this.copy();
while (iterator.next()) {
yield iterator.value();
}
}

public reset(): void
{
this.source.reset();
Expand Down
7 changes: 6 additions & 1 deletion test/Test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export namespace Test
{
export function fail(): void
{
throw new Error("Assertion failed")
}

export function isTrue(result: boolean): void
{
if (result !== true)
{
throw new Error("Assertion failed");
fail();
}
}

Expand Down
50 changes: 50 additions & 0 deletions test/unitary/Enumerable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,62 @@ export namespace EnumerableUnitTest
{
export function run(): void
{
describe("ForOf", forOf);
it("FromSource", fromSource);
it("Empty", empty);
describe("Range", range);
it("Repeat", repeat);
}

function forOf(): void
{
it("Empty enumerable forOf works", () => {
const e = Enumerable.fromSource([]);
for (const item of e) {
Test.fail();
}
});

it("Enumerable forOf works", () => {
const e = Enumerable.fromSource([2, 4, 6]);

const array = [];
for (const item of e) {
array.push(item);
}

Test.isArrayEqual([2, 4, 6], array);
});

it("Enumerable where forOf works", () => {
const e = Enumerable.fromSource([2, 4, 6]).where(x => x < 5);

const array = [];
for (const item of e) {
array.push(item);
}

Test.isArrayEqual([2, 4], array);
});

it("Enumerable multiple enumerations forOf works", () => {
const e = Enumerable.fromSource([2, 4, 6]).where(x => x < 5);

const array = [];
for (const item of e) {
array.push(item);
}

const array2 = [];
for (const item of e) {
array2.push(item);
}

Test.isArrayEqual([2, 4], array);
Test.isArrayEqual([2, 4], array2);
});
}

function fromSource(): void
{
let e = Enumerable.fromSource(new ArrayIterator<number>([]));
Expand Down
22 changes: 22 additions & 0 deletions test/unitary/List.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export namespace ListUnitTest
{
export function run(): void
{
describe("ForOf", forOf);
describe("AsArray", asArray);
describe("AsReadOnly", asReadOnly);
describe("Copy", copy);
Expand All @@ -22,6 +23,27 @@ export namespace ListUnitTest
describe("Insert", insert);
}

function forOf(): void
{
it("Empty list forOf works", () => {
const e = new List([]);
for (const item of e) {
Test.fail();
}
});

it("List forOf works", () => {
const e = new List([2, 4, 6]);

const array = [];
for (const item of e) {
array.push(item);
}

Test.isArrayEqual([2, 4, 6], array);
});
}

function asArray(): void
{
it("Returns a reference, not a copy", () =>
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"strictFunctionTypes": true,
"types": ["mocha"],
"outDir": "build",
"strictPropertyInitialization": false
"strictPropertyInitialization": false,
"downlevelIteration": true
},
"exclude": [
"node_modules",
Expand Down