all files / util/ Registry.js

79.07% Statements 34/43
66.67% Branches 12/18
83.33% Functions 10/12
78.57% Lines 33/42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                          1964× 1964× 1964×   1964× 526× 5509×                         31631×                       23282× 2870×   23282×     23282× 371×   23282× 23282×                     371× 371× 371×   371×                                         18228× 18228×     18228×                 428× 4310× 4310× 4310×                             428× 428× 4310× 4310×     428×       4310×          
import forEach from './forEach'
 
// just as a reference to detect name collisions
// with native Object properties
const PLAINOBJ = {}
 
/*
 * Simple registry implementation.
 *
 * @class Registry
 * @private
 */
class Registry {
  constructor(entries, validator) {
    this.entries = {}
    this.names = []
    this.validator = validator
 
    if (entries) {
      forEach(entries, function(entry, name) {
        this.add(name, entry)
      }.bind(this))
    }
  }
 
  /**
   * Check if an entry is registered for a given name.
   *
   * @param {String} name
   * @method contains
   * @memberof module:Basics.Registry.prototype
   */
  contains(name) {
    return this.entries.hasOwnProperty(name)
  }
 
  /**
   * Add an entry to the registry.
   *
   * @param {String} name
   * @param {Object} entry
   * @method add
   * @memberof module:Basics.Registry.prototype
   */
  add(name, entry) {
    if (this.validator) {
      this.validator(entry)
    }
    Iif (PLAINOBJ[name]) {
      throw new Error('Illegal key: "'+name+'" is a property of Object which is thus not allowed as a key.')
    }
    if (this.contains(name)) {
      this.remove(name)
    }
    this.entries[name] = entry
    this.names.push(name)
  }
 
  /**
   * Remove an entry from the registry.
   *
   * @param {String} name
   * @method remove
   * @memberof module:Basics.Registry.prototype
   */
  remove(name) {
    let pos = this.names.indexOf(name)
    Eif (pos >= 0) {
      this.names.splice(pos, 1)
    }
    delete this.entries[name]
  }
 
  /**
   * @method clear
   * @memberof module:Basics.Registry.prototype
   */
  clear() {
    this.names = []
    this.entries = {}
  }
 
  /**
   * Get the entry registered for a given name.
   *
   * @param {String} name
   * @return The registered entry
   * @method get
   * @memberof module:Basics.Registry.prototype
   */
  get(name, strict) {
    let result = this.entries[name]
    Iif (strict && !result) {
      throw new Error('No entry registered for name '+name)
    }
    return result
  }
 
  /*
    Iterate all registered entries in the order they were registered.
 
    @param {Function} callback with signature function(entry, name)
   */
  forEach(callback) {
    for (let i = 0; i < this.names.length; i++) {
      let name = this.names[i]
      let _continue = callback(this.entries[name], name)
      Iif (_continue === false) {
        break
      }
    }
  }
 
  map(callback) {
    let result = []
    this.forEach((entry, name) => {
      result.push(callback(entry, name))
    })
    return result
  }
 
  filter(callback) {
    let result = []
    this.forEach(function(entry, name) {
      Eif (callback(entry, name)) {
        result.push(entry)
      }
    })
    return result
  }
 
  values() {
    return this.filter(() => { return true })
  }
}
 
Registry.prototype._isRegistry = true
 
export default Registry