-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpmotd.js
31 lines (24 loc) · 839 Bytes
/
bpmotd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
const { createHash } = require('crypto');
const sha256 = (input) =>
createHash('sha256').update(input).digest('hex');
const toUniformFloat = (input) =>
parseInt(sha256(input).slice(0, 4), 16) / 0x10000;
const getCurrentDate = () =>
(new Date()).toISOString().replace(/T.*/, '');
/**
* Generate a tempo in beats per minute (BPM) for the given ISO 8601 date.
* The result will be an integer uniformly distributed in the range [80, 160).
*/
const getBPMForDay = (day) =>
Math.floor(80 * (1 + toUniformFloat(day)));
/**
* Generate a tempo in beats per minute (BPM) for today (UTC offset zero).
* The result will be an integer uniformly distributed in the range [80, 160).
*/
const getBPMForToday = () =>
getBPMForDay(getCurrentDate());
module.exports = {
getBPMForDay,
getBPMForToday,
};