Commit e836f599f0095c03c6bb4b228c714e7130f797f6
Override lib.obs.timeAgo and add int method for time and dates.
Marcos Gutierrez committed on 9/29/2017, 3:30:49 PMParent: e5dd29c7a0d68080004e28769bbfed143db66881
Files changed
overrides/patchcore/lib/timeAgo.js | added |
plugs/intl/sync/i18n.js | changed |
overrides/patchcore/lib/timeAgo.js | ||
---|---|---|
@@ -1,0 +1,42 @@ | ||
1 | +const Value = require('mutant/value') | |
2 | +const computed = require('mutant/computed') | |
3 | +const nest = require('depnest') | |
4 | +const human = require('human-time') | |
5 | + | |
6 | +exports.gives = nest('lib.obs.timeAgo') | |
7 | + | |
8 | +exports.needs = nest({ | |
9 | + 'intl.sync.time': 'first' | |
10 | +}) | |
11 | + | |
12 | +exports.create = function (api) { | |
13 | + return nest('lib.obs.timeAgo', timeAgo) | |
14 | + | |
15 | + function timeAgo (timestamp) { | |
16 | + var timer | |
17 | + var value = Value(TimeIntl(timestamp)) | |
18 | + return computed([value], (a) => a, { | |
19 | + onListen: () => { | |
20 | + timer = setInterval(refresh, 30e3) | |
21 | + refresh() | |
22 | + }, | |
23 | + onUnlisten: () => { | |
24 | + clearInterval(timer) | |
25 | + } | |
26 | + }, { | |
27 | + idle: true | |
28 | + }) | |
29 | + | |
30 | + function refresh () { | |
31 | + value.set(TimeIntl(timestamp)) | |
32 | + } | |
33 | + | |
34 | + function TimeIntl(timestamp) { | |
35 | + return api.intl.sync.time(Time(timestamp)) | |
36 | + } | |
37 | + } | |
38 | +} | |
39 | + | |
40 | +function Time (timestamp) { | |
41 | + return human(new Date(timestamp)) | |
42 | +} |
plugs/intl/sync/i18n.js | ||
---|---|---|
@@ -6,8 +6,9 @@ | ||
6 | 6 | exports.gives = nest('intl.sync', [ |
7 | 7 | 'locale', |
8 | 8 | 'locales', |
9 | 9 | 'i18n', |
10 | + 'time', | |
10 | 11 | ]) |
11 | 12 | |
12 | 13 | exports.needs = nest({ |
13 | 14 | 'intl.sync.locale':'first', |
@@ -27,9 +28,10 @@ | ||
27 | 28 | |
28 | 29 | return nest('intl.sync', { |
29 | 30 | locale, |
30 | 31 | locales, |
31 | - i18n | |
32 | + i18n, | |
33 | + time | |
32 | 34 | }) |
33 | 35 | |
34 | 36 | //Get locale value in setting |
35 | 37 | function locale () { |
@@ -46,8 +48,28 @@ | ||
46 | 48 | _init() |
47 | 49 | return i18nL.__(value) |
48 | 50 | } |
49 | 51 | |
52 | + function time (date){ | |
53 | + return date | |
54 | + .replace(/from now/, i18n('form now')) | |
55 | + .replace(/ago/, i18n('ago')) | |
56 | + .replace(/years/,i18n('years')) | |
57 | + .replace(/months/,i18n('months')) | |
58 | + .replace(/weeks/,i18n('weeks')) | |
59 | + .replace(/days/,i18n('days')) | |
60 | + .replace(/hours/,i18n('hours')) | |
61 | + .replace(/minutes/,i18n('minutes')) | |
62 | + .replace(/seconds/,i18n('seconds')) | |
63 | + .replace(/year/,i18n('year')) | |
64 | + .replace(/month/,i18n('month')) | |
65 | + .replace(/week/,i18n('week')) | |
66 | + .replace(/day/,i18n('day')) | |
67 | + .replace(/hour/,i18n('hour')) | |
68 | + .replace(/minute/,i18n('minute')) | |
69 | + .replace(/second/,i18n('second')) | |
70 | + } | |
71 | + | |
50 | 72 | //Init an subscribe to settings changes. |
51 | 73 | function _init() { |
52 | 74 | if (_locale) return |
53 | 75 | //TODO: Depject this! |
Built with git-ssb-web