Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * Create a pipe object, that holds a list of services |
| 3 | * meant to transform the KQ passed before it's finally |
| 4 | * passed to the search engine. |
| 5 | * |
| 6 | * @author Nils Diewald |
| 7 | */ |
| 8 | define(function () { |
| 9 | |
| 10 | "use strict"; |
| 11 | |
| 12 | const notNullRe = new RegExp("[a-zA-Z0-9]"); |
| 13 | |
| 14 | // Trim and check |
| 15 | function _notNull (service) { |
| 16 | service = service.trim(); |
| 17 | if (service.match(notNullRe)) { |
| 18 | return service; |
| 19 | }; |
| 20 | return null; |
| 21 | } |
| 22 | |
| 23 | return { |
| 24 | |
| 25 | /** |
| 26 | * Constructor |
| 27 | */ |
| 28 | create : function () { |
| 29 | let obj = Object.create(this); |
| 30 | obj._pipe = []; |
| 31 | return obj; |
| 32 | }, |
| 33 | |
| 34 | /** |
| 35 | * Append service to pipe. |
| 36 | */ |
| 37 | append : function (service) { |
| 38 | service = _notNull(service); |
| 39 | if (service) |
| 40 | this._pipe.push(service); |
| 41 | }, |
| 42 | |
| 43 | /** |
| 44 | * Prepend service to pipe. |
| 45 | */ |
| 46 | prepend : function (service) { |
| 47 | service = _notNull(service); |
| 48 | if (service) |
| 49 | this._pipe.unshift(service); |
| 50 | }, |
| 51 | |
| 52 | /** |
| 53 | * Remove service from the pipe. |
| 54 | */ |
| 55 | remove : function (service) { |
| 56 | let i = this._pipe.indexOf(service); |
| 57 | if (i != -1) { |
| 58 | this._pipe.splice(i, 1); |
| 59 | }; |
| 60 | }, |
| 61 | |
| 62 | /** |
| 63 | * The number of services in the pipe. |
| 64 | */ |
| 65 | size : function () { |
| 66 | return this._pipe.length; |
| 67 | }, |
| 68 | |
| 69 | /** |
| 70 | * Return the pipe as a string. |
| 71 | */ |
| 72 | toString : function () { |
| 73 | return this._pipe.join(','); |
| 74 | } |
| 75 | }; |
| 76 | }); |