Skip to content

Commit

Permalink
Changed to map
Browse files Browse the repository at this point in the history
  • Loading branch information
juspears committed Mar 2, 2016
1 parent e4f15d3 commit 07fd162
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 18 additions & 21 deletions src/injectorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 07fd162

Please sign in to comment.