-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
163 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
import { defineNuxtConfig } from 'nuxt/config' | ||
|
||
export default defineNuxtConfig({ | ||
components: [{ path: './components' }], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
import { defineNuxtConfig } from 'nuxt/config' | ||
|
||
export default defineNuxtConfig({ | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
import { defineNuxtConfig } from 'nuxt/config' | ||
|
||
export default defineNuxtConfig({ | ||
|
||
}) |
7 changes: 1 addition & 6 deletions
7
packages/utils/test/formatter.test.ts → packages/utils/src/formatter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { expect, test } from 'vitest' | ||
import { calcPercentValue, calcPercentage, decreaseByPercentage, increaseByPercentage } from './percent' | ||
|
||
test('calc percent of the given value', () => { | ||
expect(calcPercentValue('100', 10, 2)).toMatchInlineSnapshot('10n') | ||
expect(calcPercentValue('100', 25, 2)).toMatchInlineSnapshot('25n') | ||
expect(calcPercentValue(BigInt(125e9), 50, 9)).toMatchInlineSnapshot('62500000000n') | ||
expect(calcPercentValue('1234567898765432123456789', 50, 18)).toMatchInlineSnapshot('617283949382716061728394n') | ||
expect(calcPercentValue('1248933', 25.5, 6)).toMatchInlineSnapshot('318477n') | ||
expect(calcPercentValue('1248933', 45.324624, 6)).toMatchInlineSnapshot('566074n') | ||
expect(calcPercentValue(1000000, 60, 6)).toMatchInlineSnapshot('600000n') | ||
expect(calcPercentValue(BigInt(100e18), 50, 18)).toMatchInlineSnapshot('50000000000000000000n') | ||
expect(calcPercentValue(1e6, 0, 6)).toMatchInlineSnapshot('0n') | ||
expect(calcPercentValue(50, 150, 0)).toMatchInlineSnapshot('75n') | ||
expect(calcPercentValue(50, 1500000000000000, 0)).toMatchInlineSnapshot('750000000000000n') | ||
}) | ||
|
||
test('calc percentage of the second value from the main value', () => { | ||
expect(calcPercentage('100', '10', 2)).toMatchInlineSnapshot('10') | ||
expect(calcPercentage('100', '25', 2)).toMatchInlineSnapshot('25') | ||
expect(calcPercentage(BigInt(125e9), '62500000000', 9)).toMatchInlineSnapshot('50') | ||
expect(calcPercentage('1234567898765432123456789', '617283949382716061728394', 18)).toMatchInlineSnapshot('50') // 49.9999999999999999999999594999999190000006 | ||
expect(calcPercentage('1248933', '318477', 6)).toMatchInlineSnapshot('25.5') | ||
expect(calcPercentage('1248933', 566074, 6)).toMatchInlineSnapshot('45.325') // 45.3246090863160794 | ||
expect(calcPercentage('1248933', 566074, 6, 5)).toMatchInlineSnapshot('45.32461') // 45.3246090863160794 | ||
expect(calcPercentage(1000000, 600000, 6)).toMatchInlineSnapshot('60') | ||
expect(calcPercentage(BigInt(100e18), '50000000000000000000', 18)).toMatchInlineSnapshot('50') | ||
expect(calcPercentage(10000000000, 10000000, 10)).toMatchInlineSnapshot('0.1') | ||
expect(calcPercentage(50, 75, 0)).toMatchInlineSnapshot('150') | ||
expect(calcPercentage(50, 750, 0)).toMatchInlineSnapshot('1500') | ||
expect(calcPercentage(1e6, 0, 6)).toMatchInlineSnapshot('0') | ||
}) | ||
|
||
test('increase a number by given percentage', () => { | ||
expect(increaseByPercentage('100', 10, 2)).toMatchInlineSnapshot('110n') | ||
expect(increaseByPercentage('100', 25, 2)).toMatchInlineSnapshot('125n') | ||
expect(increaseByPercentage('100', 0, 2)).toMatchInlineSnapshot('100n') | ||
expect(increaseByPercentage(-100, 50, 9)).toMatchInlineSnapshot('-50n') | ||
}) | ||
|
||
test('decease a number by given percentage', () => { | ||
expect(decreaseByPercentage('100', 10, 2)).toMatchInlineSnapshot('90n') | ||
expect(decreaseByPercentage('100', 25, 2)).toMatchInlineSnapshot('75n') | ||
expect(decreaseByPercentage('100', 0, 2)).toMatchInlineSnapshot('100n') | ||
expect(decreaseByPercentage(-100, 50, 9)).toMatchInlineSnapshot('-150n') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { scale } from './scale' | ||
import { unScale, unScaleToBase } from './unscale' | ||
|
||
/** | ||
* Calculates the specified percentage of a given value. | ||
* | ||
* @param value - The value to calculate the percentage of | ||
* @param percentage - The percentage to calculate | ||
* @param valueDecimals - The decimals of the value. | ||
* | ||
* @returns The percentage of the given value scaled according to the specified decimals | ||
* | ||
* @example | ||
* calcPercentValue(100, 10, 6) // 10 | ||
* calcPercentValue(100, 25, 6) // 25 | ||
* calcPercentValue(100e18, 50, 18) // 50000000000000000000n || 50e18 | ||
*/ | ||
export function calcPercentValue( | ||
value: string | number | bigint, percentage: number | string | bigint, valueDecimals: number, | ||
) { | ||
return unScaleToBase( | ||
BigInt(value) * scale(percentage, valueDecimals), | ||
valueDecimals + valueDecimals + 2, // 2 is percentage decimals (100) | ||
valueDecimals, | ||
) | ||
} | ||
|
||
/** | ||
* To avoid total loss of precision if the given decimals are not enough in case of more than 100% | ||
*/ | ||
const percentCatalyst = 6 | ||
|
||
/** | ||
* Calculates the percentage of the given secondary value from the main value | ||
* | ||
* @param value - The main value | ||
* @param secondValue - The secondary value | ||
* @param decimals - The decimals of the value. | ||
* @param precision - The precision of the result. Default is 3. | ||
* | ||
* @returns The percentage with the given precision | ||
* | ||
* @example | ||
* calcPercentage('100', '10', 2) // 10 | ||
* calcPercentage(50, 75, 0) // 150 | ||
* calcPercentage(BigInt(125e9), '62500000000', 9) // 50 | ||
* calcPercentage('1234567898765432123456789', '617283949382716061728394', 18) // 50 | ||
*/ | ||
export function calcPercentage( | ||
value: string | number | bigint, secondValue: string | number | bigint, decimals: number, precision = 3, | ||
) { | ||
return Number(Number(unScale( | ||
scale(BigInt(secondValue), decimals + decimals + percentCatalyst) / BigInt(value), | ||
decimals + decimals + percentCatalyst - 2, | ||
)).toFixed(precision)) | ||
} | ||
|
||
/** | ||
* Increase a number by given percentage | ||
* | ||
* @param value - The number to increase | ||
* @param percentage - The percentage to increase by | ||
* @param decimals - The decimals of the value. | ||
* | ||
* @returns The increased number (e.g. 100 + 10% of 100 = 110) | ||
* | ||
* @example | ||
* increaseByPercentage('100', 10, 2) // 110 | ||
* increaseByPercentage('100', 25, 2) // 125 | ||
* increaseByPercentage(-100, 50, 9) // -50 | ||
*/ | ||
export function increaseByPercentage(value: string | bigint | number, percentage: number | string, decimals: number) { | ||
value = BigInt(value) | ||
return value + (calcPercentValue(value, percentage, decimals) * (value < BigInt(0) ? BigInt(-1) : BigInt(1))) | ||
} | ||
|
||
/** | ||
* Decrease a number by given percentage | ||
* | ||
* @param value - The number to decrease | ||
* @param percentage - The percentage to decrease by | ||
* @param decimals - The decimals of the value. | ||
* | ||
* @returns The decreased number (e.g. 100 - 30% of 100 = 70) | ||
* | ||
* @example | ||
* decreaseByPercentage('100', 10, 2) // 90 | ||
* decreaseByPercentage('100', 25, 2) // 75 | ||
* decreaseByPercentage(-100, 50, 9) // -150 | ||
*/ | ||
export function decreaseByPercentage(value: string | bigint | number, percentage: number | string, decimals: number) { | ||
value = BigInt(value) | ||
return value - (calcPercentValue(value, percentage, decimals) * (value < BigInt(0) ? BigInt(-1) : BigInt(1))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.