Skip to content

Commit

Permalink
fix: expose plotPointRelativeToStandardDeviation (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Feb 23, 2024
1 parent 2214295 commit 72f1eb5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
22 changes: 12 additions & 10 deletions src/bars.test.ts
Expand Up @@ -5,17 +5,18 @@ import { barHorizontal, plotPointRelativeToStandardDeviation, simpleHistogram }
describe('simpleHistogram', () => {
test('should return a string of the histogram', () => {
const data = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const result = simpleHistogram(data);
const result = simpleHistogram(data, 0, 9);
expect(result).toBe(' ▁▂▃▄▅▆▇█');
});

test.each`
data | min | max | expected
${[0, 1, 2, 3, 4, 5, 6, 7, 8]} | ${undefined} | ${undefined} | ${' ▁▂▃▄▅▆▇█'}
${[0, 1, 2, 3, 4, 5, 6, 7, 8]} | ${undefined} | ${undefined} | ${'▁▂▃▄▄▅▆▇█'}
${[0, 0, 0]} | ${undefined} | ${undefined} | ${' '}
${[1, 2, 3, 4, 5, 6, 7, 8]} | ${0} | ${undefined} | ${'▁▂▃▄▅▆▇█'}
${[1, 2, 3, 4, 5, 6, 7, 8]} | ${4} | ${undefined} | ${' ▂▄▆█'}
${[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]} | ${0} | ${undefined} | ${' ▁▂▃▄▅▆▇█'}
${[0, 1, 2, 3, 4, 5, 1, 7, 8, 9, 10]} | ${undefined} | ${8} | ${' ▁▂▃▄▅▁▇███'}
${[0, 1, 2, 3, 4, 5, 1, 7, 8, 9, 10]} | ${undefined} | ${8} | ${'▁▂▃▄▅▆▂████'}
`('simpleHistogram $data', ({ data, min, max, expected }) => {
const result = simpleHistogram(data, min, max);
expect(result).toBe(expected);
Expand All @@ -26,7 +27,7 @@ describe('simpleHistogram', () => {
const barChars = ['-', '+', '*', '#'];
const result = simpleHistogram(data, undefined, undefined, barChars);
expect(result.length).toBe(data.length);
expect(result).toBe(' -+*#');
expect(result).toBe('-++*#');
});
});

Expand Down Expand Up @@ -58,12 +59,13 @@ describe('plotPointRelativeToStandardDeviation', () => {
});

test.each`
point | sd | mean | width | range | expected
${-1} | ${2} | ${0} | ${25} | ${2} | ${'┣━━━━━┻━━●━━╋━━━━━┻━━━━━┫'}
${1} | ${2} | ${0} | ${35} | ${3} | ${' ┣━━━━━┻━━━━━╋━━●━━┻━━━━━┫ '}
${5} | ${2} | ${0} | ${35} | ${3} | ${' ┣━━━━━┻━━━━━╋━━━━━┻━━━━━┫ ● '}
${-10} | ${2} | ${0} | ${35} | ${3} | ${'● ┣━━━━━┻━━━━━╋━━━━━┻━━━━━┫ '}
${1} | ${2} | ${0} | ${34} | ${3} | ${' ┣━━━━┻━━━━━╋━━●━━┻━━━━┫ '}
point | sd | mean | width | range | expected
${-1} | ${2} | ${0} | ${25} | ${2} | ${'┣━━━━━┻━━●━━╋━━━━━┻━━━━━┫'}
${-1} | ${2} | ${0} | ${25} | ${undefined} | ${' ┣━━━━┻━━●━╋━━━━┻━━━━┫ '}
${1} | ${2} | ${0} | ${35} | ${3} | ${' ┣━━━━━┻━━━━━╋━━●━━┻━━━━━┫ '}
${5} | ${2} | ${0} | ${35} | ${3} | ${' ┣━━━━━┻━━━━━╋━━━━━┻━━━━━┫ ● '}
${-10} | ${2} | ${0} | ${35} | ${3} | ${'● ┣━━━━━┻━━━━━╋━━━━━┻━━━━━┫ '}
${1} | ${2} | ${0} | ${34} | ${3} | ${' ┣━━━━┻━━━━━╋━━●━━┻━━━━┫ '}
`('plotPointRelativeToStandardDeviation $point', ({ point, sd, mean, width, range, expected }) => {
const result = plotPointRelativeToStandardDeviation(point, sd, mean, width, range);
expect(result).toBe(expected);
Expand Down
11 changes: 9 additions & 2 deletions src/bars.ts
Expand Up @@ -14,8 +14,7 @@ export function simpleHistogram(
max?: number,
barChars: string[] = histoCharsBottomToTop,
): string {
const minVal = min ?? Math.min(...data);
const maxVal = max ?? Math.max(...data);
const [minVal, maxVal] = minMaxRange(data, min, max);
const range = maxVal - minVal || 1;
const lenBarChars = barChars.length;
const scale = lenBarChars / range;
Expand Down Expand Up @@ -96,3 +95,11 @@ export function plotPointRelativeToStandardDeviation(

return plot.join('');
}

function minMaxRange(values: readonly number[], min?: number, max?: number): Readonly<[number, number]> {
const minVal = min ?? Math.min(...values);
const maxVal = max ?? Math.max(...values);
const adj = (maxVal - minVal) * 0.05;
const r = [min ?? minVal - adj, max ?? maxVal + adj] as const;
return r;
}
11 changes: 10 additions & 1 deletion src/index.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'vitest';

import { type Data, histogram } from './index.js';
import type { Data } from './index.js';
import { barHorizontal, histogram, plotPointRelativeToStandardDeviation, simpleHistogram } from './index.js';

describe('histogram', () => {
test('simple', () => {
Expand Down Expand Up @@ -225,6 +226,14 @@ describe('histogram', () => {
});
});

describe('api', () => {
test('api', () => {
expect(typeof barHorizontal).toBe('function');
expect(typeof simpleHistogram).toBe('function');
expect(typeof plotPointRelativeToStandardDeviation).toBe('function');
});
});

function sampleData(num: number, scale = 1, step = 5): Data {
const data: [string, number][] = [];

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
@@ -1,3 +1,3 @@
export { barHorizontal, simpleHistogram } from './bars.js';
export { barHorizontal, plotPointRelativeToStandardDeviation, simpleHistogram } from './bars.js';
export type { Data, DataLine, HistogramDrawOptions, HistogramOptions } from './histogram.js';
export { histogram } from './histogram.js';

0 comments on commit 72f1eb5

Please sign in to comment.