translated sparkly to rescript, still debugging it tho

This commit is contained in:
Quinn Dougherty 2022-03-29 02:13:52 -04:00
parent b8159a88e0
commit 03318ee2e1
4 changed files with 53 additions and 8 deletions

View File

@ -2,7 +2,6 @@ open Jest
open Expect
open Js.Array
open SymbolicDist
open E.Sp
let makeTest = (~only=false, str, item1, item2) =>
only
@ -18,6 +17,6 @@ let forSparkline = (thisPdf, inps) => map(thisPdf, inps)
describe("normal combine", () => {
let pdf1 = x => Normal.pdf(x, normalParams1)
let forSparkline1 = forSparkline(pdf1, range20)
let x = forSparkline1 -> toString -> sparkly -> Js.Console.log
let x = forSparkline1 -> toString -> Sparklines.sparkly -> Js.Console.log
makeTest("Spark1", 1, 0)
})

View File

@ -24,8 +24,7 @@
"mathjs": "10.4.1",
"pdfast": "^0.2.0",
"rationale": "0.2.0",
"rescript": "^9.1.4",
"sparkly": "6.0.0"
"rescript": "^9.1.4"
},
"devDependencies": {
"@glennsl/rescript-jest": "^0.9.0",

View File

@ -441,7 +441,3 @@ module JsArray = {
|> Js.Array.map(Rationale.Option.toExn("Warning: This should not have happened"))
let filter = Js.Array.filter
}
module Sp = {
@module("sparkly") @scope("sparkly") external sparkly: string => string = "sparkly"
}

View File

@ -0,0 +1,51 @@
// Port of Sindre Sorhus' Sparkly to Rescript
// reference implementation: https://github.com/sindresorhus/sparkly
// Omitting rgb "fire" style, so no `chalk` dependency
type sparklyConfig = {
minimum: option<float>,
maximum: option<float>
}
let sparkly = (
numbers: list<float>,
~options = {minimum: None, maximum: None}
) => {
// if not numbers is not an array, throw typeerror "Expected an array"
// Unlike reference impl, we assume that all numbers are finite, i.e. no NaN.
let ticks = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]
let minimum = switch options.minimum {
| None => Js.Math.minimum(numbers)
| Some(x) => x
}
let maximum = switch options.maximum {
| None => Js.Math.maximum(numbers)
| Some(x) => x
}
// Use a high tick if data is constant and max is not equal
let ticks = if minimum == maximum && maximum != 0.0 {
[ticks[4]]
} else {
ticks
}
let toMapWith = number => {
let ret = if (! Js.Number.isFinite(number)) {
" "
} else {
let tickIndex = Js.Math.ceil((number / maximum) * ticks.length) - 1
let tickIndex = if maximum == 0.0 || tickIndex < 0 {
0
} else {
tickIndex
}
ticks[tickIndex]
}
ret
}
let ret = map(toMapWith, numbers)
Js.Array.joinWith("", ret)
}