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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Schubert committed Oct 28, 2018
2 parents 5c1bd03 + 1529850 commit d151802
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# TimeXt-JavaScript - master branch

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
<a target="_blank" href="https://www.paypal.me/GuepardoApps" title="Donate using PayPal"><img src="https://img.shields.io/badge/paypal-donate-blue.svg" /></a>

[![Build](https://img.shields.io/badge/build-success-green.svg)](https://github.com/TimeXt/TimeXt-JavaScript/blob/master/releases/timext-2018-10-28-1.min.js)
[![Version](https://img.shields.io/badge/version-v0.3.0.181028-blue.svg)](https://github.com/TimeXt/TimeXt-JavaScript/tree/master/releases/)
[![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)](https://github.com/TimeXt/TimeXt-JavaScript/blob/master/releases/timext-2018-10-28-2.min.js)
[![Version](https://img.shields.io/badge/version-v0.4.0.181028-blue.svg)](https://github.com/TimeXt/TimeXt-JavaScript/releases)
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-98-green.svg)](https://github.com/TimeXt/TimeXt-JavaScript/tree/master/coverage/)

[![Npm](https://img.shields.io/badge/npm-getit-red.svg)](https://www.npmjs.com/package/timext-js)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
<a target="_blank" href="https://www.paypal.me/GuepardoApps" title="Donate using PayPal"><img src="https://img.shields.io/badge/paypal-donate-blue.svg" /></a>

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

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

```javascript
const dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
Expand Down Expand Up @@ -60,9 +59,13 @@ const sixMinutes = Number(6).toMinutes(); // returns timext(6,
const fiftySeconds = Number(50).toSeconds(); // returns timext(50, u.S)
const hundredMilliseconds = Number(100).toMilliseconds(); // returns timext(100, u.MS)

// Return in other time units
const oneDayInMillis = Number(1).toDays().inMilliseconds();// Returns one day in milliseconds === 24 * 60 * 60 * 1e3
const twoWeeksInHours = Number(2).toWeeks().inHours(); // Returns two weeks in hours === 2 * 7 * 24

// Convert to other time units
const oneDayInMillis = Number(1).toDays().inMilliseconds();// Converts one day into milliseconds === 24 * 60 * 60 * 1e3
const twoWeeksInHours = Number(2).toWeeks().inHours(); // Converts two weeks into hours === 2 * 7 * 24
const oneDayInMillis = Number(1).toDays().toHours(); // Converts one day into timext(24, u.H)
const twoWeeksInHours = Number(120).toMinutes().toHours(); // Converts 120 hours into timext(2, u.H)

// add timext to date using date extensions
const inFiveDays = Date.now.plus(Number(5).toDays());
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "timext-js",
"version": "0.3.0",
"description": "2kB time library to handle date and time more easily",
"version": "0.4.0",
"description": "3kB time library to handle date and time more easily",
"main": "timext.min.js",
"types": "",
"scripts": {
Expand All @@ -28,7 +28,7 @@
],
"size-limit": [
{
"limit": "2.00 KB",
"limit": "3.00 KB",
"path": "timext.min.js"
}
],
Expand Down
1 change: 1 addition & 0 deletions releases/timext-2018-10-28-2.min.js

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

28 changes: 28 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ class TimeXt {
return (this.val * this.unit) / u.MS;
}

toYears() {
return new TimeXt(this.inYears(), u.Y);
}

toWeeks() {
return new TimeXt(this.inWeeks(), u.W);
}

toDays() {
return new TimeXt(this.inDays(), u.D);
}

toHours() {
return new TimeXt(this.inHours(), u.H);
}

toMinutes() {
return new TimeXt(this.inMinutes(), u.M);
}

toSeconds() {
return new TimeXt(this.inSeconds(), u.S);
}

toMilliseconds() {
return new TimeXt(this.inMilliseconds(), u.MS);
}

plus(t) {
this.val = ((this.inMilliseconds() + t.inMilliseconds()) / this.unit) * u.MS;
return this;
Expand Down
13 changes: 12 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import timext from '../src/index';
import * as u from '../src/units';

it('All conversions should work as expected', () => {
it('All in conversions should work as expected', () => {
expect(Math.round(timext(730, u.D).inYears())).toBe(2);
expect(Math.round(timext(21, u.D).inWeeks())).toBe(3);
expect(Math.round(timext(1, u.Y).inDays())).toBe(365);
Expand All @@ -12,6 +12,17 @@ it('All conversions should work as expected', () => {
expect(Math.round(timext(2, u.S).inMilliseconds())).toBe(2000);
})

it('All to conversions should work as expected', () => {
expect(timext(730, u.D).toYears()).toEqual(timext(2, u.Y));
expect(timext(21, u.D).toWeeks()).toEqual(timext(3, u.W));
expect(timext(1, u.Y).toDays()).toEqual(timext(365, u.D));
expect(timext(2, u.W).toDays()).toEqual(timext(14, u.D));
expect(timext(2, u.D).toHours()).toEqual(timext(48, u.H));
expect(timext(2, u.H).toMinutes()).toEqual(timext(120, u.M));
expect(timext(2, u.M).toSeconds()).toEqual(timext(120, u.S));
expect(timext(2, u.S).toMilliseconds()).toEqual(timext(2000, u.MS));
})

it('All arithmetic operator should work as expected', () => {
expect(timext(2, u.W).plus(timext(7, u.D))).toEqual(timext(3, u.W));
expect(timext(4, u.H).plus(timext(30, u.M))).toEqual(timext(4.5, u.H));
Expand Down
2 changes: 1 addition & 1 deletion timext.min.js

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

0 comments on commit d151802

Please sign in to comment.