Skip to content

Instantly share code, notes, and snippets.

@bearcanrun
Created May 13, 2016 06:15
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 bearcanrun/84a3b10ee9cafc144c32340bf4463cf9 to your computer and use it in GitHub Desktop.
Save bearcanrun/84a3b10ee9cafc144c32340bf4463cf9 to your computer and use it in GitHub Desktop.
Unit Testing Fundamentals: Sec. 2 Exercise

strings.js

module.exports = {,
    dashSeparated: function(string) {
        return string.replace(/([A-Z]|[0-9]+)/g, function(match) {
            return '-' + match.toLowerCase();
        });
    }
};

strings.tests.js

describe ('strings module @focus', function(){
  it('should replace capitalized letters with lower-case letter and dash', function() {
    var camel = 'sampleString';
    var expected = 'sample-string';

    var dashed = strings.dashSeparated(camel);

    assert.equal(dashed, expected);
  });

  it('should replace capitalized letters with lower-case letters prefixed by dashes', function() {
    var camel = 'sampleStringWithDashes';
    var expected = 'sample-string-with-dashes';

    var dashed = strings.dashSeparated(camel);

    assert.equal(dashed, expected);
  });

  it('should insert dash in front of numbers', function() {
    var camel = 'sample1';
    var expected = 'sample-1';

    var dashed = strings.dashSeparated(camel);

    assert.equal(dashed, expected);
  });

  it('should insert dash in front of numbers but keep numbers grouped', function() {
    var camel = 'sample123';
    var expected = 'sample-123';

    var dashed = strings.dashSeparated(camel);

    assert.equal(dashed, expected);
  });

  it('should insert dash & lower-case capitalized letters and insert dash before number groups', function() {
    var camel = 'sample123Dashed456TEST7a';
    var expected = 'sample-123-dashed-456-t-e-s-t-7a';

    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