Compare commits

..

14 Commits

Author SHA1 Message Date
da9c71eedb 3.0.42 2021-11-08 16:52:14 +01:00
63405fc577 fix(readme): fix readme 2021-11-08 16:52:13 +01:00
d789eec558 3.0.41 2021-11-08 16:49:33 +01:00
22027d2308 fix(update croner): update croner package, closes #1. Thank you @Hexagon4 2021-11-08 16:49:33 +01:00
87d29fbb50 3.0.40 2021-11-08 16:40:19 +01:00
1c5a1c21da fix(core): update 2021-11-08 16:40:19 +01:00
969088ad04 3.0.39 2021-10-06 11:58:36 +02:00
76768a1307 fix(core): update 2021-10-06 11:58:36 +02:00
419fe80bde 3.0.38 2021-01-02 19:30:16 +00:00
5a9584917c fix(core): update 2021-01-02 19:30:15 +00:00
2b21ee9a24 3.0.37 2020-10-05 22:28:40 +00:00
3a110c9fd9 fix(core): update 2020-10-05 22:28:40 +00:00
25f7ad37ae 3.0.36 2020-10-05 17:48:42 +00:00
992d167a34 fix(core): update 2020-10-05 17:48:41 +00:00
14 changed files with 19394 additions and 3746 deletions

22954
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"private": false, "private": false,
"version": "3.0.35", "version": "3.0.42",
"description": "handle time in smart ways", "description": "handle time in smart ways",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
@ -12,22 +12,23 @@
"build": "(tsbuild --web && tsbundle npm)" "build": "(tsbuild --web && tsbundle npm)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.28",
"@gitzone/tsbundle": "^1.0.78", "@gitzone/tsbundle": "^1.0.88",
"@gitzone/tsrun": "^1.2.12", "@gitzone/tsrun": "^1.2.18",
"@gitzone/tstest": "^1.0.44", "@gitzone/tstest": "^1.0.60",
"@pushrocks/tapbundle": "^3.2.9", "@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^14.6.2", "@types/node": "^16.11.6",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0" "tslint-config-prettier": "^1.18.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/lik": "^4.0.17", "@pushrocks/lik": "^5.0.0",
"@pushrocks/smartdelay": "^2.0.10", "@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartpromise": "^3.0.2", "@pushrocks/smartpromise": "^3.1.6",
"croner": "^1.1.23", "croner": "^4.0.69",
"dayjs": "^1.8.35", "dayjs": "^1.10.7",
"is-nan": "^1.3.0" "is-nan": "^1.3.2",
"pretty-ms": "^7.0.1"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

View File

@ -36,7 +36,7 @@ This class provides scheduling of functions with a cron syntax
```typescript ```typescript
import { CronManager } from '@pushrocks/smarrtime'; import { CronManager } from '@pushrocks/smarrtime';
const cronManagerInstance = new CronManager(); const cronManagerInstance = new CronManager();
cronManagerInstance.addConrjob('* * * * * *', async () => { cronManagerInstance.addCronjob('* * * * * *', async () => {
console.log('hello'); // will log 'hello' to console once every second; console.log('hello'); // will log 'hello' to console once every second;
}); });
cronManagerInstance.start(); cronManagerInstance.start();

View File

