@peter-present/format-number 0.0.1 → 0.0.12
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.
- package/README.md +77 -67
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +212 -5
- package/dist/index.js +203 -105
- package/package.json +11 -3
- package/dist/compact.d.ts +0 -2
- package/dist/format.d.ts +0 -8
- package/dist/round.d.ts +0 -3
- package/dist/types.d.ts +0 -14
- package/dist/utils.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,115 +1,125 @@
|
|
|
1
1
|
# format-number
|
|
2
2
|
|
|
3
|
-
A lightweight,
|
|
3
|
+
A lightweight, zero-dependency JavaScript/TypeScript library for precise number formatting. Designed for financial applications, crypto dashboards, and scientific data where precision is paramount.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Unlike standard libraries that rely on floating-point math, **format-number** uses internal string-based arithmetic to handle numbers of any size (including `string`, `number`, and `bigint`) without losing a single decimal point.
|
|
6
6
|
|
|
7
|
-
For detailed documentation
|
|
7
|
+
For detailed documentation, visit: [here](https://blog.peter-present.xyz/format-number)
|
|
8
8
|
|
|
9
9
|
## Key Features
|
|
10
10
|
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **Flexible
|
|
16
|
-
- **Fluent API**:
|
|
17
|
-
|
|
11
|
+
- **Zero Precision Loss**: Uses string manipulation for rounding and formatting to avoid binary floating-point errors.
|
|
12
|
+
- **Robust Input Parsing**: Centralized parsing handles messy strings with currency symbols ($€£¥), commas, underscores, and scientific notation.
|
|
13
|
+
- **Advanced Rounding**: 5 strategies (`half`, `up`, `down`, `banker`, `truncate`) with customizable decimal precision.
|
|
14
|
+
- **Intelligent Notation**: Format small numbers with subscript zeros (e.g., `0.0₃5`) or standard scientific notation.
|
|
15
|
+
- **Flexible Compacting**: Shorten massive numbers into readable strings like `1.5T` or `20.4B`.
|
|
16
|
+
- **Fluent Chainable API**: Build complex formatting logic with a clean, readable syntax.
|
|
17
|
+
|
|
18
|
+
## Robust Parsing & Error Handling
|
|
19
|
+
|
|
20
|
+
The library follows a **Single-Gate Parsing** philosophy. High-level entry points (`FN`, `formatNumber`, and `parseNum`) are designed to be robust and will attempt to clean and normalize nearly any "number-like" string.
|
|
21
|
+
|
|
22
|
+
> [!IMPORTANT]
|
|
23
|
+
> **Internal functions** (like `round`, `compact`, `scientific`, etc.) are optimized for performance and **assume valid numeric strings**. Passing completely invalid non-numeric data (e.g., `"abc"`) directly to these utilities may result in fallback values or undefined behavior.
|
|
24
|
+
|
|
25
|
+
For manual parsing with custom error handling, use `parseNum`:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { parseNum } from '@peter-present/format-number';
|
|
29
|
+
|
|
30
|
+
// Default fallback is '--'
|
|
31
|
+
parseNum('invalid'); // '--'
|
|
32
|
+
|
|
33
|
+
// Custom fallback for your own error boundaries
|
|
34
|
+
parseNum('not-a-number', { fallback: 'NaN' }); // 'NaN'
|
|
35
|
+
parseNum('null', { fallback: '0' }); // '0'
|
|
36
|
+
```
|
|
18
37
|
|
|
19
38
|
## Installation
|
|
20
39
|
|
|
21
40
|
```shell
|
|
22
41
|
# npm
|
|
23
|
-
npm install format-number
|
|
42
|
+
npm install @peter-present/format-number
|
|
24
43
|
|
|
25
44
|
# yarn
|
|
26
|
-
yarn add format-number
|
|
45
|
+
yarn add @peter-present/format-number
|
|
27
46
|
|
|
28
47
|
# bun
|
|
29
|
-
bun install format-number
|
|
48
|
+
bun install @peter-present/format-number
|
|
30
49
|
```
|
|
31
50
|
|
|
32
|
-
## Core
|
|
51
|
+
## Core API
|
|
33
52
|
|
|
34
|
-
###
|
|
53
|
+
### `formatNumber(value, options)`
|
|
35
54
|
|
|
36
|
-
|
|
55
|
+
The primary entry point for one-off formatting. It combines parsing, rounding, and visualization.
|
|
37
56
|
|
|
38
57
|
```typescript
|
|
39
|
-
import {
|
|
40
|
-
|
|
41
|
-
round(123.456, { precision: 2 }); // '123.46'
|
|
42
|
-
round(-123.45, { mode: 'up', precision: 1 }); // '-123.4' (Towards +Infinity)
|
|
43
|
-
```
|
|
58
|
+
import { formatNumber } from '@peter-present/format-number';
|
|
44
59
|
|
|
45
|
-
|
|
60
|
+
// Currency & Rounding
|
|
61
|
+
formatNumber('$1,234.567', { prefix: '€', precision: 2 }); // '€1234.57'
|
|
46
62
|
|
|
47
|
-
|
|
63
|
+
// Compact Notation
|
|
64
|
+
formatNumber(1500000000, { isCompact: true }); // '1.5B'
|
|
48
65
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
compact(1500); // '1.5K'
|
|
53
|
-
compact(1200000, { precision: 1 }); // '1.2M'
|
|
54
|
-
compact('1000000000000'); // '1T'
|
|
66
|
+
// Special Notations
|
|
67
|
+
formatNumber(0.00005, { notation: 'subscript' }); // '0.0₄5'
|
|
68
|
+
formatNumber(12345, { notation: 'scientific' }); // '1.2345e+4'
|
|
55
69
|
```
|
|
56
70
|
|
|
57
|
-
###
|
|
71
|
+
### `FN(value)` - Fluent API
|
|
58
72
|
|
|
59
|
-
|
|
73
|
+
Best for complex, multi-step formatting requirements where readability is key.
|
|
60
74
|
|
|
61
75
|
```typescript
|
|
62
|
-
import {
|
|
76
|
+
import { FN } from '@peter-present/format-number';
|
|
63
77
|
|
|
64
|
-
|
|
65
|
-
|
|
78
|
+
const formatted = FN('1234567.89')
|
|
79
|
+
.round({ precision: 0, rounding: 'banker' })
|
|
80
|
+
.prefix('Balance: ')
|
|
81
|
+
.suffix(' USD')
|
|
82
|
+
.toNumber();
|
|
66
83
|
|
|
67
|
-
//
|
|
68
|
-
formatNumber(1500000, { isCompact: true, suffix: ' units' }); // '1.5M units'
|
|
84
|
+
console.log(formatted); // 'Balance: 1234568 USD'
|
|
69
85
|
|
|
70
|
-
//
|
|
71
|
-
|
|
86
|
+
// Seamlessly combine with compact and notation
|
|
87
|
+
FN(1500).compact({ precision: 1 }).prefix('$').toNumber(); // '$1.5K'
|
|
72
88
|
```
|
|
73
89
|
|
|
74
|
-
###
|
|
90
|
+
### `round(value, options)`
|
|
75
91
|
|
|
76
|
-
|
|
92
|
+
Independent rounding utility with high-precision string logic.
|
|
77
93
|
|
|
78
94
|
```typescript
|
|
79
|
-
import {
|
|
80
|
-
|
|
81
|
-
const result = FN('1234567.89')
|
|
82
|
-
.round({ precision: 0 })
|
|
83
|
-
.format({ prefix: 'Total: ', suffix: ' tokens' });
|
|
84
|
-
|
|
85
|
-
console.log(result); // 'Total: 1234568 tokens'
|
|
95
|
+
import { round } from '@peter-present/format-number';
|
|
86
96
|
|
|
87
|
-
//
|
|
88
|
-
|
|
97
|
+
round(1.235, { precision: 2, rounding: 'half' }); // '1.24'
|
|
98
|
+
round(1.235, { precision: 2, rounding: 'down' }); // '1.23'
|
|
89
99
|
```
|
|
90
100
|
|
|
91
|
-
##
|
|
101
|
+
## Configuration Reference
|
|
92
102
|
|
|
93
|
-
###
|
|
103
|
+
### `RoundingMode`
|
|
94
104
|
|
|
95
|
-
| Mode | Description
|
|
96
|
-
| :--------- |
|
|
97
|
-
| `half` |
|
|
98
|
-
| `up` |
|
|
99
|
-
| `down` |
|
|
100
|
-
| `truncate` |
|
|
101
|
-
| `banker` |
|
|
105
|
+
| Mode | Description |
|
|
106
|
+
| :--------- | :--------------------------------------------------------------------- |
|
|
107
|
+
| `half` | Rounds to the nearest neighbor (rounds away from zero if equidistant). |
|
|
108
|
+
| `up` | Rounds towards Positive Infinity. |
|
|
109
|
+
| `down` | Rounds towards Negative Infinity. |
|
|
110
|
+
| `truncate` | Rounds towards Zero (trims decimals). |
|
|
111
|
+
| `banker` | Rounds to the nearest even neighbor (minimizes statistical bias). |
|
|
102
112
|
|
|
103
|
-
###
|
|
113
|
+
### `FormattingConfigType`
|
|
104
114
|
|
|
105
|
-
|
|
|
106
|
-
| :---------- |
|
|
107
|
-
| `precision` | `number`
|
|
108
|
-
| `
|
|
109
|
-
| `prefix` | `string`
|
|
110
|
-
| `suffix` | `string`
|
|
111
|
-
| `isCompact` | `boolean`
|
|
112
|
-
| `
|
|
115
|
+
| Property | Type | Default | Description |
|
|
116
|
+
| :---------- | :---------------------------- | :---------- | :------------------------------------------------- |
|
|
117
|
+
| `precision` | `number` | `0` | Number of decimal places to maintain. |
|
|
118
|
+
| `rounding` | `RoundingMode` | `'half'` | Strategy used for rounding. |
|
|
119
|
+
| `prefix` | `string` | `""` | Text prepended to the result. |
|
|
120
|
+
| `suffix` | `string` | `""` | Text appended to the result. |
|
|
121
|
+
| `isCompact` | `boolean` | `false` | Whether to use K/M/B/T suffixes for large numbers. |
|
|
122
|
+
| `notation` | `'subscript' \| 'scientific'` | `undefined` | Special formatting for extreme values. |
|
|
113
123
|
|
|
114
124
|
## License
|
|
115
125
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Z=t=>t.replace(/0+$/,""),B=t=>t.replace(/^0+(?!$)/,"");function p(t){return t.replace(/^(-?)0+(?=\d)/,"$1").replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function z(t,r={}){const{fallback:n="--"}=r;if(typeof t=="number"||typeof t=="bigint")return t.toString();const e=String(t??"").trim();if(!e)return"0";const c=e.match(/^[\s+-]*/),i=(c&&c[0].match(/-/g)||[]).length%2===1,o=e.replace(/[$€£¥,_\s]/g,""),s=o.match(/^([+-]?\d+)\.0([\u2080-\u2089]+)([0-9]*)$/i);if(s){const[,l,K,Q]=s,V=K.split("").reduce((H,L)=>H+(L.charCodeAt(0)-8320),0),E=p(l.replace(/^[+-]/,"")+"."+"0".repeat(V)+Q);return(i?"-":"")+(E||"0")}const a=o.match(/[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?[KMBTQ]*/i);if(!a)return n;let f=a[0].toUpperCase(),b=0;const U=f.slice(-1),S="KMBTQ".indexOf(U);S!==-1&&(b=(S+1)*3,f=f.slice(0,-1));const h=f.match(/[E]([+-]?\d+)$/);if(h&&(b+=parseInt(h[1],10),f=f.slice(0,h.index)),f=f.replace(/^[+-]/,""),!/^(?:\d+\.?\d*|\.\d+)$/.test(f))return"0";const[D="0",F=""]=f.split("."),d=(D.replace(/^0+/,"")||"0")+F,x=F.length-b;let P;if(x<=0)P=(d+"0".repeat(-x)).replace(/^0+(?=\d)/,"")||"0";else{const l=d.length-x;P=l<=0?"0."+"0".repeat(-l)+d:d.slice(0,l)+"."+d.slice(l),P=p(P)}return(i?"-":"")+(P||"0")}function m(t){let r=String(t).trim();const n=r.startsWith("-")?"-":"";n&&(r=r.slice(1));const[e,c=""]=p(r).split(".");return{sign:n,intPart:e,fracPart:c}}const I=t=>t.fracPart?`${t.sign}${t.intPart}.${t.fracPart}`:`${t.sign}${t.intPart}`;function A(t){return typeof t=="object"&&t!==null&&"intPart"in t?t:m(t)}function $(t){if(!t)return{result:"",carry:!0};const n=(BigInt(t)+1n).toString();return n.length>t.length?{result:n.slice(1),carry:!0}:{result:n.padStart(t.length,"0"),carry:!1}}function g({intPart:t}){const{result:r,carry:n}=$(t);return{intPart:n?"1"+r:r,fracPart:""}}function k(t,r=0){const{intPart:n,fracPart:e}=t;if(!e||r>=e.length)return t;if(parseInt(e[r],10)>=5){if(r===0)return g(t);const i=e.slice(0,r),{result:o,carry:s}=$(i);return s?g(t):{intPart:n,fracPart:o}}return{intPart:n,fracPart:e.slice(0,r)}}function M(t,r=0){const{intPart:n,fracPart:e}=t;if(!e||r>=e.length)return t;if(r===0)return g(t);const c=e.slice(0,r),{result:i,carry:o}=$(c);return o?g(t):{intPart:n,fracPart:i}}function y(t,r=0){const{intPart:n,fracPart:e}=t;if(!e||r>=e.length)return t;const c=e.slice(0,r);return{intPart:n,fracPart:c.replace(/0+$/,"")}}function W(t,r=0){const{intPart:n,fracPart:e}=t;if(!e||r>=e.length)return t;const c=parseInt(e[r],10),i=e.slice(r+1).replace(/0+$/,""),o=c===5&&i==="";let s=!1;if(c>5||c===5&&i!=="")s=!0;else if(o){const a=r===0?n[n.length-1]:e[r-1];parseInt(a,10)%2!==0&&(s=!0)}if(s){if(r===0)return g(t);const{result:a,carry:f}=$(e.slice(0,r));return f?g(t):{intPart:n,fracPart:a}}return{intPart:n,fracPart:e.slice(0,r)}}function N(t,r={}){const n=r.rounding||"half",e=r.precision||0,{sign:c,intPart:i,fracPart:o}=t,s={intPart:i,fracPart:o};let a;switch(n){case"half":a=k(s,e);break;case"up":a=c?y(s,e):M(s,e);break;case"down":a=c?M(s,e):y(s,e);break;case"truncate":a=y(s,e);break;case"banker":a=W(s,e);break;default:a=k(s,e)}return{sign:c,...a}}function q(t,r={}){const n=N(m(t),r);return I(n)}const G=["K","M","B","T","Q"];function T(t,r={}){let{sign:n,intPart:e,fracPart:c}=t,i=e.length,o=-1;for(;i>3&&o<4;){const a=e.slice(0,i-3);c=e.slice(i-3)+c,e=a,i-=3,o++}const s=r.precision!=null?N({sign:n,intPart:e,fracPart:c},r):{intPart:e,fracPart:c};return{sign:n,intPart:B(s.intPart),fracPart:Z(s.fracPart),symbol:o>=0?G[o]:""}}function J(t,r={}){const{sign:n,intPart:e,fracPart:c,symbol:i}=T(m(t),r);return I({sign:n,intPart:e,fracPart:c})+i}function _(t){return typeof t=="object"&&"intPart"in t?t:m(t)}function j(t){const{sign:r,intPart:n,fracPart:e}=_(t),c=e.match(/^0+/)?.[0]?.length||0,i=c.toString().split("").reduce((o,s)=>o+String.fromCharCode(8320+Number(s)),"");return`${r}${n}.0${i}${e.slice(c)}`}function R(t){const{sign:r,intPart:n,fracPart:e}=t,c=r==="-"?"-":"";if(n!=="0"){const a=n.length-1,f=(n[0]+"."+(n.slice(1)+e).replace(/0+$/,"")).replace(/\.$/,"");return{value:`${c}${f}`,exponent:a,sign:"+"}}const i=e.search(/[1-9]/);if(i===-1)return{value:"0",exponent:0};const o=-(i+1),s=(e[i]+"."+e.slice(i+1).replace(/0+$/,"")).replace(/\.$/,"");return{value:`${c}${s}`,exponent:o,sign:""}}function O(t){const{value:r,exponent:n,sign:e}=R(_(t));return n!==0?`${r}e${e}${n}`:r}function u(t){return{round:r=>{const n=N(t,r);return u({...t,...n})},compact:r=>{if(t.compactedSymbol)return u(t);const n=T(t,r);return u({...t,intPart:n.intPart,fracPart:n.fracPart,compactedSymbol:n.symbol})},notation(r="scientific"){return u({...t,notation:r})},prefix(r){return u({...t,prefix:r})},suffix(r){return u({...t,suffix:r})},toNumber(){const{sign:r,intPart:n,fracPart:e,prefix:c,suffix:i,compactedSymbol:o,notation:s}=t;let a=e.length>0?`${n}.${e}`:n;return s==="scientific"?a=O({sign:"",intPart:n,fracPart:e}):s==="subscript"&&(a=j({sign:"",intPart:n,fracPart:e})),`${c||""}${r}${a}${o||""}${i||""}`},toObject(){return t}}}function w(t){return u(A(t))}function C(t,r={}){let n=w(t).round(r);return r.isCompact&&(n=n.compact(r)),r.notation&&(n=n.notation(r.notation)),r.prefix&&(n=n.prefix(r.prefix)),r.suffix&&(n=n.suffix(r.suffix)),n.toNumber()}function X(t={}){return r=>C(r,t)}exports.FN=w;exports.clearLeadingZero=B;exports.clearTrailingZero=Z;exports.clearUnnecessaryZero=p;exports.compact=J;exports.createFormatFunction=X;exports.formatNumber=C;exports.getBaseNumberNumber=m;exports.parseNum=z;exports.round=q;exports.scientific=O;exports.subscript=j;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,212 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
1
|
+
export declare type BaseObjectNumberType = {
|
|
2
|
+
sign: SignType;
|
|
3
|
+
} & BasePositiveNumberType;
|
|
4
|
+
|
|
5
|
+
export declare type BasePositiveNumberType = {
|
|
6
|
+
intPart: string;
|
|
7
|
+
fracPart: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Removes leading zeros from a string (e.g., '005' -> '5').
|
|
12
|
+
*/
|
|
13
|
+
export declare const clearLeadingZero: (value: string) => string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Removes trailing zeros from a decimal string (e.g., '1.500' -> '1.5').
|
|
17
|
+
*/
|
|
18
|
+
export declare const clearTrailingZero: (value: string) => string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Removes redundant leading and trailing zeros from a number string.
|
|
22
|
+
*/
|
|
23
|
+
export declare function clearUnnecessaryZero(value: string): string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Formats a number into a short, human-readable string with a suffix (e.g., K, M, B).
|
|
27
|
+
*
|
|
28
|
+
* @param value - The value to be compacted. Can be a number, string, or bigint.
|
|
29
|
+
* @param options - Configuration for rounding the compacted value.
|
|
30
|
+
* @param options.rounding - The rounding strategy to use. Default is 'half'.
|
|
31
|
+
* @param options.precision - The number of decimal places to include after compacting. Default is 0.
|
|
32
|
+
* @returns The compacted number with a suffix as a string.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* compact(1500); // '1.5K'
|
|
36
|
+
* compact(1200000, { precision: 1 }); // '1.2M'
|
|
37
|
+
* compact('1000000000000'); // '1T'
|
|
38
|
+
*/
|
|
39
|
+
export declare function compact(value: NumberType, options?: RoundingConfigType): string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a reusable formatting function with predefined options.
|
|
43
|
+
* Useful for applying consistent formatting across multiple numbers.
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration options for the formatter.
|
|
46
|
+
* @returns A function that accepts a value and returns the formatted string.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const formatCurrency = createFormatFunction({ prefix: '$', precision: 2 });
|
|
50
|
+
* formatCurrency(1234.56); // '$1234.56'
|
|
51
|
+
* formatCurrency(100); // '$100.00'
|
|
52
|
+
*/
|
|
53
|
+
export declare function createFormatFunction(options?: FormattingConfigType): (value: NumberType) => string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Fluent API for performing multiple formatting operations in a readable chain.
|
|
57
|
+
* Wraps the input value into a formatter object that supports rounding,
|
|
58
|
+
* compacting, notation, prefixes, and suffixes.
|
|
59
|
+
*
|
|
60
|
+
* @param value - The initial value to format, can be a number, string, or bigint.
|
|
61
|
+
* @returns A chainable `FNType` object.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* FN('1234567.89')
|
|
65
|
+
* .round({ precision: 0 })
|
|
66
|
+
* .prefix('$')
|
|
67
|
+
* .toNumber(); // '$1234568'
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* FN(1000000)
|
|
71
|
+
* .compact()
|
|
72
|
+
* .suffix(' total')
|
|
73
|
+
* .toNumber(); // '1M total'
|
|
74
|
+
*/
|
|
75
|
+
export declare function FN(value: NumberType | ObjectNumberType): FNType;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Fluent API interface for chainable number formatting operations.
|
|
79
|
+
*/
|
|
80
|
+
export declare type FNType = {
|
|
81
|
+
/**
|
|
82
|
+
* Rounds the number based on the provided precision and strategy.
|
|
83
|
+
* @param options - Rounding configuration (precision, rounding strategy).
|
|
84
|
+
* @returns The same FNType instance for further chaining.
|
|
85
|
+
*/
|
|
86
|
+
round(options?: RoundingConfigType): FNType;
|
|
87
|
+
/**
|
|
88
|
+
* Converts the number to a compact representation (e.g., 1K, 1M, 1B).
|
|
89
|
+
* @param options - Optional rounding configuration to apply during compaction.
|
|
90
|
+
* @returns The same FNType instance for further chaining.
|
|
91
|
+
*/
|
|
92
|
+
compact(options?: RoundingConfigType): FNType;
|
|
93
|
+
/**
|
|
94
|
+
* Sets the notation style for the number (e.g., scientific, subscript).
|
|
95
|
+
* @param mode - The notation mode to use. Defaults to 'scientific'.
|
|
96
|
+
* @returns The same FNType instance for further chaining.
|
|
97
|
+
*/
|
|
98
|
+
notation(mode?: NotationMode): FNType;
|
|
99
|
+
/**
|
|
100
|
+
* Adds a prefix to the formatted number string.
|
|
101
|
+
* @param symbol - The string to prepend.
|
|
102
|
+
* @returns The same FNType instance for further chaining.
|
|
103
|
+
*/
|
|
104
|
+
prefix(symbol: string): FNType;
|
|
105
|
+
/**
|
|
106
|
+
* Adds a suffix to the formatted number string.
|
|
107
|
+
* @param symbol - The string to append.
|
|
108
|
+
* @returns The same FNType instance for further chaining.
|
|
109
|
+
*/
|
|
110
|
+
suffix(symbol: string): FNType;
|
|
111
|
+
/**
|
|
112
|
+
* Finalizes the chain and returns the formatted number as a string.
|
|
113
|
+
* @returns The final formatted number string.
|
|
114
|
+
*/
|
|
115
|
+
toNumber(): string;
|
|
116
|
+
/**
|
|
117
|
+
* Returns the internal state of the number as an ObjectNumberType.
|
|
118
|
+
* @returns The internal object representation.
|
|
119
|
+
*/
|
|
120
|
+
toObject(): ObjectNumberType;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Comprehensive formatting function that combines rounding, compacting, and metadata.
|
|
125
|
+
* This is the quickest way to format a number with multiple options in a single call.
|
|
126
|
+
*
|
|
127
|
+
* @param value - The value to format (number, string, or bigint).
|
|
128
|
+
* @param options - Formatting configuration (precision, rounding strategy, compact mode, prefix, suffix, and notation style).
|
|
129
|
+
* @returns The final formatted number string.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* formatNumber(1234.56, { prefix: '$', precision: 1 }); // '$1234.6'
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* formatNumber(1500000, { isCompact: true, notation: 'scientific' }); // '1.5M' (compacted before potential notation)
|
|
136
|
+
*/
|
|
137
|
+
export declare function formatNumber(value: NumberType, options?: FormattingConfigType): string;
|
|
138
|
+
|
|
139
|
+
/** Full configuration options for the formatNumber function */
|
|
140
|
+
export declare type FormattingConfigType = RoundingConfigType & NumberConfigType & Partial<{
|
|
141
|
+
/** Whether to use compact notation (K, M, B, etc.) */
|
|
142
|
+
isCompact: boolean;
|
|
143
|
+
}>;
|
|
144
|
+
|
|
145
|
+
export declare function getBaseNumberNumber(value: NumberType): BaseObjectNumberType;
|
|
146
|
+
|
|
147
|
+
/** Modes for special numerical notation */
|
|
148
|
+
export declare type NotationMode = 'subscript' | 'scientific';
|
|
149
|
+
|
|
150
|
+
/** Configuration for basic number display properties */
|
|
151
|
+
export declare type NumberConfigType = Partial<{
|
|
152
|
+
/** Text to prepend to the number (e.g., '$') */
|
|
153
|
+
prefix: string;
|
|
154
|
+
/** Text to append to the number (e.g., ' units') */
|
|
155
|
+
suffix: string;
|
|
156
|
+
/** The notation style to apply */
|
|
157
|
+
notation: NotationMode;
|
|
158
|
+
}>;
|
|
159
|
+
|
|
160
|
+
/** Supported input types for number formatting */
|
|
161
|
+
export declare type NumberType = number | string | bigint;
|
|
162
|
+
|
|
163
|
+
/** Internal object structure containing full formatting state */
|
|
164
|
+
export declare type ObjectNumberType = BaseObjectNumberType & NumberConfigType & Partial<{
|
|
165
|
+
/** The symbol used for compact notation (e.g., 'K', 'M') */
|
|
166
|
+
compactedSymbol: string;
|
|
167
|
+
}>;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Parses various input formats into a standard decimal string.
|
|
171
|
+
*/
|
|
172
|
+
export declare function parseNum(value: NumberType, options?: ParseNumberParamsType): string;
|
|
173
|
+
|
|
174
|
+
/** Configuration for input parsing */
|
|
175
|
+
export declare type ParseNumberParamsType = Partial<{
|
|
176
|
+
/** Value to return if parsing fails. Default is '--' */
|
|
177
|
+
fallback: string;
|
|
178
|
+
}>;
|
|
179
|
+
|
|
180
|
+
export declare function round(value: NumberType, options?: RoundingConfigType): string;
|
|
181
|
+
|
|
182
|
+
/** Configuration for rounding operations */
|
|
183
|
+
export declare type RoundingConfigType = Partial<{
|
|
184
|
+
/** The rounding strategy to use */
|
|
185
|
+
rounding: RoundingMode;
|
|
186
|
+
/** Number of decimal places to keep */
|
|
187
|
+
precision: number;
|
|
188
|
+
}>;
|
|
189
|
+
|
|
190
|
+
/** Strategies for rounding numbers */
|
|
191
|
+
export declare type RoundingMode = 'half' | 'up' | 'down' | 'banker' | 'truncate';
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Formats a number string using standard scientific notation.
|
|
195
|
+
*/
|
|
196
|
+
export declare function scientific(value: NumberType | BaseObjectNumberType): string;
|
|
197
|
+
|
|
198
|
+
export declare type ScientificReturnType = {
|
|
199
|
+
value: string;
|
|
200
|
+
exponent: number;
|
|
201
|
+
sign?: string;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/** Possible sign values for internal number representation */
|
|
205
|
+
export declare type SignType = '-' | '';
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Formats the decimal part of small numbers using subscript characters.
|
|
209
|
+
*/
|
|
210
|
+
export declare function subscript(value: NumberType | BaseObjectNumberType): string;
|
|
211
|
+
|
|
212
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,125 +1,223 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
function u(s) {
|
|
16
|
-
let r = "", t = s.length - 1, e = "1";
|
|
17
|
-
for (; t >= 0; ) {
|
|
18
|
-
const n = s[t];
|
|
19
|
-
let i = Number(n) + (e ? 1 : 0);
|
|
20
|
-
i >= 10 ? (e = "1", i -= 10) : e = "", r = i.toString() + r, t--;
|
|
1
|
+
const K = (t) => t.replace(/0+$/, ""), Q = (t) => t.replace(/^0+(?!$)/, "");
|
|
2
|
+
function y(t) {
|
|
3
|
+
return t.replace(/^(-?)0+(?=\d)/, "$1").replace(/(\.\d*?)0+$/, "$1").replace(/\.$/, "");
|
|
4
|
+
}
|
|
5
|
+
function G(t, r = {}) {
|
|
6
|
+
const { fallback: n = "--" } = r;
|
|
7
|
+
if (typeof t == "number" || typeof t == "bigint") return t.toString();
|
|
8
|
+
const e = String(t ?? "").trim();
|
|
9
|
+
if (!e) return "0";
|
|
10
|
+
const c = e.match(/^[\s+-]*/), i = (c && c[0].match(/-/g) || []).length % 2 === 1, o = e.replace(/[$€£¥,_\s]/g, ""), s = o.match(/^([+-]?\d+)\.0([\u2080-\u2089]+)([0-9]*)$/i);
|
|
11
|
+
if (s) {
|
|
12
|
+
const [, l, C, O] = s, T = C.split("").reduce((U, D) => U + (D.charCodeAt(0) - 8320), 0), Z = y(l.replace(/^[+-]/, "") + "." + "0".repeat(T) + O);
|
|
13
|
+
return (i ? "-" : "") + (Z || "0");
|
|
21
14
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
15
|
+
const a = o.match(/[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?[KMBTQ]*/i);
|
|
16
|
+
if (!a) return n;
|
|
17
|
+
let f = a[0].toUpperCase(), $ = 0;
|
|
18
|
+
const j = f.slice(-1), N = "KMBTQ".indexOf(j);
|
|
19
|
+
N !== -1 && ($ = (N + 1) * 3, f = f.slice(0, -1));
|
|
20
|
+
const h = f.match(/[E]([+-]?\d+)$/);
|
|
21
|
+
if (h && ($ += parseInt(h[1], 10), f = f.slice(0, h.index)), f = f.replace(/^[+-]/, ""), !/^(?:\d+\.?\d*|\.\d+)$/.test(f)) return "0";
|
|
22
|
+
const [w = "0", k = ""] = f.split("."), P = (w.replace(/^0+/, "") || "0") + k, b = k.length - $;
|
|
23
|
+
let d;
|
|
24
|
+
if (b <= 0)
|
|
25
|
+
d = (P + "0".repeat(-b)).replace(/^0+(?=\d)/, "") || "0";
|
|
26
|
+
else {
|
|
27
|
+
const l = P.length - b;
|
|
28
|
+
d = l <= 0 ? "0." + "0".repeat(-l) + P : P.slice(0, l) + "." + P.slice(l), d = y(d);
|
|
29
|
+
}
|
|
30
|
+
return (i ? "-" : "") + (d || "0");
|
|
31
|
+
}
|
|
32
|
+
function p(t) {
|
|
33
|
+
let r = String(t).trim();
|
|
34
|
+
const n = r.startsWith("-") ? "-" : "";
|
|
35
|
+
n && (r = r.slice(1));
|
|
36
|
+
const [e, c = ""] = y(r).split(".");
|
|
37
|
+
return { sign: n, intPart: e, fracPart: c };
|
|
38
|
+
}
|
|
39
|
+
const _ = (t) => t.fracPart ? `${t.sign}${t.intPart}.${t.fracPart}` : `${t.sign}${t.intPart}`;
|
|
40
|
+
function V(t) {
|
|
41
|
+
return typeof t == "object" && t !== null && "intPart" in t ? t : p(t);
|
|
42
|
+
}
|
|
43
|
+
function m(t) {
|
|
44
|
+
if (!t) return { result: "", carry: !0 };
|
|
45
|
+
const n = (BigInt(t) + 1n).toString();
|
|
46
|
+
return n.length > t.length ? { result: n.slice(1), carry: !0 } : { result: n.padStart(t.length, "0"), carry: !1 };
|
|
47
|
+
}
|
|
48
|
+
function g({ intPart: t }) {
|
|
49
|
+
const { result: r, carry: n } = m(t);
|
|
50
|
+
return { intPart: n ? "1" + r : r, fracPart: "" };
|
|
51
|
+
}
|
|
52
|
+
function I(t, r = 0) {
|
|
53
|
+
const { intPart: n, fracPart: e } = t;
|
|
54
|
+
if (!e || r >= e.length) return t;
|
|
55
|
+
if (parseInt(e[r], 10) >= 5) {
|
|
56
|
+
if (r === 0) return g(t);
|
|
57
|
+
const i = e.slice(0, r), { result: o, carry: s } = m(i);
|
|
58
|
+
return s ? g(t) : { intPart: n, fracPart: o };
|
|
59
|
+
}
|
|
60
|
+
return { intPart: n, fracPart: e.slice(0, r) };
|
|
61
|
+
}
|
|
62
|
+
function M(t, r = 0) {
|
|
63
|
+
const { intPart: n, fracPart: e } = t;
|
|
64
|
+
if (!e || r >= e.length) return t;
|
|
65
|
+
if (r === 0) return g(t);
|
|
66
|
+
const c = e.slice(0, r), { result: i, carry: o } = m(c);
|
|
67
|
+
return o ? g(t) : { intPart: n, fracPart: i };
|
|
68
|
+
}
|
|
69
|
+
function x(t, r = 0) {
|
|
70
|
+
const { intPart: n, fracPart: e } = t;
|
|
71
|
+
if (!e || r >= e.length) return t;
|
|
72
|
+
const c = e.slice(0, r);
|
|
73
|
+
return { intPart: n, fracPart: c.replace(/0+$/, "") };
|
|
74
|
+
}
|
|
75
|
+
function E(t, r = 0) {
|
|
76
|
+
const { intPart: n, fracPart: e } = t;
|
|
77
|
+
if (!e || r >= e.length) return t;
|
|
78
|
+
const c = parseInt(e[r], 10), i = e.slice(r + 1).replace(/0+$/, ""), o = c === 5 && i === "";
|
|
79
|
+
let s = !1;
|
|
80
|
+
if (c > 5 || c === 5 && i !== "")
|
|
81
|
+
s = !0;
|
|
82
|
+
else if (o) {
|
|
83
|
+
const a = r === 0 ? n[n.length - 1] : e[r - 1];
|
|
84
|
+
parseInt(a, 10) % 2 !== 0 && (s = !0);
|
|
85
|
+
}
|
|
86
|
+
if (s) {
|
|
87
|
+
if (r === 0) return g(t);
|
|
88
|
+
const { result: a, carry: f } = m(e.slice(0, r));
|
|
89
|
+
return f ? g(t) : { intPart: n, fracPart: a };
|
|
90
|
+
}
|
|
91
|
+
return { intPart: n, fracPart: e.slice(0, r) };
|
|
92
|
+
}
|
|
93
|
+
function S(t, r = {}) {
|
|
94
|
+
const n = r.rounding || "half", e = r.precision || 0, { sign: c, intPart: i, fracPart: o } = t, s = { intPart: i, fracPart: o };
|
|
95
|
+
let a;
|
|
96
|
+
switch (n) {
|
|
72
97
|
case "half":
|
|
73
|
-
|
|
98
|
+
a = I(s, e);
|
|
99
|
+
break;
|
|
74
100
|
case "up":
|
|
75
|
-
|
|
101
|
+
a = c ? x(s, e) : M(s, e);
|
|
102
|
+
break;
|
|
76
103
|
case "down":
|
|
77
|
-
|
|
104
|
+
a = c ? M(s, e) : x(s, e);
|
|
105
|
+
break;
|
|
78
106
|
case "truncate":
|
|
79
|
-
|
|
107
|
+
a = x(s, e);
|
|
108
|
+
break;
|
|
80
109
|
case "banker":
|
|
81
|
-
|
|
110
|
+
a = E(s, e);
|
|
111
|
+
break;
|
|
112
|
+
default:
|
|
113
|
+
a = I(s, e);
|
|
82
114
|
}
|
|
115
|
+
return { sign: c, ...a };
|
|
83
116
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
117
|
+
function J(t, r = {}) {
|
|
118
|
+
const n = S(p(t), r);
|
|
119
|
+
return _(n);
|
|
120
|
+
}
|
|
121
|
+
const H = ["K", "M", "B", "T", "Q"];
|
|
122
|
+
function B(t, r = {}) {
|
|
123
|
+
let { sign: n, intPart: e, fracPart: c } = t, i = e.length, o = -1;
|
|
124
|
+
for (; i > 3 && o < 4; ) {
|
|
125
|
+
const a = e.slice(0, i - 3);
|
|
126
|
+
c = e.slice(i - 3) + c, e = a, i -= 3, o++;
|
|
127
|
+
}
|
|
128
|
+
const s = r.precision != null ? S({ sign: n, intPart: e, fracPart: c }, r) : { intPart: e, fracPart: c };
|
|
129
|
+
return {
|
|
130
|
+
sign: n,
|
|
131
|
+
intPart: Q(s.intPart),
|
|
132
|
+
fracPart: K(s.fracPart),
|
|
133
|
+
symbol: o >= 0 ? H[o] : ""
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function R(t, r = {}) {
|
|
137
|
+
const { sign: n, intPart: e, fracPart: c, symbol: i } = B(p(t), r);
|
|
138
|
+
return _({ sign: n, intPart: e, fracPart: c }) + i;
|
|
139
|
+
}
|
|
140
|
+
function F(t) {
|
|
141
|
+
return typeof t == "object" && "intPart" in t ? t : p(t);
|
|
142
|
+
}
|
|
143
|
+
function z(t) {
|
|
144
|
+
const { sign: r, intPart: n, fracPart: e } = F(t), c = e.match(/^0+/)?.[0]?.length || 0, i = c.toString().split("").reduce((o, s) => o + String.fromCharCode(8320 + Number(s)), "");
|
|
145
|
+
return `${r}${n}.0${i}${e.slice(c)}`;
|
|
146
|
+
}
|
|
147
|
+
function A(t) {
|
|
148
|
+
const { sign: r, intPart: n, fracPart: e } = t, c = r === "-" ? "-" : "";
|
|
149
|
+
if (n !== "0") {
|
|
150
|
+
const a = n.length - 1, f = (n[0] + "." + (n.slice(1) + e).replace(/0+$/, "")).replace(
|
|
151
|
+
/\.$/,
|
|
152
|
+
""
|
|
153
|
+
);
|
|
154
|
+
return { value: `${c}${f}`, exponent: a, sign: "+" };
|
|
92
155
|
}
|
|
93
|
-
|
|
156
|
+
const i = e.search(/[1-9]/);
|
|
157
|
+
if (i === -1) return { value: "0", exponent: 0 };
|
|
158
|
+
const o = -(i + 1), s = (e[i] + "." + e.slice(i + 1).replace(/0+$/, "")).replace(/\.$/, "");
|
|
159
|
+
return { value: `${c}${s}`, exponent: o, sign: "" };
|
|
94
160
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const e = r.prefix || "", n = r.suffix || "", i = r.isSmall || !1;
|
|
99
|
-
return `${e}${i ? P(t) : t}${n}`;
|
|
161
|
+
function L(t) {
|
|
162
|
+
const { value: r, exponent: n, sign: e } = A(F(t));
|
|
163
|
+
return n !== 0 ? `${r}e${e}${n}` : r;
|
|
100
164
|
}
|
|
101
|
-
function
|
|
165
|
+
function u(t) {
|
|
102
166
|
return {
|
|
103
|
-
round(r
|
|
104
|
-
|
|
167
|
+
round: (r) => {
|
|
168
|
+
const n = S(t, r);
|
|
169
|
+
return u({ ...t, ...n });
|
|
170
|
+
},
|
|
171
|
+
compact: (r) => {
|
|
172
|
+
if (t.compactedSymbol) return u(t);
|
|
173
|
+
const n = B(t, r);
|
|
174
|
+
return u({
|
|
175
|
+
...t,
|
|
176
|
+
intPart: n.intPart,
|
|
177
|
+
fracPart: n.fracPart,
|
|
178
|
+
compactedSymbol: n.symbol
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
notation(r = "scientific") {
|
|
182
|
+
return u({ ...t, notation: r });
|
|
183
|
+
},
|
|
184
|
+
prefix(r) {
|
|
185
|
+
return u({ ...t, prefix: r });
|
|
105
186
|
},
|
|
106
|
-
|
|
107
|
-
return
|
|
187
|
+
suffix(r) {
|
|
188
|
+
return u({ ...t, suffix: r });
|
|
108
189
|
},
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
190
|
+
toNumber() {
|
|
191
|
+
const { sign: r, intPart: n, fracPart: e, prefix: c, suffix: i, compactedSymbol: o, notation: s } = t;
|
|
192
|
+
let a = e.length > 0 ? `${n}.${e}` : n;
|
|
193
|
+
return s === "scientific" ? a = L({ sign: "", intPart: n, fracPart: e }) : s === "subscript" && (a = z({ sign: "", intPart: n, fracPart: e })), `${c || ""}${r}${a}${o || ""}${i || ""}`;
|
|
112
194
|
},
|
|
113
|
-
|
|
114
|
-
return
|
|
195
|
+
toObject() {
|
|
196
|
+
return t;
|
|
115
197
|
}
|
|
116
198
|
};
|
|
117
199
|
}
|
|
200
|
+
function W(t) {
|
|
201
|
+
return u(V(t));
|
|
202
|
+
}
|
|
203
|
+
function q(t, r = {}) {
|
|
204
|
+
let n = W(t).round(r);
|
|
205
|
+
return r.isCompact && (n = n.compact(r)), r.notation && (n = n.notation(r.notation)), r.prefix && (n = n.prefix(r.prefix)), r.suffix && (n = n.suffix(r.suffix)), n.toNumber();
|
|
206
|
+
}
|
|
207
|
+
function X(t = {}) {
|
|
208
|
+
return (r) => q(r, t);
|
|
209
|
+
}
|
|
118
210
|
export {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
211
|
+
W as FN,
|
|
212
|
+
Q as clearLeadingZero,
|
|
213
|
+
K as clearTrailingZero,
|
|
214
|
+
y as clearUnnecessaryZero,
|
|
215
|
+
R as compact,
|
|
216
|
+
X as createFormatFunction,
|
|
217
|
+
q as formatNumber,
|
|
218
|
+
p as getBaseNumberNumber,
|
|
219
|
+
G as parseNum,
|
|
220
|
+
J as round,
|
|
221
|
+
L as scientific,
|
|
222
|
+
z as subscript
|
|
125
223
|
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter-present/format-number",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.12",
|
|
4
|
+
"description": "A lightweight, zero-dependency JavaScript/TypeScript library for precise number formatting. Designed for financial applications, crypto dashboards, and scientific data where precision is paramount.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
9
17
|
"files": [
|
|
10
18
|
"dist"
|
|
11
19
|
],
|
|
12
20
|
"scripts": {
|
|
13
|
-
"build": "bun run clear &&
|
|
21
|
+
"build": "bun run clear && vite build",
|
|
14
22
|
"preview": "vite preview",
|
|
15
23
|
"clear": "rm -rf dist",
|
|
16
24
|
"test": "vitest run",
|
package/dist/compact.d.ts
DELETED
package/dist/format.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { MetadataType, NumberType, OtherMetadataType, RoundingConfigType } from './types';
|
|
2
|
-
export declare function formatNumber(value: NumberType, options?: MetadataType): string;
|
|
3
|
-
export declare function FN(value: NumberType): {
|
|
4
|
-
round(options?: RoundingConfigType): any;
|
|
5
|
-
compact(options?: RoundingConfigType): string;
|
|
6
|
-
format(options?: OtherMetadataType): string;
|
|
7
|
-
toString(): string;
|
|
8
|
-
};
|
package/dist/round.d.ts
DELETED
package/dist/types.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type NumberType = number | string | bigint;
|
|
2
|
-
export type RoundingMode = 'half' | 'up' | 'down' | 'banker' | 'truncate';
|
|
3
|
-
export type RoundingConfigType = Partial<{
|
|
4
|
-
mode: RoundingMode;
|
|
5
|
-
precision: number;
|
|
6
|
-
}>;
|
|
7
|
-
export type OtherMetadataType = Partial<{
|
|
8
|
-
prefix: string;
|
|
9
|
-
suffix: string;
|
|
10
|
-
isSmall: boolean;
|
|
11
|
-
}>;
|
|
12
|
-
export type MetadataType = RoundingConfigType & OtherMetadataType & Partial<{
|
|
13
|
-
isCompact: boolean;
|
|
14
|
-
}>;
|
package/dist/utils.d.ts
DELETED