-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
61 lines (57 loc) · 1.74 KB
/
utils.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { settings } from './settings.ts'
const { API_DEBUGGING } = settings
/**
* Api Request helper.
* Simple wrapper around fetch to do easy logging
* @param {Request | URL | string} - input
* @param {RequestInit} - [init]
* @returns {Promise<Response>}
*/
export const apiRequest = async (input: Request | URL | string, init?: RequestInit) => {
if (API_DEBUGGING) {
console.log('API REQUEST:', { input, init })
}
const response = await fetch(input, init)
if (API_DEBUGGING) {
const responseClone = response.clone()
try {
const data = await responseClone.json()
console.log('API RESPONSE', { response: responseClone, data })
} catch (e) {
console.error(e)
console.log('API RESPONSE', { response: responseClone })
}
}
return response
}
type CreateLinearScaleData = {
minScale: number
maxScale: number
minValue: number
maxValue: number
}
/**
* Create Linear Scale
* Create a linear scale mapping one dimensions to another
* @param {CreateLinearScaleData} data
* @returns {(value: number) => number} scale
*/
export const createLinearScale = (data: CreateLinearScaleData) => {
const { maxScale, maxValue, minScale, minValue } = data
const ratio = (maxScale - minScale) / (maxValue - minValue)
return (value: number) => minScale + ratio * (value - minValue)
}
type CreateAndUseLinearScaleData = CreateLinearScaleData & {
value: number
}
/**
* Create And Use Linear Scale
* Create a linear scale mapping one dimensions to another and then use it with hte value
* @param {CreateAndUseLinearScaleData} data
* @returns {number} value - mapped to the scale
*/
export const createAndUseLinearScale = (data: CreateAndUseLinearScaleData) => {
const { value, ...rest } = data
const scale = createLinearScale(rest)
return scale(value)
}