javascript-time-ago
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -38,5 +38,5 @@ 'use strict'; | ||
var _preset = require('./preset'); | ||
var _style = require('./style'); | ||
var _preset2 = _interopRequireDefault(_preset); | ||
var _style2 = _interopRequireDefault(_style); | ||
@@ -67,3 +67,3 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Presets | ||
this.preset = (0, _preset2.default)(locales); | ||
this.style = (0, _style2.default)(locales); | ||
} | ||
@@ -109,7 +109,7 @@ | ||
// Get locale messages for this formatting style | ||
// Get locale messages for this formatting flavour | ||
var _locale_data = this.locale_data(options.style); | ||
var _locale_data = this.locale_data(options.flavour); | ||
var style = _locale_data.style; | ||
var flavour = _locale_data.flavour; | ||
var locale_data = _locale_data.locale_data; | ||
@@ -175,3 +175,3 @@ | ||
var formatters = this.get_formatters(unit, style); | ||
var formatters = this.get_formatters(unit, flavour); | ||
@@ -198,16 +198,16 @@ // default formatter: "X units" | ||
// Gets locale messages for this formatting 'style' | ||
// Gets locale messages for this formatting flavour | ||
}, { | ||
key: 'locale_data', | ||
value: function locale_data(style) { | ||
value: function locale_data(flavour) { | ||
// Get relative time formatter messages for this locale | ||
var locale_data = React_time_ago.locale_data[this.locale]; | ||
// Fallback to "default" style if the given style isn't available | ||
if (!style || !locale_data[style]) { | ||
style = 'default'; | ||
// Fallback to "default" flavour if the given flavour isn't available | ||
if (!flavour || !locale_data[flavour]) { | ||
flavour = 'default'; | ||
} | ||
return { style: style, locale_data: locale_data[style] }; | ||
return { flavour: flavour, locale_data: locale_data[flavour] }; | ||
} | ||
@@ -220,12 +220,12 @@ | ||
key: 'get_formatters', | ||
value: function get_formatters(unit, style) { | ||
if (!this.formatters[style]) { | ||
this.formatters[style] = {}; | ||
value: function get_formatters(unit, flavour) { | ||
if (!this.formatters[flavour]) { | ||
this.formatters[flavour] = {}; | ||
} | ||
var formatters = this.formatters[style]; | ||
var formatters = this.formatters[flavour]; | ||
// Create a new synthetic message based on the locale data from CLDR. | ||
if (!formatters[unit]) { | ||
formatters[unit] = this.compile_formatters(unit, style); | ||
formatters[unit] = this.compile_formatters(unit, flavour); | ||
} | ||
@@ -241,6 +241,6 @@ | ||
key: 'compile_formatters', | ||
value: function compile_formatters(unit, style) { | ||
value: function compile_formatters(unit, flavour) { | ||
// Locale specific time interval formatter messages | ||
// for the given time interval measurement unit | ||
var formatter_messages = React_time_ago.locale_data[this.locale][style][unit]; | ||
var formatter_messages = React_time_ago.locale_data[this.locale][flavour][unit]; | ||
@@ -247,0 +247,0 @@ // Locale specific time interval formatter messages |
@@ -478,4 +478,6 @@ export const long = { | ||
short, | ||
short_concise, | ||
long, | ||
long_concise, | ||
default: long | ||
} |
@@ -572,4 +572,6 @@ export const long = { | ||
short, | ||
short_concise, | ||
long, | ||
long_concise, | ||
default: long | ||
} |
{ | ||
"name": "javascript-time-ago", | ||
"version": "0.1.0", | ||
"description": "Relative time formatting for React", | ||
"version": "0.1.1", | ||
"description": "International highly customizable relative time formatting", | ||
"main": "build/index.js", | ||
@@ -6,0 +6,0 @@ "jsnext:main": "source/index.js", |
// just an npm package helper | ||
module.exports = require('./babel-transpiled-modules/page-server/web server') |
@@ -116,5 +116,5 @@ # javascript-time-ago | ||
// A `preset` is simply an `options` object | ||
// A `style` is simply an `options` object | ||
// passed to the `.format()` function as a second parameter. | ||
const twitter = time_ago.preset.twitter() | ||
const twitter = time_ago.style.twitter() | ||
@@ -131,2 +131,38 @@ time_ago.format(new Date(), twitter) | ||
## Fuzzy style | ||
```js | ||
time_ago.style.fuzzy() | ||
``` | ||
Same as the default style but with "ago" omitted: | ||
* just now | ||
* 5 minutes | ||
* 10 minutes | ||
* 15 minutes | ||
* 20 minutes | ||
* half an hour | ||
* an hour | ||
* 2 hours | ||
* … | ||
* 20 hours | ||
* yesterday | ||
* 2 days | ||
* 3 days | ||
* 4 days | ||
* 5 days | ||
* a week | ||
* 2 weeks | ||
* 3 weeks | ||
* a month | ||
* 2 months | ||
* 3 months | ||
* 4 months | ||
* half a year | ||
* a year | ||
* 2 years | ||
* 3 years | ||
* … | ||
## Intl polyfill installation | ||
@@ -239,3 +275,3 @@ | ||
One can pass `options` as a second parameter to the `.format(date, options)` function. It's called a `preset` (see "twitter" preset for example). The `options` object can specify: | ||
One can pass `options` as a second parameter to the `.format(date, options)` function. It's called a `style` (see "twitter" style for example). The `options` object can specify: | ||
@@ -333,4 +369,4 @@ * `style` – a preferred formatting style (e.g. `tiny`, `short`, `long`) | ||
[MIT](LICENSE) | ||
[npm]: https://img.shields.io/npm/v/javascript-time-ago.svg?style=flat-square | ||
[npm-badge]: https://www.npmjs.org/package/javascript-time-ago | ||
[npm]: https://www.npmjs.org/package/javascript-time-ago | ||
[npm-badge]: https://img.shields.io/npm/v/javascript-time-ago.svg?style=flat-square | ||
[travis]: https://travis-ci.org/halt-hammerzeit/javascript-time-ago | ||
@@ -337,0 +373,0 @@ [travis-badge]: https://img.shields.io/travis/halt-hammerzeit/javascript-time-ago/master.svg?style=flat-square |
@@ -0,0 +0,0 @@ // https://www.quora.com/What-is-the-average-number-of-days-in-a-month |
@@ -0,0 +0,0 @@ // just an npm package helper |
@@ -6,3 +6,3 @@ // a part of this code is adopted from | ||
import classify_elapsed from './classify elapsed' | ||
import preset from './preset' | ||
import style from './style' | ||
@@ -38,3 +38,3 @@ export default class React_time_ago | ||
// Presets | ||
this.preset = preset(locales) | ||
this.style = style(locales) | ||
} | ||
@@ -69,4 +69,4 @@ | ||
{ | ||
// Get locale messages for this formatting style | ||
const { style, locale_data } = this.locale_data(options.style) | ||
// Get locale messages for this formatting flavour | ||
const { flavour, locale_data } = this.locale_data(options.flavour) | ||
@@ -132,3 +132,3 @@ let date | ||
const formatters = this.get_formatters(unit, style) | ||
const formatters = this.get_formatters(unit, flavour) | ||
@@ -158,4 +158,4 @@ // default formatter: "X units" | ||
// Gets locale messages for this formatting 'style' | ||
locale_data(style) | ||
// Gets locale messages for this formatting flavour | ||
locale_data(flavour) | ||
{ | ||
@@ -165,9 +165,9 @@ // Get relative time formatter messages for this locale | ||
// Fallback to "default" style if the given style isn't available | ||
if (!style || !locale_data[style]) | ||
// Fallback to "default" flavour if the given flavour isn't available | ||
if (!flavour || !locale_data[flavour]) | ||
{ | ||
style = 'default' | ||
flavour = 'default' | ||
} | ||
return { style, locale_data: locale_data[style] } | ||
return { flavour, locale_data: locale_data[flavour] } | ||
} | ||
@@ -177,10 +177,10 @@ | ||
// (second, minute, hour, day, etc) | ||
get_formatters(unit, style) | ||
get_formatters(unit, flavour) | ||
{ | ||
if (!this.formatters[style]) | ||
if (!this.formatters[flavour]) | ||
{ | ||
this.formatters[style] = {} | ||
this.formatters[flavour] = {} | ||
} | ||
const formatters = this.formatters[style] | ||
const formatters = this.formatters[flavour] | ||
@@ -190,3 +190,3 @@ // Create a new synthetic message based on the locale data from CLDR. | ||
{ | ||
formatters[unit] = this.compile_formatters(unit, style) | ||
formatters[unit] = this.compile_formatters(unit, flavour) | ||
} | ||
@@ -199,7 +199,7 @@ | ||
// (second, minute, hour, day, etc) | ||
compile_formatters(unit, style) | ||
compile_formatters(unit, flavour) | ||
{ | ||
// Locale specific time interval formatter messages | ||
// for the given time interval measurement unit | ||
const formatter_messages = React_time_ago.locale_data[this.locale][style][unit] | ||
const formatter_messages = React_time_ago.locale_data[this.locale][flavour][unit] | ||
@@ -206,0 +206,0 @@ // Locale specific time interval formatter messages |
@@ -0,0 +0,0 @@ import are_intl_locales_supported from 'intl-locales-supported' |
@@ -6,3 +6,3 @@ // import chai from 'chai' | ||
import javascript_time_ago, { a_day, days_in_a_month, days_in_a_year, from_CLDR, gradation, preset } from '../source' | ||
import javascript_time_ago, { a_day, days_in_a_month, days_in_a_year, from_CLDR, gradation } from '../source' | ||
@@ -70,6 +70,6 @@ // Load locale specific relative date/time messages | ||
const time_ago = new javascript_time_ago('en') | ||
const twitter_preset = time_ago.preset.twitter() | ||
const twitter_style = time_ago.style.twitter() | ||
const now = new Date(2016, 3, 10, 22, 59).getTime() | ||
const elapsed = time => time_ago.format(now + time * 1000, { now, ...twitter_preset }) | ||
const elapsed = time => time_ago.format(now + time * 1000, { now, ...twitter_style }) | ||
@@ -104,6 +104,6 @@ elapsed(0).should.equal('') | ||
const time_ago = new javascript_time_ago(['ru']) | ||
const twitter_preset = time_ago.preset.twitter() | ||
const twitter_style = time_ago.style.twitter() | ||
const now = new Date(2016, 3, 10, 22, 59).getTime() | ||
const elapsed = time => time_ago.format(now + time * 1000, { now, ...twitter_preset }) | ||
const elapsed = time => time_ago.format(now + time * 1000, { now, ...twitter_style }) | ||
@@ -133,2 +133,136 @@ elapsed(0).should.equal('') | ||
it(`should format fuzzy style relative time (English)`, function() | ||
{ | ||
javascript_time_ago.locale('en', english) | ||
const time_ago = new javascript_time_ago('en-US') | ||
convenient_gradation_test | ||
([ | ||
'just now', | ||
'5 minutes', | ||
'10 minutes', | ||
'15 minutes', | ||
'20 minutes', | ||
'half an hour', | ||
'half an hour', | ||
'half an hour', | ||
'half an hour', | ||
'an hour', | ||
'an hour', | ||
'an hour', | ||
'2 hours', | ||
'3 hours', | ||
'4 hours', | ||
'5 hours', | ||
'6 hours', | ||
'7 hours', | ||
'8 hours', | ||
'9 hours', | ||
'10 hours', | ||
'11 hours', | ||
'12 hours', | ||
'13 hours', | ||
'14 hours', | ||
'15 hours', | ||
'16 hours', | ||
'17 hours', | ||
'18 hours', | ||
'19 hours', | ||
'20 hours', | ||
'yesterday', | ||
'2 days', | ||
'3 days', | ||
'4 days', | ||
'5 days', | ||
'a week', | ||
'2 weeks', | ||
'3 weeks', | ||
'a month', | ||
'2 months', | ||
'3 months', | ||
'4 months', | ||
'half a year', | ||
'half a year', | ||
'half a year', | ||
'half a year', | ||
'half a year', | ||
'a year', | ||
'a year', | ||
'a year', | ||
'2 years', | ||
'3 years', | ||
'100 years' | ||
], | ||
time_ago, | ||
time_ago.style.fuzzy()) | ||
}) | ||
it(`should format fuzzy style relative time (Russian)`, function() | ||
{ | ||
javascript_time_ago.locale('ru', russian) | ||
const time_ago = new javascript_time_ago('ru-RU') | ||
convenient_gradation_test | ||
([ | ||
'только что', | ||
'5 минут', | ||
'10 минут', | ||
'15 минут', | ||
'20 минут', | ||
'полчаса', | ||
'полчаса', | ||
'полчаса', | ||
'полчаса', | ||
'час', | ||
'час', | ||
'час', | ||
'2 часа', | ||
'3 часа', | ||
'4 часа', | ||
'5 часов', | ||
'6 часов', | ||
'7 часов', | ||
'8 часов', | ||
'9 часов', | ||
'10 часов', | ||
'11 часов', | ||
'12 часов', | ||
'13 часов', | ||
'14 часов', | ||
'15 часов', | ||
'16 часов', | ||
'17 часов', | ||
'18 часов', | ||
'19 часов', | ||
'20 часов', | ||
'вчера', | ||
'2 дня', | ||
'3 дня', | ||
'4 дня', | ||
'5 дней', | ||
'неделю', | ||
'2 недели', | ||
'3 недели', | ||
'месяц', | ||
'2 месяца', | ||
'3 месяца', | ||
'4 месяца', | ||
'полгода', | ||
'полгода', | ||
'полгода', | ||
'полгода', | ||
'полгода', | ||
'год', | ||
'год', | ||
'год', | ||
'2 года', | ||
'3 года', | ||
'100 лет' | ||
], | ||
time_ago, | ||
time_ago.style.fuzzy()) | ||
}) | ||
it(`should reduce locale to language code`, function() | ||
@@ -140,3 +274,3 @@ { | ||
const twitter_preset = time_ago.preset.twitter() | ||
const twitter_style = time_ago.style.twitter() | ||
@@ -152,2 +286,4 @@ const now = Date.now() | ||
{ | ||
const units = ['just-now', 'minute', 'half-hour', 'hour', 'day', 'week', 'month', 'half-year', 'year'] | ||
javascript_time_ago.locale('en', english_short) | ||
@@ -212,3 +348,4 @@ | ||
], | ||
new javascript_time_ago('en')) | ||
new javascript_time_ago('en'), | ||
{ units }) | ||
}) | ||
@@ -218,2 +355,4 @@ | ||
{ | ||
const units = ['just-now', 'minute', 'half-hour', 'hour', 'day', 'week', 'month', 'half-year', 'year'] | ||
javascript_time_ago.locale('en', english_long) | ||
@@ -278,3 +417,4 @@ | ||
], | ||
new javascript_time_ago('en')) | ||
new javascript_time_ago('en'), | ||
{ units }) | ||
}) | ||
@@ -284,2 +424,4 @@ | ||
{ | ||
const units = ['just-now', 'minute', 'half-hour', 'hour', 'day', 'week', 'month', 'half-year', 'year'] | ||
javascript_time_ago.locale('ru', russian_short) | ||
@@ -344,3 +486,4 @@ | ||
], | ||
new javascript_time_ago('ru')) | ||
new javascript_time_ago('ru'), | ||
{ units }) | ||
}) | ||
@@ -350,2 +493,4 @@ | ||
{ | ||
const units = ['just-now', 'minute', 'half-hour', 'hour', 'day', 'week', 'month', 'half-year', 'year'] | ||
javascript_time_ago.locale('ru', russian_long) | ||
@@ -410,12 +555,11 @@ | ||
], | ||
new javascript_time_ago('ru')) | ||
new javascript_time_ago('ru'), | ||
{ units }) | ||
}) | ||
}) | ||
function convenient_gradation_test(convenient_gradation_labels, time_ago) | ||
function convenient_gradation_test(convenient_gradation_labels, time_ago, options = {}) | ||
{ | ||
const units = ['just-now', 'minute', 'half-hour', 'hour', 'day', 'week', 'month', 'half-year', 'year'] | ||
const now = Date.now() | ||
const elapsed = time => time_ago.format(now + time * 1000, { now, units }) | ||
const elapsed = time => time_ago.format(now + time * 1000, { now, ...options }) | ||
@@ -422,0 +566,0 @@ if (convenient_gradation.length !== convenient_gradation_labels.length) |
@@ -0,0 +0,0 @@ 'use strict' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151408
3897
374