Functional Weave
Code in TypeScript

charts.downsample@1.0.0

vectors.json

2,429 bytes · view raw

[
  {"name": "five points to three keeps the peak", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 5}, {"x": 2, "y": 1}, {"x": 3, "y": 2}, {"x": 4, "y": 0}], 3], "expect": [{"x": 0, "y": 0}, {"x": 1, "y": 5}, {"x": 4, "y": 0}]},
  {"name": "the largest triangle, not the highest point: (2,-1) beats (1,3)", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 3}, {"x": 2, "y": -1}, {"x": 3, "y": 4}, {"x": 4, "y": 4}, {"x": 5, "y": 1}, {"x": 6, "y": 2}], 4], "expect": [{"x": 0, "y": 0}, {"x": 2, "y": -1}, {"x": 3, "y": 4}, {"x": 6, "y": 2}]},
  {"name": "ten points to four keeps the spike and the dip", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 2}, {"x": 2, "y": 8}, {"x": 3, "y": 3}, {"x": 4, "y": 1}, {"x": 5, "y": 1}, {"x": 6, "y": -6}, {"x": 7, "y": 2}, {"x": 8, "y": 0}, {"x": 9, "y": 1}], 4], "expect": [{"x": 0, "y": 0}, {"x": 2, "y": 8}, {"x": 6, "y": -6}, {"x": 9, "y": 1}]},
  {"name": "on equal areas the earlier point wins", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 1}, {"x": 2, "y": -1}, {"x": 3, "y": 0}], 3], "expect": [{"x": 0, "y": 0}, {"x": 1, "y": 1}, {"x": 3, "y": 0}]},
  {"name": "a series no longer than the threshold is returned as it is", "args": [[{"x": 0, "y": 1}, {"x": 1, "y": 2}, {"x": 2, "y": 3}], 3], "expect": [{"x": 0, "y": 1}, {"x": 1, "y": 2}, {"x": 2, "y": 3}]},
  {"name": "a threshold above the length changes nothing", "args": [[{"x": 0, "y": 1}, {"x": 1, "y": 9}], 10], "expect": [{"x": 0, "y": 1}, {"x": 1, "y": 9}]},
  {"name": "an empty series stays empty", "args": [[], 5], "expect": []},
  {"name": "a flat line keeps the first point of each bucket", "args": [[{"x": 0, "y": 2}, {"x": 1, "y": 2}, {"x": 2, "y": 2}, {"x": 3, "y": 2}, {"x": 4, "y": 2}, {"x": 5, "y": 2}], 4], "expect": [{"x": 0, "y": 2}, {"x": 1, "y": 2}, {"x": 3, "y": 2}, {"x": 5, "y": 2}]},
  {"name": "uneven x spacing is used, not the index", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 1}, {"x": 10, "y": 1}, {"x": 11, "y": 3}], 3], "expect": [{"x": 0, "y": 0}, {"x": 10, "y": 1}, {"x": 11, "y": 3}]},
  {"name": "a threshold of 2 is an error (LTTB always keeps both ends plus one bucket)", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 1}, {"x": 2, "y": 2}], 2], "expectError": "threshold must be a whole number of at least 3"},
  {"name": "a threshold of 0 is an error, not a silent no-op", "args": [[{"x": 0, "y": 0}, {"x": 1, "y": 1}, {"x": 2, "y": 2}], 0], "expectError": "threshold must be a whole number of at least 3"}
]