├── README.md ├── www ├── worker.js ├── monokai.css ├── crono.js ├── index.html └── highlight.pack.js ├── LICENSE ├── dist └── crono.js └── src └── crono.js /README.md: -------------------------------------------------------------------------------- 1 | # crono 2 | A simple and minimalistic timeago library without dependencies 3 | -------------------------------------------------------------------------------- /www/worker.js: -------------------------------------------------------------------------------- 1 | onmessage = function (event) { 2 | importScripts('highlight.pack.js') 3 | var result = self.hljs.highlightAuto(event.data) 4 | postMessage(result.value) 5 | } 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Miguel Ángel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /www/monokai.css: -------------------------------------------------------------------------------- 1 | /* 2 | Monokai style - ported by Luigi Maselli - http://grigio.org 3 | */ 4 | 5 | .hljs { 6 | display: block; 7 | overflow-x: auto; 8 | padding: 0.5em; 9 | background: #272822; color: #ddd; 10 | } 11 | 12 | .hljs-tag, 13 | .hljs-keyword, 14 | .hljs-selector-tag, 15 | .hljs-literal, 16 | .hljs-strong, 17 | .hljs-name { 18 | color: #f92672; 19 | } 20 | 21 | .hljs-code { 22 | color: #66d9ef; 23 | } 24 | 25 | .hljs-class .hljs-title { 26 | color: white; 27 | } 28 | 29 | .hljs-attribute, 30 | .hljs-symbol, 31 | .hljs-regexp, 32 | .hljs-link { 33 | color: #bf79db; 34 | } 35 | 36 | .hljs-string, 37 | .hljs-bullet, 38 | .hljs-subst, 39 | .hljs-title, 40 | .hljs-section, 41 | .hljs-emphasis, 42 | .hljs-type, 43 | .hljs-built_in, 44 | .hljs-builtin-name, 45 | .hljs-selector-attr, 46 | .hljs-selector-pseudo, 47 | .hljs-addition, 48 | .hljs-variable, 49 | .hljs-template-tag, 50 | .hljs-template-variable { 51 | color: #a6e22e; 52 | } 53 | 54 | .hljs-comment, 55 | .hljs-quote, 56 | .hljs-deletion, 57 | .hljs-meta { 58 | color: #75715e; 59 | } 60 | 61 | .hljs-keyword, 62 | .hljs-selector-tag, 63 | .hljs-literal, 64 | .hljs-doctag, 65 | .hljs-title, 66 | .hljs-section, 67 | .hljs-type, 68 | .hljs-selector-id { 69 | font-weight: bold; 70 | } 71 | -------------------------------------------------------------------------------- /dist/crono.js: -------------------------------------------------------------------------------- 1 | (function (root, factory) { 2 | 'use strict' 3 | 4 | if (typeof define === 'function' && define.amd) { 5 | define(factory) 6 | } else { 7 | root.crono = factory() 8 | } 9 | }(this, function () { 10 | // default options for crono 11 | var defaultOptions = { 12 | locale: { 13 | prefixAgo: null, 14 | prefixFromNow: null, 15 | suffixAgo: 'ago', 16 | suffixFromNow: 'from now', 17 | inPast: 'any moment now', 18 | seconds: 'less than a minute', 19 | minute: 'about a minute', 20 | minutes: '%d minutes', 21 | hour: 'about an hour', 22 | hours: 'about %d hours', 23 | day: 'a day', 24 | days: '%d days', 25 | month: 'about a month', 26 | months: '%d months', 27 | year: 'about a year', 28 | years: '%d years', 29 | wordSeparator: ' ', 30 | numbers: [] 31 | } 32 | } 33 | 34 | function parse (iso8601) { 35 | var s = iso8601.trim() 36 | s = s.replace(/\.\d+/, '') // remove milliseconds 37 | s = s.replace(/-/, '/').replace(/-/, '/') 38 | s = s.replace(/T/, ' ').replace(/Z/, ' UTC') 39 | s = s.replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2') // -04:00 -> -0400 40 | s = s.replace(/([\+\-]\d\d)$/, ' $100') // +09 -> +0900 41 | return new Date(s) 42 | } 43 | 44 | function distance (date) { 45 | return (new Date().getTime() - date.getTime()) 46 | } 47 | 48 | function inWords (distanceMillis, options) { 49 | var $l = options.locale 50 | var prefix = $l.prefixAgo 51 | var suffix = $l.suffixAgo 52 | 53 | var seconds = Math.abs(distanceMillis) / 1000 54 | var minutes = seconds / 60 55 | var hours = minutes / 60 56 | var days = hours / 24 57 | var years = days / 365 58 | 59 | function substitute (stringOrFunction, number) { 60 | var string = typeof stringOrFunction === 'string' ? stringOrFunction : stringOrFunction(number, distanceMillis) 61 | var value = ($l.numbers && $l.numbers[number]) || number 62 | return string.replace(/%d/i, value) 63 | } 64 | 65 | var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || 66 | seconds < 90 && substitute($l.minute, 1) || 67 | minutes < 45 && substitute($l.minutes, Math.round(minutes)) || 68 | minutes < 90 && substitute($l.hour, 1) || 69 | hours < 24 && substitute($l.hours, Math.round(hours)) || 70 | hours < 42 && substitute($l.day, 1) || 71 | days < 30 && substitute($l.days, Math.round(days)) || 72 | days < 45 && substitute($l.month, 1) || 73 | days < 365 && substitute($l.months, Math.round(days / 30)) || 74 | years < 1.5 && substitute($l.year, 1) || 75 | substitute($l.years, Math.round(years)) 76 | 77 | var separator = $l.wordSeparator || '' 78 | if ($l.wordSeparator === undefined) { separator = ' ' } 79 | return [prefix, words, suffix].join(separator).trim() 80 | } 81 | // create a variable crono 82 | return function (selector, options) { 83 | options = options || defaultOptions 84 | var elements = document.querySelectorAll(selector) 85 | console.log(elements) 86 | for (var i = 0; i < elements.length; i++) { 87 | var date = elements[i].getAttribute('datetime') 88 | var iso8601Date = parse(date) 89 | elements[i].innerHTML = inWords(distance(iso8601Date), options) 90 | } 91 | } 92 | })) 93 | -------------------------------------------------------------------------------- /www/crono.js: -------------------------------------------------------------------------------- 1 | (function (root, factory) { 2 | 'use strict' 3 | 4 | if (typeof define === 'function' && define.amd) { 5 | define(factory) 6 | } else { 7 | root.crono = factory() 8 | } 9 | }(this, function () { 10 | // default options for crono 11 | var defaultOptions = { 12 | locale: { 13 | prefixAgo: null, 14 | prefixFromNow: null, 15 | suffixAgo: 'ago', 16 | suffixFromNow: 'from now', 17 | inPast: 'any moment now', 18 | seconds: 'less than a minute', 19 | minute: 'about a minute', 20 | minutes: '%d minutes', 21 | hour: 'about an hour', 22 | hours: 'about %d hours', 23 | day: 'a day', 24 | days: '%d days', 25 | month: 'about a month', 26 | months: '%d months', 27 | year: 'about a year', 28 | years: '%d years', 29 | wordSeparator: ' ', 30 | numbers: [] 31 | } 32 | } 33 | 34 | function parse (iso8601) { 35 | var s = iso8601.trim() 36 | s = s.replace(/\.\d+/, '') // remove milliseconds 37 | s = s.replace(/-/, '/').replace(/-/, '/') 38 | s = s.replace(/T/, ' ').replace(/Z/, ' UTC') 39 | s = s.replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2') // -04:00 -> -0400 40 | s = s.replace(/([\+\-]\d\d)$/, ' $100') // +09 -> +0900 41 | return new Date(s) 42 | } 43 | 44 | function distance (date) { 45 | return (new Date().getTime() - date.getTime()) 46 | } 47 | 48 | function inWords (distanceMillis, options) { 49 | var $l = options.locale 50 | var prefix = $l.prefixAgo 51 | var suffix = $l.suffixAgo 52 | 53 | var seconds = Math.abs(distanceMillis) / 1000 54 | var minutes = seconds / 60 55 | var hours = minutes / 60 56 | var days = hours / 24 57 | var years = days / 365 58 | 59 | function substitute (stringOrFunction, number) { 60 | var string = typeof stringOrFunction === 'string' ? stringOrFunction : stringOrFunction(number, distanceMillis) 61 | var value = ($l.numbers && $l.numbers[number]) || number 62 | return string.replace(/%d/i, value) 63 | } 64 | 65 | var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || 66 | seconds < 90 && substitute($l.minute, 1) || 67 | minutes < 45 && substitute($l.minutes, Math.round(minutes)) || 68 | minutes < 90 && substitute($l.hour, 1) || 69 | hours < 24 && substitute($l.hours, Math.round(hours)) || 70 | hours < 42 && substitute($l.day, 1) || 71 | days < 30 && substitute($l.days, Math.round(days)) || 72 | days < 45 && substitute($l.month, 1) || 73 | days < 365 && substitute($l.months, Math.round(days / 30)) || 74 | years < 1.5 && substitute($l.year, 1) || 75 | substitute($l.years, Math.round(years)) 76 | 77 | var separator = $l.wordSeparator || '' 78 | if ($l.wordSeparator === undefined) { separator = ' ' } 79 | return [prefix, words, suffix].join(separator).trim() 80 | } 81 | // create a variable crono 82 | return function (selector, options) { 83 | options = options || defaultOptions 84 | var elements = document.querySelectorAll(selector) 85 | console.log(elements) 86 | for (var i = 0; i < elements.length; i++) { 87 | var date = elements[i].getAttribute('datetime') 88 | var iso8601Date = parse(date) 89 | elements[i].innerHTML = inWords(distance(iso8601Date), options) 90 | } 91 | } 92 | })) 93 | -------------------------------------------------------------------------------- /src/crono.js: -------------------------------------------------------------------------------- 1 | (function (root, factory) { 2 | 'use strict' 3 | 4 | if (typeof window.define === 'function' && window.define.amd) { 5 | window.define(factory) 6 | } else { 7 | root.crono = factory() 8 | } 9 | }(this, function () { 10 | // default options for crono 11 | var defaultOptions = { 12 | locale: { 13 | prefixAgo: null, 14 | prefixFromNow: null, 15 | suffixAgo: 'ago', 16 | suffixFromNow: 'from now', 17 | inPast: 'any moment now', 18 | seconds: 'less than a minute', 19 | minute: 'about a minute', 20 | minutes: '%d minutes', 21 | hour: 'about an hour', 22 | hours: 'about %d hours', 23 | day: 'a day', 24 | days: '%d days', 25 | month: 'about a month', 26 | months: '%d months', 27 | year: 'about a year', 28 | years: '%d years', 29 | wordSeparator: ' ', 30 | numbers: [] 31 | } 32 | } 33 | 34 | function parse (iso8601) { 35 | var s = iso8601.trim() 36 | s = s.replace(/\.\d+/, '') // remove milliseconds 37 | s = s.replace(/-/, '/').replace(/-/, '/') 38 | s = s.replace(/T/, ' ').replace(/Z/, ' UTC') 39 | s = s.replace(/([+-]\d\d):?(\d\d)/, ' $1$2') // -04:00 -> -0400 40 | s = s.replace(/([+-]\d\d)$/, ' $100') // +09 -> +0900 41 | return new Date(s) 42 | } 43 | 44 | function distance (date) { 45 | return (new Date().getTime() - date.getTime()) 46 | } 47 | 48 | function inWords (distanceMillis, options) { 49 | var $l = options.locale 50 | var prefix = $l.prefixAgo 51 | var suffix = $l.suffixAgo 52 | 53 | var seconds = Math.abs(distanceMillis) / 1000 54 | var minutes = seconds / 60 55 | var hours = minutes / 60 56 | var days = hours / 24 57 | var years = days / 365 58 | 59 | function substitute (stringOrFunction, number) { 60 | var string = typeof stringOrFunction === 'string' 61 | ? stringOrFunction 62 | : stringOrFunction(number, distanceMillis) 63 | 64 | var value = ($l.numbers && $l.numbers[number]) || number 65 | return string.replace(/%d/i, value) 66 | } 67 | 68 | var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || 69 | seconds < 90 && substitute($l.minute, 1) || 70 | minutes < 45 && substitute($l.minutes, Math.round(minutes)) || 71 | minutes < 90 && substitute($l.hour, 1) || 72 | hours < 24 && substitute($l.hours, Math.round(hours)) || 73 | hours < 42 && substitute($l.day, 1) || 74 | days < 30 && substitute($l.days, Math.round(days)) || 75 | days < 45 && substitute($l.month, 1) || 76 | days < 365 && substitute($l.months, Math.round(days / 30)) || 77 | years < 1.5 && substitute($l.year, 1) || 78 | substitute($l.years, Math.round(years)) 79 | 80 | var separator = $l.wordSeparator || '' 81 | if ($l.wordSeparator === undefined) { separator = ' ' } 82 | return [prefix, words, suffix].join(separator).trim() 83 | } 84 | 85 | // create a variable crono 86 | return function (selector, options) { 87 | options = options || defaultOptions 88 | var elements = document.querySelectorAll(selector) 89 | console.log(elements) 90 | for (var i = 0; i < elements.length; i++) { 91 | var date = elements[i].getAttribute('datetime') 92 | var iso8601Date = parse(date) 93 | elements[i].innerHTML = inWords(distance(iso8601Date), options) 94 | } 95 | } 96 | })) 97 | -------------------------------------------------------------------------------- /www/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
132 | Crono is a timeago minimalist library that just do what you expect it has to do and without dependencies. 133 |
134 | 135 |
137 |
138 | <time datetime="2016-01-20T19:27:48.000Z"></time>
139 |
140 |
141 |
142 |
147 |
148 | <time datetime="2016-01-20T19:27:48.000Z"></time>
149 |
150 |
151 |
152 |
157 |
158 | <time datetime="2016-01-20T19:27:48.000Z"></time>
159 |
160 |
161 |
162 | 166 | Read the full documentation 167 | for more information about using GitHub Pages. 168 |
169 | 170 |