diff options
author | Ev Bogue <ev@evbogue.com> | 2019-04-19 16:10:15 -0500 |
---|---|---|
committer | Ev Bogue <ev@evbogue.com> | 2019-04-19 16:10:15 -0500 |
commit | 6c60ac6e55da99aac1958c87ee36a6acb4d399b6 (patch) | |
tree | 07e4eff9fc8b5a4b9ce8b88487a6cb180900c962 /lib | |
parent | 3db2bee4c108858ea9ac3417f1645a9b05d80940 (diff) |
move some stuff around and try to get it to work
Diffstat (limited to 'lib')
-rw-r--r-- | lib/misc.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/misc.js b/lib/misc.js new file mode 100644 index 0000000..afc3a33 --- /dev/null +++ b/lib/misc.js @@ -0,0 +1,42 @@ +// human-time by Dave Eddy https://github.com/bahamas10/human + +function human(seconds) { + if (seconds instanceof Date) + seconds = Math.round((Date.now() - seconds) / 1000); + var suffix = seconds < 0 ? 'from now' : 'ago'; + seconds = Math.abs(seconds); + + var times = [ + seconds / 60 / 60 / 24 / 365, // years + seconds / 60 / 60 / 24 / 30, // months + seconds / 60 / 60 / 24 / 7, // weeks + seconds / 60 / 60 / 24, // days + seconds / 60 / 60, // hours + seconds / 60, // minutes + seconds // seconds + ]; + var names = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']; + + for (var i = 0; i < names.length; i++) { + var time = Math.floor(times[i]); + var name = names[i]; + if (time > 1) + name += 's'; + + if (time >= 1) + return time + ' ' + name + ' ' + suffix; + } + return '0 seconds ' + suffix; +} + +// hscrpt by Dominic Tarr https://github.com/dominictarr/hscrpt/blob/master/LICENSE +function h (tag, attrs, content) { + if(Array.isArray(attrs)) content = attrs, attrs = {} + var el = document.createElement(tag) + for(var k in attrs) el[k] = attrs[k] + if(content) content.forEach(function (e) { + if(e) el.appendChild('string' == typeof e ? document.createTextNode(e) : e) + }) + return el +} + |