Skip to content

Commit

Permalink
Count returns as part of all property
Browse files Browse the repository at this point in the history
  • Loading branch information
RadLikeWhoa committed Feb 19, 2014
1 parent d213e86 commit 8900df1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 60 deletions.
50 changes: 0 additions & 50 deletions CHANGELOG.md

This file was deleted.

14 changes: 8 additions & 6 deletions Countable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* counting on an HTML element.
*
* @author Sacha Schmid (<https://github.com/RadLikeWhoa>)
* @version 2.0.1
* @version 2.0.2
* @license MIT
* @see <http://radlikewhoa.github.io/Countable/>
*/
Expand Down Expand Up @@ -124,9 +124,11 @@
* `_extendDefaults` is a function to extend a set of default options with the
* ones given in the function call. Available options are described below.
*
* {Boolean} hardReturns Use two returns to seperate a paragraph instead of
* one.
* {Boolean} stripTags Strip HTML tags before counting the values.
* {Boolean} hardReturns Use two returns to seperate a paragraph instead
* of one.
* {Boolean} stripTags Strip HTML tags before counting the values.
* {Boolean} ignoreReturns Ignore returns when calculating the `all`
* property.
*
* @private
*
Expand All @@ -138,7 +140,7 @@
*/

function _extendDefaults (options) {
var defaults = { hardReturns: false, stripTags: false }
var defaults = { hardReturns: false, stripTags: false, ignoreReturns: false }

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

Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2013 Sacha Schmid
Copyright (C) 2014 Sacha Schmid

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:

Expand Down
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,17 @@ Countable.enabled(area)
```javascript
{
hardReturns: false,
stripTags: false
stripTags: false,
ignoreReturns: false
}
```

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

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.

In most cases, returns should be counted as part of the `all` property. Set `ignoreReturns` to false to remove them from the counter.

## Browser Support

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.
Expand Down Expand Up @@ -125,6 +128,61 @@ Countable.live(area, function (counter) {
* `Countable.live()` and `Countable.once()` both accept one or more elements, rather than just a single one
* Inside the callback, `this` is now bound to the current element

## Changelog

### 2.0.2 _(2014-02-19)_

* NEW: Returns are counted as part of the `all` property. A new option `ignoreReturns` was added to restore the old behaviour.

### 2.0.1 _(2013-07-13)_

* FIX: Missing parameter in `Countable.once`. (Thanks to [MrOPR](https://github.com/RadLikeWhoa/Countable/pull/18))

### 2.0.0 _(2013-05-25)_

* 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.
* NEW: Countable can now work on multiple elements with one function call.
* FIX: Prevent a XSS bug. (Thanks to [Rob--W](https://github.com/RadLikeWhoa/Countable/pull/17))

### 1.4.2 _(2013-05-23)_

* FIX: Fix a bug where options wouldn't be applied correctly.

### 1.4.1 _(2013-05-22)_

* NEW: Added option to execute the callback only once.

### 1.4.0 _(2013-05-20)_

* NEW: Allow for an options object as the third parameter.

### 1.3.0 _(2013-05-16)_

* NEW: Countable is now available as an AMD and CommonJS module.
* FIX: Better handle `textarea` with predefined value. (Thanks to [besmithett](https://github.com/RadLikeWhoa/Countable/pull/15))

### 1.2.0 _(2013-05-02)_

* NEW: Optionally strip HTML tags. (Thanks to [craniumslows](https://github.com/RadLikeWhoa/Countable/pull/13))
* 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))
* IMPROVED: Better handling of punctuation.

### 1.1.1 _(2013-03-16)_

* IMPROVED: Better support for foreign languages and special characters.

### 1.1.0 _(2013-03-12)_

* NEW: Include number of characters including whitespace.
* NEW: Countable is now available on Bower.
* IMPROVED: Improve performance when counting the values.
* IMPROVED: Improve performance when trimming strings by using `String::trim` when available.
* IMPROVED: Better documentation.

### 1.0.0 _(2013-03-11)_

* Initial release

## About the Author

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).
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Countable",
"description": "Countable is a JavaScript function to add live paragraph-, word- and character-counting to an HTML element.",
"version": "2.0.1",
"version": "2.0.2",
"main": "Countable.js",
"ignore": [
"test",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Countable.js",
"description": "Countable is a JavaScript function to add live paragraph-, word- and character-counting to an HTML element.",
"author": "Sacha Schmid <https://twitter.com/sachaschmid>",
"version": "2.0.1",
"version": "2.0.2",
"keywords": [
"paragraphs",
"words",
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ describe('Countable', function () {

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

expect(results.paragraphs.innerHTML).to.equal('2')
expect(results.words.innerHTML).to.equal('2')
expect(results.characters.innerHTML).to.equal('10')
expect(results.all.innerHTML).to.equal('12')
})

it('should ignore returns', function () {
optionsArea.value = 'Hello\nworld'

Countable.once(optionsArea, callback, { ignoreReturns: true })

expect(results.paragraphs.innerHTML).to.equal('2')
expect(results.words.innerHTML).to.equal('2')
expect(results.characters.innerHTML).to.equal('10')
Expand Down

0 comments on commit 8900df1

Please sign in to comment.