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

Commit 85e469f

Browse files
author
Jonas Schubert
committed
v0.2.2 , now available on npm
1 parent 015e9c1 commit 85e469f

12 files changed

+223
-109
lines changed

.eslintrc.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"timext": true
1212
},
1313
"rules": {
14-
"semi": [
15-
2,
16-
"never"
14+
"max-len": [
15+
"error",
16+
200
1717
],
1818
"comma-dangle": [
1919
"error",
@@ -39,6 +39,15 @@
3939
"import/extensions": [
4040
2,
4141
"ignorePackages"
42-
]
42+
],
43+
"semi": "off",
44+
"no-extend-native": "off",
45+
"linebreak-style": "off",
46+
"indent": "off",
47+
"no-underscore-dangle": "off",
48+
"no-plusplus": "off",
49+
"padded-blocks": "off",
50+
"no-var": "off",
51+
"vars-on-top": "off"
4352
}
4453
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ coverage
1818

1919
#dev
2020
demo.js
21+
22+
.vs

README.md

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,64 @@
33
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
44
<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>
55

6-
[![Build](https://img.shields.io/badge/build-success-green.svg)](https://github.com/TimeXt/TimeXt-JavaScript/blob/develop/releases/timext-2018-10-20-1.min.js)
7-
[![Version](https://img.shields.io/badge/version-v0.1.0.181020-blue.svg)](https://github.com/TimeXt/TimeXt-JavaScript/tree/develop/releases/)
6+
[![Build](https://img.shields.io/badge/build-success-green.svg)](https://github.com/TimeXt/TimeXt-JavaScript/blob/develop/releases/timext-2018-10-27-2.min.js)
7+
[![Version](https://img.shields.io/badge/version-v0.2.2.181027-blue.svg)](https://github.com/TimeXt/TimeXt-JavaScript/tree/develop/releases/)
8+
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-98-green.svg)](https://github.com/TimeXt/TimeXt-JavaScript/tree/develop/test/)
9+
10+
[![Npm](https://img.shields.io/badge/npm-getit-red.svg)](https://www.npmjs.com/package/timext-js)
811

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

11-
This library shall help to reduce code like
14+
This minimized ( < 2kB) library shall help to reduce code like
1215

1316
```javascript
14-
const dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
17+
const dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
1518
```
1619

1720
## How to use
1821

22+
If you want to use the original files
23+
1924
```javascript
2025
import timext from '../src/index';
21-
import * as Unit from '../src/units';
22-
23-
const oneWeek = timext(1, Unit.Week);
24-
const threeDays = timext(3, Unit.Day);
25-
const elevenHours = timext(11, Unit.Hour);
26-
const sixMinutes = timext(6, Unit.Minute);
27-
const fiftySeconds = timext(50, Unit.Second);
28-
const hundredMilliSeconds = timext(100, Unit.MilliSecond);
29-
const fiveMicroSeconds = timext(5, Unit.MicroSecond);
30-
const oneNanoSecond = timext(1, Unit.NanoSecond);
31-
const onePicoSecond = timext(1, Unit.PicoSecond);
32-
33-
const oneDayInMillis = timext(1, Unit.Day).inMilliSeconds(); // Converts one day into milliseconds
34-
const twoWeeksInHours = timext(2, Unit.Week).inHours(); // Converts two weeks into hours
35-
36-
const duration = timext(1, Unit.Day).plus(timext(6, Unit.Hour));
37-
const difference = timext(34, Unit.Minute).minus(timext(420, Unit.Second));
38-
const multipliedDuration = timext(2, Unit.Week).times(1.5);
39-
const dividedDuration = timext(2500, Unit.PicoSecond).times(2);
26+
import * as u from '../src/units';
27+
28+
// Create by using constructor method
29+
const oneWeek = timext(1, u.W);
30+
const threeDays = timext(3, u.D);
31+
const elevenHours = timext(11, u.H);
32+
const sixMinutes = timext(6, u.M);
33+
const fiftySeconds = timext(50, u.S);
34+
const hundredMilliseconds = timext(100, u.MS);
35+
36+
// Convert to other time units
37+
const oneDayInMillis = timext(1, u.D).inMilliseconds(); // Converts one day into milliseconds
38+
const twoWeeksInHours = timext(2, u.W).inHours(); // Converts two weeks into hours
39+
40+
// "operator" + - * /
41+
const duration = timext(1, u.D).plus(timext(6, u.H));
42+
const difference = timext(34, u.M).minus(timext(420, u.S));
43+
const multipliedDuration = timext(2, u.W).multiply(1.5);
44+
const dividedDuration = timext(2500, u.MS).divide(2);
45+
46+
```
47+
48+
If you want to use the minified script from the releases
49+
50+
```javascript
51+
import 'timext.min';
52+
53+
// Create by using number extensions
54+
const oneWeek = Number(1).toWeeks(); // returns timext(1, u.W)
55+
const threeDays = Number(3).toDays(); // returns timext(3, u.D)
56+
const elevenHours = Number(11).toHours(); // returns timext(11, u.H)
57+
const sixMinutes = Number(6).toMinutes(); // returns timext(6, u.M)
58+
const fiftySeconds = Number(50).toSeconds(); // returns timext(50, u.S)
59+
const hundredMilliseconds = Number(100).toMilliseconds(); // returns timext(100, u.MS)
60+
61+
// add timext to date using date extensions
62+
const inFiveDays = Date.now.plus(Number(5).toDays());
63+
const threeWeeksAgo = Date.now.minus(Number(3).toWeeks());
4064

4165
```
4266

build/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const rollup = require('rollup')
22
const configFactory = require('./rollup.config')
3-
const fs = require('fs')
4-
const util = require('util')
5-
const path = require('path')
3+
// const fs = require('fs')
4+
// const util = require('util')
5+
// const path = require('path')
66

7-
const { promisify } = util
7+
// const { promisify } = util
88

9-
const promisifyReadDir = promisify(fs.readdir)
9+
// const promisifyReadDir = promisify(fs.readdir)
1010

11-
const formatName = n => n.replace(/\.js/, '').replace('-', '_')
11+
// const formatName = n => n.replace(/\.js/, '').replace('-', '_')
1212

1313
async function build(option) {
1414
const bundle = await rollup.rollup(option.input)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "timext-javascript",
3-
"version": "0.1.0",
2+
"name": "timext-js",
3+
"version": "0.2.2",
44
"description": "",
55
"main": "timext.min.js",
66
"types": "",

releases/timext-2018-10-27-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.

releases/timext-2018-10-27-2.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 & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,125 @@
1-
import * as Unit from './units';
1+
import * as u from './units';
22

3-
class Interval {
3+
class TimeXt {
44

5-
constructor(value, unit) {
6-
this.value = value;
5+
constructor(val, unit) {
6+
this.val = val;
77
this.unit = unit;
88
}
99

1010
inWeeks() {
11-
return (this.value * this.unit) / Unit.Week;
11+
return (this.val * this.unit) / u.W;
1212
}
1313

1414
inDays() {
15-
return (this.value * this.unit) / Unit.Day;
15+
return (this.val * this.unit) / u.D;
1616
}
1717

1818
inHours() {
19-
return (this.value * this.unit) / Unit.Hour;
19+
return (this.val * this.unit) / u.H;
2020
}
2121

2222
inMinutes() {
23-
return (this.value * this.unit) / Unit.Minute;
23+
return (this.val * this.unit) / u.M;
2424
}
2525

2626
inSeconds() {
27-
return (this.value * this.unit) / Unit.Second;
27+
return (this.val * this.unit) / u.S;
2828
}
2929

30-
inMilliSeconds() {
31-
return (this.value * this.unit) / Unit.MilliSecond;
30+
inMilliseconds() {
31+
return (this.val * this.unit) / u.MS;
3232
}
3333

34-
inMicroSeconds() {
35-
return (this.value * this.unit) / Unit.MicroSecond;
34+
plus(t) {
35+
this.val = ((this.inMilliseconds() + t.inMilliseconds()) / this.unit) * u.MS;
36+
return this;
3637
}
3738

38-
inNanoSeconds() {
39-
return (this.value * this.unit) / Unit.NanoSecond;
39+
minus(t) {
40+
this.val = ((this.inMilliseconds() - t.inMilliseconds()) / this.unit) * u.MS;
41+
return this;
4042
}
4143

42-
inPicoSeconds() {
43-
return (this.value * this.unit) / Unit.PicoSecond;
44+
multiply(val) {
45+
this.val *= val;
46+
return this;
4447
}
4548

46-
plus(interval) {
47-
this.value = ((this.inPicoSeconds() + interval.inPicoSeconds()) / this.unit) * Unit.PicoSecond;
48-
return this;
49-
}
50-
51-
minus(interval) {
52-
this.value = ((this.inPicoSeconds() - interval.inPicoSeconds()) / this.unit) * Unit.PicoSecond;
53-
return this;
54-
}
55-
56-
times(timesValue) {
57-
this.value *= timesValue;
58-
return this;
59-
}
60-
61-
div(diversionValue) {
62-
if (diversionValue === 0){
49+
divide(val) {
50+
if (val === 0) {
6351
throw Error('Diversion value may not be 0!');
6452
}
65-
this.value /= diversionValue;
66-
return this;
53+
this.val /= val;
54+
return this;
6755
}
6856

6957
inc() {
70-
this.value++;
71-
return this;
58+
this.val++;
59+
return this;
7260
}
7361

7462
dec() {
75-
this.value--;
76-
return this;
63+
this.val--;
64+
return this;
7765
}
7866

79-
compareTo(interval) {
80-
return this.inMilliSeconds() - interval.inMilliSeconds();
81-
}
82-
83-
contains(interval) {
84-
return this.inPicoSeconds() >= interval.inPicoSeconds();
67+
compareTo(t) {
68+
if (!t) {
69+
return this.inMilliseconds();
70+
}
71+
return this.inMilliseconds() - t.inMilliseconds();
8572
}
8673

87-
equals(interval) {
88-
return !!interval && this.compareTo(interval) === 0;
74+
equals(t) {
75+
return !!t && this.compareTo(t) === 0;
8976
}
9077

9178
hashCode() {
92-
return this.inPicoSeconds();
79+
return this.inMilliseconds();
9380
}
9481

9582
toString() {
96-
return this.inMilliSeconds().toString();
83+
return this.inMilliseconds().toString();
9784
}
9885
}
9986

100-
const timext = (value, unit) => {
101-
return new Interval(value, unit);
87+
const timext = (val, unit) => new TimeXt(val, unit);
88+
89+
// Date extensions
90+
91+
Date.prototype.plus = function (val) {
92+
return new Date(this.getTime() + val.inMilliseconds());
93+
}
94+
95+
Date.prototype.minus = function (val) {
96+
return new Date(this.getTime() - val.inMilliseconds());
97+
}
98+
99+
// Number extensions
100+
101+
Number.prototype.toWeeks = function () {
102+
return timext(this, u.W);
103+
}
104+
105+
Number.prototype.toDays = function () {
106+
return timext(this, u.D);
107+
}
108+
109+
Number.prototype.toHours = function () {
110+
return timext(this, u.H);
111+
}
112+
113+
Number.prototype.toMinutes = function () {
114+
return timext(this, u.M);
115+
}
116+
117+
Number.prototype.toSeconds = function () {
118+
return timext(this, u.S);
119+
}
120+
121+
Number.prototype.toMilliseconds = function () {
122+
return timext(this, u.MS);
102123
}
103124

104125
export default timext;

src/units.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
export const Week = 7 * 24 * 60 * 60;
2-
export const Day = 24 * 60 * 60;
3-
export const Hour = 60 * 60;
4-
export const Minute = 60;
5-
export const Second = 1;
6-
export const MilliSecond = 1e-3;
7-
export const MicroSecond = 1e-6;
8-
export const NanoSecond = 1e-9;
9-
export const PicoSecond = 1e-12;
1+
export const W = 7 * 24 * 60 * 60 * 1e3;
2+
export const D = 24 * 60 * 60 * 1e3;
3+
export const H = 60 * 60 * 1e3;
4+
export const M = 60 * 1e3;
5+
export const S = 1e3;
6+
export const MS = 1;

test/date.extensions.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import '../src/index';
2+
3+
it('All arithmetic operator should work as exptected', () => {
4+
expect((new Date(2018, 10, 15, 20, 14, 0, 0).plus(Number(3).toHours())).getHours()).toBe(23);
5+
expect((new Date(2018, 10, 15, 20, 14, 0, 0).minus(Number(3).toDays())).getDate()).toBe(12);
6+
})

0 commit comments

Comments
 (0)