all files / lib/ Dependency.js

100% Statements 22/22
100% Branches 8/8
100% Functions 4/4
100% Lines 22/22
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                    146×   146× 146×                 250×             142×               147×     146×   146×       146× 146×     146×        
"use strict";
var assert = require('./util').assert;
 
/**
 * Instances are created internally during component registration.
 *
 * @param {String} key The component key.
 * @param {boolean} optional <code>true</code> when the dependency is optional.
 * @constructor
 * @classdesc Private to junkie internals.
 */
function Dependency(key, optional) {
  assert(key, "key must be defined");
 
  this._key = key;
  this._optional = optional;
}
 
/** @lends Dependency# */
var D = Dependency.prototype;
 
/**
 * The key of the dependent component.
 * @returns {String} The dependency key.
 */
D.key = function() {
  return this._key;
};
 
/**
 * Determine if the dependency is optional.
 * @returns {boolean} <code>true</code> for an optional dependency.
 */
D.optional = function() {
  return this._optional;
};
 
/**
 *
 * @param {String|Dependency} key
 * @returns {Dependency}
 */
Dependency.getOrCreate = function(keyOrDep, options) {
  if (keyOrDep instanceof Dependency) {
    return keyOrDep;
  }
 
  var optional = false;
 
  if (options && typeof options.optional === 'boolean') {
    optional = options.optional;
  }
 
  // Parse optional from key suffix
  var key = keyOrDep;
  if (key[key.length - 1] === '?') {
    key = key.substring(0, key.length - 1);
    optional = true;
  }
 
  return new Dependency(key, optional);
};
 
module.exports = Dependency;