Compare commits

..

16 Commits

Author SHA1 Message Date
9935396f2d 3.0.7 2019-04-10 14:37:39 +02:00
1bc379daa5 3.0.6 2019-04-10 14:06:21 +02:00
f9b81fc801 fix(core): update 2019-04-10 14:06:20 +02:00
6d8e39f11b 3.0.6 2019-04-10 11:34:30 +02:00
cff5b5a7f4 fix(core): update 2019-04-10 11:34:30 +02:00
f6be9b71f1 3.0.5 2018-11-23 19:26:42 +01:00
9c2d584170 fix(core): update 2018-11-23 19:26:41 +01:00
10bff069c1 3.0.4 2018-11-23 18:48:08 +01:00
8f6272eca4 fix(core): update 2018-11-23 18:48:07 +01:00
f02df7789c 3.0.3 2018-11-23 18:29:38 +01:00
d720b8b084 fix(core): update 2018-11-23 18:29:38 +01:00
6bca8557f9 3.0.2 2018-10-21 17:32:03 +02:00
f0a5e18335 fix(ExtendedDate): now respects single digit dates 2018-10-21 17:32:03 +02:00
57fbdb4a70 update 2018-10-21 17:23:58 +02:00
4a56527f7d 3.0.1 2018-10-21 12:12:45 +02:00
2a14d928a8 fix(date): now supports date and time 2018-10-21 12:12:44 +02:00
15 changed files with 1081 additions and 281 deletions

View File

@ -37,18 +37,6 @@ snyk:
# ==================== # ====================
# test stage # test stage
# ==================== # ====================
testLEGACY:
stage: test
script:
- npmci npm prepare
- npmci node install legacy
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
allow_failure: true
testLTS: testLTS:
stage: test stage: test
@ -135,13 +123,3 @@ pages:
paths: paths:
- public - public
allow_failure: true allow_failure: true
windowsCompatibility:
image: stefanscherer/node-windows:10-build-tools
stage: metadata
script:
- npm install & npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- windows
allow_failure: true

View File

@ -2,5 +2,15 @@
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smarttime",
"shortDescription": "handle time in smart ways",
"npmPackagename": "@pushrocks/smarttime",
"license": "MIT"
}
} }
} }

1108
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"private": false, "private": false,
"version": "3.0.0", "version": "3.0.7",
"description": "handle timeformats in smart ways", "description": "handle time in smart ways",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
"author": "Lossless GmbH", "author": "Lossless GmbH",
@ -12,17 +12,19 @@
"build": "(tsbuild)" "build": "(tsbuild)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.0.22", "@gitzone/tsbuild": "^2.1.8",
"@gitzone/tsrun": "^1.1.13", "@gitzone/tsrun": "^1.2.5",
"@gitzone/tstest": "^1.0.15", "@gitzone/tstest": "^1.0.20",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^3.0.9",
"@types/node": "^10.12.0", "@types/node": "^11.13.2",
"tslint": "^5.11.0", "tslint": "^5.15.0",
"tslint-config-prettier": "^1.15.0" "tslint-config-prettier": "^1.18.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartpromise": "^2.0.5", "@pushrocks/smartpromise": "^3.0.2",
"@types/luxon": "^1.4.0", "@types/cron": "^1.7.0",
"luxon": "^1.4.3" "@types/luxon": "^1.12.0",
"cron": "^1.7.0",
"luxon": "^1.12.1"
} }
} }

View File

