@codecademy/gamut 67.6.1-alpha.21416d.0 → 67.6.1-alpha.4615f3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,70 +0,0 @@
1
- import { BarProps, BarChartProps } from '../types';
2
- export declare const numDigits: ({ num }: {
3
- num: number;
4
- }) => number;
5
- export declare const columnBaseSize: ({ experience }: {
6
- experience?: number | undefined;
7
- }) => {
8
- sm: number;
9
- md: number;
10
- lg: number;
11
- xl: number;
12
- };
13
- export declare const calculatePercent: ({ value, total }: {
14
- value: number;
15
- total: number;
16
- }) => number;
17
- export declare const calculateBarWidth: ({ value, maxRange, }: {
18
- value: number;
19
- maxRange: number;
20
- }) => number;
21
- export declare const calculateTicksAndRange: ({ maxTicks, minPoint, maxPoint, }: {
22
- maxTicks: number;
23
- minPoint: number;
24
- maxPoint: number;
25
- }) => [number, number, number];
26
- /**
27
- * Returns a "nice" number approximately equal to range
28
- * Rounds the number if round = true
29
- * Takes the ceiling if round = false.
30
- * A nice number is a simple decimal number, for example if a number is 1234, a nice number would be 1000 or 2000.
31
- */
32
- export declare const niceNum: ({ range, roundDown }: {
33
- range: number;
34
- roundDown: boolean;
35
- }) => number;
36
- export declare const getPercentDiff: ({ v1, v2 }: {
37
- v1: number;
38
- v2: number;
39
- }) => number;
40
- export declare const formatNumberUS: ({ num }: {
41
- num: number;
42
- }) => string;
43
- export declare const formatNumberUSCompact: ({ num }: {
44
- num: number;
45
- }) => string;
46
- /**
47
- * Sort bars based on sortBy and order configuration
48
- */
49
- export declare const sortBars: ({ bars, sortBy, order, }: {
50
- bars: BarProps[];
51
- sortBy: BarChartProps['sortBy'];
52
- order: BarChartProps['order'];
53
- }) => BarProps[];
54
- /**
55
- * Generates an accessible summary of the bar values
56
- */
57
- export declare const getValuesSummary: ({ yLabel, seriesOneValue, seriesTwoValue, unit, }: {
58
- yLabel: string;
59
- seriesOneValue: number;
60
- seriesTwoValue?: number | undefined;
61
- unit: string;
62
- }) => string;
63
- /**
64
- * Calculates the value for a given label position
65
- */
66
- export declare const getLabel: ({ labelCount, labelIndex, max, }: {
67
- labelCount: number;
68
- labelIndex: number;
69
- max: number;
70
- }) => number;
@@ -1,140 +0,0 @@
1
- export const numDigits = ({
2
- num
3
- }) => {
4
- return Math.max(Math.floor(Math.log10(Math.abs(num))), 0) + 1;
5
- };
6
- export const columnBaseSize = ({
7
- experience = 3
8
- }) => {
9
- const digits = numDigits({
10
- num: experience
11
- });
12
- return {
13
- sm: digits > 4 ? 5 : 4,
14
- md: digits > 4 ? 5 : 4,
15
- lg: digits > 4 ? 4 : 5,
16
- xl: digits > 4 ? 5 : 4
17
- };
18
- };
19
- export const calculatePercent = ({
20
- value,
21
- total
22
- }) => {
23
- return value / total * 100;
24
- };
25
- export const calculateBarWidth = ({
26
- value,
27
- maxRange
28
- }) => {
29
- return Math.floor(calculatePercent({
30
- value,
31
- total: maxRange
32
- }));
33
- };
34
-
35
- // Calculate tick spacing and nice minimum and maximum data points on the axis.
36
-
37
- export const calculateTicksAndRange = ({
38
- maxTicks,
39
- minPoint,
40
- maxPoint
41
- }) => {
42
- const range = niceNum({
43
- range: maxPoint - minPoint,
44
- roundDown: false
45
- });
46
- const tickSpacing = niceNum({
47
- range: range / (maxTicks - 1),
48
- roundDown: true
49
- });
50
- const niceMin = Math.floor(minPoint / tickSpacing) * tickSpacing;
51
- const niceMax = Math.ceil(maxPoint / tickSpacing) * tickSpacing;
52
- const tickCount = range / tickSpacing;
53
- return [tickCount, niceMin, niceMax];
54
- };
55
-
56
- /**
57
- * Returns a "nice" number approximately equal to range
58
- * Rounds the number if round = true
59
- * Takes the ceiling if round = false.
60
- * A nice number is a simple decimal number, for example if a number is 1234, a nice number would be 1000 or 2000.
61
- */
62
- export const niceNum = ({
63
- range,
64
- roundDown
65
- }) => {
66
- const exponent = Math.floor(Math.log10(range));
67
- const fraction = range / 10 ** exponent;
68
- let niceFraction;
69
- if (roundDown) {
70
- if (fraction < 1.5) niceFraction = 1;else if (fraction < 3) niceFraction = 2;else if (fraction < 7) niceFraction = 5;else niceFraction = 10;
71
- } else if (fraction <= 1) niceFraction = 1;else if (fraction <= 2) niceFraction = 2;else if (fraction <= 5) niceFraction = 5;else niceFraction = 10;
72
- return niceFraction * 10 ** exponent;
73
- };
74
- export const getPercentDiff = ({
75
- v1,
76
- v2
77
- }) => {
78
- return Math.abs(v1 - v2) / ((v1 + v2) / 2) * 100;
79
- };
80
- export const formatNumberUS = ({
81
- num
82
- }) => Intl.NumberFormat('en').format(num);
83
- export const formatNumberUSCompact = ({
84
- num
85
- }) => Intl.NumberFormat('en', {
86
- notation: 'compact',
87
- compactDisplay: 'short'
88
- }).format(num);
89
-
90
- /**
91
- * Sort bars based on sortBy and order configuration
92
- */
93
- export const sortBars = ({
94
- bars,
95
- sortBy,
96
- order
97
- }) => {
98
- if (sortBy === 'none' || !sortBy) {
99
- return bars;
100
- }
101
- const sorted = [...bars].sort((a, b) => {
102
- if (sortBy === 'label') {
103
- return a.yLabel.localeCompare(b.yLabel);
104
- }
105
- // sortBy === 'value' - use seriesTwoValue if available, otherwise seriesOneValue
106
- const aValue = a.seriesTwoValue ?? a.seriesOneValue;
107
- const bValue = b.seriesTwoValue ?? b.seriesOneValue;
108
- return aValue - bValue;
109
- });
110
- return order === 'descending' ? sorted.reverse() : sorted;
111
- };
112
-
113
- /**
114
- * Generates an accessible summary of the bar values
115
- */
116
- export const getValuesSummary = ({
117
- yLabel,
118
- seriesOneValue,
119
- seriesTwoValue,
120
- unit
121
- }) => {
122
- if (seriesTwoValue !== undefined) {
123
- const gained = seriesOneValue;
124
- return `${gained} ${unit} gained - now at ${seriesTwoValue} ${unit} in ${yLabel} category`;
125
- }
126
- return `${seriesOneValue} ${unit} in ${yLabel} category`;
127
- };
128
-
129
- /**
130
- * Calculates the value for a given label position
131
- */
132
- export const getLabel = ({
133
- labelCount,
134
- labelIndex,
135
- max
136
- }) => {
137
- if (labelCount <= 1) return max;
138
- const incrementalDecimal = labelIndex / (labelCount - 1);
139
- return Math.floor(incrementalDecimal * max);
140
- };