squiggle/packages/squiggle-lang/src/rescript/utility/Sparklines.res

29 lines
884 B
Plaintext
Raw Normal View History

// Port of Sindre Sorhus' Sparkly to Rescript
// reference implementation: https://github.com/sindresorhus/sparkly
// Omitting rgb "fire" style, so no `chalk` dependency
2022-03-29 19:27:23 +00:00
// Omitting: NaN handling, special consideration for constant data.
let create = (
2022-03-29 14:04:08 +00:00
numbers: array<float>,
~minimum=?,
~maximum=?,
()
) => {
2022-03-29 14:40:38 +00:00
let ticks = [`▁`, `▂`, `▃`, `▄`, `▅`, `▆`, `▇`, `█`]
2022-03-29 14:55:36 +00:00
let minimum = E.O.default(Js.Math.minMany_float(numbers), minimum)
let maximum = E.O.default(Js.Math.maxMany_float(numbers), maximum)
2022-03-29 15:05:27 +00:00
let toHeight = (number: float) => {
let tickIndex = Js.Math.ceil_int((number /. maximum) *. (ticks -> Belt.Array.length -> Belt.Int.toFloat)) - 1
2022-03-29 15:05:27 +00:00
let tickIndex = if maximum == 0.0 || tickIndex < 0 {
0
} else {
tickIndex
}
2022-03-29 15:05:27 +00:00
ticks[tickIndex]
}
2022-03-29 19:27:23 +00:00
toHeight -> E.A.fmap(numbers) -> (arr => E.A.joinWith("", arr))
}