Skip to content

Instantly share code, notes, and snippets.

@NickHeiner
Created March 28, 2013 19:55
Show Gist options
  • Save NickHeiner/5266286 to your computer and use it in GitHub Desktop.
Save NickHeiner/5266286 to your computer and use it in GitHub Desktop.
sample node module
/**
*
* Simple, stateless function with no external dependencies.
*
* Example:
*
* add(1, 2, 3) === 6
*
* To test: verify that add() works for all types of input.
*/
function add() {
return Array.prototype.slice.call(arguments).reduce(function(acc, el) {
return acc + el;
}, 0);
}
/**
*
* Simple function that returns an object with default values.
*
* @param userId
* @returns {{userId: *, userName: string, favoriteFoods: Array}}
*
* Example:
*
* createNewUserData(4) === {userId: 4, userName: 'New User', favoriteFoods: ['apples', 'oranges']}
*
* To test: verify that the value returned matches the expected defaults.
*/
function createNewUserData(userId) {
return {
userId: userId,
userName: 'New User',
favoriteFoods: ['apples', 'oranges']
}
}
/**
*
* More complex method with an external dependency on http service
*
* @param http an object that asynchronously fetches the resource for user data
* @param userId the user id for the data
* @param success callback for success
* @param failure callback for failure; called if userId is not a positive number or the network call failed
*
* Example:
*
* getUserData(http,
* 808,
* function(name) { console.log('current user name', name);},
* function(err) { console.log('getUserData failed', err); });
*
* To test: verify that success and failure are called at the right times,
* and that http is delegated to correctly.
*/
function getUserName(http, userId, success, failure) {
if (!(typeof userId === 'number' && userId > 0)) {
failure('invalid user id');
return;
}
http.get('/api/v1/user/' + userId, success, failure);
}
/**
*
* Method side effects and no return value
*
* @param userDataManager object responsible for saving user data
* @param userData the user data to save. If null, this method will be a no-op.
*
* Example:
* saveUserData(userDataManager, null) // no-op
* saveUserData(userDataManager, data) // data is saved
*
* To test: verify that userDataManager.save() is called the right number of times, and under the right circumstances.
*/
function saveUserData(userDataManager, userData) {
if (userData !== null) {
userDataManager.save(userData);
}
}
/**
*
* An expensive calculation that can take up to 3000ms
*
* @param onComplete a callback that will be called with the computed value
*
* To test: verify that expensiveCalculation eventually calls the callback with a computed value.
*/
function expensiveCalculation(onComplete) {
setTimeout(function() {
onComplete('damn this string took a long time to compute')
}, 2500)
}
/**
* Database connection
*/
var connection = {}
/**
* Get the current count of rows by reading connection
*
* @returns {number}
*
* To test: verify that rowCount starts at 0.
*/
function rowCount() {
return connection.rowCount;
}
/**
* Increment the current count of rows
*
* To test: verify that calling this method causes rowCount() to return one more than it previously did.
*/
function addRow() {
connection.rowCount++;
}
function resetConnection() {
connection = {
rowCount: 0
};
}
resetConnection();
module.exports = {
add: add,
getUserData: getUserName,
createNewUserData: createNewUserData,
saveUserData: saveUserData,
expensiveCalculation: expensiveCalculation,
resetConnection: resetConnection,
rowCount: rowCount,
addRow: addRow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment