Skip to content

Commit

Permalink
Merge b3f695a into 2d8a193
Browse files Browse the repository at this point in the history
  • Loading branch information
taras committed Nov 12, 2018
2 parents 2d8a193 + b3f695a commit 9bd99fb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ export default parameterized(T => class ObjectType {
filter(fn) {
return filter(({ key, value }) => valueOf(fn(create(T, value), key)), valueOf(this));
}

[Symbol.iterator]() {
let object = this;
let iterator = Object.keys(valueOf(this))[Symbol.iterator]();
return {
next() {
let next = iterator.next();
return {
get done() { return next.done; },
get value() { return object[next.value]; }
};
}
};
}
});
30 changes: 30 additions & 0 deletions tests/types/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import expect from 'expect';
import { create } from '../../src/microstates';
import { valueOf } from '../../src/meta';
import { ObjectType } from '../../src/types';
import { map } from '../../src/query';

describe('created without value', () => {
class Thing {}
Expand Down Expand Up @@ -225,4 +226,33 @@ describe("map/filter/reduce", () => {
expect(filtered["c"]).toBeDefined();
});
});

describe('iterable', () => {
let obj, calls;
beforeEach(() => {
obj = create(Object, { a: 'A', b: 'B', c: 'C'});
calls = [];
});

it('supports for of', () => {
for (let o of obj) {
calls.push(o);
}

expect(calls.length).toBe(3);
expect(valueOf(calls[0])).toBe('A');
expect(valueOf(calls[1])).toBe('B');
expect(valueOf(calls[2])).toBe('C');
});

it('allows to map object', () => {
for (let o of map(obj, o => `${o.state}!!!`)) {
calls.push(o);
}
expect(calls.length).toBe(3);
expect(valueOf(calls[0])).toBe('A!!!');
expect(valueOf(calls[1])).toBe('B!!!');
expect(valueOf(calls[2])).toBe('C!!!');
});
});
});

0 comments on commit 9bd99fb

Please sign in to comment.