<commit_before>var mercury = require("../index.js")
var h = mercury.h

var events = mercury.input(["height", "weight", "bmi"])
var bmiData = mercury.hash({
    height: mercury.value(180),
    weight: mercury.value(80),
    bmi: mercury.value(null)
})

function updateData(type, data) {
    bmiData[type].set(data.slider)
    bmiData[type === "bmi" ? "weight" : "bmi"].set(null)
}

events.height(updateData.bind(null, "height"))
events.weight(updateData.bind(null, "weight"))
events.bmi(updateData.bind(null, "bmi"))

function calcBmi(data) {
    var h = data.height / 100
    return {
        height: data.height,
        bmi: data.bmi ? data.bmi : data.weight / (h * h),
        weight: data.bmi ? data.bmi * (h * h) : data.weight
    }
}

function slider(value, sink, min, max) {
    return h("input", {
        type: "range", min: min, max: max, value: value,
        style: { width: "100%" }, name: "slider",
        "data-event": mercury.changeEvent(sink)
    })
}

function render(bmiData) {
    var values = calcBmi(bmiData)
    var color = values.bmi < 18.5 ? "orange" :
        values.bmi < 25 ? "inherit" :
        values.bmi < 30 ? "orange" : "red"
    var diagnose = values.bmi < 18.5 ? "underweight" :
        values.bmi < 25 ? "normal" :
        values.bmi < 30 ? "overweight" : "obese"

    return h("div", [
        h("h3", "BMI calculator"),
        h("div", [
            "Weight: " + ~~values.weight + "kg",
            mercury.partial(slider, values.weight, events.weight, 30, 150)
        ]),
        h("div", [
            "Height: " + ~~values.height + "cm",
            mercury.partial(slider, values.height,
                events.height, 100, 220)
        ]),
        h("div", [
            "BMI: " + ~~values.bmi + " ",
            h("span", { style: { color: color } }, diagnose),
            mercury.partial(slider, values.bmi, events.bmi, 10, 50)
        ])
    ])
}

mercury.app(document.body, bmiData, render)<commit_msg>prevent bmi sliders resetting<commit_after>var mercury = require("../index.js")
var h = mercury.h

var events = mercury.input(["height", "weight", "bmi"])
var bmiData = mercury.hash({
    height: mercury.value(180),
    weight: mercury.value(80),
    bmi: mercury.value(calcBmi(180, 80))
})

events.height(updateData.bind(null, "height"))
events.weight(updateData.bind(null, "weight"))
events.bmi(updateData.bind(null, "bmi"))

function updateData(type, data) {
    bmiData[type].set(data.slider)

    if (type !== "bmi") {
        bmiData.bmi.set(calcBmi(bmiData.height(), bmiData.weight()))
    } else {
        bmiData.weight.set(calcWeight(bmiData.height(), bmiData.bmi()))
    }
}

function calcWeight(height, bmi) {
    var h = height / 100
    return h * h * bmi
}

function calcBmi(height, weight) {
    var h = height / 100
    return weight / (h * h)
}

function slider(value, sink, min, max) {
    return h("input", {
        type: "range", min: min, max: max, value: value,
        style: { width: "100%" }, name: "slider",
        "data-event": mercury.changeEvent(sink)
    })
}

function render(values) {
    var color = values.bmi < 18.5 ? "orange" :
        values.bmi < 25 ? "inherit" :
        values.bmi < 30 ? "orange" : "red"
    var diagnose = values.bmi < 18.5 ? "underweight" :
        values.bmi < 25 ? "normal" :
        values.bmi < 30 ? "overweight" : "obese"

    return h("div", [
        h("h3", "BMI calculator"),
        h("div", [
            "Weight: " + ~~values.weight + "kg",
            mercury.partial(slider, values.weight, events.weight, 30, 150)
        ]),
        h("div", [
            "Height: " + ~~values.height + "cm",
            mercury.partial(slider, values.height,
                events.height, 100, 220)
        ]),
        h("div", [
            "BMI: " + ~~values.bmi + " ",
            h("span", { style: { color: color } }, diagnose),
            mercury.partial(slider, values.bmi, events.bmi, 10, 50)
        ])
    ])
}

mercury.app(document.body, bmiData, render)
{'fraction_non_alphanumeric': 0.1757910597689603, 'fraction_numerical': 0.022601707684580613, 'mean_word_length': 2.427710843373494, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2936796', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1372, 'n_words': 295}