Package: alphanumeric - uhop/console-toolkit GitHub Wiki
The alphanumeric
package provides utilities for working with numbers, letters and symbols
used to improve user experience in the console.
- Utilities:
- Converting to Roman numbers:
alphanumeric/roman.js
. - Number formatters/humanizers:
alphanumeric/number-formatters.js
. - Special Unicode symbols for well-known fractions:
alphanumeric/fractions.js
. - Transcoding utilities:
alphanumeric/utils.js
.
- Converting to Roman numbers:
- Special symbols:
- Arrows:
alphanumeric/arrows.js
. - Decorative symbols for Latin letters:
alphanumeric/unicode-letters.js
. - Decorative symbols for numbers:
alphanumeric/unicode-numbers.js
. - Alternative symbols for numbers:
alphanumeric/unicode-cultural-numbers.js
.
- Arrows:
alphanumeric/arrows.js
The alphanumeric/arrows.js module provides constants for arrows and their Unicode symbols. See the file for more details.
Arrows are represented as a four-element array of Unicode symbols. THe module defines the following directional constants to access array elements:
Name | Value |
---|---|
LEFT |
0 |
UP |
1 |
RIGHT |
2 |
DOWN |
3 |
This table was generated by scripts/arrows.js:
โญโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ Name โ Result โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโค
โ dashed โ โ โก โข โฃ โ
โ double โ โ โ โ โ โ
โ doubleWithStroke โ โ โ โ
โ fromBar โ โค โฅ โฆ โง โ
โ openHeaded โ โฝ โพ โ
โ simple โ โ โ โ โ โ
โ squiggle โ โ โ โ
โ toBar โ โค โฅ โ
โ triple โ โ โ โ
โ twoHeaded โ โ โ โ โก โ
โ wave โ โ โ โ
โ white โ โฆ โง โจ โฉ โ
โ withBarbDown โ โฝ โฟ โ โ โ
โ withBarbUp โ โผ โพ โ โ โ
โ withDoubleVStroke โ โบ โป โ
โ withLoop โ โซ โฌ โ
โ withStroke โ โ โ โ
โ withTail โ โข โฃ โ
โ withVStroke โ โท โธ โ
โฐโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโฏ
alphanumeric/fractions.js
Unicode has special symbols to represent fractions: a half, thirds, quarters, fifths, sixths, eighths, etc. The alphanumeric/fractions.js module provides tables of such symbols and functions, that choose the appropriate symbol for the given fraction. See the file for more details.
Tables
Fractions are represented by an array of objects with the following properties:
numerator
โ the numerator of the fraction.denominator
โ the denominator of the fraction.symbol
โ the symbol used to represent the fraction.value
โ the value of the fraction.
Example: {numerator: 1, denominator: 2, symbol: '\u00BD', value: 0.5}
.
Available tables:
fractions
โ the table of all fraction symbols.thirds
โ the table of all third symbols.quarters
โ the table of all quarter symbols. AKAfourths
.fifths
โ the table of all fifth symbols.sixths
โ the table of all sixth symbols.eighths
โ the table of all eighth symbols.
Utilities
All utilities have the following signature: getXXX(fraction, useFractionForZero)
and return a string:
fraction
โ a number. Its fractional part will be replaced by the closest fraction symbol.useFractionForZero
โ iftrue
, a fractional zero symbol will be used if available.- Example: a symbol representing
0/3
will be used ifuseFractionForZero
istrue
.
- Example: a symbol representing
The following utilities are available:
getFraction(fraction, useFractionForZero)
โ uses thefractions
table to find the symbol for the given fraction.getThird(fraction, useFractionForZero)
โ uses thethirds
table.getQuarter(fraction, useFractionForZero)
โ uses thequarters
table. AKAgetFourth()
.getFifth(fraction, useFractionForZero)
โ uses thefifths
table.getSixth(fraction, useFractionForZero)
โ uses thesixths
table.getEighth(fraction, useFractionForZero)
โ uses theeighths
table.
Example
This table was generated by scripts/fractions.js:
Available fraction symbols
fractions: โ โ
โ
โ
โ
โ
โ
ยผ โ
โ
โ
ยฝ โ
โ
โ
ยพ โ
โ
โ
thirds: โ โ
โ
quarters: 0 ยผ ยฝ ยพ
fifths: 0 โ
โ
โ
โ
sixths: โ โ
โ
ยฝ โ
โ
eighths: 0 โ
ยผ โ
ยฝ โ
ยพ โ
Mapping fractions to symbols
โญโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ Chosen symbols โ
โ Value โโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโค
โ โ Thirds โ Quarters โ Fifths โ Sixths โ Eighths โ Fractions โ
โโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโค
โ 1.00 โ 1 โ 1 โ 1 โ 1 โ 1 โ 1 โ
โ 1.05 โ 1 โ 1 โ 1 โ 1 โ 1 โ 1โ
โ
โ 1.10 โ 1 โ 1 โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.15 โ 1 โ 1ยผ โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.20 โ 1โ
โ 1ยผ โ 1โ
โ 1โ
โ 1ยผ โ 1โ
โ
โ 1.25 โ 1โ
โ 1ยผ โ 1โ
โ 1โ
โ 1ยผ โ 1ยผ โ
โ 1.30 โ 1โ
โ 1ยผ โ 1โ
โ 1โ
โ 1ยผ โ 1โ
โ
โ 1.35 โ 1โ
โ 1ยผ โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.40 โ 1โ
โ 1ยฝ โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.45 โ 1โ
โ 1ยฝ โ 1โ
โ 1ยฝ โ 1ยฝ โ 1โ
โ
โ 1.50 โ 1โ
โ 1ยฝ โ 1โ
โ 1ยฝ โ 1ยฝ โ 1ยฝ โ
โ 1.55 โ 1โ
โ 1ยฝ โ 1โ
โ 1ยฝ โ 1ยฝ โ 1โ
โ
โ 1.60 โ 1โ
โ 1ยฝ โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.65 โ 1โ
โ 1ยพ โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.70 โ 1โ
โ 1ยพ โ 1โ
โ 1โ
โ 1ยพ โ 1โ
โ
โ 1.75 โ 1โ
โ 1ยพ โ 1โ
โ 1โ
โ 1ยพ โ 1ยพ โ
โ 1.80 โ 1โ
โ 1ยพ โ 1โ
โ 1โ
โ 1ยพ โ 1โ
โ
โ 1.85 โ 2 โ 1ยพ โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.90 โ 2 โ 2 โ 1โ
โ 1โ
โ 1โ
โ 1โ
โ
โ 1.95 โ 2 โ 2 โ 2 โ 2 โ 2 โ 2 โ
โ 2.00 โ 2 โ 2 โ 2 โ 2 โ 2 โ 2 โ
โฐโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโฏ
alphanumeric/number-formatters.js
The alphanumeric/number-formatters.js module provides functions to format numbers. See the file for more details.
The following utilities are available:
formatTime()
โ format time values. It selects a time unit.prepareTimeFormat()
โ prepare time format forformatTime()
.formatInteger()
โ format integer values. It adds thousands separators.formatNumber()
โ format floating point values. It adds thousands separators and adds decimals if necessary.abbrNumber()
โ abbreviate numbers. It uses standard number units:k
for thousands,M
for millions, andG
for billions, etc.simplifyExponent()
โ simplify exponential notation. It removes unnecessary zeros after the decimal point.compareDifference()
โ compare two numbers and return a humanized difference in percentage, as a ratio, etc.
prepareTimeFormat()
Function prepareTimeFormat(values, scale = 1, useUnicode = false)
prepares time format for formatTime()
.
It takes an array of time values and finds a common time format so they can be directly comparable.
The following parameters are supported:
values
โ an array of time values.scale
โ the initial scale factor. Defaults to1
(seconds).Date.now()
returns the number of milliseconds. In this case thescale
should be set to 1000.
useUnicode
โ iftrue
, special Unicode unit symbols will be used. Otherwise, ASCII unit names will be used. Defaults tofalse
.
It returns an object with the following properties:
scale
โ the common scale factor.precision
โ the number of digits after the decimal point.unit
โ the time unit as a string.
Example
import {prepareTimeFormat} from 'console-toolkit/alphanumeric/number-formatters.js';
const fmt1 = prepareTimeFormat([0.000002, 0.000005, 0.000020]);
// {scale: 1000000, precision: 1, unit: 'ฮผs'}
const fmt2 = prepareTimeFormat([2, 5, 20], 1000, true);
// {scale: 1, precision: 1, unit: 'ใณ'}
formatTime()
Function formatTime(value, format)
formats a time value returning a string:
value
โ the time value.format
โ the time format. SeeprepareTimeFormat()
for details.- It implements additional properties:
keepFractionAsIs
โ keeps fractional part as is iftrue
. Otherwise, it removes trailing zeros and, if warranted, the decimal point. Defaults tofalse
.
- It implements additional properties:
The definition of the function explains how format
is used the best:
const formatTime = (value, format) => {
let result = (value * format.scale).toFixed(format.precision);
if (format.precision > 0 && !format.keepFractionAsIs)
result = result.replace(/\.0+$/, '');
return result + format.unit;
};
Example
import {formatTime, prepareTimeFormat} from 'console-toolkit/alphanumeric/number-formatters.js';
const fmt = prepareTimeFormat([2, 3, 20], 1000);
console.log(formatTime(20, fmt)); // 20ms
formatInteger()
Function formatInteger(value, options)
formats an integer value returning a string:
value
โ the integer value.options
โ an object with the following properties:keepSign
โ forces+
sign for positive numbers iftrue
. Defaults tofalse
.comma
โ a thousand separator. Defaults to','
.
Example
import {formatInteger} from 'console-toolkit/alphanumeric/number-formatters.js';
console.log(formatInteger(123456789)); // 123,456,789
formatNumber()
Function formatNumber(value, options)
formats a real number value using a fixed-point notation returning a string:
value
โ the real number value.options
โ an object with the following properties:decimals
โ the number of decimals. Defaults to0
.keepSign
โ forces+
sign for positive numbers iftrue
. Defaults tofalse
.keepFractionAsIs
โ keeps fractional part as is iftrue
. Otherwise, it removes trailing zeros and, if warranted, the decimal point. Defaults tofalse
.dot
โ the decimal point symbol. Defaults to.
.comma
โ a thousand separator. Defaults to','
.
The integer part of a number is formatted using formatInteger()
with the same options.
Example
import {formatNumber} from 'console-toolkit/alphanumeric/number-formatters.js';
console.log(formatNumber(1234.78956, {decimals: 3})); // 1,234.79
console.log(formatNumber(1234.78956, {
decimals: 3, keepFractionAsIs: true})); // 1,234.790
abbrNumber()
Function abbrNumber(value, options)
abbreviates a number using standard notation of k
, M
, G
, or T
returning a string:
value
โ the number value.options
โ an object with the following properties:decimals
โ the number of decimals. Defaults to0
.keepSign
โ forces+
sign for positive numbers iftrue
. Defaults tofalse
.keepFractionAsIs
โ keeps fractional part as is iftrue
. Otherwise, it removes trailing zeros and, if warranted, the decimal point. Defaults tofalse
.dot
โ the decimal point symbol. Defaults to.
.comma
โ a thousand separator. Defaults to','
.
Example
import {abbrNumber} from 'console-toolkit/alphanumeric/number-formatters.js';
console.log(abbrNumber(123)); // 123
console.log(abbrNumber(1234)); // 1,234
console.log(abbrNumber(12345)); // 12k
console.log(abbrNumber(123456)); // 123k
console.log(abbrNumber(1234567)); // 1M
console.log(abbrNumber(12345678)); // 12M
console.log(abbrNumber(123456789)); // 123M
console.log(abbrNumber(1234567890)); // 1G
console.log(abbrNumber(123, {decimals: 1})); // 123
console.log(abbrNumber(1234, {decimals: 1})); // 1,234
console.log(abbrNumber(12345, {decimals: 1})); // 12.3k
console.log(abbrNumber(123456, {decimals: 1})); // 123.5k
console.log(abbrNumber(1234567, {decimals: 1})); // 1.2M
console.log(abbrNumber(12345678, {decimals: 1})); // 12.3M
console.log(abbrNumber(123456789, {decimals: 1})); // 123.5M
console.log(abbrNumber(1234567890, {decimals: 1})); // 1.2G
simplifyExponent()
Function simplifyExponent(value, options)
simplifies an exponent by removing trailing zeros in
the fractional part and removing a plus sign in the exponent returning the result as a string:
value
โ a number value or a string representing a number in an exponent notation.options
โ an object with the following properties:keepExpPlus
โ forces+
sign for positive exponents iftrue
. Defaults tofalse
.
Example
import {simplifyExponent} from 'console-toolkit/alphanumeric/number-formatters.js';
console.log((2e99).toExponential(3)); // 2.000e+99
console.log(simplifyExponent((2e99).toExponential(3))); // 2e99
console.log(
simplifyExponent((2e99).toExponential(3), {keepExpPlus: true}})); // 2e+99
compareDifference()
Function compareDifference(a, b)
compares two numbers in a "human" way. It returns an object with
the following properties:
less
โtrue
ifa
is smaller thanb
.- Other properties are mutually exclusive:
infinity
โtrue
if the smaller number is infinitely smaller than the bigger number.equality
โtrue
if the numbers are equal within 0.00001%.percentage
โ the difference in percentage formatted usingformatNumber()
.ratio
โ the difference in ratio formatted usingformatNumber()
.
percentage
is calculated as 100 * Math.abs(b - a) / a
.
ratio
is calculated as b / a
, where a
is the smaller number.
Example
import {compareDifference} from 'console-toolkit/alphanumeric/number-formatters.js';
const announce = result => {
if (result.equality) return 'equal to';
if (result.infinity) return `infinitely ${result.less ? 'smaller' : 'bigger'} than`;
if (result.percentage) return `${result.percentage}% ${result.less ? 'smaller' : 'bigger'} than`;
if (result.ratio) return `${result.ratio}x ${result.less ? 'smaller' : 'bigger'} than`;
return 'cannot be compared';
};
const cmp = (a, b) => {
const result = compareDifference(a, b);
console.log(a, 'is', announce(result), b);
};
cmp(98, 130); // 98 is 32.7% smaller than 130
cmp(198, 15); // 198 is 13.2x bigger than 15
alphanumeric/roman.js
The alphanumeric/roman.js module provides utilities to convert integers to Roman numbers. See the file for more details.
The utilities work with integer numbers from 1 to 3,999.
toRoman()
Function toRoman(value)
converts an integer number to a Roman number string using uppercase letters.
Example
import {toRoman} from 'console-toolkit/alphanumeric/roman.js';
console.log(toRoman(1)); // I
console.log(toRoman(2)); // II
console.log(toRoman(3)); // III
console.log(toRoman(4)); // IV
console.log(toRoman(5)); // V
console.log(toRoman(1776)); // MDCCLXXVI
console.log(toRoman(2000)); // MM
console.log(toRoman(2024)); // MMXXIV
toRomanUnicode()
Function toRomanUnicode(value)
converts an integer number to a Roman number string using
uppercase Unicode characters.
Example
import {toRomanUnicode} from 'console-toolkit/alphanumeric/roman.js';
console.log(toRomanUnicode(1)); // โ
console.log(toRomanUnicode(2)); // โ
ก
console.log(toRomanUnicode(3)); // โ
ข
console.log(toRomanUnicode(4)); // โ
ฃ
console.log(toRomanUnicode(5)); // โ
ค
console.log(toRomanUnicode(1776)); // โ
ฏโ
ฎโ
ญโ
ญโ
ฌโ
ฉโ
ฉโ
ฅ
console.log(toRomanUnicode(2000)); // โ
ฏโ
ฏ
console.log(toRomanUnicode(2024)); // โ
ฏโ
ฏโ
ฉโ
ฉโ
ฃ
toRomanLowerUnicode()
Function toRomanLowerUnicode(value)
converts an integer number to a Roman number string using
lowercase Unicode characters.
alphanumeric/unicode-letters.js
The alphanumeric/unicode-letters.js module provide tables of Unicode symbols for variously decorated Latin characters. See the file for more details.
transcode()
Function transcode(text, table, options)
transcodes the text using the specified table returning a new string:
text
โ the text to be transcoded.table
โ the table to be used for transcoding. It can be a name of a table, which is defined in this module, as a string, or a table object itself.options
โ an object with the following properties:missing
โ if specified it will be a character to be used in place of characters in the text that are not found in the table. Otherwise, a character is left as is.
Tables
The module defines many tables. The following sample table was generated by scripts/letters.js (the output depends on symbols available in the system, run the utility yourself to see the exact result):
โญโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Name โ Result โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ pattern โ ABCDEFGHIJKLMNOPQRSTUVWXYZ โ
โ โ abcdefghijklmnopqrstuvwxyz โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ bold โ ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ โ
โ โ ๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ italic โ ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐ โ
โ โ ๐๐๐๐๐๐๐โ๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ boldItalic โ ๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐ โ
โ โ ๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ sansSerif โ ๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น โ
โ โ ๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐๐๐๐ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ sansSerifBold โ ๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ โ
โ โ ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ sansSerifItalic โ ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก โ
โ โ ๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ sansSerifBoldItalic โ ๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ โ
โ โ ๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ script โ ๐โฌ๐๐โฐโฑ๐ขโโ๐ฅ๐ฆโโณ๐ฉ๐ช๐ซ๐ฌโ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต โ
โ โ ๐ถ๐ท๐ธ๐นโฏ๐ปโ๐ฝ๐พ๐ฟ๐๐๐๐โด๐
๐๐๐๐๐๐๐๐๐๐ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ scriptBold โ ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ โ
โ โ ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ fraktur โ ๐๐
โญ๐๐๐๐โโ๐๐๐๐๐๐๐๐โ๐๐๐๐๐๐๐โจ โ
โ โ ๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ frakturBold โ ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
โ
โ โ ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ mono โ ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐๐๐๐๐๐
๐๐๐๐ โ
โ โ ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ doubleStruck โ ๐ธ๐นโ๐ป๐ผ๐ฝ๐พโ๐๐๐๐๐โ๐โโโ๐๐๐๐๐๐๐โค โ
โ โ ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ parens โ ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ โ
โ โ โโโโโ โกโขโฃโคโฅโฆโงโจโฉโชโซโฌโญโฎโฏโฐโฑโฒโณโดโต โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ circled โ โถโทโธโนโบโปโผโฝโพโฟโโโโโโ
โโโโโโโโโโ โ
โ โ โโโโโโโโโโโโโโโโโ โกโขโฃโคโฅโฆโงโจโฉ โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ squared โ ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
โ
โ โ abcdefghijklmnopqrstuvwxyz โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ negativeCircled โ ๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
ก๐
ข๐
ฃ๐
ค๐
ฅ๐
ฆ๐
ง๐
จ๐
ฉ โ
โ โ abcdefghijklmnopqrstuvwxyz โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ negativeSquared โ ๐
ฐ๐
ฑ๐
ฒ๐
ณ๐
ด๐
ต๐
ถ๐
ท๐
ธ๐
น๐
บ๐
ป๐
ผ๐
ฝ๐
พ๐
ฟ๐๐๐๐๐๐
๐๐๐๐ โ
โ โ abcdefghijklmnopqrstuvwxyz โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ regionalIndicators โ ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ๐ด๐ต๐ถ๐ท๐ธ๐น๐บ๐ป๐ผ๐ฝ๐พ๐ฟ โ
โ โ abcdefghijklmnopqrstuvwxyz โ
โฐโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
The tables are exposed as the transcodeTables
object. Its keys are names of tables and values
are table objects.
alphanumeric/unicode-numbers.js
The alphanumeric/unicode-numbers.js module provides tables of Unicode symbols for numbers. It is used mostly to decorate numbers, e.g., as list numbers or to enumerate headers. See the file for more details.
The module is structured similar to alphanumeric/unicode-letters.js
described above.
It defined the transcode()
function and the transcodeTables
object.
Additionally, it defines two more functions: numberExponent()
and numberPunctuation()
.
numberExponent()
Function numberExponent(value, options)
converts a number to a string with an exponent notation:
value
โ number with an exponent represented as a string.options
โ an object with the following properties:useSpecialMinus
โ iftrue
, use a special math minus symbol instead of the dash.
Example
import {numberExponent} from 'console-toolkit/src/alphanumeric/unicode-numbers.js';
console.log(1e9); // 1000000000
console.log(1e9.toExponential()); // 1e+9
console.log(numberExponent((-1e9).toExponential())); // -1ร10โบโน
console.log(numberExponent((-1e9).toExponential(),
{useSpecialMinus: true})); // โ1ร10โบโน
numberPunctuation()
Function numberPunctuation(value, options)
converts a number to a string with a punctuation notation:
value
โ number with a punctuation represented as a string. It can be a result offormatNumber()
orformatInteger()
described above.options
โ an object with the following properties:addBefore
โ string to add before a digit. Defaults to the empty string (''
).addAfter
โ string to add after a digit. Defaults to the space (' '
).
Example
import {numberPunctuation} from 'console-toolkit/src/alphanumeric/unicode-numbers.js';
console.log('1,234.567'); // ๐ 23โ 567
Tables
The modules defines many transcoding tables. The following table was generated by scripts/numbers.js:
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโฎ
โ Name โ Result โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโค
โ pattern โ 0 1 2 3 4 5 6 7 8 9 โ
โ circled โ โช โ โก โข โฃ โค โฅ โฆ โง โจ โ
โ parens โ 0 โด โต โถ โท โธ โน โบ โป โผ โ
โ dots โ ๐ โ โ โ โ โ โ โ โ โ โ
โ doubleCircled โ 0 โต โถ โท โธ โน โบ โป โผ โฝ โ
โ bold โ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ โ
โ doubleStruck โ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ก โ
โ sansSerif โ ๐ข ๐ฃ ๐ค ๐ฅ ๐ฆ ๐ง ๐จ ๐ฉ ๐ช ๐ซ โ
โ sansSerifBold โ ๐ฌ ๐ญ ๐ฎ ๐ฏ ๐ฐ ๐ฑ ๐ฒ ๐ณ ๐ด ๐ต โ
โ mono โ ๐ถ ๐ท ๐ธ ๐น ๐บ ๐ป ๐ผ ๐ฝ ๐พ ๐ฟ โ
โ commas โ ๐ ๐ ๐ ๐ ๐
๐ ๐ ๐ ๐ ๐ โ
โ roman โ 0 โ
โ
ก โ
ข โ
ฃ โ
ค โ
ฅ โ
ฆ โ
ง โ
จ โ
โ romanLower โ 0 โ
ฐ โ
ฑ โ
ฒ โ
ณ โ
ด โ
ต โ
ถ โ
ท โ
ธ โ
โ dingbatsCircledSansSerif โ 0 โ โ โ โ โ โ
โ โ โ โ
โ dingbatsNegativeCircled โ โฟ โถ โท โธ โน โบ โป โผ โฝ โพ โ
โ dingbatsNegativeCircledSansSerif โ 0 โ โ โ โ โ โ โ โ โ โ
โ superscript โ โฐ ยน ยฒ ยณ โด โต โถ โท โธ โน โ
โ subscript โ โ โ โ โ โ โ
โ โ โ โ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโฏ
alphanumeric/unicode-cultural-numbers.js
The alphanumeric/unicode-cultural-numbers.js module provides tables of Unicode symbols for numbers of different cultures. See the file for more details.
It exports the transcodeTables
object.
alphanumeric/utils.js
The alphanumeric/utils.js
module provides utility functions used by the alphanumeric
package to define transcode tables.
See the file for more details.
SymbolRange
Class The SymbolRange
class represents a range of symbols.
Name | Returns | Description |
---|---|---|
SymbolRange(fromSymbol, from = 0, to = 9, inputBase = '0') |
SymbolRange |
Creates a new SymbolRange object. |
from |
number | The starting value of the range. |
to |
number | The ending value of the range. |
base |
number | The offset from the starting value to the code point of fromSymbol . |
inputBase |
number | The starting code point of the range. |
overlay |
object or null |
The other SymbolRange object or a transcoding table. |
get(i) |
string | Returns the symbol. If index a number, it is assumed to be an index in the range. If it is a character, it is assumed to be a code point. |
transcode(text, options) |
string | Returns the transcoded text. |
If overlay
is specified, it will be consulted first. overlay
is used to override some symbols
and create disjoint ranges. An overlay can have an overlay. An overlay can be a simple table:
[a: '1', b: '2', c: '3']
.
get()
is used to get the symbol from the range. If index
is a number, it is assumed to be
an index in the range. Some ranges can have entries, which are not defined by a single symbol.
For example, some decorated numbers can be in a range from 0 to 20. If we want to get a symbol
for 12, we should use get(12)
. If it is a character, it is assumed to be a code point.
transcode(text, options)
is used to transcode the text:
text
โ the text to be transcoded.options
โ an object with the following properties:missing
โ if specified it will be a character to be used in place of characters in the text that are not transcoded. Otherwise, a character is left as is.
It consults the transcoding table using get()
for every character in the text.
transcode()
Function transcode(text, options)
is similar to the transcode()
function in
the alphanumeric/unicode-letters.js
module:
text
โ the text to be transcoded.tables
โ an object defining the transcoding tables:- If it is a function it is consulted on every character and cna return a new value or a falsy value to indicate that the character cannot be transcoded.
- If it is an object with the
transcode()
method, it is used to transcode thetext
. - If it is an array, it is assumed be an array of transcoding tables. They will be tried in order.
options
โ an object with the following properties:missing
โ if specified it will be a character to be used in place of characters in the text that are not transcoded. Otherwise, a character is left as is.