Skip to content

Commit 5281da6

Browse files
author
Dan Bornstein
committed
Convert Slicer to take constructor options.
1 parent 1f9a727 commit 5281da6

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,9 @@ encodings specified by those. This includes:
156156
Common Options
157157
--------------
158158

159-
Some of the classes (as of this writing, most but not all of them)
160-
take an optional `options` constructor parameter. If not `undefined`,
161-
this must be a map from option names to values as specified by the
162-
class.
159+
All of the classes in this module take an optional `options`
160+
constructor parameter. If not `undefined`, this must be a map from
161+
option names to values as specified by the class.
163162

164163
The following are three commonly-accepted options. Classes all accept
165164
whichever of these make sense.
@@ -517,12 +516,16 @@ The ordering and meaning of the callback arguments are meant to be (a)
517516
compatible with callbacks used with `fs.read()` and (b) somewhat more
518517
informative and unambiguous.
519518

520-
### var slicer = new Slicer(source, [incomingEncoding])
519+
### var slicer = new Slicer(source, [options])
520+
521+
Constructs a new slicer, which listens to the given source.
522+
523+
Of the common options, the only one recognized by this class is
524+
`incomingEncoding`. The class accepts no other options.
525+
526+
This class recognizes all three of the common options (see above), and
527+
no others.
521528

522-
Constructs a new slicer, which listens to the given source. The optional
523-
`incomingEncoding` indicates the initial encoding to use when `data`
524-
events are received with string payloads (defaults to `undefined`; see
525-
`setIncomingEncoding()`).
526529

527530
### slicer.readable => boolean
528531

@@ -723,10 +726,7 @@ containing a string payload.
723726
To Do
724727
-----
725728

726-
* Use `options` arguments consistently on construction.
727-
728-
* Make `encoding`, `incomingEncoding`, and `paused` all be available
729-
as constructor options.
729+
* Figure out something to do!
730730

731731

732732
Contributing

lib/slicer.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ var typ = require("typ");
1717
var consts = require("./consts");
1818
var Codec = require("./codec").Codec;
1919
var errors = require("./errors");
20+
var opts = require("./opts");
2021
var sealer = require("./sealer");
2122
var sourcesanity = require("./sourcesanity");
2223

2324

25+
/*
26+
* Module variables
27+
*/
28+
29+
/** Options spec */
30+
var OPTIONS = {
31+
incomingEncoding: {}
32+
};
33+
34+
2435
/*
2536
* Helper functions
2637
*/
@@ -152,14 +163,14 @@ Object.freeze(PendingRead.prototype);
152163
/**
153164
* Constructs a Slicer state object.
154165
*/
155-
function State(source, incomingEncoding) {
166+
function State(source) {
156167
sourcesanity.validate(source);
157168

158169
/** upstream source (must be stream-like, if not actually a Stream) */
159170
this.source = source;
160171

161172
/** event receipt encoding handler */
162-
this.encoder = new Codec(incomingEncoding);
173+
this.encoder = new Codec();
163174

164175
/** queue of pending read operations */
165176
this.pendingReads = [];
@@ -341,8 +352,10 @@ Object.freeze(State.prototype);
341352
* `read(..., callback)` interface for consuming the so-collected
342353
* data.
343354
*/
344-
function Slicer(source, incomingEncoding) {
345-
this.slicer = sealer.seal(new State(source, incomingEncoding));
355+
function Slicer(source, options) {
356+
options = opts.validate(options, OPTIONS);
357+
this.slicer = sealer.seal(new State(source));
358+
opts.handleCommon(options, this);
346359
}
347360

348361
/**

test/slicer.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ var emit = require("./emit").emit;
2323
* Makes sure the constructor doesn't fail off the bat.
2424
*/
2525
function constructor() {
26-
new Slicer(new events.EventEmitter());
27-
new Slicer(new events.EventEmitter(), "hex");
26+
var emitter = new events.EventEmitter();
27+
28+
new Slicer(emitter);
29+
new Slicer(emitter, {});
30+
new Slicer(emitter, { incomingEncoding: "hex" });
2831
}
2932

3033
/**
@@ -42,9 +45,14 @@ function constructorFailures() {
4245
assert.throws(f2, /Source not an EventEmitter/);
4346

4447
function f3() {
45-
new Slicer(new events.EventEmitter(), "bad-encoding");
48+
new Slicer(new events.EventEmitter(), { incomingEncoding: "bad-encoding" });
49+
}
50+
assert.throws(f3, /Bad value for option: incomingEncoding/);
51+
52+
function f4() {
53+
new Slicer(new events.EventEmitter(), { frobnitz: "fizmo" });
4654
}
47-
assert.throws(f3, /Invalid encoding name/);
55+
assert.throws(f4, /Unknown option: frobnitz/);
4856
}
4957

5058
/**
@@ -480,7 +488,7 @@ function constructorEncodings() {
480488

481489
function tryWith(encodingName, dataString) {
482490
var source = new events.EventEmitter();
483-
var slicer = new Slicer(source, encodingName);
491+
var slicer = new Slicer(source, { incomingEncoding: encodingName });
484492
var coll = new CallbackCollector();
485493

486494
if (encodingName === "utf16le") {

0 commit comments

Comments
 (0)