Comparing version 1.1.0 to 1.1.1
70
index.js
@@ -1,52 +0,24 @@ | ||
module.exports = () => { | ||
var dependencies = {}; | ||
var factories = {}; | ||
var sLoc = {}; | ||
sLoc.factory = (name, factory) => { | ||
if(typeof factory === 'object') { | ||
factories[name] = {}; | ||
factories[name].orig = factory[Object.keys(factory)[0]]; | ||
if(factory[Object.keys(factory)[1]]) { | ||
factories[name].second = factory[Object.keys(factory)[1]]; | ||
} | ||
factories[name].parent = factory | ||
} else { | ||
factories[name] = {}; | ||
factories[name].orig = factory | ||
} | ||
}; | ||
sLoc.register = (name, instance) => { | ||
if(typeof instance === 'object') { | ||
dependencies[name] = {}; | ||
dependencies[name].orig = instance[Object.keys(instance)[0]]; | ||
if(instance[Object.keys(instance)[1]]) { | ||
dependencies[name].second = instance[Object.keys(instance)[1]]; | ||
} | ||
dependencies[name].parent = instance | ||
} else { | ||
dependencies[name] = {}; | ||
dependencies[name].orig = instance | ||
} | ||
}; | ||
sLoc.get = (name,override, overrideName) => { | ||
if(overrideName){ | ||
return dependencies[name].parent[overrideName]; | ||
} | ||
else if(override === true) { | ||
return dependencies[name].second | ||
} | ||
else { | ||
if(!dependencies[name].orig) { | ||
var dependencies = {}; | ||
var factories = {}; | ||
var sLoc = { | ||
factory: (name, factory) => { | ||
factories[name] = {}; | ||
factories[name] = factory | ||
}, | ||
register: (name, instance) => { | ||
dependencies[name] = {}; | ||
dependencies[name] = instance | ||
console.log(dependencies); | ||
}, | ||
get: (name, override, overrideName) => { | ||
if (!dependencies[name]) { | ||
var factory = factories[name]; | ||
dependencies[name].orig = factory && factory(sLoc); | ||
if(dependencies[name].orig) { | ||
throw new Error (`Cannot locate ${name}`) | ||
dependencies[name] = factory && factory(sLoc); | ||
if (dependencies[name]) { | ||
throw new Error(`Cannot locate ${name}`) | ||
} | ||
} | ||
return dependencies[name].orig | ||
} | ||
}; | ||
return sLoc; | ||
}; | ||
return dependencies[name] | ||
} | ||
} | ||
module.exports = sLoc; |
{ | ||
"name": "s-loc", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A simple service locator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
s-loc | ||
== | ||
Description: Service locator with some functionality behond basic register/get | ||
Description: Basic service locator | ||
@@ -25,3 +25,2 @@ var locator = require('s-loc')(); | ||
locator.register('testGet',obj); | ||
locator.register('func',obj.last); | ||
@@ -31,5 +30,2 @@ | ||
var testGet = locator.get('testGet'); | ||
var testSecond = locator.get('testGet',true); | ||
var testThird = locator.get('testGet',true,'third'); | ||
@@ -40,5 +36,2 @@ | ||
func(); //LOGS 'real lastone' | ||
testGet(); //LOGS 'THIS HAPPENED' | ||
testSecond(); //LOGS 'THIS RIGHT HERE' | ||
testThird(); //LOGS 'last one happened' | ||
@@ -49,15 +42,2 @@ ###locator.register (NAME, Anything) | ||
###locator.get(NAME,OVERRIDE(optional,boolean),OVERRIDE_NAME(optional,string)) | ||
locator.get is used to get something back out of the registry. | ||
*IF NOT OBJECT:* If you registered a non object you ONLY pass in name to this function. Everything else will error. | ||
*IF OBJECT:* | ||
just name passed in === Will return first property of object | ||
name and override = true === Will return second property of object | ||
name and overridename === Will return the property with override name | ||
@@ -18,18 +18,8 @@ var locator = require('./index')(); | ||
}; | ||
locator.register('testGet',obj); | ||
locator.register('func',obj.last); | ||
var func = locator.get('func'); | ||
var testGet = locator.get('testGet'); | ||
var testSecond = locator.get('testGet',true); | ||
var testThird = locator.get('testGet',true,'third'); | ||
func(); | ||
testGet(); | ||
testSecond(); | ||
testThird(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3060
43
40