@ -1,33 +1,39 @@
# smarttime # @pushrocks/smarttime
handle time in smart ways
handle timeformats in smart ways ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smarttime)
## Availabililty * [gitlab.com (source)](https://gitlab.com/pushrocks/smarttime)
* [github.com (source mirror)](https://github.com/pushrocks/smarttime)
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smarttime) * [docs (typedoc)](https://pushrocks.gitlab.io/smarttime/)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smarttime)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smarttime)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smarttime/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/smarttime/badges/master/build.svg)](https://gitlab.com/pushrocks/smarttime/commits/master)
[![build status](https://GitLab.com/pushrocks/smarttime/badges/master/build.svg)](https://GitLab.com/pushrocks/smarttime/commits/master) [![coverage report](https://gitlab.com/pushrocks/smarttime/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smarttime/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smarttime/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smarttime/commits/master) [![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smarttime.svg)](https://www.npmjs.com/package/@pushrocks/smarttime)
[![npm downloads per month](https://img.shields.io/npm/dm/smarttime.svg)](https://www.npmjs.com/package/smarttime) [![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smarttime/badge.svg)](https://snyk.io/test/npm/@pushrocks/smarttime)
[![Dependency Status](https://david-dm.org/pushrocks/smarttime.svg)](https://david-dm.org/pushrocks/smarttime) [![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smarttime/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smarttime/master/dependencies/npm) [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smarttime/badges/code.svg)](https://www.bithound.io/github/pushrocks/smarttime) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage ## Usage
Use TypeScript for best in class instellisense. Use TypeScript for best in class instellisense.
For further information read the linked docs at the top of this README. Smarttime offers smart ways to deal with time.
### class ExtendedDate
This class offers static functions to create zone speicific JavaScript dates from European formated time strings.
```TypeScript
import { ExtendedDate } from '@pushrocks/smarttime'
const myDate: Date = ExtendedDate.fromEuropeanDate('8.6.2018')
```
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) > MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html) | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks) [![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

27
test/test.cronmanager.ts Normal file
View File

@ -0,0 +1,27 @@
import { tap, expect } from '@pushrocks/tapbundle';
import * as smarttime from '../ts/index';
let testCronManager: smarttime.CronManager;
tap.test('should create a valid instance of cronmanager', async () => {
testCronManager = new smarttime.CronManager();
expect(testCronManager).to.be.instanceOf(smarttime.CronManager);
});
tap.test('should create a valid cronJon', async (tools) => {
const done = tools.defer();
let counter = 0;
testCronManager.addCronjob('* * * * * *', () => {
if(counter === 10) {
done.resolve();
}
counter ++;
console.log(`hey ${counter}`);
});
testCronManager.start();
await done.promise;
testCronManager.stop();
});
tap.start();

View File

@ -3,7 +3,16 @@ import { expect, tap } from '@pushrocks/tapbundle';
import * as smarttime from '../ts/index'; import * as smarttime from '../ts/index';
tap.test('should create a valid JavaScript Date from European TimeStamp', async () => { tap.test('should create a valid JavaScript Date from European TimeStamp', async () => {
const extendedDate = smarttime.ExtendedDate.fromEuropeanDate('10.10.2018'); const extendedDate = smarttime.ExtendedDate.fromEuropeanDate('1.6.2018');
console.log(extendedDate);
});
tap.test('should create a date and time with European Format', async () => {
const extendedDate = smarttime.ExtendedDate.fromEuropeanDateAndTime(
'9.8.2018',
'08:00:00',
'Europe/Berlin'
);
console.log(extendedDate); console.log(extendedDate);
}); });

View File

@ -15,4 +15,12 @@ tap.test('should start timer', async () => {
await testTimer.completed; await testTimer.completed;
}); });
tap.test('should reset a timer', async () => {
testTimer.reset();
testTimer.start();
testTimer.reset();
testTimer.start();
await testTimer.completed;
});
tap.start(); tap.start();

View File

@ -1,5 +1,6 @@
import * as plugins from './smarttime.plugins'; import * as plugins from './smarttime.plugins';
export * from './smarttime.classes.cronmanager';
export * from './smarttime.classes.date'; export * from './smarttime.classes.date';
export * from './smarttime.classes.hrtmeasurement'; export * from './smarttime.classes.hrtmeasurement';
export * from './smarttime.classes.timer'; export * from './smarttime.classes.timer';

View File

@ -0,0 +1,35 @@
import * as plugins from './smarttime.plugins';
export class CronManager {
public status: 'started' | 'stopped' = 'stopped';
public cronjobs: plugins.cron.CronJob[] = [];
public addCronjob(cronIdentifierArg: string, cronFunctionArg: plugins.cron.CronCommand) {
const newCronJob = new plugins.cron.CronJob(cronIdentifierArg,cronFunctionArg);
if (this.status === 'started') {
newCronJob.start();
}
this.cronjobs.push(newCronJob);
};
/**
* starts the cronjob
*/
public start() {
this.status = 'started';
for (const cron of this.cronjobs) {
cron.start();
}
}
/**
* stops all cronjobs
*/
public stop() {
this.status = 'stopped';
for (const cron of this.cronjobs) {
cron.stop();
}
}
}

View File

@ -1,5 +1,7 @@
import * as plugins from './smarttime.plugins'; import * as plugins from './smarttime.plugins';
export type TAvailableZone = 'Europe/Berlin';
export class ExtendedDate extends Date { export class ExtendedDate extends Date {
public static fromEuropeanDate(europeanDate: string) { public static fromEuropeanDate(europeanDate: string) {
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate); const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
@ -12,6 +14,26 @@ export class ExtendedDate extends Date {
return new ExtendedDate(unixMilli); return new ExtendedDate(unixMilli);
} }
/** */
public static fromEuropeanDateAndTime(
europeanDateArg: string,
timeArg: string,
zoneArg: TAvailableZone
) {
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
const sliceDate = (dateString: string) => {
return `0${dateString}`.slice(-2);
};
const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate(
dateArray[1]
)}T${timeArg}`;
const luxonDate = plugins.luxon.DateTime.fromISO(dateTimeString, {
zone: zoneArg
});
const unixMilli = luxonDate.toMillis();
return new ExtendedDate(unixMilli);
}
constructor(unixMilli: number) { constructor(unixMilli: number) {
super(unixMilli); super(unixMilli);
} }

View File

@ -61,15 +61,26 @@ export class Timer {
} }
public pause() { public pause() {
if (this.startedAt) {
clearTimeout(this.currentTimeout); clearTimeout(this.currentTimeout);
this.currentTimeout = null;
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt); this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
} }
}
public resume() { public resume() {
if (this.startedAt) { if (this.startedAt) {
this.currentTimeout = setTimeout(() => { this.currentTimeout = setTimeout(() => {
this.completedDeferred.resolve(); this.completedDeferred.resolve();
}, this.timeLeft); }, this.timeLeft);
} else {
throw new Error('timer has NOT been started before. Please use .start() instead');
} }
} }
public reset() {
this.pause();
this.startedAt = null;
this.pausedAt = null;
}
} }

View File

@ -65,6 +65,11 @@ export class TimeStamp {
} }
} }
/**
* returns a boolean for wether a timestamp is younger
* @param TimeStampArg
* @param tresholdTimeArg
*/
public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) { public isYoungerThanOtherTimeStamp(TimeStampArg: TimeStamp, tresholdTimeArg: number = 0) {
if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) { if (this.milliSeconds > TimeStampArg.milliSeconds + tresholdTimeArg) {
return true; return true;

View File

@ -5,5 +5,6 @@ export { smartpromise };
// third parties // third parties
import * as luxon from 'luxon'; import * as luxon from 'luxon';
import * as cron from 'cron';
export { luxon }; export { luxon, cron };

View File

@ -1,6 +1,17 @@
{ {
"extends": [ "extends": ["tslint:latest", "tslint-config-prettier"],
"tslint:latest", "rules": {
"tslint-config-prettier" "semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
] ]
}
}
},
"defaultSeverity": "warning"
} }