123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- lunr.Pipeline = function () {
- this._stack = []
- }
- lunr.Pipeline.registeredFunctions = {}
- lunr.Pipeline.registerFunction = function (fn, label) {
- if (label in this.registeredFunctions) {
- lunr.utils.warn('Overwriting existing registered function: ' + label)
- }
- fn.label = label
- lunr.Pipeline.registeredFunctions[fn.label] = fn
- }
- lunr.Pipeline.warnIfFunctionNotRegistered = function (fn) {
- var isRegistered = fn.label && (fn.label in this.registeredFunctions)
- if (!isRegistered) {
- lunr.utils.warn('Function is not registered with pipeline. This may cause problems when serialising the index.\n', fn)
- }
- }
- lunr.Pipeline.load = function (serialised) {
- var pipeline = new lunr.Pipeline
- serialised.forEach(function (fnName) {
- var fn = lunr.Pipeline.registeredFunctions[fnName]
- if (fn) {
- pipeline.add(fn)
- } else {
- throw new Error('Cannot load un-registered function: ' + fnName)
- }
- })
- return pipeline
- }
- lunr.Pipeline.prototype.add = function () {
- var fns = Array.prototype.slice.call(arguments)
- fns.forEach(function (fn) {
- lunr.Pipeline.warnIfFunctionNotRegistered(fn)
- this._stack.push(fn)
- }, this)
- }
- lunr.Pipeline.prototype.after = function (existingFn, newFn) {
- lunr.Pipeline.warnIfFunctionNotRegistered(newFn)
- var pos = this._stack.indexOf(existingFn)
- if (pos == -1) {
- throw new Error('Cannot find existingFn')
- }
- pos = pos + 1
- this._stack.splice(pos, 0, newFn)
- }
- lunr.Pipeline.prototype.before = function (existingFn, newFn) {
- lunr.Pipeline.warnIfFunctionNotRegistered(newFn)
- var pos = this._stack.indexOf(existingFn)
- if (pos == -1) {
- throw new Error('Cannot find existingFn')
- }
- this._stack.splice(pos, 0, newFn)
- }
- lunr.Pipeline.prototype.remove = function (fn) {
- var pos = this._stack.indexOf(fn)
- if (pos == -1) {
- return
- }
- this._stack.splice(pos, 1)
- }
- lunr.Pipeline.prototype.run = function (tokens) {
- var out = [],
- tokenLength = tokens.length,
- stackLength = this._stack.length
- for (var i = 0; i < tokenLength; i++) {
- var token = tokens[i]
- for (var j = 0; j < stackLength; j++) {
- token = this._stack[j](token, i, tokens)
- if (token === void 0 || token === '') break
- };
- if (token !== void 0 && token !== '') out.push(token)
- };
- return out
- }
- lunr.Pipeline.prototype.reset = function () {
- this._stack = []
- }
- lunr.Pipeline.prototype.toJSON = function () {
- return this._stack.map(function (fn) {
- lunr.Pipeline.warnIfFunctionNotRegistered(fn)
- return fn.label
- })
- }
|