├── .editorconfig
├── .gitignore
├── ANNOTATED.md
├── LICENSE
├── README.md
├── lib
└── index.js
├── package.json
├── src
└── index.js
├── test.js
└── tiny-tim.jpg
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig helps developers define and maintain consistent
2 | # coding styles between different editors and IDEs
3 | # editorconfig.org
4 |
5 | root = true
6 |
7 | [*]
8 |
9 | # Change these settings to your own preference
10 | indent_style = space
11 | indent_size = 2
12 |
13 | # We recommend you to keep these unchanged
14 | end_of_line = lf
15 | charset = utf-8
16 | trim_trailing_whitespace = true
17 | insert_final_newline = true
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 |
3 | .idea/
4 |
--------------------------------------------------------------------------------
/ANNOTATED.md:
--------------------------------------------------------------------------------
1 | ```js
2 | // Use arrow functions to avoid function keyword
3 | module.exports = (unit, suffix) => {
4 | // Use exponents to save on those zeroes
5 | var divs = { ms: 1, s: 1e3, m: 6e4, h: 36e5 }
6 |
7 | var div = divs[unit || 's']
8 |
9 | var append = suffix && div ? unit : 0
10 |
11 | // Do `new` constructor without paretheses, not required without args
12 | // Use + to cast Date to number, which in turn calls `Date.valueOf()`
13 | var start = +new Date
14 |
15 | return () => {
16 | var time = (+new Date - start) / div
17 | return (suffix !== 'ms' ? +time.toFixed(2) : time) + append
18 | }
19 | }
20 |
21 | ```
22 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 Sam Gluck
2 |
3 | MIT License
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining
6 | a copy of this software and associated documentation files (the
7 | "Software"), to deal in the Software without restriction, including
8 | without limitation the rights to use, copy, modify, merge, publish,
9 | distribute, sublicense, and/or sell copies of the Software, and to
10 | permit persons to whom the Software is furnished to do so, subject to
11 | the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be
14 | included in all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Tiny Tim
6 |
7 | Meet Tim, a simple, small timer
8 |
9 | Made with ❤ at @outlandish
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | :cookie: Comes in at `156 bytes` minified. Zero dependencies.
18 |
19 | :sparkles: Tiny Tim is compiled using [`babili`](https://github.com/babel/babili) for ES6+ and CommonJS environments.
20 |
21 | :point_right: Use your preferred bundler and transpiler as required.
22 |
23 | :page_facing_up: Read the [annotated source code](https://github.com/sdgluck/tiny-tim/blob/master/ANNOTATED.md).
24 |
25 | ## Install
26 |
27 | ```sh
28 | npm install --save tiny-tim
29 | ```
30 |
31 | ```sh
32 | yarn add tiny-tim
33 | ```
34 |
35 | ## Import
36 |
37 | ```js
38 | // ES2015
39 | import timer from 'tiny-tim'
40 | ```
41 |
42 | ```js
43 | // CommonJS
44 | var timer = require('tiny-tim')
45 | ```
46 |
47 | ## Usage
48 |
49 | ### `timer([unit[, suffix]]) : Function`
50 |
51 | Create a timer and start counting!
52 |
53 | - __unit__ {String} Unit of time to return _(optional, default=`"s"`)_
54 | - __suffix__ {Boolean} Attach suffix, e.g. unit "s" appends `"s"` _(optional, default=`false`)_
55 |
56 | Available units: `"ms"`, `"s"`, `"m"`, `"h"`
57 |
58 | Returns a function that stops the timer and returns the duration:
59 | - as a string if with suffix
60 | - or as a number without
61 |
62 | ## Examples
63 |
64 | ```js
65 | // Time in seconds with suffix
66 | const seconds = timer('s', true)
67 |
68 | setTimeout(() => {
69 | console.log(seconds()) //=> '10s'
70 | }, 10000)
71 |
72 | // reuse a timer...
73 |
74 | setTimeout(() => {
75 | console.log(seconds()) //=> '15s'
76 | }, 15000)
77 | ```
78 |
79 | ```js
80 | // Time milliseconds without suffix
81 | const milliseconds = timer()
82 |
83 | setTimeout(() => {
84 | console.log(milliseconds()) //=> 1000
85 | }, 1000)
86 | ```
87 |
88 | ## Contributing
89 |
90 | All pull requests and issues welcome!
91 |
92 | If you're not sure how, check out the [great video tutorials on egghead.io](http://bit.ly/2aVzthz)!
93 |
--------------------------------------------------------------------------------
/lib/index.js:
--------------------------------------------------------------------------------
1 | module.exports=(a,b)=>{var d={ms:1,s:1e3,m:6e4,h:36e5}[a||'s'],e=b&&d?a:0,f=+new Date;return()=>{var g=(+new Date-f)/d;return('ms'===b?g:+g.toFixed(2))+e}};
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tiny-tim",
3 | "version": "0.0.4",
4 | "description": "Meet Tim, a small, simple timer",
5 | "main": "lib/index.js",
6 | "esnext:main": "src/index.js",
7 | "scripts": {
8 | "build": "rimraf lib && babili src -d lib",
9 | "test": "npm run build && node test.js",
10 | "prepublishOnly": "npm run build"
11 | },
12 | "author": "Sam Gluck ",
13 | "license": "MIT",
14 | "repository": "sdgluck/tiny-tim",
15 | "keywords": [
16 | "timer",
17 | "tiny",
18 | "profile",
19 | "stopwatch",
20 | "stop",
21 | "watch",
22 | "tim"
23 | ],
24 | "devDependencies": {
25 | "babel-eslint": "^7.1.1",
26 | "babili": "0.0.10",
27 | "rimraf": "^2.5.4",
28 | "snazzy": "^6.0.0",
29 | "standard": "^7.1.1",
30 | "tape": "^4.6.3"
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | module.exports = (unit, suffix) => {
2 | var divs = { ms: 1, s: 1e3, m: 6e4, h: 36e5 }
3 | var div = divs[unit || 's']
4 | var append = suffix && div ? unit : 0
5 | var start = +new Date
6 |
7 | return () => {
8 | var time = (+new Date - start) / div
9 | return (suffix !== 'ms' ? +time.toFixed(2) : time) + append
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const test = require('tape')
4 |
5 | const timer = require('./lib')
6 |
7 | test('exports fn', (t) => {
8 | t.equals(typeof timer, 'function')
9 | t.end()
10 | })
11 |
12 | test('timer returns fn', (t) => {
13 | t.equals(typeof timer(), 'function')
14 | t.end()
15 | })
16 |
17 | test('no suffix returns number', (t) => {
18 | t.equals(isNaN(Number(timer()())), false)
19 | t.end()
20 | })
21 |
22 | ;['ms', 's', 'm', 'h'].forEach((unit) => {
23 | test(`${unit} returns "${unit}" suffix`, (t) => {
24 | const res = timer(unit, true)()
25 | t.equals(res.indexOf(unit) !== -1, true)
26 | t.end()
27 | })
28 | })
29 |
30 | test('counts ok', (t) => {
31 | const start = Date.now()
32 |
33 | const ms = timer('ms')
34 | const s = timer('s')
35 | const m = timer('m')
36 | const h = timer('s')
37 |
38 | const end = start + (1000 * 60 * 60)
39 | const expected = end - start
40 |
41 | Date = function () {
42 | this.valueOf = () => end
43 | }
44 |
45 | t.equals(ms() >= expected, true)
46 | t.equals(s() >= expected / 1000, true)
47 | t.equals(m() >= expected / (1000 * 60), true)
48 | t.equals(h() >= expected / (1000 * 60 * 60), true)
49 | t.end()
50 | })
51 |
--------------------------------------------------------------------------------
/tiny-tim.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sdgluck/tiny-tim/81b0108eed50f7f750183eaf43972181a6f5fd6b/tiny-tim.jpg
--------------------------------------------------------------------------------