nanohour
v1.1.1
Published
Nano Hour
Downloads
14
Readme
NanoHour
An integer representable time (military time).
Install
npm install nanohour
Imports
// ESM
import { decode, encode } from 'nanohour';
// CommonJS
const { decode, encode } = require('nanohour');
// Browser
import { decode, encode } from 'https://unpkg.com/nanohour'
Encode
Convert individual hour and minute to a single integer
const integer = encode(10, 30);
// => 1030
const integer = encode(0, 30);
// => 30
// automatically normalize inputs
// 90 minutes = 1 hour and 30 minutes
const integer = encode(0, 90);
// => 130
Decode
Convert an integer into individual hour and minute
const values = decode(1030);
// => [10, 30]
const values = decode(30);
// => [0, 30]
// automatically normalize inputs
// 90 minutes = 1 hours and 30 minutes
const values = decode(1090);
// => [11, 30]
Add hours
Add extra hours to an integer
const integer = addHours(1030, 1);
// => 1130
const integer = addHours(1030, -1);
// => 930
Add minutes
Add extra minutes to an integer
const integer = addHours(1030, 10);
// => 1140
const integer = addHours(1030, -10);
// => 1020
Between
Check if an integer is between
// 1030 is the value
const value = isBetween(1030, 930, 1130);
// => true
// 1030 is the value
const value = isBetween(1030, 1035, 1130);
// => false
// 1030 is the value
const value = isBetween(1030, 1130, 930);
// => true
Normalize
Sanitize dirty integer to be a valid one.
const integer = normalize(1030);
// => 1030
const integer = normalize(1090);
// => 1130
const integer = normalize(2400);
// => 0
API
export function normalize(time: number): number;
export function isBetween(
value: number,
timeLeft: number,
timeRight: number
): boolean;
export function addMinutes(time: number, minute: number): number;
export function addHours(time: number, hour: number): number;
export function decode(time: number): [number, number];
export function encode(hour?: number, minute?: number): number;