From 07fd1620709aaf57c5dd666c81874c87687737cf Mon Sep 17 00:00:00 2001 From: juspears Date: Wed, 2 Mar 2016 11:08:12 -0800 Subject: [PATCH] Changed to map --- package.json | 3 ++- src/injectorFactory.js | 39 ++++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 5d5437e..f825a5b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "subschema-injection", "version": "1.0.7", "description": "Subschema Injection System.", - "main": "", + "webpack": "src/index", + "main":"dist/index", "dependencies": {}, "repository": { "type": "git", diff --git a/src/injectorFactory.js b/src/injectorFactory.js index d79f876..3274353 100644 --- a/src/injectorFactory.js +++ b/src/injectorFactory.js @@ -12,32 +12,29 @@ function hasAnyKeys(obj) { if (!obj) return false; return Object.keys(obj).length > 0; } -export default function injector(resolvers = []) { - - function resolveProp(propType) { - if (propType == null) { - return propType; - } - const length = resolvers.length; - for (let i = 0; i < length; i++) { - const resolver = resolvers[i], pT = resolver.propType; - - if (pT === propType || pT.isRequired === propType) { - return resolver; - } +function isIterable(obj) { + // checks for null and undefined + if (obj == null) { + return false + } + return obj[Symbol.iterator] !== void(0) +} +export default function injector(resolvers = new Map()) { + if (!(resolvers instanceof Map )) { + if (isIterable(resolvers)) { + resolvers = new Map(resolvers); + } else { + throw new Error('resolvers must be iterable'); } - return null; } + const Injector = { resolver(propType, resolve){ if (propType == null || resolve == null) { throw new Error('must define both a propType and a resolver'); } - resolvers.push({ - propType, - resolve - }); + resolvers.set(propType, resolve); }, unmount, listener, @@ -58,7 +55,7 @@ export default function injector(resolvers = []) { class InjectedClass extends BaseInjectComponent { static defaultProps = {}; static contextTypes = {}; - static displayName = `${name}$Wrapper`; + static displayName = `${name}$Wrapper`; render = render; } return InjectedClass @@ -87,7 +84,7 @@ export default function injector(resolvers = []) { const injected = propTypeKeys.reduce((injectedClass, key)=> { - const resolver = resolveProp(keyIn(key, propTypes, extraPropTypes)); + const resolver = resolvers.get(keyIn(key, propTypes, extraPropTypes)); //resolver is null, nothing to do just return. if (resolver == null) { return injectedClass; @@ -99,7 +96,7 @@ export default function injector(resolvers = []) { injectedClass.defaultProps[key] = keyIn(key, defaultProps, extraProps); //Resolver could return a different class. - const nextClass = this::resolver.resolve(injectedClass, key, propTypeKeys, Clazz); + const nextClass = this::resolver(injectedClass, key, propTypeKeys, Clazz); //If a different class was null, return the original class. return (nextClass == null) ? injectedClass : nextClass;