Skip to content
This repository was archived by the owner on Jan 22, 2023. It is now read-only.

Commit 3b368dd

Browse files
author
Jonas Schubert
committed
adds new number extensions to format number in human readable string
1 parent 4fc9444 commit 3b368dd

6 files changed

Lines changed: 171 additions & 17 deletions

File tree

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# TimeXt-JavaScript
22

33
[![Npm](https://img.shields.io/badge/npm-getit-red.svg)](https://www.npmjs.com/package/timext-js)
4-
[![Build](https://img.shields.io/badge/build-success-green.svg)](releases/timext-2019-05-02-1.min.js)
5-
[![Version](https://img.shields.io/badge/version-v0.5.0-blue.svg)](releases)
6-
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-98-green.svg)](coverage)
4+
[![Build](https://img.shields.io/badge/build-success-green.svg)](releases/timext-2019-05-26-1.min.js)
5+
[![Version](https://img.shields.io/badge/version-0.6.0-blue.svg)](releases)
6+
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-99-green.svg)](coverage)
77

88
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
99
[![Paypal](https://img.shields.io/badge/paypal-donate-blue.svg)](https://www.paypal.me/GuepardoApps)
1010
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)
1111

1212
First of all many thanks to [Kizitonwose](https://github.com/kizitonwose/Time) for the original idea and already awesome library!
1313

14-
This minimized ( < 3kB) library shall help to reduce code like
14+
This minimized ( < 6kB) library shall help to reduce code like
1515

1616
```javascript
1717
const dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
@@ -74,6 +74,39 @@ const threeWeeksAgo = Date.now.minus(Number(3).toWeeks());
7474

7575
```
7676

77+
Since version 0.6.0 TimeXt has new extensions for the long type to display this number value in a human readable string format.
78+
79+
```javascript
80+
const readableStringFromMilliseconds = Number(34325055574).formatMilliseconds() // 56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds
81+
const readableStringFromSeconds = Number(4350554).formatSeconds() // 7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds
82+
const readableStringFromMinutes = Number(432555).formatMinutes() // 42 weeks, 6 days, 9 hours, 15 minutes
83+
const readableStringFromHours = Number(4574).formatHours() // 27 weeks, 1 day, 14 hours
84+
```
85+
7786
## License
7887

7988
TimeXt-JavaScript is distributed under the MIT license. [See LICENSE](LICENSE.md) for details.
89+
90+
```
91+
MIT License
92+
93+
Copyright (c) 2018 - 2019 GuepardoApps (Jonas Schubert)
94+
95+
Permission is hereby granted, free of charge, to any person obtaining a copy
96+
of this software and associated documentation files (the "Software"), to deal
97+
in the Software without restriction, including without limitation the rights
98+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99+
copies of the Software, and to permit persons to whom the Software is
100+
furnished to do so, subject to the following conditions:
101+
102+
The above copyright notice and this permission notice shall be included in all
103+
copies or substantial portions of the Software.
104+
105+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
106+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
107+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
108+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
109+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
110+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
111+
SOFTWARE.
112+
```

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "timext-js",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "3kB time library to handle date and time more easily",
55
"main": "timext.min.js",
66
"types": "",
@@ -28,7 +28,7 @@
2828
],
2929
"size-limit": [
3030
{
31-
"limit": "3.00 KB",
31+
"limit": "6.00 KB",
3232
"path": "timext.min.js"
3333
}
3434
],
@@ -54,28 +54,28 @@
5454
},
5555
"devDependencies": {
5656
"@babel/cli": "7.4.4",
57-
"@babel/core": "7.4.4",
58-
"@babel/node": "7.2.2",
59-
"@babel/preset-env": "7.4.4",
57+
"@babel/core": "7.4.5",
58+
"@babel/node": "7.4.5",
59+
"@babel/preset-env": "7.4.5",
6060
"babel-core": "6.26.3",
61-
"babel-jest": "24.7.1",
61+
"babel-jest": "24.8.0",
6262
"babel-plugin-external-helpers": "6.22.0",
6363
"cross-env": "5.2.0",
6464
"eslint": "5.16.0",
6565
"eslint-config-airbnb-base": "13.1.0",
66-
"eslint-plugin-import": "2.17.2",
67-
"eslint-plugin-jest": "22.5.1",
66+
"eslint-plugin-import": "2.17.3",
67+
"eslint-plugin-jest": "22.6.4",
6868
"gzip-size-cli": "3.0.0",
6969
"jasmine-core": "2.99.1",
70-
"jest": "24.7.1",
70+
"jest": "24.8.0",
7171
"karma": "4.1.0",
7272
"karma-jasmine": "1.1.2",
7373
"karma-sauce-launcher": "2.0.2",
7474
"pre-commit": "1.2.2",
75-
"rollup": "1.10.1",
75+
"rollup": "1.12.3",
7676
"rollup-plugin-babel": "4.3.2",
7777
"rollup-plugin-uglify": "6.0.2",
78-
"size-limit": "1.3.1",
78+
"size-limit": "1.3.3",
7979
"typescript": "3.4.5"
8080
},
8181
"dependencies": {}

releases/timext-2019-05-26-1.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class TimeXt {
118118

119119
const timext = (val, unit) => new TimeXt(val, unit);
120120

121+
export default timext;
122+
121123
// Date extensions
122124

123125
Date.prototype.plus = function (val) {
@@ -158,4 +160,74 @@ Number.prototype.toMilliseconds = function () {
158160
return timext(this, u.MS);
159161
}
160162

161-
export default timext;
163+
// String format extensions
164+
165+
Number.prototype.formatMilliseconds = function () {
166+
const dictionary = [
167+
{ key: 'week', value: { first: 7 * 24 * 60 * 60 * 1000, second: Number.MAX_SAFE_INTEGER } },
168+
{ key: 'day', value: { first: 24 * 60 * 60 * 1000, second: 7 } },
169+
{ key: 'hour', value: { first: 60 * 60 * 1000, second: 24 } },
170+
{ key: 'minute', value: { first: 60 * 1000, second: 60 } },
171+
{ key: 'second', value: { first: 1000, second: 60 } },
172+
{ key: 'millisecond', value: { first: 1, second: 1000 } }
173+
];
174+
175+
const stringArray = dictionary
176+
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
177+
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
178+
: undefined))
179+
.filter(item => !!item);
180+
181+
return stringArray.length > 0 ? stringArray.join(', ') : '0 milliseconds';
182+
}
183+
184+
Number.prototype.formatSeconds = function () {
185+
const dictionary = [
186+
{ key: 'week', value: { first: 7 * 24 * 60 * 60 * 1, second: Number.MAX_SAFE_INTEGER } },
187+
{ key: 'day', value: { first: 24 * 60 * 60 * 1, second: 7 } },
188+
{ key: 'hour', value: { first: 60 * 60 * 1, second: 24 } },
189+
{ key: 'minute', value: { first: 60 * 1, second: 60 } },
190+
{ key: 'second', value: { first: 1, second: 60 } }
191+
];
192+
193+
const stringArray = dictionary
194+
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
195+
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
196+
: undefined))
197+
.filter(item => !!item);
198+
199+
return stringArray.length > 0 ? stringArray.join(', ') : (this * 1000).formatMilliseconds();
200+
}
201+
202+
Number.prototype.formatMinutes = function () {
203+
const dictionary = [
204+
{ key: 'week', value: { first: 7 * 24 * 60 * 1, second: Number.MAX_SAFE_INTEGER } },
205+
{ key: 'day', value: { first: 24 * 60 * 1, second: 7 } },
206+
{ key: 'hour', value: { first: 60 * 1, second: 24 } },
207+
{ key: 'minute', value: { first: 1, second: 60 } }
208+
];
209+
210+
const stringArray = dictionary
211+
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
212+
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
213+
: undefined))
214+
.filter(item => !!item);
215+
216+
return stringArray.length > 0 ? stringArray.join(', ') : (this * 60).formatSeconds();
217+
}
218+
219+
Number.prototype.formatHours = function () {
220+
const dictionary = [
221+
{ key: 'week', value: { first: 7 * 24 * 1, second: Number.MAX_SAFE_INTEGER } },
222+
{ key: 'day', value: { first: 24 * 1, second: 7 } },
223+
{ key: 'hour', value: { first: 1, second: 24 } }
224+
];
225+
226+
const stringArray = dictionary
227+
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
228+
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
229+
: undefined))
230+
.filter(item => !!item);
231+
232+
return stringArray.length > 0 ? stringArray.join(', ') : (this * 60).formatMinutes();
233+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import '../src/index';
2+
3+
it('formatMilliseconds should work as expected', () => {
4+
// Assert
5+
const expected = '56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds';
6+
const testValue = 34325055574;
7+
8+
// Act
9+
const actual = testValue.formatMilliseconds();
10+
11+
// Assert
12+
expect(actual).toBe(expected);
13+
})
14+
15+
it('formatSeconds should work as expected', () => {
16+
// Assert
17+
const expected = '7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds';
18+
const testValue = 4350554;
19+
20+
// Act
21+
const actual = testValue.formatSeconds();
22+
23+
// Assert
24+
expect(actual).toBe(expected);
25+
})
26+
27+
it('formatMinutes should work as expected', () => {
28+
// Assert
29+
const expected = '42 weeks, 6 days, 9 hours, 15 minutes';
30+
const testValue = 432555;
31+
32+
// Act
33+
const actual = testValue.formatMinutes();
34+
35+
// Assert
36+
expect(actual).toBe(expected);
37+
})
38+
39+
it('formatHours should work as expected', () => {
40+
// Assert
41+
const expected = '27 weeks, 1 day, 14 hours';
42+
const testValue = 4574;
43+
44+
// Act
45+
const actual = testValue.formatHours();
46+
47+
// Assert
48+
expect(actual).toBe(expected);
49+
})

timext.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)