Skip to content

Instantly share code, notes, and snippets.

@ischenkodv
Created April 18, 2015 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ischenkodv/2869384aefb58f12efee to your computer and use it in GitHub Desktop.
Save ischenkodv/2869384aefb58f12efee to your computer and use it in GitHub Desktop.
Unit tests for lesson 2
/**
* This is module to test.
*/
module.exports = {
camelCase: function(string) {
return string.replace(/-[a-z]/g, function(match) {
return match[1].toUpperCase() + match.slice(2);
});
},
dashSeparated: function(string) {
// Added [0-9]+ regex to add dash before numbers.
return string.replace(/[A-Z]|[0-9]+/g, function(match) {
return '-' + match.toLowerCase();
});
}
};
var strings = require('./strings');
var assert = require('assert');
describe('strings module', function() {
it('should camelCase string with onde dash correctly', function() {
var dashed = 'sample-string';
var expected = 'sampleString';
var camel = strings.camelCase(dashed);
assert.equal(camel, expected);
});
it('should dash separate string with one camelCased character', function() {
var camel = 'sampleString';
var expected = 'sample-string';
var dashed = strings.dashSeparated(camel);
assert.equal(dashed, expected);
});
it('should dash separate string with many camelCased character', function() {
var camel = 'sampleStringWithManyCamelCases';
var expected = 'sample-string-with-many-camel-cases';
var dashed = strings.dashSeparated(camel);
assert.equal(dashed, expected);
});
it('should dash separate camel cased string with numbers', function() {
var camel = 'sampleStringWithNumbers1234';
var expected = 'sample-string-with-numbers-1234';
var dashed = strings.dashSeparated(camel);
assert.equal(dashed, expected);
});
it('should dash separate camel cased string with many numbers', function() {
var camel = 'sampleStringWith9876ManyNumbers1234';
var expected = 'sample-string-with-9876-many-numbers-1234';
var dashed = strings.dashSeparated(camel);
assert.equal(dashed, expected);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment