diff --git a/Document.js b/Document.js index 35347d7..62c5a1b 100644 --- a/Document.js +++ b/Document.js @@ -72,7 +72,7 @@ Document.prototype.removePostProcessingScript = function( fn ){ // call all post-processing scripts Document.prototype.callPostProcessingScripts = function(){ - this._post.forEach(function(fn){ fn.call(this); }, this); + this._post.forEach(function(fn){ fn.call(null, this); }, this); return this; }; diff --git a/post/intersections.js b/post/intersections.js index 10bf1c5..6faaff6 100644 --- a/post/intersections.js +++ b/post/intersections.js @@ -4,32 +4,32 @@ const INTERSECTION_LAYER_NAME = 'intersection'; const ADDRESS_STREET_PROP = 'street'; const ADDRESS_CROSS_STREET_PROP = 'cross_street'; -function intersections(){ +function intersections( doc ){ // only apply to docs from the intersection layer - if( this.getLayer() !== INTERSECTION_LAYER_NAME ){ return; } + if( doc.getLayer() !== INTERSECTION_LAYER_NAME ){ return; } // ensure both street & cross street props are set - let street = this.getAddress(ADDRESS_STREET_PROP); + let street = doc.getAddress(ADDRESS_STREET_PROP); if( !_.isString(street) || _.isEmpty(street) ){ return; } - let cross_street = this.getAddress(ADDRESS_CROSS_STREET_PROP); + let cross_street = doc.getAddress(ADDRESS_CROSS_STREET_PROP); if( !_.isString(cross_street) || _.isEmpty(cross_street) ){ return; } // generate name aliases for the intersection based on common syntactic variations. // note: currently only english is supported, PRs for additional languages welcome. // corner of A & B - this.setNameAlias('default', `${street} & ${cross_street}`); - this.setNameAlias('default', `${street} @ ${cross_street}`); - this.setNameAlias('default', `${street} at ${cross_street}`); - this.setNameAlias('default', `Corner of ${street} & ${cross_street}`); + doc.setNameAlias('default', `${street} & ${cross_street}`); + doc.setNameAlias('default', `${street} @ ${cross_street}`); + doc.setNameAlias('default', `${street} at ${cross_street}`); + doc.setNameAlias('default', `Corner of ${street} & ${cross_street}`); // corner of B & A - this.setNameAlias('default', `${cross_street} & ${street}`); - this.setNameAlias('default', `${cross_street} @ ${street}`); - this.setNameAlias('default', `${cross_street} at ${street}`); - this.setNameAlias('default', `Corner of ${cross_street} & ${street}`); + doc.setNameAlias('default', `${cross_street} & ${street}`); + doc.setNameAlias('default', `${cross_street} @ ${street}`); + doc.setNameAlias('default', `${cross_street} at ${street}`); + doc.setNameAlias('default', `Corner of ${cross_street} & ${street}`); } module.exports = intersections; \ No newline at end of file diff --git a/test/document/post.js b/test/document/post.js new file mode 100644 index 0000000..6fc8263 --- /dev/null +++ b/test/document/post.js @@ -0,0 +1,95 @@ + +const Document = require('../../Document'); +const intersections = require('../../post/intersections'); +const DEFAULT_SCRIPTS = [ intersections ]; + +module.exports.tests = {}; + +module.exports.tests.addPostProcessingScript = function(test) { + test('default scripts', function(t) { + let doc = new Document('mysource','mylayer','myid'); + t.deepEqual(doc._post, DEFAULT_SCRIPTS, 'default processing scripts'); + t.end(); + }); + test('invalid type', function(t) { + let doc = new Document('mysource','mylayer','myid'); + t.throws( doc.addPostProcessingScript.bind(doc, 'invalid'), /invalid document type, expecting: function got/ ); + t.throws( doc.addPostProcessingScript.bind(doc, 100), /invalid document type, expecting: function got/ ); + t.throws( doc.addPostProcessingScript.bind(doc, []), /invalid document type, expecting: function got/ ); + t.throws( doc.addPostProcessingScript.bind(doc, {}), /invalid document type, expecting: function got/ ); + t.end(); + }); + test('set function', function(t) { + let script = function(){}; + let doc = new Document('mysource','mylayer','myid'); + doc.addPostProcessingScript( script ); + t.deepEqual(doc._post, DEFAULT_SCRIPTS.concat( script ), 'default processing scripts'); + t.end(); + }); + test('set same function twice (allowed)', function(t) { + let script = function(){}; + let doc = new Document('mysource','mylayer','myid'); + doc.addPostProcessingScript( script ); + doc.addPostProcessingScript( script ); + t.deepEqual(doc._post, DEFAULT_SCRIPTS.concat( script, script ), 'default processing scripts'); + t.end(); + }); +}; + +module.exports.tests.removePostProcessingScript = function(test) { + test('invalid type', function(t) { + let doc = new Document('mysource','mylayer','myid'); + t.throws( doc.removePostProcessingScript.bind(doc, 'invalid'), /invalid document type, expecting: function got/ ); + t.throws( doc.removePostProcessingScript.bind(doc, 100), /invalid document type, expecting: function got/ ); + t.throws( doc.removePostProcessingScript.bind(doc, []), /invalid document type, expecting: function got/ ); + t.throws( doc.removePostProcessingScript.bind(doc, {}), /invalid document type, expecting: function got/ ); + t.end(); + }); + test('remove function', function(t) { + let script = function(){}; + let doc = new Document('mysource','mylayer','myid'); + doc._post = [ script ]; + doc.removePostProcessingScript( script ); + t.deepEqual(doc._post, [], 'script removed'); + doc.removePostProcessingScript( script ); + t.deepEqual(doc._post, [], 'no-op'); + t.end(); + }); + test('remove duplicate functions (allowed)', function(t) { + let script = function(){}; + let doc = new Document('mysource','mylayer','myid'); + doc._post = [ script, script ]; + doc.removePostProcessingScript( script ); + t.deepEqual(doc._post, [], 'scripts removed'); + doc.removePostProcessingScript( script ); + t.deepEqual(doc._post, [], 'no-op'); + t.end(); + }); +}; + +module.exports.tests.callPostProcessingScript = function(test) { + test('call all scripts', function(t) { + let doc = new Document('mysource','mylayer','myid'); + doc._post = []; // remove any default scripts + t.plan(3); + + // document pointer passed as first arg to scripts + doc.addPostProcessingScript((ref) => t.equal(doc, ref)); + doc.addPostProcessingScript((ref) => t.equal(doc, ref)); + doc.addPostProcessingScript((ref) => t.equal(doc, ref)); + + // call all scripts + doc.callPostProcessingScripts(); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('post processing: ' + name, testFunction); + } + + for( let testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/document/toESDocument.js b/test/document/toESDocument.js index 2b36d20..d851b6f 100644 --- a/test/document/toESDocument.js +++ b/test/document/toESDocument.js @@ -217,6 +217,23 @@ module.exports.tests.toESDocumentWithCustomConfig = function(test) { }); }; +module.exports.tests.toESDocumentCallsProcessingScripts = function(test) { + test('toESDocument must call all post-processing scripts', function(t) { + let Document = proxyquire('../../Document', { 'pelias-config': fakeConfig }); + let doc = new Document('mysource','mylayer','myid'); + doc._post = []; // remove any default scripts + t.plan(3); + + // document pointer passed as first arg to scripts + doc.addPostProcessingScript((ref) => t.equal(doc, ref)); + doc.addPostProcessingScript((ref) => t.equal(doc, ref)); + doc.addPostProcessingScript((ref) => t.equal(doc, ref)); + + // toESDocument() should, in tern, call callPostProcessingScripts() + doc.toESDocument(); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { return tape('Document: ' + name, testFunction); diff --git a/test/post/intersections.js b/test/post/intersections.js index 7978b1d..16acc5b 100644 --- a/test/post/intersections.js +++ b/test/post/intersections.js @@ -9,21 +9,21 @@ module.exports.tests.functional = function(test) { var doc = new Document('mysource','intersection','myid'); // street and cross_street not set - intersections.call(doc); + intersections(doc); t.deepEqual(doc.getNameAliases('default'), [], 'no names set'); // set street doc.setAddress('street', 'Example Street'); // street set, cross_street not set - intersections.call(doc); + intersections(doc); t.deepEqual(doc.getNameAliases('default'), [], 'no names set'); // set cross_street doc.setAddress('cross_street', 'Cross Street'); // street and cross_street set - intersections.call(doc); + intersections(doc); // intersection aliases defined t.deepEqual(doc.getNameAliases('default'), [ diff --git a/test/run.js b/test/run.js index ece606a..ebc3c55 100644 --- a/test/run.js +++ b/test/run.js @@ -22,6 +22,7 @@ var tests = [ require('./document/layer.js'), require('./document/source_id.js'), require('./document/toESDocument.js'), + require('./document/post.js'), require('./post/intersections.js'), require('./DocumentMapperStream.js'), require('./util/transform.js'),