Skip to content

Commit 11826b7

Browse files
committed
WIP
1 parent 3386e43 commit 11826b7

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

letterbox.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
'use strict'
2020

21+
var Writable = require('stream').Writable;
22+
var Tracker = require('./tracker.js');
23+
2124
/**
2225
* Construct a new letterbox accepting EDIFACT envelopes. A letterbox is a
2326
* Writable stream accepting segment objects. Enveloping is optional, so a
@@ -26,22 +29,29 @@
2629
*
2730
* @constructs Letterbox
2831
*/
29-
var Letterbox = function () {
30-
var letterbox = this;
32+
var Letterbox = function (path) {
33+
var that = this;
34+
35+
Writable.call(this, {
36+
write: this.accept,
37+
objectMode: true
38+
});
39+
40+
this.path = path;
3141

3242
this.depth = {};
3343
this.depth.current = 0;
3444
this.depth.minimum = 0;
3545
this.depth.maximum = 2;
3646

37-
this.next = function () {
38-
letterbox.depth.current -= 1;
47+
this.next = function (segment) {
48+
that.depth.current -= 1;
3949
};
4050
}
4151

42-
module.exports = Letterbox;
52+
Letterbox.prototype = Object.create(Writable.prototype);
4353

44-
function open_envelope(name, level) {
54+
function openEnvelope(name, level) {
4555
var message = '';
4656
var depth = this.depth.current + 1;
4757

@@ -57,7 +67,7 @@ function open_envelope(name, level) {
5767
}
5868
}
5969

60-
function close_envelope(name, level) {
70+
function closeEnvelope(name, level) {
6171
var message = '';
6272
var depth = this.depth.current - 1;
6373

@@ -69,46 +79,46 @@ function close_envelope(name, level) {
6979
}
7080
}
7181

72-
/**
73-
* Accept a segment.
74-
*
75-
* @param {String} segment A segment object.
76-
*/
77-
Letterbox.prototype.write = function (segment) {
82+
Letterbox.prototype.accept = function (segment) {
7883
// Most of the time we are tracking segments in a message. To optimize for
7984
// this case we start by detecting if we are currently in the middle of a
8085
// message. We can do this with only one comparison.
8186
if (this.depth.current > this.depth.maximum) {
82-
this.track(segment);
87+
this.tracker.accept(segment.name);
8388
} else {
8489
switch (segment.name) {
8590
case 'UNB':
86-
open_envelope.call(this, 'interchange', 0);
91+
openEnvelope.call(this, 'interchange', 0);
8792
break;
8893
case 'UNG':
89-
open_envelope.call(this, 'group', 1);
94+
openEnvelope.call(this, 'group', 1);
9095
break;
9196
case 'UNH':
9297
if (this.depth.current < this.depth.minimum) {
9398
throw Error('Cannot omit an envelope');
9499
} else {
95100
this.depth.maximum = this.depth.current;
96101
this.depth.current += 1;
97-
this.track(segment);
102+
this.setup(segment);
98103
}
99104
break;
100105
case 'UNE':
101-
close_envelope.call(this, 'group', 1);
106+
closeEnvelope.call(this, 'group', 1);
102107
break;
103108
case 'UNZ':
104-
close_envelope.call(this, 'interchange', 0);
109+
closeEnvelope.call(this, 'interchange', 0);
105110
break;
106111
default:
107112
throw Error('Did not expect a ' + segment + ' segment');
108113
}
109114
}
110-
};
115+
}
111116

112-
Letterbox.prototype.track = function () {};
117+
Letterbox.prototype.setup = function (segment) {
118+
this.cork();
119+
this.tracker = new Tracker(require(this.path + '/' + segment.components[1]));
120+
this.tracker.accept(segment.name);
121+
this.uncork();
122+
};
113123

114124
module.exports = Letterbox;

spec/LetterboxSpec.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,82 @@ describe('Letterbox', function () {
66
let letterbox;
77
beforeEach(function () {
88
letterbox = new Letterbox();
9-
spyOn(letterbox, 'track');
9+
letterbox.tracker = { accept: function () {} };
10+
spyOn(letterbox, 'setup');
11+
spyOn(letterbox.tracker, 'accept');
1012
});
1113
it('should accept a message without an envelope', function () {
12-
expect(function () { letterbox.write({ name: 'UNH' }); }).not.toThrow();
14+
expect(function () { letterbox.accept({ name: 'UNH' }); }).not.toThrow();
1315
expect(letterbox.depth.maximum).toEqual(0);
1416
expect(letterbox.depth.current).toEqual(1);
1517
expect(function () { letterbox.next(); }).not.toThrow();
1618
expect(letterbox.depth.current).toEqual(0);
1719
});
1820
it('cannot nest interchanges', function () {
19-
expect(function () { letterbox.write({ name: 'UNB' }); }).not.toThrow();
21+
expect(function () { letterbox.accept({ name: 'UNB' }); }).not.toThrow();
2022
expect(letterbox.depth.minimum).toEqual(1);
2123
expect(letterbox.depth.current).toEqual(1);
22-
expect(function () { letterbox.write({ name: 'UNB' }); }).toThrow();
24+
expect(function () { letterbox.accept({ name: 'UNB' }); }).toThrow();
2325
});
2426
it('cannot open an interchange after a single message', function () {
25-
expect(function () { letterbox.write({ name: 'UNH' }); }).not.toThrow();
27+
expect(function () { letterbox.accept({ name: 'UNH' }); }).not.toThrow();
2628
expect(letterbox.depth.maximum).toEqual(0);
2729
expect(letterbox.depth.current).toEqual(1);
2830
expect(function () { letterbox.next(); }).not.toThrow();
2931
expect(letterbox.depth.current).toEqual(0);
30-
expect(function () { letterbox.write({ name: 'UNB' }); }).toThrow();
32+
expect(function () { letterbox.accept({ name: 'UNB' }); }).toThrow();
3133
});
3234
it('cannot open a group without an interchange', function () {
33-
expect(function () { letterbox.write({ name: 'UNG' }); }).toThrow();
35+
expect(function () { letterbox.accept({ name: 'UNG' }); }).toThrow();
3436
});
3537
for (var name of ['UNB', 'UNZ', 'UNG', 'UNE']) {
3638
it('should not validate a ' + name + ' segment while tracking a message', function () {
3739
// While no valid message contains an enveloping segment in it's segment
3840
// table, this test is the equivalence of allowing such a messsage
3941
// definition. Prohibiting enveloping segments in messages should be
4042
// done by providing a correct segment table, not by algorithm design.
41-
expect(function () { letterbox.write({ name: 'UNH' }); }).not.toThrow();
43+
expect(function () { letterbox.accept({ name: 'UNH' }); }).not.toThrow();
4244
expect(letterbox.depth.maximum).toEqual(0);
4345
expect(letterbox.depth.current).toEqual(1);
44-
expect(function () { letterbox.write({ name: name }); }).not.toThrow();
45-
expect(letterbox.track).toHaveBeenCalled();
46+
expect(function () { letterbox.accept({ name: name }); }).not.toThrow();
47+
expect(letterbox.tracker.accept).toHaveBeenCalled();
4648
});
4749
}
4850
it('should accept a message in an interchange', function () {
49-
expect(function () { letterbox.write({ name: 'UNB' }); }).not.toThrow();
51+
expect(function () { letterbox.accept({ name: 'UNB' }); }).not.toThrow();
5052
expect(letterbox.depth.minimum).toEqual(1);
5153
expect(letterbox.depth.current).toEqual(1);
52-
expect(function () { letterbox.write({ name: 'UNH' }); }).not.toThrow();
54+
expect(function () { letterbox.accept({ name: 'UNH' }); }).not.toThrow();
5355
expect(letterbox.depth.maximum).toEqual(1);
5456
expect(letterbox.depth.current).toEqual(2);
5557
expect(function () { letterbox.next(); }).not.toThrow();
5658
expect(letterbox.depth.current).toEqual(1);
57-
expect(function () { letterbox.write({ name: 'UNZ' }); }).not.toThrow();
59+
expect(function () { letterbox.accept({ name: 'UNZ' }); }).not.toThrow();
5860
expect(letterbox.depth.current).toEqual(0);
5961
});
6062
it('should not accept a group after a message', function () {
61-
expect(function () { letterbox.write({ name: 'UNB' }); }).not.toThrow();
63+
expect(function () { letterbox.accept({ name: 'UNB' }); }).not.toThrow();
6264
expect(letterbox.depth.minimum).toEqual(1);
6365
expect(letterbox.depth.current).toEqual(1);
64-
expect(function () { letterbox.write({ name: 'UNH' }); }).not.toThrow();
66+
expect(function () { letterbox.accept({ name: 'UNH' }); }).not.toThrow();
6567
expect(letterbox.depth.maximum).toEqual(1);
6668
expect(letterbox.depth.current).toEqual(2);
6769
expect(function () { letterbox.next(); }).not.toThrow();
6870
expect(letterbox.depth.current).toEqual(1);
69-
expect(function () { letterbox.write({ name: 'UNG' }); }).toThrow();
71+
expect(function () { letterbox.accept({ name: 'UNG' }); }).toThrow();
7072
});
7173
it('should not accept a group without an interchange', function () {
72-
expect(function () { letterbox.write({ name: 'UNG' }); }).toThrow();
74+
expect(function () { letterbox.accept({ name: 'UNG' }); }).toThrow();
7375
});
7476
it('should not accept a message after a group', function () {
75-
expect(function () { letterbox.write({ name: 'UNB' }); }).not.toThrow();
77+
expect(function () { letterbox.accept({ name: 'UNB' }); }).not.toThrow();
7678
expect(letterbox.depth.minimum).toEqual(1);
7779
expect(letterbox.depth.current).toEqual(1);
78-
expect(function () { letterbox.write({ name: 'UNG' }); }).not.toThrow();
80+
expect(function () { letterbox.accept({ name: 'UNG' }); }).not.toThrow();
7981
expect(letterbox.depth.minimum).toEqual(2);
8082
expect(letterbox.depth.current).toEqual(2);
81-
expect(function () { letterbox.write({ name: 'UNE' }); }).not.toThrow();
83+
expect(function () { letterbox.accept({ name: 'UNE' }); }).not.toThrow();
8284
expect(letterbox.depth.current).toEqual(1);
83-
expect(function () { letterbox.write({ name: 'UNH' }); }).toThrow();
85+
expect(function () { letterbox.accept({ name: 'UNH' }); }).toThrow();
8486
});
8587
});

0 commit comments

Comments
 (0)