Skip to content

Commit fedaaf2

Browse files
committed
docs: ✏️ add comment
1 parent d326793 commit fedaaf2

File tree

7 files changed

+231
-8
lines changed

7 files changed

+231
-8
lines changed

src/capacity/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,76 @@
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+
121
import prettyBytes, { Options } from "pretty-bytes"
222

23+
/**
24+
* Represents a capacity value, providing methods for formatting and arithmetic operations.
25+
*/
326
export interface Capacity {
27+
/**
28+
* Formats the capacity value as a human-readable string.
29+
* @param options Formatting options (optional).
30+
* @returns The formatted capacity string.
31+
*/
432
prettyBytes(options?: Options): string
33+
34+
/**
35+
* Adds another capacity value to the current one.
36+
* @param other The other capacity value to add.
37+
* @returns A new `Capacity` instance representing the sum.
38+
*/
539
plus(other: Capacity): Capacity
640
}
741

42+
/** Default formatting options for `prettyBytes`. */
843
export const DefaultCapacityOptions = { maximumFractionDigits: 2, binary: true }
44+
45+
/**
46+
* Implementation of the `Capacity` interface.
47+
*/
948
export class Capacity implements Capacity {
1049
private param: string | number
50+
51+
/**
52+
* Constructs a `Capacity` instance with the provided value.
53+
* @param param The initial value for the capacity.
54+
*/
1155
constructor(param: string | number) {
1256
this.param = param
1357
}
1458

59+
/**
60+
* Formats the capacity value as a human-readable string.
61+
* @param options Formatting options (optional).
62+
* @returns The formatted capacity string.
63+
*/
1564
prettyBytes(options?: Options): string {
1665
options = { ...DefaultCapacityOptions, ...options }
1766
return prettyBytes(Number(this.param), options)
1867
}
1968

69+
/**
70+
* Adds another capacity value to the current one.
71+
* @param other The other capacity value to add.
72+
* @returns A new `Capacity` instance representing the sum.
73+
*/
2074
plus(other: Capacity): Capacity {
2175
return new Capacity(Number(this.param) + Number(other.param))
2276
}

src/enhanceNumber/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,75 @@
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+
121
import { FormatNumber, FormatPercent } from "num-format"
222

23+
/**
24+
* Represents an enhanced number with additional formatting and arithmetic operations.
25+
*/
326
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+
*/
433
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+
*/
541
formatPercent(precision?: number, locale?: string): string
642
}
743

44+
/**
45+
* Implementation of the `EnhanceNumber` interface.
46+
*/
847
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+
*/
954
formatNumber(precision?: number, locale?: string): string {
1055
return FormatNumber(this.valueOf(), precision, locale)
1156
}
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+
*/
1264
formatPercent(precision?: number, locale?: string): string {
1365
return FormatPercent(this.valueOf(), precision, locale)
1466
}
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+
*/
1573
plus(other: EnhanceNumber): EnhanceNumber {
1674
return new EnhanceNumber(this.valueOf() + other.valueOf())
1775
}

src/enhanceNumber/num-format.d.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
declare module 'num-format' {
2-
function FormatNumber(number: number, precision?: number, locale?: string): string
3-
function FormatPercent(number: number, precision?: number, locale?: string): string
4-
}
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+
21+
declare module "num-format" {
22+
function FormatNumber(
23+
number: number,
24+
precision?: number,
25+
locale?: string
26+
): string
27+
function FormatPercent(
28+
number: number,
29+
precision?: number,
30+
locale?: string
31+
): string
32+
}

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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+
121
export { ValueFields } from "./valueFields"
222
export { Result } from "./result"
323
export { Capacity, DefaultCapacityOptions } from "./capacity"

src/result/index.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
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+
21+
/**
22+
* Represents the result of an operation that can be either successful or have an error.
23+
* @template T The type of data associated with the result.
24+
*/
125
export interface Result<T> {
2-
ok: boolean;
3-
data?: T;
4-
error?: any;
5-
}
26+
/**
27+
* Indicates whether the operation was successful (`true`) or had an error (`false`).
28+
*/
29+
ok: boolean
30+
31+
/**
32+
* The data associated with the result. It is present when the operation is successful.
33+
*/
34+
data?: T
35+
36+
/**
37+
* The error information. It is present when the operation has an error.
38+
*/
39+
error?: any
40+
}

src/valueFields/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
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+
21+
/**
22+
* Represents a type that includes all non-function fields from another type.
23+
* @template T The type from which to extract non-function fields.
24+
*/
125
export type ValueFields<T> = Pick<
226
T,
327
{
28+
/**
29+
* Extracts non-function fields from the provided type.
30+
*/
431
[K in keyof T]: T[K] extends Function ? never : K
532
}[keyof T]
633
>

test/enhanceNumber/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* limitations under the respective licenses.
1919
********************************************************************************/
2020

21+
//@ts-ignore
2122
import assert from "assert"
2223
import { it } from "mocha"
2324
import { EnhanceNumber } from "../../src/enhanceNumber"

0 commit comments

Comments
 (0)