Compare commits
64 Commits
Author | SHA1 | Date | |
---|---|---|---|
d16fd8b69b | |||
f7ed88cd57 | |||
19b5ba4a5d | |||
457182a97a | |||
a7a961c869 | |||
f1d7771e30 | |||
ff16b70206 | |||
8e43ce7e9b | |||
f129ce42b6 | |||
56a802664f | |||
743c9fb847 | |||
ac2edd6d74 | |||
725102d02c | |||
7c73579c9f | |||
a1996ea1bd | |||
dc92ae7140 | |||
3e862f233c | |||
73f7360f8c | |||
2c74de8aef | |||
78498626ea | |||
b2299b149a | |||
885b2432a8 | |||
7a231b7686 | |||
ef6e21080e | |||
3ea8b1564d | |||
1b92240c4e | |||
95bf558503 | |||
d1ff77d9f5 | |||
b77677b089 | |||
5ebd9ce6de | |||
702365aba3 | |||
93778e1bcf | |||
4d0fb3eb30 | |||
3f0b230d23 | |||
0c0b08e2be | |||
dd722146f4 | |||
c968e156ae | |||
b5509711e7 | |||
f8540b8076 | |||
30d18db4af | |||
388649659c | |||
e8ff289a9a | |||
a5deb25bfb | |||
ce2d1b278b | |||
aa16e5d5bb | |||
f221e9c0fa | |||
9935396f2d | |||
1bc379daa5 | |||
f9b81fc801 | |||
6d8e39f11b | |||
cff5b5a7f4 | |||
f6be9b71f1 | |||
9c2d584170 | |||
10bff069c1 | |||
8f6272eca4 | |||
f02df7789c | |||
d720b8b084 | |||
6bca8557f9 | |||
f0a5e18335 | |||
57fbdb4a70 | |||
4a56527f7d | |||
2a14d928a8 | |||
f161f71c4e | |||
ed1f79caa4 |
17
.gitignore
vendored
17
.gitignore
vendored
@ -1,5 +1,20 @@
|
|||||||
.nogit/
|
.nogit/
|
||||||
node_modules/
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
139
.gitlab-ci.yml
139
.gitlab-ci.yml
@ -1,16 +1,16 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
@ -18,108 +18,114 @@ stages:
|
|||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g snyk
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --production --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
auditDevDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
testSTABLE:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# metadata stage
|
# metadata stage
|
||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
only:
|
||||||
- docker:stable-dind
|
- tags
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--volume "$PWD":/code
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- priv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g npmpage
|
- npmci node install lts
|
||||||
- npmci command npmpage
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -127,4 +133,5 @@ pages:
|
|||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
21
license
Normal file
21
license
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Lossless GmbH (hello@lossless.com)
|
||||||
|
|
||||||
|
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.
|
@ -1,8 +1,17 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"npmts"
|
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smarttime",
|
||||||
|
"shortDescription": "handle time in smart ways",
|
||||||
|
"npmPackagename": "@pushrocks/smarttime",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
10995
package-lock.json
generated
10995
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
48
package.json
48
package.json
@ -1,23 +1,47 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.2",
|
"version": "3.0.30",
|
||||||
"description": "handle timeformats in smart ways",
|
"description": "handle time in smart ways",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tsrun ./test/test.ts)",
|
"test": "(tstest ./test)",
|
||||||
"build": "(npmts)"
|
"build": "(tsbuild --web && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.1.2",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@types/node": "^10.5.2",
|
"@gitzone/tsbundle": "^1.0.78",
|
||||||
"tapbundle": "^2.0.2"
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
|
"@gitzone/tstest": "^1.0.44",
|
||||||
|
"@pushrocks/tapbundle": "^3.2.9",
|
||||||
|
"@types/node": "^14.6.2",
|
||||||
|
"tslint": "^6.1.3",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/lik": "^4.0.17",
|
||||||
"moment": "^2.21.0"
|
"@pushrocks/smartdelay": "^2.0.10",
|
||||||
}
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
|
"croner": "^1.1.23",
|
||||||
|
"dayjs": "^1.8.35",
|
||||||
|
"is-nan": "^1.3.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
72
readme.md
72
readme.md
@ -1,33 +1,63 @@
|
|||||||
# 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)
|
||||||
[](https://www.npmjs.com/package/smarttime)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smarttime/)
|
||||||
[](https://GitLab.com/pushrocks/smarttime)
|
|
||||||
[](https://github.com/pushrocks/smarttime)
|
|
||||||
[](https://pushrocks.gitlab.io/smarttime/)
|
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
|
|
||||||
[](https://GitLab.com/pushrocks/smarttime/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://GitLab.com/pushrocks/smarttime/commits/master)
|
-- | --
|
||||||
[](https://www.npmjs.com/package/smarttime)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://david-dm.org/pushrocks/smarttime)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://www.bithound.io/github/pushrocks/smarttime/master/dependencies/npm)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://www.bithound.io/github/pushrocks/smarttime)
|
Snyk | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
TypeScript Support | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](http://standardjs.com/)
|
Code Style | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
## 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 CronManager
|
||||||
|
|
||||||
|
This class provides scheduling of functions with a cron syntax
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { CronManager } from '@pushrocks/smarrtime';
|
||||||
|
const cronManagerInstance = new CronManager();
|
||||||
|
cronManagerInstance.addConrjob('* * * * * *', async () => {
|
||||||
|
console.log('hello'); // will log 'hello' to console once every second;
|
||||||
|
});
|
||||||
|
cronManagerInstance.start();
|
||||||
|
```
|
||||||
|
|
||||||
|
### class ExtendedDate
|
||||||
|
|
||||||
|
This class offers static functions to create zone specific JavaScript dates from European formated time strings.
|
||||||
|
|
||||||
|
```TypeScript
|
||||||
|
import { ExtendedDate } from '@pushrocks/smarttime'
|
||||||
|
const myDate: Date = ExtendedDate.fromEuropeanDate('8.6.2018')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contribution
|
||||||
|
|
||||||
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://maintainedby.lossless.com)
|
||||||
|
27
test/test.cronmanager.browser.ts
Normal file
27
test/test.cronmanager.browser.ts
Normal 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('*/2 * * * * *', async () => {
|
||||||
|
if (counter === 10) {
|
||||||
|
done.resolve();
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
console.log(`hey ${counter}`);
|
||||||
|
});
|
||||||
|
testCronManager.start();
|
||||||
|
await done.promise;
|
||||||
|
testCronManager.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
49
test/test.cronmanager.ts
Normal file
49
test/test.cronmanager.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
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();
|
||||||
|
const done2 = tools.defer();
|
||||||
|
const done3 = tools.defer();
|
||||||
|
let counter = 0;
|
||||||
|
let counter2 = 0;
|
||||||
|
let counter3 = 0;
|
||||||
|
const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => {
|
||||||
|
if (counter === 10) {
|
||||||
|
testCronManager.removeCronjob(cronJob);
|
||||||
|
done.resolve();
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
console.log(`${new Date().getSeconds()} hey ${counter} -> runs every 2 seconds`);
|
||||||
|
});
|
||||||
|
const cronJob2 = testCronManager.addCronjob('*/3 * * * * *', async () => {
|
||||||
|
if (counter2 === 10) {
|
||||||
|
testCronManager.removeCronjob(cronJob2);
|
||||||
|
done2.resolve();
|
||||||
|
}
|
||||||
|
counter2++;
|
||||||
|
console.log(`${new Date().getSeconds()} hey ${counter2} -> runs every 3 seconds`);
|
||||||
|
});
|
||||||
|
const cronJob3 = testCronManager.addCronjob('*/4 * * * * *', async () => {
|
||||||
|
if (counter3 === 10) {
|
||||||
|
done3.resolve();
|
||||||
|
}
|
||||||
|
counter3++;
|
||||||
|
console.log(`${new Date().getSeconds()} hey ${counter3} -> runs every 4 seconds`);
|
||||||
|
});
|
||||||
|
testCronManager.start();
|
||||||
|
await done.promise;
|
||||||
|
await done2.promise;
|
||||||
|
await done3.promise;
|
||||||
|
testCronManager.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
24
test/test.extendeddate.ts
Normal file
24
test/test.extendeddate.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as smarttime from '../ts/index';
|
||||||
|
|
||||||
|
tap.test('should create a valid JavaScript Date from European TimeStamp', async () => {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a European date string', async () => {
|
||||||
|
const extendedDate = smarttime.ExtendedDate.fromHyphedDate('2018-02-13');
|
||||||
|
expect(extendedDate.exportToEuropeanDate()).to.equal('13.02.2018');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
@ -1,5 +1,5 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import { Timer } from '../ts/index';
|
import { Timer } from '../ts/index';
|
||||||
|
|
||||||
@ -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();
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smarttime from '../ts/index';
|
import * as smarttime from '../ts/index';
|
||||||
|
|
||||||
// Test TimeStamp class
|
// Test TimeStamp class
|
||||||
let testTimeStamp: smarttime.TimeStamp;
|
let testTimeStamp: smarttime.TimeStamp;
|
||||||
let testTimeStamp2: smarttime.TimeStamp;
|
let testTimeStamp2: smarttime.TimeStamp;
|
||||||
tap.test('should create valid testTimeStamp instance', async tools => {
|
tap.test('should create valid testTimeStamp instance', async (tools) => {
|
||||||
testTimeStamp = new smarttime.TimeStamp();
|
testTimeStamp = new smarttime.TimeStamp();
|
||||||
await tools.delayFor(2);
|
await tools.delayFor(2);
|
||||||
testTimeStamp2 = new smarttime.TimeStamp();
|
testTimeStamp2 = new smarttime.TimeStamp();
|
||||||
@ -23,7 +23,7 @@ tap.test('should have valid linuxtime', async () => {
|
|||||||
let testHrtMeasurement: smarttime.HrtMeasurement;
|
let testHrtMeasurement: smarttime.HrtMeasurement;
|
||||||
|
|
||||||
// Test HrtMeasurement
|
// Test HrtMeasurement
|
||||||
tap.test('should create valid HrtMeasurements', async tools => {
|
tap.test('should create valid HrtMeasurements', async (tools) => {
|
||||||
testHrtMeasurement = new smarttime.HrtMeasurement();
|
testHrtMeasurement = new smarttime.HrtMeasurement();
|
||||||
testHrtMeasurement.start();
|
testHrtMeasurement.start();
|
||||||
await tools.delayFor(20);
|
await tools.delayFor(20);
|
||||||
@ -41,7 +41,7 @@ tap.test('should combine units', async () => {
|
|||||||
weeks: 2,
|
weeks: 2,
|
||||||
days: 2,
|
days: 2,
|
||||||
hours: 2,
|
hours: 2,
|
||||||
minutes: 2
|
minutes: 2,
|
||||||
});
|
});
|
||||||
// tslint:disable-next-line:no-console
|
// tslint:disable-next-line:no-console
|
||||||
console.log(computedTime);
|
console.log(computedTime);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
export * from './smarttime.classes.cronmanager';
|
||||||
|
export * from './smarttime.classes.cronjob';
|
||||||
|
export * from './smarttime.classes.extendeddate';
|
||||||
export * from './smarttime.classes.hrtmeasurement';
|
export * from './smarttime.classes.hrtmeasurement';
|
||||||
|
export * from './smarttime.classes.interval';
|
||||||
export * from './smarttime.classes.timer';
|
export * from './smarttime.classes.timer';
|
||||||
export * from './smarttime.classes.timestamp';
|
export * from './smarttime.classes.timestamp';
|
||||||
export * from './smarttime.units';
|
export * from './smarttime.units';
|
||||||
|
|
||||||
export { moment } from './smarttime.plugins';
|
|
||||||
|
54
ts/smarttime.classes.cronjob.ts
Normal file
54
ts/smarttime.classes.cronjob.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
import { CronManager } from './smarttime.classes.cronmanager';
|
||||||
|
|
||||||
|
export type TJobFunction = (() => void) | (() => Promise<any>);
|
||||||
|
|
||||||
|
export class CronJob {
|
||||||
|
public croner;
|
||||||
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
|
public cronExpression: string;
|
||||||
|
public jobFunction: TJobFunction;
|
||||||
|
private nextExecutionUnix: number = 0;
|
||||||
|
|
||||||
|
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction) {
|
||||||
|
this.cronExpression = cronExpressionArg;
|
||||||
|
this.jobFunction = jobFunction;
|
||||||
|
this.croner = plugins.croner(this.cronExpression);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checks wether the cronjob needs to be executed
|
||||||
|
*/
|
||||||
|
public checkExecution(): number {
|
||||||
|
if (this.nextExecutionUnix === 0) {
|
||||||
|
this.getNextExecutionTime();
|
||||||
|
}
|
||||||
|
if (Date.now() > this.nextExecutionUnix) {
|
||||||
|
const maybePromise = this.jobFunction();
|
||||||
|
if (maybePromise instanceof Promise) {
|
||||||
|
maybePromise.catch(e => console.log(e));
|
||||||
|
}
|
||||||
|
this.nextExecutionUnix = this.getNextExecutionTime();
|
||||||
|
}
|
||||||
|
return this.nextExecutionUnix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getNextExecutionTime() {
|
||||||
|
return this.nextExecutionUnix = Date.now() + this.getTimeToNextExecution();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the time to next execution
|
||||||
|
*/
|
||||||
|
public getTimeToNextExecution() {
|
||||||
|
return this.croner.msToNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.status = 'started';
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop() {
|
||||||
|
this.status = 'stopped';
|
||||||
|
}
|
||||||
|
}
|
74
ts/smarttime.classes.cronmanager.ts
Normal file
74
ts/smarttime.classes.cronmanager.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
import { CronJob } from './smarttime.classes.cronjob';
|
||||||
|
|
||||||
|
export class CronManager {
|
||||||
|
public executionTimeout: plugins.smartdelay.Timeout<void>;
|
||||||
|
|
||||||
|
public status: 'started' | 'stopped' = 'stopped';
|
||||||
|
public cronjobs = new plugins.lik.ObjectMap<CronJob>();
|
||||||
|
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => Promise<void>) {
|
||||||
|
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
||||||
|
this.cronjobs.add(newCronJob);
|
||||||
|
if (this.status === 'started') {
|
||||||
|
newCronJob.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
return newCronJob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public removeCronjob(cronjobArg: CronJob) {
|
||||||
|
cronjobArg.stop();
|
||||||
|
this.cronjobs.remove(cronjobArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* starts the cronjob
|
||||||
|
*/
|
||||||
|
public start() {
|
||||||
|
if (this.status !== 'started') {
|
||||||
|
this.status = 'started';
|
||||||
|
for (const cronJob of this.cronjobs.getArray()) {
|
||||||
|
cronJob.start();
|
||||||
|
}
|
||||||
|
const runCronCycle = async () => {
|
||||||
|
this.executionTimeout = new plugins.smartdelay.Timeout(0);
|
||||||
|
do {
|
||||||
|
let nextRunningCronjob: CronJob;
|
||||||
|
for (const cronJob of this.cronjobs.getArray()) {
|
||||||
|
cronJob.checkExecution();
|
||||||
|
if (
|
||||||
|
!nextRunningCronjob ||
|
||||||
|
cronJob.getTimeToNextExecution() < nextRunningCronjob.getTimeToNextExecution()
|
||||||
|
) {
|
||||||
|
nextRunningCronjob = cronJob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution());
|
||||||
|
console.log(
|
||||||
|
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
||||||
|
);
|
||||||
|
await this.executionTimeout.promise;
|
||||||
|
} while (this.status === 'started');
|
||||||
|
};
|
||||||
|
runCronCycle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stops all cronjobs
|
||||||
|
*/
|
||||||
|
public stop() {
|
||||||
|
if (this.status === 'started') {
|
||||||
|
this.status = 'stopped';
|
||||||
|
this.executionTimeout.cancel();
|
||||||
|
} else {
|
||||||
|
console.log(`You tried to stop a CronManager that was not actually started.`);
|
||||||
|
}
|
||||||
|
for (const cron of this.cronjobs.getArray()) {
|
||||||
|
cron.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
|
124
ts/smarttime.classes.extendeddate.ts
Normal file
124
ts/smarttime.classes.extendeddate.ts
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export type TAvailableZone = 'Europe/Berlin';
|
||||||
|
|
||||||
|
export interface IDateUnits {
|
||||||
|
year: number;
|
||||||
|
yearString: string;
|
||||||
|
month: number;
|
||||||
|
monthString: string;
|
||||||
|
monthName: string;
|
||||||
|
day: number;
|
||||||
|
dayString: string;
|
||||||
|
dayOfTheWeek: number;
|
||||||
|
dayOfTheWeekName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ExtendedDate extends Date {
|
||||||
|
// STATIC factories
|
||||||
|
public static fromMillis(milliSeconds: number) {
|
||||||
|
return new ExtendedDate(milliSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static fromDate(dateArg: Date) {
|
||||||
|
return new ExtendedDate(dateArg.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static fromEuropeanDate(europeanDate: string) {
|
||||||
|
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
||||||
|
const date = new Date(
|
||||||
|
parseFloat(dateArray[3]), // year
|
||||||
|
parseFloat(dateArray[2]) - 1, // month
|
||||||
|
parseFloat(dateArray[1]) // day
|
||||||
|
);
|
||||||
|
const unixMilli = date.getTime();
|
||||||
|
return new ExtendedDate(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates an Extended date from a hypedDate like "2018-03-28"
|
||||||
|
* @param dateString
|
||||||
|
*/
|
||||||
|
public static fromHyphedDate(dateString: string) {
|
||||||
|
// guards
|
||||||
|
// implementation
|
||||||
|
const dateMillis = new Date(dateString).getTime();
|
||||||
|
return new ExtendedDate(dateMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as .fromEuropeanDate(), but accepts additional timeArg and zoneArg
|
||||||
|
*/
|
||||||
|
public static fromEuropeanDateAndTime(
|
||||||
|
europeanDateArg: string,
|
||||||
|
timeArg: string = '12:00:00',
|
||||||
|
zoneArg: TAvailableZone = 'Europe/Berlin'
|
||||||
|
) {
|
||||||
|
// guards
|
||||||
|
|
||||||
|
// implementation
|
||||||
|
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 date = plugins.dayjs(dateTimeString);
|
||||||
|
const unixMilli = date.toDate().getTime();
|
||||||
|
return new ExtendedDate(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public timezone: TAvailableZone;
|
||||||
|
|
||||||
|
constructor(unixMilli: number) {
|
||||||
|
super(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
public exportToEuropeanDate() {
|
||||||
|
const units = this.exportToUnits();
|
||||||
|
return `${units.dayString}.${units.monthString}.${units.yearString}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* exports units
|
||||||
|
*/
|
||||||
|
public exportToUnits(): IDateUnits {
|
||||||
|
const monthsArray = [
|
||||||
|
'January',
|
||||||
|
'February',
|
||||||
|
'March',
|
||||||
|
'April',
|
||||||
|
'May',
|
||||||
|
'June',
|
||||||
|
'July',
|
||||||
|
'August',
|
||||||
|
'September',
|
||||||
|
'October',
|
||||||
|
'November',
|
||||||
|
'December',
|
||||||
|
];
|
||||||
|
const daysArray = [
|
||||||
|
'Monday',
|
||||||
|
'Tuesday',
|
||||||
|
'Wednesday',
|
||||||
|
'Thursday',
|
||||||
|
'Friday',
|
||||||
|
'Saturday',
|
||||||
|
'Sunday',
|
||||||
|
];
|
||||||
|
return {
|
||||||
|
year: this.getFullYear(),
|
||||||
|
yearString: `${this.getFullYear()}`,
|
||||||
|
month: this.getMonth() + 1,
|
||||||
|
monthString: ('0' + (this.getMonth() + 1)).slice(-2),
|
||||||
|
monthName: monthsArray[this.getMonth()],
|
||||||
|
day: this.getDate(),
|
||||||
|
dayString: ('0' + this.getDate()).slice(-2),
|
||||||
|
dayOfTheWeek: this.getDay(),
|
||||||
|
dayOfTheWeekName: daysArray[this.getDay()],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,45 +1,43 @@
|
|||||||
import * as process from 'process';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* easy high resolution time measurement
|
* easy high resolution time measurement
|
||||||
*/
|
*/
|
||||||
export class HrtMeasurement {
|
export class HrtMeasurement {
|
||||||
nanoSeconds: number = null;
|
public nanoSeconds: number = null;
|
||||||
milliSeconds: number = null;
|
public milliSeconds: number = null;
|
||||||
private _hrTimeStart = null;
|
private _milliStart: number = null;
|
||||||
private _hrTimeStopDiff = null;
|
private _milliDiff: number = null;
|
||||||
private _started: boolean = false;
|
private _started: boolean = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start the measurement
|
* start the measurement
|
||||||
*/
|
*/
|
||||||
start() {
|
public start() {
|
||||||
this._started = true;
|
this._started = true;
|
||||||
this._hrTimeStart = process.hrtime();
|
this._milliStart = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stop the measurement
|
* stop the measurement
|
||||||
*/
|
*/
|
||||||
stop() {
|
public stop() {
|
||||||
if (this._started === false) {
|
if (this._started === false) {
|
||||||
console.log("Hasn't started yet");
|
console.log("Hasn't started yet");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._hrTimeStopDiff = process.hrtime(this._hrTimeStart);
|
this._milliDiff = Date.now() - this._milliStart;
|
||||||
this.nanoSeconds = this._hrTimeStopDiff[0] * 1e9 + this._hrTimeStopDiff[1];
|
this.nanoSeconds = this._milliDiff * 1000;
|
||||||
this.milliSeconds = this.nanoSeconds / 1000000;
|
this.milliSeconds = this._milliDiff;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reset the measurement
|
* reset the measurement
|
||||||
*/
|
*/
|
||||||
reset() {
|
public reset() {
|
||||||
this.nanoSeconds = null;
|
this.nanoSeconds = null;
|
||||||
this.milliSeconds = null;
|
this.milliSeconds = null;
|
||||||
this._hrTimeStart = null;
|
this._milliStart = null;
|
||||||
this._hrTimeStopDiff = null;
|
this._milliDiff = null;
|
||||||
this._started = false;
|
this._started = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
ts/smarttime.classes.interval.ts
Normal file
43
ts/smarttime.classes.interval.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export class Interval {
|
||||||
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
|
private statusAuthorization: any = null;
|
||||||
|
|
||||||
|
// timings
|
||||||
|
public intervalMilliseconds: number;
|
||||||
|
public nextIntervalMillisenconds: number;
|
||||||
|
|
||||||
|
public intervalJobs: Array<() => any> = [];
|
||||||
|
constructor(intervalMillisencondsArg: number) {
|
||||||
|
this.intervalMilliseconds = intervalMillisencondsArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.status = 'started';
|
||||||
|
const statusAuth = new Date();
|
||||||
|
this.statusAuthorization = statusAuth;
|
||||||
|
const runInterval = async () => {
|
||||||
|
while (this.status === 'started' && this.statusAuthorization === statusAuth) {
|
||||||
|
await plugins.smartdelay.delayFor(this.intervalMilliseconds);
|
||||||
|
this.executeIntervalJobs();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
runInterval();
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop() {
|
||||||
|
this.status = 'stopped';
|
||||||
|
this.statusAuthorization = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addIntervalJob(funcArg: () => any) {
|
||||||
|
this.intervalJobs.push(funcArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private executeIntervalJobs() {
|
||||||
|
for (const funcArg of this.intervalJobs) {
|
||||||
|
funcArg();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -61,8 +61,11 @@ export class Timer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public pause() {
|
public pause() {
|
||||||
clearTimeout(this.currentTimeout);
|
if (this.startedAt) {
|
||||||
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
|
clearTimeout(this.currentTimeout);
|
||||||
|
this.currentTimeout = null;
|
||||||
|
this.pausedAt = TimeStamp.fromTimeStamp(this.startedAt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public resume() {
|
public resume() {
|
||||||
@ -70,6 +73,14 @@ export class Timer {
|
|||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
import * as moment from 'moment';
|
// @pushrocks scope
|
||||||
|
import * as lik from '@pushrocks/lik';
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export { moment, smartpromise };
|
export { lik, smartdelay, smartpromise };
|
||||||
|
|
||||||
|
// third parties
|
||||||
|
import croner from 'croner';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
|
export { croner, dayjs };
|
||||||
|
@ -16,7 +16,7 @@ export let units = {
|
|||||||
},
|
},
|
||||||
minutes: (timesArg = 1) => {
|
minutes: (timesArg = 1) => {
|
||||||
return timesArg * 60000;
|
return timesArg * 60000;
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IUnitCombinationArg {
|
export interface IUnitCombinationArg {
|
||||||
@ -30,7 +30,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) => {
|
||||||
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
|
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
|
||||||
};
|
};
|
||||||
if (combinationArg.years) {
|
if (combinationArg.years) {
|
||||||
|
19
tslint.json
19
tslint.json
@ -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"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user