├── .gitignore
├── History.md
├── Readme.md
├── component.json
├── package.json
├── examples
└── index.html
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/History.md:
--------------------------------------------------------------------------------
1 |
2 | 0.0.1 / 2015-04-27
3 | ==================
4 |
5 | * Drop component(1) support
6 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 |
2 | # timeago
3 |
4 | Simple timeago component with moment.js i18n support.
5 |
6 | ## Installation
7 |
8 | `$ npm install timeago`
9 |
10 | ## API
11 |
12 |
13 |
14 | ## License
15 |
16 | MIT
17 |
--------------------------------------------------------------------------------
/component.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "timeago",
3 | "description": "Simple timeago component with moment.js i18n support.",
4 | "version": "0.0.1",
5 | "keywords": [ "time", "ago", "i18n" ],
6 | "dependencies": {
7 | "DemocracyOS/moment": "develop"
8 | },
9 | "scripts": [
10 | "index.js"
11 | ],
12 | "main": "index.js",
13 | "development": {},
14 | "repo": "DemocracyOS/timeago",
15 | "license": "MIT"
16 | }
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "democracyos-timeago",
3 | "version": "0.0.1",
4 | "description": "Simple timeago component with moment.js i18n support.",
5 | "main": "index.js",
6 | "directories": {
7 | "example": "examples"
8 | },
9 | "scripts": {
10 | "test": "echo \"Error: no test specified\" && exit 1"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "https://github.com/DemocracyOS/timeago.git"
15 | },
16 | "author": "DemocracyOS",
17 | "license": "MIT",
18 | "bugs": {
19 | "url": "https://github.com/DemocracyOS/timeago/issues"
20 | },
21 | "homepage": "https://github.com/DemocracyOS/timeago",
22 | "keywords": ["time", "ago", "i18n"],
23 | "dependencies": {
24 | "moment": "^2.10.2"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/examples/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Timeago examples
4 |
5 |
8 |
9 |
10 |
11 | Simple install and init.
12 |
13 |
22 |
23 |
24 |
25 | This should output yesterday
26 |
27 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Module dependencies.
3 | */
4 |
5 | var moment = require('moment')
6 | , half = 1000 * 30
7 | , o = document.querySelectorAll.bind(document);
8 |
9 | module.exports = Timeago;
10 |
11 | /**
12 | * Returns a timea
13 | */
14 |
15 | function Timeago (selector, options) {
16 | if (!(this instanceof Timeago)) {
17 | return new Timeago(selector, options);
18 | }
19 |
20 | if (0 === arguments.length) {
21 | throw new Error("Timeago requires at least a query selector string as parameter.");
22 | };
23 |
24 | this.selector = selector;
25 |
26 | options = options || {};
27 | this.interval = options.interval || half; // a minute
28 | this.lang = options.lang || 'en';
29 | this.attr = options.attr || 'data-time';
30 | this.timer = null;
31 |
32 | // setup language
33 | moment.locale(this.lang);
34 |
35 | // init auto-render
36 | this.update()
37 | }
38 |
39 | /**
40 | * Updates all matching elements
41 | * with selector provided
42 | *
43 | * @return {Timeago} `Timeago` instance.
44 | * @api public
45 | */
46 |
47 | Timeago.prototype.update = function() {
48 | // if timer, delete it!
49 | if(this.timer) {
50 | clearTimeout(this.timer);
51 | }
52 |
53 | // Update all matching elements with `Timeago` string.
54 | toArray(o(this.selector)).forEach(updateElement.bind(this));
55 |
56 | // Save timer's id for next update.
57 | this.timer = setTimeout(this.update.bind(this), this.interval);
58 | }
59 |
60 | /**
61 | * Takes a NodeList Object and
62 | * returns an array
63 | *
64 | * @param {NodeList} list `NodeList` to convert
65 | * @return {Arrat} `Array` of `ElementNodes`
66 | * @api private
67 | */
68 |
69 | function toArray (list) {
70 | return Array.prototype.concat.apply([], list);
71 | }
72 |
73 | /**
74 | * Takes the dirt in hands to update
75 | * every single element matched.
76 | * Requires `momentJS` and to be
77 | * binded to another object with
78 | * `attr` property.
79 | *
80 | * @param {NodeElement} el DOM's `NodeElement` to update
81 | * @api private
82 | */
83 |
84 | function updateElement (el) {
85 | el.innerHTML = moment(el.getAttribute(this.attr)).fromNow();
86 | }
--------------------------------------------------------------------------------