Skip to content

Instantly share code, notes, and snippets.

@tiffwu
Last active December 20, 2016 21:28
Show Gist options
  • Save tiffwu/a752e1590fdd7008248132b83fdb8fc6 to your computer and use it in GitHub Desktop.
Save tiffwu/a752e1590fdd7008248132b83fdb8fc6 to your computer and use it in GitHub Desktop.
solution to Watch and Code challenge #1: 'librarySystem with dependencies'
// 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'
@tiffwu
Copy link
Author

tiffwu commented Dec 20, 2016

Ergh, found an issue trying to run these tests:

librarySystem('dependency', ['app2'], function(app2) {
  return 'loaded dependency with ' + app2;
});

librarySystem('app2', ['app3'], function(app3) {
  return 'app2 with ' + app3;
});

librarySystem('app3', ['app4', 'app4b'], function(app4) {
  return 'app3 with ' + app4 + ' and ' + app4b;
});

librarySystem('app4', [], function() {
  return 'app4';
});

librarySystem('app4b', [], function() {
  return 'app4b';
});

librarySystem('app', ['dependency'], function(dependency) {
  return 'app with ' + dependency;
});

librarySystem('app'); // 'app with loaded dependency with app2 with app3 with app4 and app4b'

@tiffwu
Copy link
Author

tiffwu commented Dec 20, 2016

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.

@jjsquillante
Copy link

very well done!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment