add time constants
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2020-12-02 15:59:54 +01:00
parent 6dbd46bb06
commit 6c1fc0c044
6 changed files with 98 additions and 6 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/.nyc_output/ /.nyc_output/
/coverage/ /coverage/
*.log *.log
*.d.ts

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.2.0
- added timing constants
## 1.1.0 ## 1.1.0
- added all documented http status codes - added all documented http status codes

View File

@ -1,6 +1,6 @@
# @sapphirecode/consts # @sapphirecode/consts
version: 1.1.x version: 1.2.x
useful constant values that are used in many applications useful constant values that are used in many applications
@ -19,10 +19,35 @@ yarn:
there are many constants, too many to document by hand so I suggest you use there are many constants, too many to document by hand so I suggest you use
autocomplete or look into the source. autocomplete or look into the source.
Http status codes:
```js ```js
const {http} = require('@sapphirecode/consts'); const {http} = require('@sapphirecode/consts');
console.log(http.status_ok) // 200 console.log(http.status_ok); // 200
```
Time
```js
const {time} = require('@sapphirecode/consts');
console.log(time.base_ms.minute); // 60000
```
The time module contains a few precalculated bases like base_s, base_ms, base_us
and base_ns. Those allow you to use timers without calculating everything
first.
> timer(base_ms.week) instead of timer(1000 x 60 x 60 x 24 x 7) or
> timer(base_us.week) if your timer counts in microseconds
time intervals above one week are functions, because they require additional
information to be calculated
```js
console.log(time.base_ms.year(2020));
console.log(time.base_ms.year(12, 2020));
``` ```
## License ## License

View File

@ -9,11 +9,13 @@
'use strict'; 'use strict';
const http = require ('./http_status_codes'); const http = require ('./http_status_codes');
const time = require ('./time');
module.exports = { module.exports = {
http: { http: {
...http.status_codes, ...http.status_codes,
...http ...http
} },
time
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/consts", "name": "@sapphirecode/consts",
"version": "1.1.28", "version": "1.2.0",
"main": "index.js", "main": "index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",
@ -28,8 +28,7 @@
"files": [ "files": [
"LICENSE", "LICENSE",
"index.d.ts", "index.d.ts",
"index.js", "*.js"
"http_status_codes.js"
], ],
"keywords": [ "keywords": [
"constants", "constants",

61
time.js Normal file
View File

@ -0,0 +1,61 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of consts which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, December 2020
*/
/* eslint-disable sort-keys */
'use strict';
function is_leap (year) {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
function base (second = 1) {
const ms = second / 1000;
const us = ms / 1000;
const ns = us / 1000;
const minute = second * 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const year = (y) => {
const days = is_leap (y) ? 366 : 365;
return days * day;
};
const month = (m, y) => {
let days = 0;
if (m === 2)
days = is_leap (y) ? 29 : 28;
else if (m < 8)
days = 30 + (m % 2);
else
days = 31 - (m % 2);
return days * day;
};
return {
ns,
us,
ms,
second,
minute,
hour,
day,
week,
month,
year
};
}
module.exports = {
base_s: base (1),
base_ms: base (1e3),
base_us: base (1e6),
base_ns: base (1e9),
base
};