|
| 1 | +/******************************************************************************* |
| 2 | + * (c) 2023 unipackage |
| 3 | + * |
| 4 | + * Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0 |
| 5 | + * (the "Apache License"). You may not use this file except in compliance with one of these |
| 6 | + * licenses. You may obtain a copy of the MIT License at |
| 7 | + * |
| 8 | + * https://opensource.org/licenses/MIT |
| 9 | + * |
| 10 | + * Or the Apache License, Version 2.0 at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the MIT License or the Apache License for the specific language governing permissions and |
| 18 | + * limitations under the respective licenses. |
| 19 | + ********************************************************************************/ |
| 20 | + |
1 | 21 | import { FormatNumber, FormatPercent } from "num-format" |
2 | 22 |
|
| 23 | +/** |
| 24 | + * Represents an enhanced number with additional formatting and arithmetic operations. |
| 25 | + */ |
3 | 26 | export interface EnhanceNumber extends Number { |
| 27 | + /** |
| 28 | + * Formats the number with the specified precision and locale. |
| 29 | + * @param precision The number of digits after the decimal point (optional). |
| 30 | + * @param locale The locale string for formatting (optional). |
| 31 | + * @returns The formatted number string. |
| 32 | + */ |
4 | 33 | formatNumber(precision?: number, locale?: string): string |
| 34 | + |
| 35 | + /** |
| 36 | + * Formats the number as a percentage with the specified precision and locale. |
| 37 | + * @param precision The number of digits after the decimal point (optional). |
| 38 | + * @param locale The locale string for formatting (optional). |
| 39 | + * @returns The formatted percentage string. |
| 40 | + */ |
5 | 41 | formatPercent(precision?: number, locale?: string): string |
6 | 42 | } |
7 | 43 |
|
| 44 | +/** |
| 45 | + * Implementation of the `EnhanceNumber` interface. |
| 46 | + */ |
8 | 47 | export class EnhanceNumber extends Number implements EnhanceNumber { |
| 48 | + /** |
| 49 | + * Formats the number with the specified precision and locale. |
| 50 | + * @param precision The number of digits after the decimal point (optional). |
| 51 | + * @param locale The locale string for formatting (optional). |
| 52 | + * @returns The formatted number string. |
| 53 | + */ |
9 | 54 | formatNumber(precision?: number, locale?: string): string { |
10 | 55 | return FormatNumber(this.valueOf(), precision, locale) |
11 | 56 | } |
| 57 | + |
| 58 | + /** |
| 59 | + * Formats the number as a percentage with the specified precision and locale. |
| 60 | + * @param precision The number of digits after the decimal point (optional). |
| 61 | + * @param locale The locale string for formatting (optional). |
| 62 | + * @returns The formatted percentage string. |
| 63 | + */ |
12 | 64 | formatPercent(precision?: number, locale?: string): string { |
13 | 65 | return FormatPercent(this.valueOf(), precision, locale) |
14 | 66 | } |
| 67 | + |
| 68 | + /** |
| 69 | + * Adds another `EnhanceNumber` to the current one. |
| 70 | + * @param other The other `EnhanceNumber` to add. |
| 71 | + * @returns A new `EnhanceNumber` instance representing the sum. |
| 72 | + */ |
15 | 73 | plus(other: EnhanceNumber): EnhanceNumber { |
16 | 74 | return new EnhanceNumber(this.valueOf() + other.valueOf()) |
17 | 75 | } |
|
0 commit comments