123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- lunr.Pipeline = function () {
- this._stack = []
- }
- lunr.Pipeline.registeredFunctions = Object.create(null)
- 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 unregistered 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 stackLength = this._stack.length
- for (var i = 0; i < stackLength; i++) {
- var fn = this._stack[i]
- var memo = []
- for (var j = 0; j < tokens.length; j++) {
- var result = fn(tokens[j], j, tokens)
- if (result === null || result === void 0 || result === '') continue
- if (Array.isArray(result)) {
- for (var k = 0; k < result.length; k++) {
- memo.push(result[k])
- }
- } else {
- memo.push(result)
- }
- }
- tokens = memo
- }
- return tokens
- }
- lunr.Pipeline.prototype.runString = function (str, metadata) {
- var token = new lunr.Token (str, metadata)
- return this.run([token]).map(function (t) {
- return t.toString()
- })
- }
- 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
- })
- }
|