Commit 7457ccd0 by Mathieu Giraud

js/tools.js, tests: nice_min_max_steps()

```Simultaneously find nice (min, max, step, nb_steps) values.
See #2699.```
parent 57ff9d65
 ... ... @@ -357,6 +357,40 @@ function nice_floor(x, force_pow10) } /** * Give nice min/max/step numbers including the given [min, max] interval in order that steps are also nice, * See examples in tools_test.js * nice_min_max_steps(0.03, 19.24, 5) -> {min: 0, max: 20, step: 4} * nice_min_max_steps(0, 7, 4) -> {min: 0, max:8, step: 2} **/ function nice_min_max_steps(min, max, nb_max_steps) { var basic_step = nice_1_2_5_ceil((max - min) / nb_max_steps) var n_min = nice_floor(min, basic_step) var n_max = nice_ceil(max, basic_step) var step = nice_1_2_5_ceil((n_max - n_min) / nb_max_steps) var nb_steps = Math.ceil((n_max - n_min) / step) // In some rare cases, we try another loop of rounding var overlength = nb_steps * step - (n_max - n_min) if (overlength) { n_min = nice_floor(min, step) n_max = nice_ceil(max, step) nb_steps = Math.ceil((n_max - n_min) / step) } return {min: n_min, max: n_max, step: step, nb_steps: nb_steps} } /** * Give a minimial number of digits to represent 'x' with at least 'sd' significant digits * nice_number_digits(14.5, 2) -> 1 ... ...
 ... ... @@ -58,6 +58,22 @@ QUnit.test("test rounding functions", function(assert) { } ); QUnit.test("test nice_min_max_steps", function(assert) { assert.deepEqual(nice_min_max_steps(347.34, 354.23, 10), {min: 347, max: 355, step: 1, nb_steps: 8}, "347.34..354.23 (10)"); assert.deepEqual(nice_min_max_steps(17, 305, 20), {min: 0, max: 320, step: 20, nb_steps: 16}, "17..305 (20)"); assert.deepEqual(nice_min_max_steps(17, 305, 10), {min: 0, max: 350, step: 50, nb_steps: 7}, "17..305 (10)"); assert.deepEqual(nice_min_max_steps(17, 305, 4), {min: 0, max: 400, step: 100, nb_steps: 4}, "17..305 (4)"); assert.deepEqual(nice_min_max_steps(190, 310, 15), {min: 190, max: 310, step: 10, nb_steps: 12}, "190..310 (15)"); assert.deepEqual(nice_min_max_steps(190, 310, 3), {min: 150, max: 350, step: 100, nb_steps: 2}, "190..310 (3)"); assert.deepEqual(nice_min_max_steps(0.03, 19.24, 5), {min: 0, max: 20, step: 5, nb_steps: 4}, "0.03..19.24 (5)"); assert.deepEqual(nice_min_max_steps(0, 7, 4), {min: 0, max: 8, step: 2, nb_steps: 4}, "0..7 (4)"); assert.deepEqual(nice_min_max_steps(43, 103, 5), {min: 40, max: 120, step: 20, nb_steps: 4}, "43..103 (5)"); assert.deepEqual(nice_min_max_steps(43, 103, 3), {min: 0, max: 150, step: 50, nb_steps: 3}, "43..103 (3)"); }); QUnit.test("prepend_path_if_not_web", function(assert) { assert.equal(prepend_path_if_not_web('/tata', 'toto'), 'toto/tata'); assert.equal(prepend_path_if_not_web('http://toto', 'toto'), 'http://toto'); ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!