@@ -0,0 +1,3 @@ | |||||
- [ ] Ordinals | |||||
- [ ] Fractions | |||||
- [ ] Other locales (long count, languages, etc.) |
@@ -1,5 +1,9 @@ | |||||
{ | { | ||||
"root": true, | "root": true, | ||||
"rules": { | |||||
"indent": ["error", "tab"], | |||||
"no-tabs": "off" | |||||
}, | |||||
"extends": [ | "extends": [ | ||||
"lxsmnsyc/typescript" | "lxsmnsyc/typescript" | ||||
], | ], | ||||
@@ -105,3 +105,4 @@ dist | |||||
.tern-port | .tern-port | ||||
.npmrc | .npmrc | ||||
types/ |
@@ -10,14 +10,14 @@ export interface StringifyOptions { | |||||
export const stringify = ( | export const stringify = ( | ||||
valueRaw: StringifyValue, | valueRaw: StringifyValue, | ||||
options = {} as StringifyOptions | |||||
options = {} as StringifyOptions, | |||||
): string => { | ): string => { | ||||
if (!(['bigint', 'number', 'string'].includes(typeof (valueRaw as unknown)))) { | if (!(['bigint', 'number', 'string'].includes(typeof (valueRaw as unknown)))) { | ||||
throw new TypeError('value must be a string, number, or bigint'); | throw new TypeError('value must be a string, number, or bigint'); | ||||
} | } | ||||
const value = valueRaw.toString().replace(/\s/g, ''); | const value = valueRaw.toString().replace(/\s/g, ''); | ||||
const { system = enUS, makeGroupOptions} = options; | |||||
const { system = enUS, makeGroupOptions } = options; | |||||
if (value.startsWith('-')) { | if (value.startsWith('-')) { | ||||
return system.makeNegative(stringify(value.slice(1), options)); | return system.makeNegative(stringify(value.slice(1), options)); | ||||
@@ -47,12 +47,12 @@ export const parse = (value: string, options = {} as ParseOptions) => { | |||||
const stringValue = system.combineGroups(groups); | const stringValue = system.combineGroups(groups); | ||||
switch (type) { | switch (type) { | ||||
case 'number': | |||||
return Number(stringValue); | |||||
case 'bigint': | |||||
return BigInt(stringValue); | |||||
default: | |||||
break; | |||||
case 'number': | |||||
return Number(stringValue); | |||||
case 'bigint': | |||||
return BigInt(stringValue); | |||||
default: | |||||
break; | |||||
} | } | ||||
return stringValue; | return stringValue; | ||||
@@ -26,7 +26,10 @@ export interface NumberToExponentialOptions { | |||||
* @param value - The string value to extract components from. | * @param value - The string value to extract components from. | ||||
* @param options - Options to use when extracting components. | * @param options - Options to use when extracting components. | ||||
*/ | */ | ||||
export const extractExponentialComponents = (value: string, options = {} as NumberToExponentialOptions) => { | |||||
export const extractExponentialComponents = ( | |||||
value: string, | |||||
options = {} as NumberToExponentialOptions, | |||||
) => { | |||||
const { | const { | ||||
decimalPoint = '.', | decimalPoint = '.', | ||||
groupingSymbol = ',', | groupingSymbol = ',', | ||||
@@ -67,15 +70,18 @@ export const extractExponentialComponents = (value: string, options = {} as Numb | |||||
exponent: exponentValue < 0 ? exponentValue.toString() : `+${exponentValue}`, | exponent: exponentValue < 0 ? exponentValue.toString() : `+${exponentValue}`, | ||||
fractional, | fractional, | ||||
}; | }; | ||||
} | |||||
}; | |||||
/** | /** | ||||
* Converts a numeric value to a string in exponential notation. Supports numbers of all types. | * Converts a numeric value to a string in exponential notation. Supports numbers of all types. | ||||
* @param value - The value to convert. | * @param value - The value to convert. | ||||
* @param options - Options to use when extracting components. | * @param options - Options to use when extracting components. | ||||
*/ | */ | ||||
export const numberToExponential = (value: ValidValue, options = {} as NumberToExponentialOptions): string => { | |||||
const stringValueRaw = value as unknown | |||||
export const numberToExponential = ( | |||||
value: ValidValue, | |||||
options = {} as NumberToExponentialOptions, | |||||
): string => { | |||||
const stringValueRaw = value as unknown; | |||||
if (typeof stringValueRaw === 'bigint' || typeof stringValueRaw === 'number') { | if (typeof stringValueRaw === 'bigint' || typeof stringValueRaw === 'number') { | ||||
return numberToExponential(stringValueRaw.toString(), options); | return numberToExponential(stringValueRaw.toString(), options); | ||||