Skip to content

Commit 8900df1

Browse files
committed
Count returns as part of all property
1 parent d213e86 commit 8900df1

7 files changed

Lines changed: 81 additions & 60 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

Countable.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* counting on an HTML element.
44
*
55
* @author Sacha Schmid (<https://github.com/RadLikeWhoa>)
6-
* @version 2.0.1
6+
* @version 2.0.2
77
* @license MIT
88
* @see <http://radlikewhoa.github.io/Countable/>
99
*/
@@ -124,9 +124,11 @@
124124
* `_extendDefaults` is a function to extend a set of default options with the
125125
* ones given in the function call. Available options are described below.
126126
*
127-
* {Boolean} hardReturns Use two returns to seperate a paragraph instead of
128-
* one.
129-
* {Boolean} stripTags Strip HTML tags before counting the values.
127+
* {Boolean} hardReturns Use two returns to seperate a paragraph instead
128+
* of one.
129+
* {Boolean} stripTags Strip HTML tags before counting the values.
130+
* {Boolean} ignoreReturns Ignore returns when calculating the `all`
131+
* property.
130132
*
131133
* @private
132134
*
@@ -138,7 +140,7 @@
138140
*/
139141

140142
function _extendDefaults (options) {
141-
var defaults = { hardReturns: false, stripTags: false }
143+
var defaults = { hardReturns: false, stripTags: false, ignoreReturns: false }
142144

143145
for (var prop in options) {
144146
if (defaults.hasOwnProperty(prop)) defaults[prop] = options[prop]
@@ -187,7 +189,7 @@
187189
paragraphs: trimmed ? (trimmed.match(options.hardReturns ? /\n{2,}/g : /\n+/g) || []).length + 1 : 0,
188190
words: trimmed ? (trimmed.replace(/['";:,.?¿\-!¡]+/g, '').match(/\S+/g) || []).length : 0,
189191
characters: trimmed ? _decode(trimmed.replace(/\s/g, '')).length : 0,
190-
all: _decode(original.replace(/[\n\r]/g, '')).length
192+
all: _decode(options.ignoreReturns ? original.replace(/[\n\r]/g, '') : original).length
191193
}
192194
}
193195

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (C) 2013 Sacha Schmid
1+
Copyright (C) 2014 Sacha Schmid
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,17 @@ Countable.enabled(area)
8686
```javascript
8787
{
8888
hardReturns: false,
89-
stripTags: false
89+
stripTags: false,
90+
ignoreReturns: false
9091
}
9192
```
9293

9394
By default, paragraphs are split by a single return (a soft return). By setting `hardReturns` to true, Countable splits paragraphs after two returns.
9495

9596
Depending on your application and audience, you might need to strip HTML tags from the text before counting it. You can do this by setting `stripTags` to true.
9697

98+
In most cases, returns should be counted as part of the `all` property. Set `ignoreReturns` to false to remove them from the counter.
99+
97100
## Browser Support
98101

99102
Countable supports all modern browsers. Internet Explorer is supported down to version 7. Note that some browsers don't implement the `oninput` event consistently so there might be differences in the way Countable works in different browsers.
@@ -125,6 +128,61 @@ Countable.live(area, function (counter) {
125128
* `Countable.live()` and `Countable.once()` both accept one or more elements, rather than just a single one
126129
* Inside the callback, `this` is now bound to the current element
127130

131+
## Changelog
132+
133+
### 2.0.2 _(2014-02-19)_
134+
135+
* NEW: Returns are counted as part of the `all` property. A new option `ignoreReturns` was added to restore the old behaviour.
136+
137+
### 2.0.1 _(2013-07-13)_
138+
139+
* FIX: Missing parameter in `Countable.once`. (Thanks to [MrOPR](https://github.com/RadLikeWhoa/Countable/pull/18))
140+
141+
### 2.0.0 _(2013-05-25)_
142+
143+
* NEW: Countable has a new Syntax. You can now use `Countable.live`, `Countable.once`, `Countable.die` and `Countable.enabled`. Notes on upgrading is provided in the README.
144+
* NEW: Countable can now work on multiple elements with one function call.
145+
* FIX: Prevent a XSS bug. (Thanks to [Rob--W](https://github.com/RadLikeWhoa/Countable/pull/17))
146+
147+
### 1.4.2 _(2013-05-23)_
148+
149+
* FIX: Fix a bug where options wouldn't be applied correctly.
150+
151+
### 1.4.1 _(2013-05-22)_
152+
153+
* NEW: Added option to execute the callback only once.
154+
155+
### 1.4.0 _(2013-05-20)_
156+
157+
* NEW: Allow for an options object as the third parameter.
158+
159+
### 1.3.0 _(2013-05-16)_
160+
161+
* NEW: Countable is now available as an AMD and CommonJS module.
162+
* FIX: Better handle `textarea` with predefined value. (Thanks to [besmithett](https://github.com/RadLikeWhoa/Countable/pull/15))
163+
164+
### 1.2.0 _(2013-05-02)_
165+
166+
* NEW: Optionally strip HTML tags. (Thanks to [craniumslows](https://github.com/RadLikeWhoa/Countable/pull/13))
167+
* NEW: Include ucs2decode function from the [punycode](https://github.com/bestiejs/punycode.js) library to better handle special characters. (Thanks to [craniumslows](https://github.com/RadLikeWhoa/Countable/pull/13))
168+
* IMPROVED: Better handling of punctuation.
169+
170+
### 1.1.1 _(2013-03-16)_
171+
172+
* IMPROVED: Better support for foreign languages and special characters.
173+
174+
### 1.1.0 _(2013-03-12)_
175+
176+
* NEW: Include number of characters including whitespace.
177+
* NEW: Countable is now available on Bower.
178+
* IMPROVED: Improve performance when counting the values.
179+
* IMPROVED: Improve performance when trimming strings by using `String::trim` when available.
180+
* IMPROVED: Better documentation.
181+
182+
### 1.0.0 _(2013-03-11)_
183+
184+
* Initial release
185+
128186
## About the Author
129187

130188
My name is [Sacha Schmid](http://sachaschmid.ch) ([**@sachaschmid**](https://twitter.com/sachaschmid)). I'm a front-end engineer from Switzerland. I am the creator of [SSGS](http://github.com/RadLikeWhoa/SSGS) and [other open source projects](https://github.com/RadLikeWhoa).

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Countable",
33
"description": "Countable is a JavaScript function to add live paragraph-, word- and character-counting to an HTML element.",
4-
"version": "2.0.1",
4+
"version": "2.0.2",
55
"main": "Countable.js",
66
"ignore": [
77
"test",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Countable.js",
33
"description": "Countable is a JavaScript function to add live paragraph-, word- and character-counting to an HTML element.",
44
"author": "Sacha Schmid <https://twitter.com/sachaschmid>",
5-
"version": "2.0.1",
5+
"version": "2.0.2",
66
"keywords": [
77
"paragraphs",
88
"words",

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ describe('Countable', function () {
118118

119119
Countable.once(optionsArea, callback, { hardReturns: true })
120120

121+
expect(results.paragraphs.innerHTML).to.equal('2')
122+
expect(results.words.innerHTML).to.equal('2')
123+
expect(results.characters.innerHTML).to.equal('10')
124+
expect(results.all.innerHTML).to.equal('12')
125+
})
126+
127+
it('should ignore returns', function () {
128+
optionsArea.value = 'Hello\nworld'
129+
130+
Countable.once(optionsArea, callback, { ignoreReturns: true })
131+
121132
expect(results.paragraphs.innerHTML).to.equal('2')
122133
expect(results.words.innerHTML).to.equal('2')
123134
expect(results.characters.innerHTML).to.equal('10')

0 commit comments

Comments
 (0)