Published
Edited
Aug 17, 2019
Insert cell
md`# Tests`
Insert cell
import { jit } from '@elisk/ji'
Insert cell
btc = jit`

₿ ⨕ ${['btc', coinCalc()]}
聡 ⨕ ${['satoshi', coinCalc(false, ['Satoshi', Math.pow(10, 8), '0,0', false, Math.floor])]}
`
Insert cell
coinCalc = (arate = ['USD', 10000, '0,0.00'], factor = ['BTC', 1, '0,0.0000']) =>
(pleft, right) => async (state) => {
const d3 = await require("d3@5")
const nb = await require('numbro')
const btcticker = await d3.json('https://api.coindesk.com/v1/bpi/currentprice.json')
const [ uindex, urate, uformat ] = arate || []
const [ fcode, fval, fformat, fsymbol, umodifier ] = factor || []
const left = pleft.replace('!', '').replace('µ', '')
const isLiteral = pleft[0] === '!'
const isMilli = pleft[pleft.length - 1] === 'µ'
const getIndex = index => index && ('' + index).toUpperCase() in btcticker.bpi
&& ('' + index).toUpperCase()
const indexRight = getIndex(right)
const indexLeft = getIndex(left)
const defaultIndex = getIndex(uindex)
const index = indexRight || indexLeft || defaultIndex || 'USD'
const product = ((left && _.isFinite(parseFloat(left))) ? parseFloat(left) : 1) * btcticker.bpi[index].rate_float

const numRight = right && _.isFinite(parseFloat(right)) && parseFloat(right)
const numLeft = !!left && _.isFinite(parseFloat(left)) && parseFloat(left)

const producti = (left && _.isFinite(parseFloat(left)) && parseFloat(left))
|| (right && _.isFinite(parseFloat(right)) && parseFloat(right))
const mag = fval / (isMilli ? 0.001 : 1)
const rate = btcticker.bpi[index].rate_float
const result = numLeft
? (numLeft * (rate / mag))
: (numRight
? ((umodifier && typeof umodifier === 'function')
? umodifier((numRight / rate) * mag)
: (numRight / rate) * mag
)
: rate / factor[1]
)

const suffix = numLeft
? index
: (numRight
? (isMilli ? (fsymbol ? 'µ' : 'm') : '') + (fsymbol || fcode)
: index)
console.log(`🏵`, {
factor: {
factor,
uindex, urate, uformat
},
arate: {
arate,
fcode, fval, fformat, fsymbol, umodifier
},
left: {
left,
isLiteral,
isMilli
},
index: {
indexRight,
indexLeft,
defaultIndex,
index
},
product,
numRight,
numLeft,
producti,
mag,
rate,
result,
suffix
})
if (isLiteral) return result

const output = `<span title="${numLeft || numRight || '1.0000'} ${((!numLeft && !numRight) && fcode) || (numRight && index) || (numLeft && (fsymbol || fcode))} equals">${nb(result).format(numLeft ? (uformat || '0,0.00') : (fformat || '0,0'))} ${suffix}</span>`
return output
}
Insert cell
_ = require('lodash')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more