Skip to content

Commit

Permalink
feat: use iterable object directly (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
LviatYi authored May 19, 2024
1 parent 5d81dd4 commit e469081
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions linq.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare namespace Enumerable {
export function from(obj: string): IEnumerable<string>;
export function from<T>(obj: T[]): IEnumerable<T>;
export function from<T>(obj: Iterator<T>): IEnumerable<T>;
export function from<T>(obj: Iterable<T>): IEnumerable<T>;
export function from<T>(obj: { length: number;[x: number]: T; }): IEnumerable<T>;
export function from<K extends PropertyKey, T>(obj: Record<K, T>): IEnumerable<{ key: K; value: T }>;
export function make<T>(element: T): IEnumerable<T>;
Expand Down
14 changes: 14 additions & 0 deletions linq.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ Enumerable.from = function (obj) {
}

// iterable object
if (typeof Symbol !== 'undefined' && typeof obj[Symbol.iterator] !== 'undefined') {
let iterator;
return new Enumerable(function () {
return new IEnumerator(
function () { iterator = obj[Symbol.iterator]()},
function () {
var next = iterator.next();
return (next.done ? false : (this.yieldReturn(next.value)));
},
Functions.Blank);
});
}

// iterator object
if (typeof Symbol !== 'undefined' && typeof obj.next !== 'undefined') {
return new Enumerable(function () {
return new IEnumerator(
Expand Down
37 changes: 27 additions & 10 deletions test/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ test("for..of", function () {
deepEqual(actual, [1, 2, 3]);
});

test("Symbol.iterator", function () {
let actual = [1, 2, 3, 4];
test("Symbol.iterator", function ()
{
let actual = [1,2,3,4];
let expected = Array.from(Enumerable.from(actual));
deepEqual(actual, expected);
let actual2 = actual.map(function (x) {
return x * 2
}); // [2,4,6,8];
expected = Enumerable.from(actual).select(function (x) {
return x * 2
});
let actual2 = actual.map(function(x) { return x * 2 }); // [2,4,6,8];
expected = Enumerable.from(actual).select(function(x) { return x * 2 });
deepEqual(actual2, Array.from(expected));
});

test("from Iterable", function () {
test("from Generator", function () {
function* words() {
yield "abc";
yield "def";
Expand All @@ -39,7 +36,27 @@ test("from Iterable", function () {
deepEqual(actual, ["abc", "def"]);
});

test("from Symbol.iterator", function () {
test("from Iterable object", function () {
const map = new Map();

map.set(1, 2);
map.set(2, 4);

deepEqual(Enumerable
.from(map)
.select(item => ({key: item[0], value: item[1]}))
.select(item=>item.key)
.toArray(),
[1, 2]);

let actual = [];
for (var a of map) {
actual.push(a[0]);
}
deepEqual(actual, [1, 2]);
});

test("from Iterator object", function () {
const n = {
// This is just a simple replacement for the data structure that needs to be traversed.
// It may actually be a tree or other data structure implemented by a custom traversal.
Expand Down

0 comments on commit e469081

Please sign in to comment.