Skip to content
Browse files

Merge pull request #20 from MicheleBertoli/master

javascript exercise unicode file to html converter: added async callback in the exercise code and in the proposed solution by MicheleBertoli
  • Loading branch information...
2 parents ccfa26b + 295a897 commit 1d88bd2df4860547f849fbe82cecbb14b0fd331e @lucaminudel committed Jan 19, 2014
View
24 ...cript/unicode-file-to-htm-text-converter-tests/unicode-file-to-htm-text-converter-test.js
@@ -5,37 +5,37 @@ describe('Unicode To Htm Converter', function () {
it('should convert ampersand', function () {
var stubTextStream = a.stub(TextStream);
- spyOn(stubTextStream, 'getText').andCallFake(function () { return 'Cut & Paste'; });
+ spyOn(stubTextStream, 'getText').andCallFake(function (callback) { return callback('Cut & Paste'); });
var target = new UnicodeFileToHtmTextConverter(stubTextStream);
- var result = target.convertToHtml();
-
- expect(result).toEqual('Cut & Paste');
+ target.convertToHtml(function(result) {
+ expect(result).toEqual('Cut & Paste');
+ });
});
it('should convert greate than and less than', function () {
var stubTextStream = a.stub(TextStream);
- spyOn(stubTextStream, 'getText').andCallFake(function () { return '10 > 5; 5 < 10'; });
+ spyOn(stubTextStream, 'getText').andCallFake(function (callback) { return callback('10 > 5; 5 < 10'); });
var target = new UnicodeFileToHtmTextConverter(stubTextStream);
- var result = target.convertToHtml();
-
- expect(result).toEqual('10 &gt; 5; 5 &lt; 10');
+ target.convertToHtml(function(result) {
+ expect(result).toEqual('10 &gt; 5; 5 &lt; 10');
+ });
});
it('should add breakline for multiple lines', function () {
var stubTextStream = a.stub(TextStream);
- spyOn(stubTextStream, 'getText').andCallFake(function () { return 'hello\nhow are you doing?\n'; });
+ spyOn(stubTextStream, 'getText').andCallFake(function (callback) { return callback('hello\nhow are you doing?\n'); });
var target = new UnicodeFileToHtmTextConverter(stubTextStream);
- var result = target.convertToHtml();
-
- expect(result).toEqual('hello<br />how are you doing?<br />');
+ target.convertToHtml(function(result) {
+ expect(result).toEqual('hello<br />how are you doing?<br />');
+ });
});
});
View
8 ...roExercises.ProposedSolution/Javascript/unicode-file-to-htm-text-converter/text-stream.js
@@ -4,16 +4,18 @@ TextStream = function (fileBlob) {
TextStream.prototype = {
- getText: function () {
+ getText: function (callback) {
var fileReader = new FileReader();
var text;
fileReader.onload = function (evt) {
text = evt.target.result;
+ if (callback) {
+ callback(text);
+ }
};
-
fileReader.readAsText(this._fileBlob);
- return text;
}
+
};
View
14 ...ution/Javascript/unicode-file-to-htm-text-converter/unicode-file-to-htm-text-converter.js
@@ -9,12 +9,16 @@ UnicodeFileToHtmTextConverter.CreateFromFileBlob = function(fileBlob) {
UnicodeFileToHtmTextConverter.prototype = {
- convertToHtml: function () {
+ convertToHtml: function (callback) {
- var text = this._textStream.getText();
- var htmlLines = this._basicHtmlEncode(text);
-
- return htmlLines;
+ var self = this;
+ self._textStream.getText(function(text) {
+ var htmlLines = self._basicHtmlEncode(text);
+ if (callback) {
+ callback(htmlLines);
+ }
+ });
+
},
_basicHtmlEncode: function (source) {
View
9 ...cises/Javascript/unicode-file-to-htm-text-converter/unicode-file-to-htm-text-converter.js
@@ -6,17 +6,20 @@ UnicodeFileToHtmTextConverter = function(fileBlob) {
UnicodeFileToHtmTextConverter.prototype = {
- convertToHtml: function () {
+ convertToHtml: function (callback) {
+ var self = this;
var fileReader = new FileReader();
var text;
fileReader.onload = function(evt) {
text = evt.target.result;
+ var htmlLines = self._basicHtmlEncode(text);
+ if (callback) {
+ callback(htmlLines);
+ }
};
fileReader.readAsText(this._fileBlob);
- var htmlLines = this._basicHtmlEncode(text);
- return htmlLines;
},
_basicHtmlEncode: function (source) {

0 comments on commit 1d88bd2

Please sign in to comment.
Something went wrong with that request. Please try again.