From 2d8c85181029689fceeb4d63a775ac8b9d98c0b7 Mon Sep 17 00:00:00 2001 From: Moos Date: Tue, 25 Feb 2014 00:24:29 -0800 Subject: [PATCH] Fix for issue #4 -- nested callbacks with same index key --- .gitignore | 2 ++ README.md | 4 ++-- package.json | 2 +- spec/wordpos_spec.js | 19 ++++++++++++++++++- src/fastIndex.js | 11 +++++++++-- 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79b5400 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dict +node_modules \ No newline at end of file diff --git a/README.md b/README.md index abebf2a..b4f0701 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ wordpos ======= -wordpos is a set of part-of-speech utilities for Node.js using [natural's](http://github.com/NaturalNode/natural) WordNet module. +wordpos is a set of part-of-speech (POS) utilities for Node.js using [natural's](http://github.com/NaturalNode/natural) WordNet module. ## Usage @@ -353,4 +353,4 @@ License (The MIT License) -Copyright (c) 2012, mooster@42at.com +Copyright (c) 2012, 2014 mooster@42at.com diff --git a/package.json b/package.json index 7e1f662..51cd296 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "author": "Moos ", "keywords": ["natural", "language", "wordnet", "pos"], "description": "wordpos is a set of part-of-speech utilities for Node.js using natural's WordNet module.", - "version": "0.1.8", + "version": "0.1.9", "homepage": "https://github.com/moos/wordpos", "engines": { "node": ">=0.6" diff --git a/spec/wordpos_spec.js b/spec/wordpos_spec.js index 0cf7583..a5c0214 100644 --- a/spec/wordpos_spec.js +++ b/spec/wordpos_spec.js @@ -182,7 +182,6 @@ describe('options passed to constructor', function() { }); describe('profile option', function() { - var wp = new WordPOS({profile : true}); it('should return time argument for isX()', function(done){ @@ -223,5 +222,23 @@ describe('profile option', function() { }); +describe('nested callbacks on same index key', function() { + var wp = new WordPOS(), + word1 = 'head', + word2 = word1 + 'er'; + + it('should call inner callback', function(done){ + wp.getPOS(word1, function(result) { + expect(result.nouns[0]).toEqual(word1); + + // inner call on word2 + wp.getPOS(word2, function(result) { + expect(result.nouns[0]).toEqual(word2); + done(); + }); + }); + }); +}); + function noop(){} diff --git a/src/fastIndex.js b/src/fastIndex.js index 31397ae..0431fa1 100644 --- a/src/fastIndex.js +++ b/src/fastIndex.js @@ -28,7 +28,8 @@ function loadFastIndex(dir, name) { data = JSON.parse( fs.readFileSync(jsonFile,'utf8') ); //console.log('loaded %d buckets for %s', data.stats.buckets, data.name); } catch(e) { - console.error('Error with fast index file %s\n ', jsonFile, e); + console.error('Error with fast index file. Try reinstalling from npm!'); + throw e; } return data; } @@ -94,7 +95,13 @@ function find(search, callback) { return line.substring(0,line.indexOf(' ')); }); - readCallbacks[key].forEach( test ); + // live access callbacks cache in case nested cb's + // add to the array. + while (readCallbacks[key].length) { + test(readCallbacks[key].shift()); + } + + // now done - delete cb cache delete readCallbacks[key]; if (--self.refcount == 0) {