reduce-precision 0.0.3 → 1.0.1

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 CHANGED
@@ -34,17 +34,29 @@ npm install reduce-precision
34
34
  ### JavaScript (CommonJS)
35
35
 
36
36
  ```javascript
37
- const { format } = require('reduce-precision');
37
+ const { NumberFormatter } = require('reduce-precision');
38
38
 
39
- const formatted = format(123456, options);
39
+ const formatter = new NumberFormatter();
40
+
41
+ formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' });
42
+
43
+ console.log(formatter.toHtmlString(123456789));
44
+ console.log(formatter.format(123456789));
45
+ console.log(formatter.toString(123456789));
40
46
  ```
41
47
 
42
48
  ### TypeScript or ES Modules
43
49
 
44
50
  ```typescript
45
- import { format } from 'reduce-precision';
51
+ import { NumberFormatter } from 'reduce-precision';
52
+
53
+ const formatter = new NumberFormatter();
46
54
 
47
- const formatted = format(123456, options);
55
+ formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' });
56
+
57
+ console.log(formatter.toHtmlString(123456789));
58
+ console.log(formatter.format(123456789));
59
+ console.log(formatter.toString(123456789));
48
60
  ```
49
61
 
50
62
  ## Options
@@ -65,32 +77,118 @@ The `format` function accepts an optional `options` object with the following pr
65
77
  ## Examples
66
78
 
67
79
  ```typescript
68
- import { format } from 'reduce-precision';
80
+ import { NumberFormatter } from 'reduce-precision';
81
+
82
+ // Create a formatter instance with default options
83
+ const formatter = new NumberFormatter();
84
+
85
+ // Create a formatter instance with custom options
86
+ const formatterWithOptions = new NumberFormatter({
87
+ language: 'fa',
88
+ template: 'irr',
89
+ precision: 'medium',
90
+ prefixMarker: 'strong',
91
+ postfixMarker: 'em',
92
+ prefix: 'مبلغ: ',
93
+ postfix: ' ریال'
94
+ });
95
+
96
+ // Basic usage
97
+ formatter.setLanguage('en');
69
98
 
70
99
  // Basic number formatting
71
- format(1234.5678); // Output: 1,234.5678
100
+ formatter.format(1234.5678); // Output: { value: '1,234.6', ... }
72
101
 
73
102
  // Formatting with medium precision
74
- format(1234.5678, { precision: 'medium' }); // Output: 1,234.57
103
+ formatter.setTemplate('number', 'medium').format(1234.5678); // Output: { value: '1.23K', ... }
75
104
 
76
105
  // Formatting as USD
77
- format(1234.5678, { template: 'usd' }); // Output: $1,234.5678
106
+ formatter.setTemplate('usd', 'high').format(1234.5678); // Output: { value: '$1,234.6', ... }
78
107
 
79
108
  // Formatting as Iranian Rial with Persian numerals
80
- format(1234.5678, { template: 'irr', language: 'fa' }); // Output: ۱٬۲۳۴٫۵۷ ر
109
+ formatterWithOptions.format(1234.5678);
110
+ // Output: { value: 'مبلغ: ۱٫۲۳ هزار ت', ... }
81
111
 
82
112
  // Formatting as a percentage with low precision
83
- format(0.1234, { template: 'percent', precision: 'low' }); // Output: 12%
113
+ formatter.setTemplate('percent', 'low').format(0.1234); // Output: { value: '0.12%', ... }
84
114
 
85
115
  // Formatting with HTML output and custom markers
86
- format(1234.5678, { outputFormat: 'html', prefixMarker: 'strong', prefix: 'USD ' });
87
- // Output: <strong>USD </strong>1,234.5678
116
+
117
+ formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' }).toHtmlString(1234.5678);
118
+ // Output: <strong>USD </strong>1,234.6
88
119
 
89
120
  // Formatting with string input for small or big numbers
90
- format("0.00000000000000000000005678521", { template: 'usd', precision: 'medium' });
91
- // Output: $0.0₂₂5678
121
+
122
+ formatter.setTemplate('usd', 'medium').format("0.00000000000000000000005678521");
123
+ // Output: { value: '$0.0₂₂5678', ... }
92
124
  ```
93
125
 
