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

feat: use iterable object directly #113

Merged
merged 5 commits into from
May 19, 2024
Merged
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
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