Last active
June 16, 2020 02:54
-
-
Save xavierchia/4bff1818aa08ecad00a41593b529f5c7 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] | |
It should work with unlimited number of dependencies levels [DONE] | |
It should throw an error if not all dependencies exist | |
callbackLibrary: | |
It should take a function as the third argument [DONE] | |
It should store the callbackLibrary return value into the libraryStorage [DONE] | |
It should work if the library is created before the dependencies [DONE] | |
It should only run the callback function once [DONE] | |
It should only run callback if all dependencies exist [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'); | |
} | |
// Storing the callback function and the dependencyArray | |
if (arguments.length > 1) { | |
// Check if second argument is an array | |
if (!Array.isArray(dependencyArray)) { | |
throw new TypeError('I am not an array'); | |
} | |
// CHeck if third argument is a function | |
if (typeof callbackLibrary !== 'function') { | |
throw new TypeError('I am not a function'); | |
} | |
// Storing the callback function and the dependencyArray | |
libraryStorage[libraryName] = callbackLibrary; | |
libraryStorage[libraryName + '-dependencyArray'] = dependencyArray; | |
} | |
// Creating the library | |
else { | |
if (typeof libraryStorage[libraryName] === 'function') { | |
// Create the dependencyArray with dependent library objects if all dependencies exist | |
var dependencyArray = libraryStorage[libraryName + '-dependencyArray']; | |
var allDependenciesExist = dependencyArray.every(function(dependency) { | |
return dependency in libraryStorage; | |
}); | |
if (allDependenciesExist) { | |
for (var i = 0; i < dependencyArray.length; i++) { | |
dependencyArray[i] = librarySystem(dependencyArray[i]); | |
} | |
// Creating library with dependencies and deleting helper property | |
var callbackLibrary = libraryStorage[libraryName]; | |
libraryStorage[libraryName] = callbackLibrary(...dependencyArray); | |
delete libraryStorage[libraryName + '-dependencyArray']; | |
} | |
// Throw an error if not all dependencies exist | |
else { | |
throw new Error('Not all dependencies exist'); | |
} | |
} | |
// Throw an error if libraryName not in libraryStorage | |
else if (libraryStorage[libraryName] === undefined) { | |
throw new Error('libraryName not in libraryStorage') | |
} | |
// Returning the library through direct call or recursion | |
return libraryStorage[libraryName]; | |
} | |
} | |
// IIFE to assign librarySystem function to the window | |
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 work with unlimited number of dependencies levels': function () { | |
librarySystem('workBlurb', ['fullName', 'company'], function (fullName, company) { | |
return fullName + ' works at ' + company | |
}); | |
librarySystem('fullName', ['name', 'surname'], function (name, surname) { | |
return name + ' ' + surname | |
}); | |
librarySystem('name', [], function () { | |
return 'Mila' | |
}); | |
librarySystem('surname', [], function () { | |
return 'Ronkko' | |
}); | |
librarySystem('company', [], function () { | |
return 'Google' | |
}); | |
let result = librarySystem('workBlurb'); | |
eq(result, 'Mila Ronkko works at Google') | |
}, | |
'It should throw an error if not all dependencies exist': function() { | |
librarySystem('pillow', ['soft'], function(soft) { | |
return soft; | |
}); | |
var isError = false; | |
try { | |
librarySystem('pillow'); | |
} | |
catch (e) { | |
isError = e instanceof Error; | |
} | |
eq(isError, true); | |
}, | |
'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'); | |
}, | |
'It should work if the library is created before the dependencies': function() { | |
librarySystem('food', ['meat', 'starch'], function(meat, starch) { | |
return meat + ' ' + starch; | |
}); | |
librarySystem('meat', [], function() { | |
return 'chicken'; | |
}); | |
librarySystem('starch', [], function() { | |
return 'rice'; | |
}); | |
eq(librarySystem('food'), 'chicken rice'); | |
}, | |
'It should only run the callback function once': function() { | |
var callbackRunNumber = 0; | |
librarySystem('drink', [], function() { | |
callbackRunNumber++; | |
return 'coke'; | |
}); | |
librarySystem('drink'); | |
eq(callbackRunNumber, 1); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment