Skip to content

Commit

Permalink
fix(fromDiagram): fix support for falsey values
Browse files Browse the repository at this point in the history
fromDiagram did not accept 0 or null or falsey values when specified in the *values* parameter. Now
it does.
  • Loading branch information
staltz committed Jul 5, 2016
1 parent 1c6ed5c commit 85c9ca7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/extra/fromDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class DiagramProducer implements InternalProducer<any> {
this.schedule({type: 'complete', time: time}, out);
break;
default:
const val = this.values[c] ? this.values[c] : c;
const val = this.values.hasOwnProperty(c) ? this.values[c] : c;
this.schedule({type: 'next', value: val, time: time}, out);
break;
}
Expand Down Expand Up @@ -75,8 +75,8 @@ export class DiagramProducer implements InternalProducer<any> {
/**
* Creates a real stream out of an ASCII drawing of a stream. Each string
* character represents an amount of time passed (by default, 20 milliseconds).
* `-` characters represent nothing special, `|` is a symbol to mark the
* completion of the stream, `#` is an error on the stream, and any other
* `-` characters represent nothing special, `|` is a symbol to mark the
* completion of the stream, `#` is an error on the stream, and any other
* character is a "next" event.
*
* Example:
Expand All @@ -93,7 +93,7 @@ export class DiagramProducer implements InternalProducer<any> {
* })
* ```
*
* The character `a` represent emission of the event `'a'`, a string. If you
* The character `a` represent emission of the event `'a'`, a string. If you
* want to emit something else than a string, you need to provide those values
* in the options argument.
*
Expand All @@ -114,7 +114,7 @@ export class DiagramProducer implements InternalProducer<any> {
* ```
*
* That way, the stream will emit the numbers 10, 20, 30, 40. The `options`
* argument may also take `timeUnit`, a number to configure how many
* argument may also take `timeUnit`, a number to configure how many
* milliseconds does each represents, and `errorValue`, a value to send out as
* the error which `#` represents.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/extra/fromDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ describe('fromDiagram (extra)', () => {
});
});

it('should support 0 as a value behind a key in the values object', (done) => {
const stream = fromDiagram('--a--b----c--|', {
values: {a: 0, b: 1, c: 2}
});
const expected = [0, 1, 2];

stream.addListener({
next: (x: number) => {
assert.equal(x, expected.shift());
},
error: (err) => done(err),
complete: () => {
assert.equal(expected.length, 0);
done();
},
});
});

it('should support null as a value behind a key in the values object', (done) => {
const stream = fromDiagram('--a--b----c--|', {
values: {a: null, b: 1, c: 2}
});
const expected = [null, 1, 2];

stream.addListener({
next: (x: number) => {
assert.equal(x, expected.shift());
},
error: (err) => done(err),
complete: () => {
assert.equal(expected.length, 0);
done();
},
});
});

it('should create a stream with some sense of order', (done) => {
const stream1 = fromDiagram('-a---c--|');
const stream2 = fromDiagram('---b---d|');
Expand Down

0 comments on commit 85c9ca7

Please sign in to comment.