Skip to content

Instantly share code, notes, and snippets.

@dhigginbotham
Last active December 25, 2015 15:59
Show Gist options
  • Save dhigginbotham/7001865 to your computer and use it in GitHub Desktop.
Save dhigginbotham/7001865 to your computer and use it in GitHub Desktop.
a little test driven development stringhelper fun
var helpers = {};
helpers.removeTrailingSlash = function (string, fn) {
var s = string || null;
var callback = ((typeof fn != 'undefined') && (typeof fn == 'function')) ? fn : null;
var sanitized, hasTrailingSlash, ln;
if (s) {
ln = s.length;
hasTrailingSlash = (s[ln - 1] == '/') ? true : false;
if (hasTrailingSlash) {
sanitized = s.substring(0, (ln - 1));
};
};
if (callback) {
return callback(sanitized);
} else {
return sanitized;
}
};
module.exports = helpers;
var _ = require('lodash');
var expect = require('expect.js');
var helpers = require('./helpers');
describe('test helpers.js methods to ensure they\'re working the way we rely on them to', function () {
var hasTrailingSlash = 'https://api.github.com/';
var noTrailingSlash = 'https://api.github.com';
var beginingSlashPath = '/path/of/endpoint/n';
var TrailingSlashPath = '/path/of/endpoint/n/';
describe('non-blocking: test removeTrailingSlash', function () {
helpers.removeTrailingSlash(hasTrailingSlash, function (helperResult) {
it('goes from ' + hasTrailingSlash + ' to ' + noTrailingSlash, function (done) {
expect(helperResult).not.to.be(null);
expect(helperResult).to.equal(noTrailingSlash);
return done();
});
});
helpers.removeTrailingSlash(TrailingSlashPath, function (helperResult) {
it('goes from ' + TrailingSlashPath + ' to ' + beginingSlashPath, function (done) {
expect(helperResult).not.to.be(null);
expect(helperResult).to.equal(beginingSlashPath);
return done();
});
});
});
describe('blocking: test removeTrailingSlash', function () {
it('goes from ' + hasTrailingSlash + ' to ' + noTrailingSlash, function (done) {
var helperResult = helpers.removeTrailingSlash(hasTrailingSlash);
expect(helperResult).not.to.be(null);
expect(helperResult).to.equal(noTrailingSlash);
return done();
});
it('goes from ' + TrailingSlashPath + ' to ' + beginingSlashPath, function (done) {
var helperResult = helpers.removeTrailingSlash(TrailingSlashPath);
expect(helperResult).not.to.be(null);
expect(helperResult).to.equal(beginingSlashPath);
return done();
});
});
// end of removeTrailingSlash tests
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment