vsm-dictionary-cacher
Advanced tools
Comparing version 1.0.6 to 1.0.7
{ | ||
"name": "vsm-dictionary-cacher", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Wrapper around a VSM-dictionary, that manages a cache of string-match results", | ||
@@ -5,0 +5,0 @@ "main": "src/VsmDictionaryCacher.js", |
@@ -39,3 +39,3 @@ /* | ||
(typeof cacheOptions.predictEmpties) === 'undefined' ? true: | ||
!!cacheOptions.predictEmpties; | ||
!!cacheOptions.predictEmpties; | ||
@@ -106,8 +106,18 @@ /** | ||
// for which a substring has a cacheEmpties hit. | ||
// So we still need to call the VsmDictionary-parent-class's | ||
// `addExtraMatchesForString()`, but based on an empty query-result. | ||
// So we still need to | ||
// - call the subclass's `getRefTerms()`, and | ||
// - call the VsmDictionary-parent-class's `addExtraMatchesForString()`. | ||
if (this._getCacheMOEmptiesPrediction(str, options)) { | ||
return this.addExtraMatchesForString(str, [], options, | ||
(err, arr) => callAsync(cb, err, { items: arr }) | ||
); | ||
return this.getRefTerms({ filter: { str } }, (err, refTerms) => { | ||
if (err) return cb(err); | ||
var arr = !refTerms.items.length ? [] : [{ // Make a match-object.. | ||
id: '', dictID: '', type: 'R', // ..for a refTerm. | ||
str: refTerms.items[0], descr: this.matchDescrs.refTerm | ||
}]; | ||
this.addExtraMatchesForString(str, arr, options, | ||
(err, arr) => callAsync(cb, err, { items: arr }) | ||
); | ||
}); | ||
} | ||
@@ -254,3 +264,3 @@ | ||
var myCb = cb; | ||
setTimeout(() => { myCb(err, value, false); }, 1); | ||
setTimeout(() => { myCb(err, value, false) }, 1); | ||
}) (); | ||
@@ -312,3 +322,3 @@ } | ||
}; | ||
} | ||
}; | ||
@@ -315,0 +325,0 @@ |
@@ -15,5 +15,8 @@ const cacher = require('./VsmDictionaryCacher'); | ||
// (See below). | ||
var calledParent; // Is 1 if stub's parent's addExtraMatchesFor..() is called. | ||
var arrGivenToParent; | ||
var calledExtra; // Is 1 if stub's parent's addExtraMatchesFor..() is called. | ||
var calledExtraArr; // Is the array given to the addExtra..() call. | ||
var calledRefTerms; // Is 1 if the stub's getRefTerms() is called explicitly. | ||
var clock; // See https://stackoverflow.com/questions/17446064 | ||
@@ -25,13 +28,7 @@ | ||
class VsmDictionaryStub { | ||
addExtraMatchesForString(str, arr, options, cb) { | ||
setTimeout( () => { | ||
calledParent = 1; | ||
arrGivenToParent = arr; | ||
cb(null, arr); | ||
}, 0); | ||
constructor() { | ||
this.matchDescrs = { refTerm: '[referring term]' }; // Cacher needs this. | ||
} | ||
}; | ||
class VsmDictionarySubclassStub extends VsmDictionaryStub { | ||
// This stub function would normally *query* an underlying datastore. | ||
@@ -49,3 +46,3 @@ // So if it is called, it means that the function which overrides it | ||
setTimeout( | ||
function f() { called = 1; cb(error, result); }, | ||
function fm() { called = 1; cb(error, result); }, | ||
delay || 0 | ||
@@ -55,4 +52,19 @@ ); | ||
addExtraMatchesForString(str, arr, options, cb) { | ||
setTimeout( | ||
function fe() { | ||
calledExtra = 1; calledExtraArr = arr; cb(null, arr); | ||
}, 0 | ||
); | ||
} | ||
getRefTerms(options, cb) { | ||
setTimeout( | ||
function fr() { calledRefTerms = 1; cb(error, { items: [] }); }, | ||
delay || 0 | ||
); | ||
} | ||
}; | ||
CachedDictionary = cacher(VsmDictionarySubclassStub, cacheOptions); | ||
CachedDictionary = cacher(VsmDictionaryStub, cacheOptions); | ||
dict = new CachedDictionary(); | ||
@@ -76,5 +88,8 @@ } | ||
called = 0; | ||
calledParent = 0; | ||
arrGivenToParent = undefined; | ||
result = { items: ['default'] }; | ||
calledExtra = 0; | ||
calledExtraArr = undefined; | ||
calledRefTerms = 0; | ||
}); | ||
@@ -394,3 +409,4 @@ | ||
it('still calls `VsmDictionary.addExtraMatchesForString()`, for "1e3", ' + | ||
it('still calls `VsmDictionary.addExtraMatchesForString()`, and ' + | ||
'`this.getRefTerms()`, for "1e3", ' + | ||
'after "1e" returned no results', function(cb) { | ||
@@ -400,6 +416,8 @@ makeVsmDictionaryStub(); | ||
dict.getMatchesForString('1e', {}, (err, res) => { | ||
calledParent = 0; | ||
calledExtra = 0; | ||
calledRefTerms = 0; | ||
dict.getMatchesForString('1e3', {}, (err, res) => { | ||
calledParent.should.equal(1); | ||
arrGivenToParent.should.deep.equal([]); // Parent gets OK data format. | ||
calledExtra .should.equal(1); // getExtra..() gets called, .. | ||
calledExtraArr.should.deep.equal([]); // ..and with right data format. | ||
calledRefTerms.should.equal(1); // getRefTerms() gets called too. | ||
res.should.deep.equal(_R0); // Cacher integrates empty result OK. | ||
@@ -406,0 +424,0 @@ cb(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38013
723