Skip to content

Commit

Permalink
v2.4.1: Enabling customization of delimiter and last unit prefix; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mckamey committed Nov 8, 2014
1 parent faaa1b0 commit b2289b7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "countdownjs",
"version": "2.3.4",
"version": "2.4.1",
"description": "A simple JavaScript API for producing an accurate, intuitive description of the timespan between two Date instances.",
"main": "countdown.js",
"keywords": [
Expand Down
33 changes: 25 additions & 8 deletions countdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*global window */
/**
* @license countdown.js v2.4.0 http://countdownjs.org
* @license countdown.js v2.4.1 http://countdownjs.org
* Copyright (c)2006-2014 Stephen M. McKamey.
* Licensed under The MIT License.
*/
Expand Down Expand Up @@ -325,18 +325,28 @@ function(module) {

/**
* @private
* @const
* @type {Array}
*/
var LABELS_SINGLUAR;

/**
* @private
* @const
* @type {Array}
*/
var LABELS_PLURAL;

/**
* @private
* @type {string}
*/
var LABEL_LAST;

/**
* @private
* @type {string}
*/
var LABEL_DELIM;

/**
* @private
* @param {number} value
Expand Down Expand Up @@ -379,9 +389,9 @@ function(module) {
return '';
}
if (count > 1) {
label[count-1] = 'and '+label[count-1];
label[count-1] = LABEL_LAST+label[count-1];
}
return label.join(', ');
return label.join(LABEL_DELIM);
};

/**
Expand All @@ -404,9 +414,9 @@ function(module) {
label[i] = '<'+tag+'>'+label[i]+'</'+tag+'>';
}
if (--count) {
label[count] = 'and '+label[count];
label[count] = LABEL_LAST+label[count];
}
return label.join(', ');
return label.join(LABEL_DELIM);
};

/**
Expand Down Expand Up @@ -1116,8 +1126,10 @@ function(module) {
* @public
* @param {string|Array} singular a pipe ('|') delimited list of singular unit name overrides
* @param {string|Array} plural a pipe ('|') delimited list of plural unit name overrides
* @param {string} last a prefix for the last unit if more than one (default: 'and ')
* @param {string} delim a delimiter to use between units (default: ', ')
*/
var setLabels = countdown.setLabels = function(singular, plural) {
var setLabels = countdown.setLabels = function(singular, plural, last, delim) {
singular = singular || [];
if (singular.split) {
singular = singular.split('|');
Expand All @@ -1132,6 +1144,9 @@ function(module) {
LABELS_SINGLUAR[i] = singular[i] || LABELS_SINGLUAR[i];
LABELS_PLURAL[i] = plural[i] || LABELS_PLURAL[i];
}

LABEL_LAST = ('string' === typeof last) ? last : LABEL_LAST;
LABEL_DELIM = ('string' === typeof delim) ? delim : LABEL_DELIM;
};

/**
Expand All @@ -1141,6 +1156,8 @@ function(module) {
var resetLabels = countdown.resetLabels = function() {
LABELS_SINGLUAR = 'millisecond|second|minute|hour|day|week|month|year|decade|century|millennium'.split('|');
LABELS_PLURAL = 'milliseconds|seconds|minutes|hours|days|weeks|months|years|decades|centuries|millennia'.split('|');
LABEL_LAST = 'and ';
LABEL_DELIM = ', ';
};

resetLabels();
Expand Down
26 changes: 13 additions & 13 deletions countdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "countdown",
"description": "A simple JavaScript API for producing an accurate, intuitive description of the timespan between two Date instances.",
"version": "2.4.0",
"version": "2.4.1",
"homepage": "http://countdownjs.org",
"author": "Stephen McKamey <stephen@mckamey.com>",
"repository": {
Expand Down
44 changes: 22 additions & 22 deletions test/formatTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,18 +517,18 @@ test('Plural overrides', function() {

var input = countdown(start, end, countdown.ALL);

countdown.setLabels(null, 'A|B|C|D|E|F|G|H|I|J|K');
countdown.setLabels(null, 'A|B|C|D|E|F|G|H|I|J|K', '', ' &amp; ');

var expected =
'<em>2 K</em>, ' +
'<em>2 J</em>, ' +
'<em>2 H</em>, ' +
'<em>2 G</em>, ' +
'<em>2 F</em>, ' +
'<em>2 E</em>, ' +
'<em>2 D</em>, ' +
'<em>2 C</em>, ' +
'<em>2 B</em>, and ' +
'<em>2 K</em> &amp; ' +
'<em>2 J</em> &amp; ' +
'<em>2 H</em> &amp; ' +
'<em>2 G</em> &amp; ' +
'<em>2 F</em> &amp; ' +
'<em>2 E</em> &amp; ' +
'<em>2 D</em> &amp; ' +
'<em>2 C</em> &amp; ' +
'<em>2 B</em> &amp; ' +
'<em>2 A</em>';

var actual = input.toHTML('em');
Expand Down Expand Up @@ -556,18 +556,18 @@ test('Partial singular overrides', function() {

var input = countdown(start, end, countdown.ALL);

countdown.setLabels('a||c||e||g||i||k', '|B||D||F||H||J|');
countdown.setLabels('a||c||e||g||i||k', '|B||D||F||H||J|', 'finally ', ' + ');

var expected =
'<em>1 k</em>, ' +
'<em>1 century</em>, ' +
'<em>1 year</em>, ' +
'<em>1 g</em>, ' +
'<em>1 week</em>, ' +
'<em>1 e</em>, ' +
'<em>1 hour</em>, ' +
'<em>1 c</em>, ' +
'<em>1 second</em>, and ' +
'<em>1 k</em> + ' +
'<em>1 century</em> + ' +
'<em>1 year</em> + ' +
'<em>1 g</em> + ' +
'<em>1 week</em> + ' +
'<em>1 e</em> + ' +
'<em>1 hour</em> + ' +
'<em>1 c</em> + ' +
'<em>1 second</em> + finally ' +
'<em>1 a</em>';

var actual = input.toHTML('em');
Expand Down Expand Up @@ -595,7 +595,7 @@ test('Partial plural overrides', function() {

var input = countdown(start, end, countdown.ALL);

countdown.setLabels('a||c||e||g||i||k', '|B||D||F||H||J|');
countdown.setLabels('a||c||e||g||i||k', '|B||D||F||H||J|', '');

var expected =
'<em>2 millennia</em>, ' +
Expand All @@ -606,7 +606,7 @@ test('Partial plural overrides', function() {
'<em>2 days</em>, ' +
'<em>2 D</em>, ' +
'<em>2 minutes</em>, ' +
'<em>2 B</em>, and ' +
'<em>2 B</em>, ' +
'<em>2 milliseconds</em>';

var actual = input.toHTML('em');
Expand Down

0 comments on commit b2289b7

Please sign in to comment.