Repository navigation
Expand file tree
/
Copy pathnumerals.js
More file actions
239 lines (213 loc) · 9.12 KB
/
Copy pathnumerals.js
File metadata and controls
239 lines (213 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Alternative numeral systems and Unicode digit styles.
* Everything here is pure, table-driven, and renders with Unicode glyphs.
* Only blocks with broad system-font coverage are included (Mayan numerals
* and tally marks were dropped for lack of fonts on macOS).
* @module numerals
*/
// ============================================================================
// SHARED
// ============================================================================
/** Normalize a non-negative integer input to BigInt, or null if invalid */
const toCount = (n) => {
if (typeof n === 'bigint') return n >= 0n ? n : null;
if (typeof n === 'number') return Number.isInteger(n) && n >= 0 ? BigInt(n) : null;
if (typeof n === 'string' && /^\d+$/.test(n.trim())) return BigInt(n.trim());
return null;
};
/** Normalize any numeric input (sign, decimal point) to a digit string, or null */
const toDigitString = (n) => {
if (typeof n === 'bigint') return n.toString();
if (typeof n === 'number') {
if (!Number.isFinite(n)) return null;
const str = n.toString();
if (str.includes('e')) return Number.isInteger(n) ? BigInt(n).toString() : null;
return str;
}
if (typeof n === 'string') {
const str = n.trim();
return /^-?\d+(\.\d+)?$/.test(str) ? str : null;
}
return null;
};
// ============================================================================
// UNICODE DIGIT STYLES
// ============================================================================
/** Digit tables: ten glyphs for 0-9, plus minus and point */
const FANCY_STYLES = Object.freeze({
circled: { digits: '⓪①②③④⑤⑥⑦⑧⑨', minus: '−', point: '·' },
superscript: { digits: '⁰¹²³⁴⁵⁶⁷⁸⁹', minus: '⁻', point: '˙' },
subscript: { digits: '₀₁₂₃₄₅₆₇₈₉', minus: '₋', point: '.' },
fullwidth: { digits: '0123456789', minus: '-', point: '.' },
bold: { digits: '𝟎𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟗', minus: '−', point: '.' },
doublestruck: { digits: '𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡', minus: '−', point: '.' },
sans: { digits: '𝟢𝟣𝟤𝟥𝟦𝟧𝟨𝟩𝟪𝟫', minus: '−', point: '.' },
monospace: { digits: '𝟶𝟷𝟸𝟹𝟺𝟻𝟼𝟽𝟾𝟿', minus: '−', point: '.' },
keycap: { digits: ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'], minus: '➖', point: '.' },
emoji: { digits: ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'], minus: '➖', point: '.' },
// Clock faces: 1-9 o'clock, with 12 o'clock standing in for 0
clock: { digits: '🕛🕐🕑🕒🕓🕔🕕🕖🕗🕘', minus: '−', point: '·' },
// Braille: numeric indicator ⠼ then a-j, decimal point ⠨, minus ⠤
braille: { digits: '⠚⠁⠃⠉⠙⠑⠋⠛⠓⠊', minus: '⠤', point: '⠨', prefix: '⠼' }
});
/** Style names accepted by fancy() */
const FANCY_STYLE_NAMES = Object.freeze(Object.keys(FANCY_STYLES));
/**
* Render a number's digits in a Unicode style.
* @param {number|bigint|string} n - The number
* @param {string} [style='circled'] - One of circled, superscript, subscript,
* fullwidth, bold, doublestruck, sans, monospace, keycap (alias emoji), clock, braille
* @returns {string|false} Styled digits or false if invalid
*
* @example
* fancy(42) // '④②'
* fancy(42, 'superscript') // '⁴²'
* fancy(-3.5, 'fullwidth') // '-3.5'
*/
const fancy = (n, style = 'circled') => {
const table = FANCY_STYLES[style];
if (!table) return false;
const str = toDigitString(n);
if (str === null) return false;
const glyphs = Array.isArray(table.digits) ? table.digits : [...table.digits];
let out = table.prefix || '';
for (const ch of str) {
if (ch === '-') out += table.minus;
else if (ch === '.') out += table.point;
else out += glyphs[Number(ch)];
}
return out;
};
// ============================================================================
// EGYPTIAN HIEROGLYPHS
// ============================================================================
/** Hieroglyphs for 1, 10, 100, ... 1,000,000 (stroke, heel bone, coil, lotus, finger, tadpole, Heh) */
const EGYPTIAN_SYMBOLS = Object.freeze(['𓏺', '𓎆', '𓍢', '𓆼', '𓂭', '𓆐', '𓁨']);
/** Largest value expressible with repeated hieroglyphs (9,999,999) */
const EGYPTIAN_MAX = 9999999n;
/**
* Egyptian hieroglyphic numerals, additive: each power of ten is a symbol
* repeated up to nine times, largest first.
* @param {number|bigint|string} n - Integer from 1 to 9,999,999
* @returns {string|false}
*
* @example
* egyptian(42) // '𓎆𓎆𓎆𓎆𓏺𓏺'
* egyptian(1000) // '𓆼'
*/
const egyptian = (n) => {
const count = toCount(n);
if (count === null || count < 1n || count > EGYPTIAN_MAX) return false;
const digits = count.toString();
let out = '';
for (let i = 0; i < digits.length; i++) {
const power = digits.length - 1 - i;
out += EGYPTIAN_SYMBOLS[power].repeat(Number(digits[i]));
}
return out;
};
// ============================================================================
// BABYLONIAN CUNEIFORM
// ============================================================================
/** DIŠ (U+12079), the positional unit wedge; GESH2 (U+12415) looks alike but means sixty */
const CUNEIFORM_ONE = '𒁹';
const CUNEIFORM_TEN = '𒌋';
/** Late Babylonian placeholder for an empty sexagesimal position */
const CUNEIFORM_ZERO = '𒑊';
/**
* Babylonian sexagesimal (base 60) numerals. Each position is written with
* tens wedges then unit wedges; positions are separated by a space and an
* empty inner position uses the Late Babylonian placeholder sign.
* @param {number|bigint|string} n - Non-negative integer
* @returns {string|false}
*
* @example
* babylonian(42) // '𒌋𒌋𒌋𒌋𒁹𒁹'
* babylonian(3600) // '𒁹 𒑊 𒑊'
*/
const babylonian = (n) => {
const count = toCount(n);
if (count === null) return false;
if (count === 0n) return CUNEIFORM_ZERO;
const places = [];
let rest = count;
while (rest > 0n) {
places.unshift(Number(rest % 60n));
rest /= 60n;
}
return places
.map((p) => (p === 0 ? CUNEIFORM_ZERO : CUNEIFORM_TEN.repeat(Math.floor(p / 10)) + CUNEIFORM_ONE.repeat(p % 10)))
.join(' ');
};
// ============================================================================
// CLOCK FACES
// ============================================================================
/** 🕐..🕛 for 1..12 o'clock (U+1F550..), 🕜..🕧 for the half hours (U+1F55C..) */
const CLOCK_HOURS = Object.freeze([...'🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛']);
const CLOCK_HALVES = Object.freeze([...'🕜🕝🕞🕟🕠🕡🕢🕣🕤🕥🕦🕧']);
/**
* Clock-face emoji for an hour (0-24, 24-hour values wrap) or an "H:MM" time.
* Minutes round to the nearest half hour; 0 and 24 are 🕛.
* @param {number|string} time - Hour, or 'H:MM'
* @returns {string|false}
*
* @example
* clock(3) // '🕒'
* clock('3:30') // '🕞'
* clock(15) // '🕒'
* clock('23:50') // '🕛'
*/
const clock = (time) => {
let hour;
let minute = 0;
if (typeof time === 'number') {
if (!Number.isInteger(time) || time < 0 || time > 24) return false;
hour = time;
} else if (typeof time === 'string') {
const m = time.trim().match(/^(\d{1,2})(?::(\d{2}))?$/);
if (!m) return false;
hour = Number(m[1]);
minute = Number(m[2] || 0);
if (hour > 24 || minute > 59) return false;
} else {
return false;
}
// Round to the nearest half hour; :45 and later roll to the next hour
if (minute >= 45) {
hour += 1;
minute = 0;
}
const half = minute >= 15;
const index = (hour + 11) % 12;
return half ? CLOCK_HALVES[index] : CLOCK_HOURS[index];
};
// ============================================================================
// GREEK (IONIC / MILESIAN)
// ============================================================================
const GREEK_ONES = Object.freeze(['', 'α', 'β', 'γ', 'δ', 'ε', 'ϛ', 'ζ', 'η', 'θ']);
const GREEK_TENS = Object.freeze(['', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π', 'ϟ']);
const GREEK_HUNDREDS = Object.freeze(['', 'ρ', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', 'ϡ']);
/** Keraia marks a number; the lower keraia marks thousands */
const GREEK_KERAIA = 'ʹ';
const GREEK_THOUSANDS = '͵';
/**
* Greek alphabetic (Ionic) numerals for 1-9999, with stigma, koppa and sampi
* for 6, 90 and 900 and the keraia marks.
* @param {number|bigint|string} n - Integer from 1 to 9999
* @returns {string|false}
*
* @example
* greek(42) // 'μβʹ'
* greek(1999) // '͵αϡϟθʹ'
*/
const greek = (n) => {
const count = toCount(n);
if (count === null || count < 1n || count > 9999n) return false;
const v = Number(count);
const thousands = Math.floor(v / 1000);
const rest = v % 1000;
let out = thousands ? GREEK_THOUSANDS + GREEK_ONES[thousands] : '';
out += GREEK_HUNDREDS[Math.floor(rest / 100)] + GREEK_TENS[Math.floor((rest % 100) / 10)] + GREEK_ONES[rest % 10];
return out + GREEK_KERAIA;
};
export { fancy, FANCY_STYLE_NAMES, egyptian, babylonian, greek, clock };