From b92980ce6d321af645172e51af62a958cee51273 Mon Sep 17 00:00:00 2001 From: William Dibbern Date: Sun, 11 Aug 2013 11:55:42 -0500 Subject: [PATCH] Fixed word count grammar Closes #349 - Added utility function to pluralize when appropriate. --- core/client/assets/lib/jquery-utils.js | 13 ++++++ core/client/views/editor.js | 6 +-- core/test/functional/admin/03_editor_test.js | 43 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/core/client/assets/lib/jquery-utils.js b/core/client/assets/lib/jquery-utils.js index c7472b1c5e..f5ee7885ae 100644 --- a/core/client/assets/lib/jquery-utils.js +++ b/core/client/assets/lib/jquery-utils.js @@ -82,4 +82,17 @@ $('.overlay').hideAway(); // TODO: Move to a more sensible global file. + /** + * Adds appropriate inflection for pluralizing the singular form of a word when appropriate. + * This is an overly simplistic implementation that does not handle irregular plurals. + * @param {Number} count + * @param {String} singularWord + * @returns {String} + */ + $.pluralize = function inflect(count, singularWord) { + var base = [count, ' ', singularWord]; + + return (count === 1) ? base.join('') : base.concat('s').join(''); + }; + }()); \ No newline at end of file diff --git a/core/client/views/editor.js b/core/client/views/editor.js index 010fdd7493..45ecf7be78 100644 --- a/core/client/views/editor.js +++ b/core/client/views/editor.js @@ -305,9 +305,9 @@ preview.innerHTML = this.converter.makeHtml(this.editor.getValue()); view.$('.js-drop-zone').upload({editor: true}); Countable.once(preview, function (counter) { - view.$('.entry-word-count').text(counter.words + ' words'); - view.$('.entry-character-count').text(counter.characters + ' characters'); - view.$('.entry-paragraph-count').text(counter.paragraphs + ' paragraphs'); + view.$('.entry-word-count').text($.pluralize(counter.words, 'word')); + view.$('.entry-character-count').text($.pluralize(counter.characters, 'character')); + view.$('.entry-paragraph-count').text($.pluralize(counter.paragraphs, 'paragraph')); }); }, diff --git a/core/test/functional/admin/03_editor_test.js b/core/test/functional/admin/03_editor_test.js index 10c99824ad..d5c9cd8192 100644 --- a/core/test/functional/admin/03_editor_test.js +++ b/core/test/functional/admin/03_editor_test.js @@ -80,6 +80,49 @@ casper.test.begin("Haunted markdown in editor works", 3, function suite(test) { test.assertSelectorHasText('.entry-preview .rendered-markdown', 'Add image of some text', 'Editor value is correct'); }); + casper.run(function () { + test.done(); + }); +}); + +casper.test.begin("Word count and plurality", 4, function suite(test) { + + casper.test.filename = "editor_plurality_test.png"; + + casper.start(url + "ghost/editor", function testTitleAndUrl() { + test.assertTitle("", "Ghost admin has no title"); + }).viewport(1280, 1024); + + casper.then(function checkZeroPlural() { + test.assertSelectorHasText('.entry-word-count', '0 words', 'count of 0 produces plural "words".'); + }); + + casper.then(function () { + casper.writeContentToCodeMirror('test'); + }); + + // We must wait after sending keys to CodeMirror + casper.wait(1000, function doneWait() { + this.echo('I\'ve waited for 1 seconds.'); + }); + + casper.then(function checkSinglular() { + test.assertSelectorHasText('.entry-word-count', '1 word', 'count of 1 produces singular "word".'); + }); + + casper.then(function () { + casper.writeContentToCodeMirror('test'); // append another word, assumes newline + }); + + // We must wait after sending keys to CodeMirror + casper.wait(1000, function doneWait() { + this.echo('I\'ve waited for 1 seconds.'); + }); + + casper.then(function checkPlural() { + test.assertSelectorHasText('.entry-word-count', '2 words', 'count of 2 produces plural "words".'); + }); + casper.run(function () { test.done(); });