Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (typeof define !== 'function') { var define = require('amdefine')(module); }

define(function() {
return {
reduceString: function(str, amount) {
},
wordWrap: function(str, cols) {
},
reverseString: function(str) {
}
}
});
60 changes: 60 additions & 0 deletions tests/app/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*globals describe:true, it:true, expect:true, beforeEach:true */
if (typeof define !== 'function') { var define = require('amdefine')(module); }
if (typeof expect !== 'function') { var expect = require('expect.js'); }

define([
'app/strings'
], function(answers) {

describe('strings', function() {
it('you should be able to reduce duplicate characters to a desired minimum', function() {
expect(answers.reduceString('aaaabbbb', 2)).to.eql('aabb');
expect(answers.reduceString('xaaabbbb', 2)).to.eql('xaabb');
expect(answers.reduceString('aaaabbbb', 1)).to.eql('ab');
expect(answers.reduceString('aaxxxaabbbb', 2)).to.eql('aaxxaabb');
});

it('you should be able to wrap lines at some arbitrary count, but don\'t break words', function() {
//create the data
var formattedStr;
var data = [
'abcdef abcde abc def',
'abc abc abc',
'a b c def'
];
var wrapCol = 5;

var computedData = [
'abcdef\nabcde\nabc\ndef',
'abc\nabc\nabc',
'a b c\ndef'
];


data.forEach(function(str, index) {
formattedStr = answers.wordWrap(str, wrapCol);
//every char at the wrap line should be a space
expect(formattedStr).to.eql(computedData[index]);
//the last characters should still be the last characters
expect(formattedStr.charAt(formattedStr.length-1)).to.eql(computedData[index].charAt(computedData[index].length-1));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check necessary? Seems like the equality check above should be sufficient.

});

});
it('you should be able to reverse a string', function() {
var data = [
'abc',
'i am a string of characters',
'A man, a plan, a canal: Panama'
];

data.forEach(function(str) {
var result = answers.reverseString(str);
//should be same length
expect(result.length).to.eql(str.length);
//middle character should be the same
var mid = Math.floor(result.length);
expect(result.charAt(mid)).to.eql(str.charAt(mid));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable mid is going to end up just being the string's length, so it doesn't seem right to expect these two things to be equal.

});
});
});
});