Last active
December 20, 2016 21:28
-
-
Save tiffwu/a752e1590fdd7008248132b83fdb8fc6 to your computer and use it in GitHub Desktop.
solution to Watch and Code challenge #1: 'librarySystem with dependencies'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// v1 attempt | |
// This version passes all tests. However, it's missing a couple of key features: | |
// (1) it only takes one 'level' of dependencies (no nested dependencies), and | |
// (2) dependencies must have already been loaded in order for a library to be loaded. | |
(function() { | |
var libraryStorage = {}; | |
function librarySystem(libraryName, dependencyArray, callback) { | |
if (arguments.length > 1) { | |
var callbackArgs = []; | |
if (dependencyArray.length) { | |
callbackArgs = dependencyArray.map(function(dependencyName) { | |
return libraryStorage[dependencyName]; | |
}); | |
} | |
libraryStorage[libraryName] = callback.apply(null, callbackArgs); | |
} else { | |
return libraryStorage[libraryName]; | |
} | |
} | |
window.librarySystem = librarySystem; | |
})(); | |
// v2 attempt | |
// This version adds support for nested dependencies, as well as for adding | |
// libraries and dependencies in any order (i.e., you can add a library even | |
// if that library's dependencies have not yet been added). | |
(function() { | |
var libraryStorage = {}; | |
function librarySystem(libraryName, dependencyArray, callback) { | |
if (arguments.length > 1) { | |
libraryStorage[libraryName] = { | |
library: callback, | |
dependencies: dependencyArray | |
}; | |
} else { | |
return loadNestedDependencies(libraryName); | |
} | |
} | |
function loadNestedDependencies(libraryName) { | |
var dependencies = []; | |
if (libraryStorage[libraryName].dependencies.length) { | |
dependencies = libraryStorage[libraryName].dependencies.map(function(dependencyName) { | |
return loadNestedDependencies(dependencyName); | |
}); | |
} | |
return libraryStorage[libraryName].library.apply(null, dependencies); | |
} | |
window.librarySystem = librarySystem; | |
})(); | |
// testing library addition prior to dependency addition | |
librarySystem('app', ['router'], function(router) { | |
return { | |
description: 'Just a silly app that has a dependency', | |
dependencies: [arguments] | |
}; | |
}); | |
librarySystem('router', [], function() { | |
return { | |
description: 'Supposedly, I can handle app routing.', | |
dependencies: [arguments] | |
}; | |
}); | |
console.log(librarySystem('app')); | |
// testing nested dependencies | |
librarySystem('dependency', ['app2'], function(app2) { | |
return 'loaded dependency with ' + app2; | |
}); | |
librarySystem('app2', [], function(dependency) { | |
return 'app2'; | |
}); | |
librarySystem('app', ['dependency'], function(dependency) { | |
return 'app with ' + dependency; | |
}); | |
librarySystem('app'); // 'app with loaded dependency' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
False alarm - the issue was with my tests, not the code. When I loaded
app3
, the callback function I passed in only took 1 parameter, but tried to access 2.