From b4ede1eb7b63a15ce2d34d1803cdcedddc5b4120 Mon Sep 17 00:00:00 2001 From: Adam Fanello Date: Sun, 15 Mar 2020 16:06:13 -0700 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ README.md | 37 +++++++++++++++++++++++++++++++++++++ package.json | 13 +++++++++++++ temporal-constants.js | 13 +++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 package.json create mode 100644 temporal-constants.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6ef218 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea + diff --git a/README.md b/README.md new file mode 100644 index 0000000..f37bbed --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Temporal Constants +Constants to compute with time units without magic numbers. + +## Why? + +How often do you have to do some calculations to convert seconds to milliseconds, hours to days, +or any number of others? Does everyone looking at your code understand why you are multipling +a number of days by 86400? + +We use these constant values in computations all the time in code, +yet we also know that magic numbers are to be avoided: + +> Unique values with unexplained meaning or multiple occurrences which could (preferably) be replaced with named constants +> -- [Wikipedia](https://en.wikipedia.org/wiki/Magic_number_(programming)) + +This tiny library defines those constants for you, so now instead of: + +`Math.round(ms / 60000)`, you can write `Math.round(ms / MillisecondsInMinute)` + +Or in place of: `setTimer(() => runMyFunction(), 24*60*60*1000)` + +you get the clearer (and faster): `setTimer(() => runMyFunction(), MillisecondsInDay)`. + +## FAQ + +*Q:* Why should I introduce another dependency for a few constants that I can define myself? + +*A:* Have you defined them? Did you make any mistakes computing them? + +If you don't want a dependency, feel free to copy the lines into your own source. +In this case, the times they are **not** a-changin'. + +## See also.... + +- [temporal-types](https://github.com/kernwig/temporal-types) - Typescript types for time units +- [js-joda](https://js-joda.github.io/js-joda/) - Complete library for date and time representation and manipulation + diff --git a/package.json b/package.json new file mode 100644 index 0000000..9759564 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "temporal-constants", + "version": "1.0.0", + "description": "Constants to compute with time units without magic numbers.", + "main": "temporal-constants.js", + "keywords": [ + "time", + "constant", + "temporal" + ], + "author": "Adam Fanello", + "license": "MIT" +} diff --git a/temporal-constants.js b/temporal-constants.js new file mode 100644 index 0000000..cd9b468 --- /dev/null +++ b/temporal-constants.js @@ -0,0 +1,13 @@ +// Covert time units without magic numbers +export const MillisecondsInSecond = 1000; +export const MillisecondsInMinute = 60000; +export const MillisecondsInHour = 3600000; +export const MillisecondsInDay = 86400000; +export const SecondsInMinute = 60; +export const SecondsInHour = 3600; +export const SecondsInDay = 86400; +export const MinutesInHour = 60; +export const MinutesInDay = 1440; +export const HoursInDay = 24; +export const DaysInWeek = 7; +export const MonthsInYear = 12;