@ -17,7 +17,7 @@ tap.test('should create a valid cronJon', async (tools) => {
let counter2 = 0; let counter2 = 0;
let counter3 = 0; let counter3 = 0;
const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => { const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => {
if (counter === 10) { if (counter === 5) {
testCronManager.removeCronjob(cronJob); testCronManager.removeCronjob(cronJob);
done.resolve(); done.resolve();
} }
@ -25,7 +25,7 @@ tap.test('should create a valid cronJon', async (tools) => {
console.log(`${new Date().getSeconds()} hey ${counter} -> runs every 2 seconds`); console.log(`${new Date().getSeconds()} hey ${counter} -> runs every 2 seconds`);
}); });
const cronJob2 = testCronManager.addCronjob('*/3 * * * * *', async () => { const cronJob2 = testCronManager.addCronjob('*/3 * * * * *', async () => {
if (counter2 === 10) { if (counter2 === 5) {
testCronManager.removeCronjob(cronJob2); testCronManager.removeCronjob(cronJob2);
done2.resolve(); done2.resolve();
} }
@ -33,7 +33,7 @@ tap.test('should create a valid cronJon', async (tools) => {
console.log(`${new Date().getSeconds()} hey ${counter2} -> runs every 3 seconds`); console.log(`${new Date().getSeconds()} hey ${counter2} -> runs every 3 seconds`);
}); });
const cronJob3 = testCronManager.addCronjob('*/4 * * * * *', async () => { const cronJob3 = testCronManager.addCronjob('*/4 * * * * *', async () => {
if (counter3 === 10) { if (counter3 === 5) {
done3.resolve(); done3.resolve();
} }
counter3++; counter3++;

View File

@ -1,27 +0,0 @@
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('*/2 * * * * *', async () => {
if (counter === 10) {
done.resolve();
}
counter++;
console.log(`hey ${counter}`);
});
testCronManager.start();
await done.promise;
testCronManager.stop();
});
tap.start();

View File

@ -2,6 +2,11 @@ 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', async () => {
const extendedDate = new smarttime.ExtendedDate(Date.now());
console.log(extendedDate);
});
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('1.6.2018'); const extendedDate = smarttime.ExtendedDate.fromEuropeanDate('1.6.2018');
console.log(extendedDate); console.log(extendedDate);
@ -21,4 +26,9 @@ tap.test('should create a European date string', async () => {
expect(extendedDate.exportToEuropeanDate()).to.equal('13.02.2018'); expect(extendedDate.exportToEuropeanDate()).to.equal('13.02.2018');
}); });
tap.test('should format a date', async () => {
const extendedDate = new smarttime.ExtendedDate(Date.now());
console.log(extendedDate.format('YYYY-MM-DD - hh:mm'));
})
tap.start(); tap.start();

View File

@ -1,5 +1,6 @@
import * as plugins from './smarttime.plugins'; import * as plugins from './smarttime.plugins';
import { CronJob } from './smarttime.classes.cronjob'; import { CronJob } from './smarttime.classes.cronjob';
import { getMilliSecondsAsHumanReadableString } from './smarttime.units';
export class CronManager { export class CronManager {
public executionTimeout: plugins.smartdelay.Timeout<void>; public executionTimeout: plugins.smartdelay.Timeout<void>;
@ -47,16 +48,19 @@ export class CronManager {
} }
} }
if (nextRunningCronjob) { if (nextRunningCronjob) {
this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution()); this.executionTimeout = new plugins.smartdelay.Timeout(
nextRunningCronjob.getTimeToNextExecution()
);
console.log( console.log(
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds` `Next CronJob scheduled in ${getMilliSecondsAsHumanReadableString(
this.executionTimeout.getTimeLeft()
)}`
); );
} else { } else {
this.executionTimeout = new plugins.smartdelay.Timeout(1000); this.executionTimeout = new plugins.smartdelay.Timeout(1000);
console.log('no cronjobs specified! Checking again in 1 second'); console.log('no cronjobs specified! Checking again in 1 second');
} }
await this.executionTimeout.promise; await this.executionTimeout.promise;
} while (this.status === 'started'); } while (this.status === 'started');
}; };

View File

@ -18,7 +18,7 @@ export class CronParser {
} }
if (cronPart.includes('/')) { if (cronPart.includes('/')) {
const every = parseInt(cronPart.split('/')[1], 10); const every = parseInt(cronPart.split('/')[1], 10);
const findEvenMatch = (recursionStartArg: number) => { const findEvenMatch = (recursionStartArg: number): number => {
if (recursionStartArg % every === 0) { if (recursionStartArg % every === 0) {
return recursionStartArg; return recursionStartArg;
} else { } else {

View File

@ -1,4 +1,5 @@
import * as plugins from './smarttime.plugins'; import * as plugins from './smarttime.plugins';
import * as units from './smarttime.units';
export type TAvailableZone = 'Europe/Berlin'; export type TAvailableZone = 'Europe/Berlin';
@ -72,7 +73,7 @@ export class ExtendedDate extends Date {
// INSTANCE // INSTANCE
public timezone: TAvailableZone; public timezone: TAvailableZone;
constructor(unixMilli: number) { constructor(unixMilli: number = Date.now()) {
super(unixMilli); super(unixMilli);
} }
@ -121,4 +122,25 @@ export class ExtendedDate extends Date {
dayOfTheWeekName: daysArray[this.getDay()], dayOfTheWeekName: daysArray[this.getDay()],
}; };
} }
public format (formatArg: string) {
return plugins.dayjs(this.getTime()).format(formatArg);
}
/**
* boolean checks
*/
public isToday () {
return plugins.dayjs(this.getTime()).isToday();
}
public lessTimePassedToNow(unitArgs: units.IUnitCombinationArg): boolean {
const maxPassedUnixTime = units.getMilliSecondsFromUnits(unitArgs);
const actualPassedUnixTime = Date.now() - this.getTime();
return actualPassedUnixTime < maxPassedUnixTime;
}
public moreTimePassedToNow(unitArgs: units.IUnitCombinationArg) {
return !this.lessTimePassedToNow(unitArgs);
}
} }

View File

@ -8,7 +8,7 @@ export class TimeStamp {
/** /**
* returns new TimeStamp from milliseconds * returns new TimeStamp from milliseconds
*/ */
public static fromMilliSeconds(milliSecondsArg) { public static fromMilliSeconds(milliSecondsArg: number) {
return new TimeStamp(milliSecondsArg); return new TimeStamp(milliSecondsArg);
} }

View File

@ -5,8 +5,12 @@ import * as smartpromise from '@pushrocks/smartpromise';
export { lik, smartdelay, smartpromise }; export { lik, smartdelay, smartpromise };
// third parties // third parties;
import croner from 'croner'; const croner: any = require('croner');
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import isToday from 'dayjs/plugin/isToday';
import prettyMs from 'pretty-ms';
export { croner, dayjs }; dayjs.extend(isToday);
export { croner, dayjs, prettyMs };

View File

@ -1,3 +1,5 @@
import * as plugins from './smarttime.plugins';
export let units = { export let units = {
years: (timesArg = 1): number => { years: (timesArg = 1): number => {
return timesArg * 3.154e10; return timesArg * 3.154e10;
@ -30,7 +32,7 @@ export interface IUnitCombinationArg {
export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => { export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
let timeInMilliseconds = 0; let timeInMilliseconds = 0;
let addMilliSeconds = (milliSecondsArg) => { let addMilliSeconds = (milliSecondsArg: number) => {
timeInMilliseconds = timeInMilliseconds + milliSecondsArg; timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
}; };
if (combinationArg.years) { if (combinationArg.years) {
@ -54,3 +56,7 @@ export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
return timeInMilliseconds; return timeInMilliseconds;
}; };
export const getMilliSecondsAsHumanReadableString = (milliSecondsArg: number): string => {
return plugins.prettyMs(milliSecondsArg);
}