diff --git a/.gitignore b/.gitignore index 86a60e9..a7056d5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /.nyc_output/ /coverage/ *.log +*.d.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1968d..0ee3b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.2.0 + +- added timing constants + ## 1.1.0 - added all documented http status codes diff --git a/README.md b/README.md index f9bba4b..ca1dbb6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # @sapphirecode/consts -version: 1.1.x +version: 1.2.x 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 autocomplete or look into the source. +Http status codes: + ```js 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 diff --git a/index.js b/index.js index 0053084..4c24901 100644 --- a/index.js +++ b/index.js @@ -9,11 +9,13 @@ 'use strict'; const http = require ('./http_status_codes'); +const time = require ('./time'); module.exports = { http: { ...http.status_codes, ...http - } + }, + time }; diff --git a/package.json b/package.json index 5bf202f..76b9d2a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sapphirecode/consts", - "version": "1.1.28", + "version": "1.2.0", "main": "index.js", "author": { "name": "Timo Hocker", @@ -28,8 +28,7 @@ "files": [ "LICENSE", "index.d.ts", - "index.js", - "http_status_codes.js" + "*.js" ], "keywords": [ "constants", diff --git a/time.js b/time.js new file mode 100644 index 0000000..d904fd1 --- /dev/null +++ b/time.js @@ -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 , 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 +};