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

Commit

Permalink
adds new number extensions to format number in human readable string
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Schubert committed May 25, 2019
1 parent 4fc9444 commit 3b368dd
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 17 deletions.
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# TimeXt-JavaScript

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

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

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

This minimized ( < 3kB) library shall help to reduce code like
This minimized ( < 6kB) library shall help to reduce code like

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

```

Since version 0.6.0 TimeXt has new extensions for the long type to display this number value in a human readable string format.

```javascript
const readableStringFromMilliseconds = Number(34325055574).formatMilliseconds() // 56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds
const readableStringFromSeconds = Number(4350554).formatSeconds() // 7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds
const readableStringFromMinutes = Number(432555).formatMinutes() // 42 weeks, 6 days, 9 hours, 15 minutes
const readableStringFromHours = Number(4574).formatHours() // 27 weeks, 1 day, 14 hours
```

## License

TimeXt-JavaScript is distributed under the MIT license. [See LICENSE](LICENSE.md) for details.

```
MIT License
Copyright (c) 2018 - 2019 GuepardoApps (Jonas Schubert)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timext-js",
"version": "0.5.0",
"version": "0.6.0",
"description": "3kB time library to handle date and time more easily",
"main": "timext.min.js",
"types": "",
Expand Down Expand Up @@ -28,7 +28,7 @@
],
"size-limit": [
{
"limit": "3.00 KB",
"limit": "6.00 KB",
"path": "timext.min.js"
}
],
Expand All @@ -54,28 +54,28 @@
},
"devDependencies": {
"@babel/cli": "7.4.4",
"@babel/core": "7.4.4",
"@babel/node": "7.2.2",
"@babel/preset-env": "7.4.4",
"@babel/core": "7.4.5",
"@babel/node": "7.4.5",
"@babel/preset-env": "7.4.5",
"babel-core": "6.26.3",
"babel-jest": "24.7.1",
"babel-jest": "24.8.0",
"babel-plugin-external-helpers": "6.22.0",
"cross-env": "5.2.0",
"eslint": "5.16.0",
"eslint-config-airbnb-base": "13.1.0",
"eslint-plugin-import": "2.17.2",
"eslint-plugin-jest": "22.5.1",
"eslint-plugin-import": "2.17.3",
"eslint-plugin-jest": "22.6.4",
"gzip-size-cli": "3.0.0",
"jasmine-core": "2.99.1",
"jest": "24.7.1",
"jest": "24.8.0",
"karma": "4.1.0",
"karma-jasmine": "1.1.2",
"karma-sauce-launcher": "2.0.2",
"pre-commit": "1.2.2",
"rollup": "1.10.1",
"rollup": "1.12.3",
"rollup-plugin-babel": "4.3.2",
"rollup-plugin-uglify": "6.0.2",
"size-limit": "1.3.1",
"size-limit": "1.3.3",
"typescript": "3.4.5"
},
"dependencies": {}
Expand Down
1 change: 1 addition & 0 deletions releases/timext-2019-05-26-1.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 73 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class TimeXt {

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

export default timext;

// Date extensions

Date.prototype.plus = function (val) {
Expand Down Expand Up @@ -158,4 +160,74 @@ Number.prototype.toMilliseconds = function () {
return timext(this, u.MS);
}

export default timext;
// String format extensions

Number.prototype.formatMilliseconds = function () {
const dictionary = [
{ key: 'week', value: { first: 7 * 24 * 60 * 60 * 1000, second: Number.MAX_SAFE_INTEGER } },
{ key: 'day', value: { first: 24 * 60 * 60 * 1000, second: 7 } },
{ key: 'hour', value: { first: 60 * 60 * 1000, second: 24 } },
{ key: 'minute', value: { first: 60 * 1000, second: 60 } },
{ key: 'second', value: { first: 1000, second: 60 } },
{ key: 'millisecond', value: { first: 1, second: 1000 } }
];

const stringArray = dictionary
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
: undefined))
.filter(item => !!item);

return stringArray.length > 0 ? stringArray.join(', ') : '0 milliseconds';
}

Number.prototype.formatSeconds = function () {
const dictionary = [
{ key: 'week', value: { first: 7 * 24 * 60 * 60 * 1, second: Number.MAX_SAFE_INTEGER } },
{ key: 'day', value: { first: 24 * 60 * 60 * 1, second: 7 } },
{ key: 'hour', value: { first: 60 * 60 * 1, second: 24 } },
{ key: 'minute', value: { first: 60 * 1, second: 60 } },
{ key: 'second', value: { first: 1, second: 60 } }
];

const stringArray = dictionary
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
: undefined))
.filter(item => !!item);

return stringArray.length > 0 ? stringArray.join(', ') : (this * 1000).formatMilliseconds();
}

Number.prototype.formatMinutes = function () {
const dictionary = [
{ key: 'week', value: { first: 7 * 24 * 60 * 1, second: Number.MAX_SAFE_INTEGER } },
{ key: 'day', value: { first: 24 * 60 * 1, second: 7 } },
{ key: 'hour', value: { first: 60 * 1, second: 24 } },
{ key: 'minute', value: { first: 1, second: 60 } }
];

const stringArray = dictionary
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
: undefined))
.filter(item => !!item);

return stringArray.length > 0 ? stringArray.join(', ') : (this * 60).formatSeconds();
}

Number.prototype.formatHours = function () {
const dictionary = [
{ key: 'week', value: { first: 7 * 24 * 1, second: Number.MAX_SAFE_INTEGER } },
{ key: 'day', value: { first: 24 * 1, second: 7 } },
{ key: 'hour', value: { first: 1, second: 24 } }
];

const stringArray = dictionary
.map(item => (Math.trunc((this / item.value.first) % item.value.second) > 0
? `${Math.trunc((this / item.value.first) % item.value.second)} ${item.key}${(Math.trunc((this / item.value.first) % item.value.second) > 1) ? 's' : ''}`
: undefined))
.filter(item => !!item);

return stringArray.length > 0 ? stringArray.join(', ') : (this * 60).formatMinutes();
}
49 changes: 49 additions & 0 deletions test/string-format.extensions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import '../src/index';

it('formatMilliseconds should work as expected', () => {
// Assert
const expected = '56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds';
const testValue = 34325055574;

// Act
const actual = testValue.formatMilliseconds();

// Assert
expect(actual).toBe(expected);
})

it('formatSeconds should work as expected', () => {
// Assert
const expected = '7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds';
const testValue = 4350554;

// Act
const actual = testValue.formatSeconds();

// Assert
expect(actual).toBe(expected);
})

it('formatMinutes should work as expected', () => {
// Assert
const expected = '42 weeks, 6 days, 9 hours, 15 minutes';
const testValue = 432555;

// Act
const actual = testValue.formatMinutes();

// Assert
expect(actual).toBe(expected);
})

it('formatHours should work as expected', () => {
// Assert
const expected = '27 weeks, 1 day, 14 hours';
const testValue = 4574;

// Act
const actual = testValue.formatHours();

// Assert
expect(actual).toBe(expected);
})
1 change: 0 additions & 1 deletion timext.min.js

This file was deleted.

0 comments on commit 3b368dd

Please sign in to comment.