126
+ ## API
127
+
128
+ ### `FormattedObject` Interface
129
+
130
+ The `FormattedObject` interface represents the structure of the formatted number object returned by the `format` method.
131
+
132
+ ```typescript
133
+ interface FormattedObject {
134
+ value: string; // The formatted value as a string
135
+ prefix: string; // The prefix string
136
+ postfix: string; // The postfix string
137
+ sign: string; // The sign of the number (either an empty string or '-')
138
+ wholeNumber: string; // The whole number part of the value
139
+ fractionalPart: string; // The complete fractional part of the value
140
+ fractionalNonZeros: string; // The non-zero digits in the fractional part
141
+ fractionalZerosCount: number; // The count of zeros in the fractional part
142
+ unit: string; // The unit postfix
143
+ }
144
+ ```
145
+
146
+ ### `NumberFormatter` Class
147
+
148
+ #### `constructor(options?: FormatterOptions)`
149
+
150
+ Creates a new instance of the `NumberFormatter` class with optional configuration options.
151
+
152
+ - `options` (optional): An object containing the initial configuration options for the formatter.
153
+
154
+ #### `setLanguage(lang: Language, config?: LanguageConfig): NumberFormatter`
155
+
156
+ Sets the language and optional language configuration for the formatter.
157
+
158
+ - `lang`: The language to be used for formatting (`'en'` for English or `'fa'` for Persian).
159
+ - `config` (optional): An object containing additional language configuration options.
160
+ - `prefixMarker` (optional): The marker for the prefix in HTML or Markdown output (default: `'i'`).
161
+ - `postfixMarker` (optional): The marker for the postfix in HTML or Markdown output (default: `'i'`).
162
+ - `prefix` (optional): The prefix string to be added before the formatted number.
163
+ - `postfix` (optional): The postfix string to be added after the formatted number.
164
+
165
+ Returns the `NumberFormatter` instance for method chaining.
166
+
167
+ #### `setTemplate(template: Template, precision: Precision): NumberFormatter`
168
+
169
+ Sets the template and precision for the formatter.
170
+
171
+ - `template`: The template to be used for formatting (`'number'`, `'usd'`, `'irt'`, `'irr'`, or `'percent'`).
172
+ - `precision`: The precision level for formatting (`'high'`, `'medium'`, `'low'`, or `'auto'`).
173
+
174
+ Returns the `NumberFormatter` instance for method chaining.
175
+
176
+ #### `format(input: string | number): FormattedObject`
177
+
178
+ Formats the input number and returns the formatted object.
179
+
180
+ - `input`: The number to be formatted, either as a string or a number.
181
+
182
+ Returns the formatted object.
183
+
184
+ #### `toHtmlString(): string`
185
+
186
+ Returns the formatted value as an HTML string.
187
+
188
+ #### `toString(): string`
189
+
190
+ Returns the formatted value as a plain string.
191
+
94
192
  ## TypeScript
95
193
 
96
194
  `reduce-precision` is written in TypeScript and includes type definitions for all exported functions and interfaces.
@@ -12,5 +12,36 @@ interface Options {
12
12
  prefix?: string;
13
13
  postfix?: string;
14
14
  }
15
- declare function format(input: string | number, options?: Options): string | number;
16
- export default format;
15
+ interface FormattedObject {
16
+ value?: string;
17
+ prefix: string;
18
+ postfix: string;
19
+ sign: string;
20
+ wholeNumber: string;
21
+ fractionalPart: string;
22
+ fractionalNonZeros: string;
23
+ fractionalZerosCount: number;
24
+ unit?: string;
25
+ }
26
+ interface LanguageConfig {
27
+ prefixMarker?: string;
28
+ postfixMarker?: string;
29
+ prefix?: string;
30
+ postfix?: string;
31
+ }
32
+ declare class NumberFormatter {
33
+ private options;
34
+ constructor(options?: Options);
35
+ setLanguage(lang: Language, config?: LanguageConfig): NumberFormatter;
36
+ setTemplate(template: Template, precision: Precision): NumberFormatter;
37
+ toJson(input: string | number): FormattedObject;
38
+ toString(input: string | number): string;
39
+ toPlainString(input: string | number): string;
40
+ toHtmlString(input: string | number): string;
41
+ toMdString(input: string | number): string;
42
+ private isENotation;
43
+ private format;
44
+ private convertENotationToRegularNumber;
45
+ private reducePrecision;
46
+ }
47
+ export default NumberFormatter;