Last active
June 16, 2020 00:13
-
-
Save xavierchia/4be9861ba1edb43f25af35a4e8970435 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<script src="simpletest.js"></script> | |
<script> | |
/* | |
The librarySystem should be able to store libraries with dependencies and return the library if called | |
Syntax: | |
Formula: librarySystem(libraryName, dependencyArray, callbackLibrary) | |
Arguments: | |
libraryName | |
dependencyArray | |
callbackLibrary | |
Return Value: The library of libraryName, if adding library, it should return undefined | |
Tests: | |
LibraryName: | |
It should take a string as the first argument [DONE] | |
It should return the library if called and the library exists [DONE] | |
It should throw an error if the library is called and the library does not exist [DONE] | |
dependencyArray: | |
It should take an array as the second argument [DONE] | |
It should load the dependencies into the callbackLibrary [DONE] | |
callbackLibrary: | |
It should take a function as the third argument [DONE] | |
It should store the callbackLibrary return value into the libraryStorage [DONE] | |
*/ | |
(function() { | |
var libraryStorage = {}; | |
function librarySystem(libraryName, dependencyArray, callbackLibrary) { | |
// Check if first argument is a string | |
if (typeof libraryName !== 'string') { | |
throw new TypeError('I am not a string'); | |
} | |
// Adding library to libraryStorage | |
if (arguments.length > 1) { | |
// Check if second argument is an array | |
if (!Array.isArray(dependencyArray)) { | |
throw new TypeError('I am not an array'); | |
} | |
// Replaces the dependencyArray names with the dependent library objects | |
for (var i = 0; i < dependencyArray.length; i++) { | |
dependencyArray[i] = librarySystem(dependencyArray[i]); | |
} | |
// Storing the library with the dependent libraries | |
libraryStorage[libraryName] = callbackLibrary(...dependencyArray); | |
} | |
// Accessing library from libraryStorage | |
else { | |
if (libraryStorage[libraryName]) { | |
return libraryStorage[libraryName]; | |
} | |
else { | |
throw new Error('libraryName not in libraryStorage') | |
} | |
} | |
} | |
window.librarySystem = librarySystem; | |
})(); | |
tests({ | |
'It should take a string as the first argument': function () { | |
var isTypeError = false; | |
try { | |
librarySystem(123); | |
} | |
catch (e) { | |
isTypeError = e instanceof TypeError; | |
} | |
eq(isTypeError, true); | |
}, | |
'It should return the library if called and the library exists': function() { | |
librarySystem('testLibrary', [], function() {return 'testValue'}); | |
eq(librarySystem('testLibrary'), 'testValue'); | |
}, | |
'It should throw an error if the library is called and the library does not exist': function() { | |
var isError = false; | |
try { | |
librarySystem('doesNotExist'); | |
} | |
catch (e) { | |
isError = e instanceof Error; | |
} | |
eq(isError, true); | |
}, | |
'It should take an array as the second argument': function() { | |
var isTypeError = false; | |
try { | |
librarySystem('libraryName', 'notAnArray'); | |
} | |
catch (e) { | |
isTypeError = e instanceof TypeError; | |
} | |
eq(isTypeError, true); | |
}, | |
'It should load the dependencies into the callbackLibrary': function() { | |
librarySystem('name', [], function() { | |
return 'Xavier'; | |
}); | |
librarySystem('country', [], function() { | |
return 'Singapore'; | |
}); | |
librarySystem('countryBlurb', ['name', 'country'], function(name, country) { | |
return name + ' lives in ' + country; | |
}); | |
eq(librarySystem('countryBlurb'), 'Xavier lives in Singapore'); | |
}, | |
'It should take a function as the third argument': function() { | |
var isTypeError = false; | |
try { | |
librarySystem('testLibrary', [], 'I am not a function'); | |
} | |
catch (e) { | |
isTypeError = e instanceof TypeError; | |
} | |
eq(isTypeError, true); | |
}, | |
'It should store the callbackLibrary return value into the libraryStorage': function() { | |
librarySystem('testLibrary2', [], function() {return 'anotherTestValue'}); | |
eq(librarySystem('testLibrary2'), 'anotherTestValue'); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment