Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose plotPointRelativeToStandardDeviation #41

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/bars.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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';