Skip to content

Commit

Permalink
opt locales testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Sep 12, 2016
1 parent c2da159 commit f3b1621
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ in 2 years
```


# Usage
# 1. Usage

**1. Install timeago.js**

Expand Down Expand Up @@ -68,7 +68,7 @@ timeago.format('2016-06-12')
```


# Detailed Usage
# 2. Detailed Usage

**1. Set relative date**

Expand Down Expand Up @@ -158,11 +158,16 @@ timeago.format('2016-06-12', 'test_local');
You can see [locales](locales) dir for more locales. Please submit a GitHub pull request for corrections or additional languages, and add the locale key into `tests/locales_test.js`.


# Contribution
# 3. Contributions

The website is base on [rmm5t/jquery-timeago](https://github.com/rmm5t/jquery-timeago) which is a nice and featured project but depends on jQuery.
1. The website is base on [rmm5t/jquery-timeago](https://github.com/rmm5t/jquery-timeago) which is a nice and featured project but depends on jQuery.

2. **locale translations**: The library need the locale translations. You can:

# LICENSE
- Open an issue to write the locale translations, how to ? see [here](https://github.com/hustcc/timeago.js/blob/master/locales/en.js).
- Or pull a request, please **test** it before by exec `npm test` or `node tests/locales_test.js`.


# 4. LICENSE

MIT
File renamed without changes.
6 changes: 6 additions & 0 deletions locales/locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** locales table.
* You can add the missing locale code
* 1. please sort by dictionary.
* 2. please ensure the accuracy.
*/
module.exports = ['ar', 'az', 'az', 'bg', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'dv', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hr', 'hu', 'hy', 'id', 'in_ID', 'is', 'it', 'ja', 'jv', 'ko', 'ky', 'lt', 'lv', 'mk', 'nl', 'no', 'pl', 'pt', 'pt_BR', 'pt', 'pt', 'ro', 'rs', 'ru', 'rw', 'si', 'sk', 'sl', 'sr', 'sv', 'th', 'tr', 'uk', 'uz', 'vi', 'zh_CN', 'zh_TW'];
File renamed without changes.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"gulp-uglify": "^1.5.3",
"jshint": "^2.9.2",
"gulp-rename": "^1.2.2",
"tape": "^3.6.1"
"tape": "^3.6.1",
"pys": "^1.0.2"
},
"scripts": {
"lint": "jshint src/timeago.js",
Expand Down
73 changes: 45 additions & 28 deletions tests/locales_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,52 @@
var fs = require('fs');
var test = require('tape');
var timeago = require('..');
var psy = require('pys');

// all the locales code, if missing, please add them.
var all_locales = require('../locales/locales.js');

function test_locales(tobeTested) {
test('Testing locales', function (t) {
for (var i = 0; i < tobeTested.length ; i++) {
var locale_name = psy(tobeTested[i])('0:-3');
t.ok(all_locales.indexOf(locale_name) >= 0, 'locale [' + locale_name + ']');

console.log('\nTesting locales [' + locale_name + ']');

var locale = require('../locales/' + locale_name);
// test locales
var newTimeAgo = timeago('2016-06-23');
newTimeAgo.register(locale_name, locale);
t.equal(newTimeAgo.format('2016-06-22', locale_name), locale(1, 6)[0]);
t.equal(newTimeAgo.format('2016-06-25', locale_name), locale(2, 7)[1].replace('%s', '2'));

// test default locale
var newTimeAgo = timeago('2016-03-01 12:00:00', locale_name);
newTimeAgo.register(locale_name, locale);
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));

// test setLocale
var newTimeAgo = timeago('2016-03-01 12:00:00');
newTimeAgo.register(locale_name, locale);
newTimeAgo.setLocale(locale_name);
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));
}
t.end();
});
}

// read all the locales in `locales` dir
fs.readdir('locales', function(err, files) {
// rm locales.js file
var index = files.indexOf('locales.js');
if (index > -1) {
files.splice(index, 1);
}
// test them
test_locales(files);
});

var tobeTested = ['en', 'zh_CN', 'fr', 'nl_BE', 'pt_BR', 'da', 'in_ID', 'it', 'es', 'pl', 'zh_TW', 'sv', 'el_GR']; // when add a new locale, add locale's name here

test('Testing locales', function (t) {
for (var i = 0; i < tobeTested.length ; i++) {
var locale_name = tobeTested[i];

console.log('\nTesting locales [' + locale_name + ']');

var locale = require('../locales/' + locale_name);
// test locales
var newTimeAgo = timeago('2016-06-23');
newTimeAgo.register(locale_name, locale);
t.equal(newTimeAgo.format('2016-06-22', locale_name), locale(1, 6)[0]);
t.equal(newTimeAgo.format('2016-06-25', locale_name), locale(2, 7)[1].replace('%s', '2'));

// test default locale
var newTimeAgo = timeago('2016-03-01 12:00:00', locale_name);
newTimeAgo.register(locale_name, locale);
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));

// test setLocale
var newTimeAgo = timeago('2016-03-01 12:00:00');
newTimeAgo.register(locale_name, locale);
newTimeAgo.setLocale(locale_name);
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));
};

t.end();
});


0 comments on commit f3b1621

Please sign in to comment.