Skip to content

Commit

Permalink
docs: ✏️ add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 14, 2023
1 parent d326793 commit fedaaf2
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 8 deletions.
54 changes: 54 additions & 0 deletions src/capacity/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,76 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import prettyBytes, { Options } from "pretty-bytes"

/**
* Represents a capacity value, providing methods for formatting and arithmetic operations.
*/
export interface Capacity {
/**
* Formats the capacity value as a human-readable string.
* @param options Formatting options (optional).
* @returns The formatted capacity string.
*/
prettyBytes(options?: Options): string

/**
* Adds another capacity value to the current one.
* @param other The other capacity value to add.
* @returns A new `Capacity` instance representing the sum.
*/
plus(other: Capacity): Capacity
}

/** Default formatting options for `prettyBytes`. */
export const DefaultCapacityOptions = { maximumFractionDigits: 2, binary: true }

/**
* Implementation of the `Capacity` interface.
*/
export class Capacity implements Capacity {
private param: string | number

/**
* Constructs a `Capacity` instance with the provided value.
* @param param The initial value for the capacity.
*/
constructor(param: string | number) {
this.param = param
}

/**
* Formats the capacity value as a human-readable string.
* @param options Formatting options (optional).
* @returns The formatted capacity string.
*/
prettyBytes(options?: Options): string {
options = { ...DefaultCapacityOptions, ...options }
return prettyBytes(Number(this.param), options)
}

/**
* Adds another capacity value to the current one.
* @param other The other capacity value to add.
* @returns A new `Capacity` instance representing the sum.
*/
plus(other: Capacity): Capacity {
return new Capacity(Number(this.param) + Number(other.param))
}
Expand Down
58 changes: 58 additions & 0 deletions src/enhanceNumber/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,75 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import { FormatNumber, FormatPercent } from "num-format"

/**
* Represents an enhanced number with additional formatting and arithmetic operations.
*/
export interface EnhanceNumber extends Number {
/**
* Formats the number with the specified precision and locale.
* @param precision The number of digits after the decimal point (optional).
* @param locale The locale string for formatting (optional).
* @returns The formatted number string.
*/
formatNumber(precision?: number, locale?: string): string

/**
* Formats the number as a percentage with the specified precision and locale.
* @param precision The number of digits after the decimal point (optional).
* @param locale The locale string for formatting (optional).
* @returns The formatted percentage string.
*/
formatPercent(precision?: number, locale?: string): string
}

/**
* Implementation of the `EnhanceNumber` interface.
*/
export class EnhanceNumber extends Number implements EnhanceNumber {
/**
* Formats the number with the specified precision and locale.
* @param precision The number of digits after the decimal point (optional).
* @param locale The locale string for formatting (optional).
* @returns The formatted number string.
*/
formatNumber(precision?: number, locale?: string): string {
return FormatNumber(this.valueOf(), precision, locale)
}

/**
* Formats the number as a percentage with the specified precision and locale.
* @param precision The number of digits after the decimal point (optional).
* @param locale The locale string for formatting (optional).
* @returns The formatted percentage string.
*/
formatPercent(precision?: number, locale?: string): string {
return FormatPercent(this.valueOf(), precision, locale)
}

/**
* Adds another `EnhanceNumber` to the current one.
* @param other The other `EnhanceNumber` to add.
* @returns A new `EnhanceNumber` instance representing the sum.
*/
plus(other: EnhanceNumber): EnhanceNumber {
return new EnhanceNumber(this.valueOf() + other.valueOf())
}
Expand Down
36 changes: 32 additions & 4 deletions src/enhanceNumber/num-format.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
declare module 'num-format' {
function FormatNumber(number: number, precision?: number, locale?: string): string
function FormatPercent(number: number, precision?: number, locale?: string): string
}
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

declare module "num-format" {
function FormatNumber(
number: number,
precision?: number,
locale?: string
): string
function FormatPercent(
number: number,
precision?: number,
locale?: string
): string
}
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

export { ValueFields } from "./valueFields"
export { Result } from "./result"
export { Capacity, DefaultCapacityOptions } from "./capacity"
Expand Down
43 changes: 39 additions & 4 deletions src/result/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

/**
* Represents the result of an operation that can be either successful or have an error.
* @template T The type of data associated with the result.
*/
export interface Result<T> {
ok: boolean;
data?: T;
error?: any;
}
/**
* Indicates whether the operation was successful (`true`) or had an error (`false`).
*/
ok: boolean

/**
* The data associated with the result. It is present when the operation is successful.
*/
data?: T

/**
* The error information. It is present when the operation has an error.
*/
error?: any
}
27 changes: 27 additions & 0 deletions src/valueFields/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

/**
* Represents a type that includes all non-function fields from another type.
* @template T The type from which to extract non-function fields.
*/
export type ValueFields<T> = Pick<
T,
{
/**
* Extracts non-function fields from the provided type.
*/
[K in keyof T]: T[K] extends Function ? never : K
}[keyof T]
>
1 change: 1 addition & 0 deletions test/enhanceNumber/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* limitations under the respective licenses.
********************************************************************************/

//@ts-ignore
import assert from "assert"
import { it } from "mocha"
import { EnhanceNumber } from "../../src/enhanceNumber"
Expand Down

0 comments on commit fedaaf2

Please sign in to comment.