@parca/utilities
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -6,2 +6,6 @@ # Change Log | ||
## [0.0.13](https://github.com/parca-dev/parca/compare/@parca/utilities@0.0.12...@parca/utilities@0.0.13) (2023-06-26) | ||
**Note:** Version bump only for package @parca/utilities | ||
## [0.0.12](https://github.com/parca-dev/parca/compare/@parca/utilities@0.0.11...@parca/utilities@0.0.12) (2023-06-26) | ||
@@ -8,0 +12,0 @@ |
/** | ||
* Divides two bigints and returns a number with two decimal places | ||
*/ | ||
export declare const lowestNumberWithSameNumberOfDigits: (a: number) => number; | ||
export declare const divide: (a: bigint, b: bigint) => number; | ||
@@ -9,2 +10,3 @@ /** | ||
export declare const abs: (a: bigint) => bigint; | ||
export declare const scaleLinear: (domain: [bigint, bigint], range: [number, number]) => (x: bigint) => number; | ||
export type ScaleFunction = (x: bigint) => number; | ||
export declare const scaleLinear: (domain: [bigint, bigint], range: [number, number]) => ScaleFunction; |
@@ -16,4 +16,10 @@ // Copyright 2022 The Parca Authors | ||
*/ | ||
export const lowestNumberWithSameNumberOfDigits = (a) => { | ||
const digits = Math.floor(Math.log10(a)) + 1; | ||
return 10 ** (digits - 1); | ||
}; | ||
const MULTIPLE = lowestNumberWithSameNumberOfDigits(Number.MAX_SAFE_INTEGER); | ||
const MULTIPLE_BIGINT = BigInt(MULTIPLE); | ||
export const divide = (a, b) => { | ||
return Number((a * 1000000n) / b) / 1000000; | ||
return Number((a * MULTIPLE_BIGINT) / b) / MULTIPLE; | ||
}; | ||
@@ -31,8 +37,7 @@ /** | ||
const rangeRange = BigInt(Math.floor(rangeMax - rangeMin)); | ||
// rate * 100000 to retain the decimal places in BigInt format, then divide by 100000 to get the final result | ||
const multiple = 100000; | ||
const rate = BigInt(Math.round(divide(rangeRange, domainRange) * multiple)); | ||
// rate * MULTIPLE to retain the decimal places in BigInt format, then divide by MULTIPLE to get the final result | ||
const rate = BigInt(Math.round(divide(rangeRange, domainRange) * MULTIPLE)); | ||
return x => { | ||
return Number(BigInt(rangeMin) + (x - domainMin) * rate) / multiple; | ||
return Number(BigInt(rangeMin) + (x - domainMin) * rate) / MULTIPLE; | ||
}; | ||
}; |
@@ -13,3 +13,3 @@ // Copyright 2022 The Parca Authors | ||
// limitations under the License. | ||
import { abs, divide, scaleLinear } from './bigint'; | ||
import { abs, divide, lowestNumberWithSameNumberOfDigits, scaleLinear } from './bigint'; | ||
describe('bigint divide', () => { | ||
@@ -61,1 +61,17 @@ it('divides two bigints and returns a number', () => { | ||
}); | ||
describe('lowestNumberWithSameNumberOfDigits', () => { | ||
it('returns the lowest number with the same number of digits', () => { | ||
expect(lowestNumberWithSameNumberOfDigits(1)).toBe(1); | ||
expect(lowestNumberWithSameNumberOfDigits(8)).toBe(1); | ||
expect(lowestNumberWithSameNumberOfDigits(10)).toBe(10); | ||
expect(lowestNumberWithSameNumberOfDigits(99)).toBe(10); | ||
expect(lowestNumberWithSameNumberOfDigits(100)).toBe(100); | ||
expect(lowestNumberWithSameNumberOfDigits(600)).toBe(100); | ||
expect(lowestNumberWithSameNumberOfDigits(999)).toBe(100); | ||
expect(lowestNumberWithSameNumberOfDigits(1000)).toBe(1000); | ||
expect(lowestNumberWithSameNumberOfDigits(6000)).toBe(1000); | ||
expect(lowestNumberWithSameNumberOfDigits(9999)).toBe(1000); | ||
expect(lowestNumberWithSameNumberOfDigits(10000)).toBe(10000); | ||
expect(lowestNumberWithSameNumberOfDigits(9007199254740991)).toBe(1000000000000000); | ||
}); | ||
}); |
{ | ||
"name": "@parca/utilities", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "A set of reusable functions for Parca", | ||
@@ -24,3 +24,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "16d74f0c7e3059f569df049d7e1ec59072691e21" | ||
"gitHead": "b338ccef380811fa2e8b42e1a793aca100b4e9a1" | ||
} |
@@ -14,3 +14,3 @@ // Copyright 2022 The Parca Authors | ||
import {abs, divide, scaleLinear} from './bigint'; | ||
import {abs, divide, lowestNumberWithSameNumberOfDigits, scaleLinear} from './bigint'; | ||
@@ -68,1 +68,18 @@ describe('bigint divide', () => { | ||
}); | ||
describe('lowestNumberWithSameNumberOfDigits', () => { | ||
it('returns the lowest number with the same number of digits', () => { | ||
expect(lowestNumberWithSameNumberOfDigits(1)).toBe(1); | ||
expect(lowestNumberWithSameNumberOfDigits(8)).toBe(1); | ||
expect(lowestNumberWithSameNumberOfDigits(10)).toBe(10); | ||
expect(lowestNumberWithSameNumberOfDigits(99)).toBe(10); | ||
expect(lowestNumberWithSameNumberOfDigits(100)).toBe(100); | ||
expect(lowestNumberWithSameNumberOfDigits(600)).toBe(100); | ||
expect(lowestNumberWithSameNumberOfDigits(999)).toBe(100); | ||
expect(lowestNumberWithSameNumberOfDigits(1000)).toBe(1000); | ||
expect(lowestNumberWithSameNumberOfDigits(6000)).toBe(1000); | ||
expect(lowestNumberWithSameNumberOfDigits(9999)).toBe(1000); | ||
expect(lowestNumberWithSameNumberOfDigits(10000)).toBe(10000); | ||
expect(lowestNumberWithSameNumberOfDigits(9007199254740991)).toBe(1000000000000000); | ||
}); | ||
}); |
@@ -17,4 +17,13 @@ // Copyright 2022 The Parca Authors | ||
*/ | ||
export const lowestNumberWithSameNumberOfDigits = (a: number): number => { | ||
const digits = Math.floor(Math.log10(a)) + 1; | ||
return 10 ** (digits - 1); | ||
}; | ||
const MULTIPLE = lowestNumberWithSameNumberOfDigits(Number.MAX_SAFE_INTEGER); | ||
const MULTIPLE_BIGINT = BigInt(MULTIPLE); | ||
export const divide = (a: bigint, b: bigint): number => { | ||
return Number((a * 1000000n) / b) / 1000000; | ||
return Number((a * MULTIPLE_BIGINT) / b) / MULTIPLE; | ||
}; | ||
@@ -29,6 +38,5 @@ | ||
export const scaleLinear = ( | ||
domain: [bigint, bigint], | ||
range: [number, number] | ||
): ((x: bigint) => number) => { | ||
export type ScaleFunction = (x: bigint) => number; | ||
export const scaleLinear = (domain: [bigint, bigint], range: [number, number]): ScaleFunction => { | ||
const [domainMin, domainMax] = domain; | ||
@@ -39,9 +47,8 @@ const [rangeMin, rangeMax] = range; | ||
// rate * 100000 to retain the decimal places in BigInt format, then divide by 100000 to get the final result | ||
const multiple = 100000; | ||
const rate = BigInt(Math.round(divide(rangeRange, domainRange) * multiple)); | ||
// rate * MULTIPLE to retain the decimal places in BigInt format, then divide by MULTIPLE to get the final result | ||
const rate = BigInt(Math.round(divide(rangeRange, domainRange) * MULTIPLE)); | ||
return x => { | ||
return Number(BigInt(rangeMin) + (x - domainMin) * rate) / multiple; | ||
return Number(BigInt(rangeMin) + (x - domainMin) * rate) / MULTIPLE; | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67903
1378