Skip to content

Commit b0810f1

Browse files
committed
feat(Epoch): move gcd to utils
1 parent e474db8 commit b0810f1

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

packages/core/src/ckb/epoch.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Zero } from "../fixedPoint/index.js";
33
import { type Hex, type HexLike } from "../hex/index.js";
44
import { mol } from "../molecule/index.js";
55
import { numFrom, NumLike, type Num } from "../num/index.js";
6+
import { gcd } from "../utils/index.js";
67

78
/**
89
* @deprecated use `Epoch.from` instead
@@ -293,19 +294,3 @@ export class Epoch extends mol.Entity.Base<EpochLike, Epoch>() {
293294
* 4 hours * 60 minutes per hour * 60 seconds per minute * 1000 milliseconds per second.
294295
*/
295296
const epochInMilliseconds = numFrom(14400000); // (Number.isSafeInteger(14400000) === true)
296-
297-
/**
298-
* Calculate the greatest common divisor (GCD) of two Num values using the Euclidean algorithm.
299-
*
300-
* @param a - First operand.
301-
* @param b - Second operand.
302-
* @returns GCD(a, b) as a Num.
303-
*/
304-
function gcd(a: Num, b: Num): Num {
305-
a = a < Zero ? -a : a;
306-
b = b < Zero ? -b : b;
307-
while (b !== Zero) {
308-
[a, b] = [b, a % b];
309-
}
310-
return a;
311-
}

packages/core/src/utils/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { NumLike, numFrom, numToHex } from "../num/index.js";
1+
import { Zero } from "../fixedPoint/index.js";
2+
import { NumLike, numFrom, numToHex, type Num } from "../num/index.js";
23

34
/**
45
* A type safe way to apply a transformer on a value if it's not empty.
@@ -264,3 +265,19 @@ export function stringify(val: unknown) {
264265
return value;
265266
});
266267
}
268+
269+
/**
270+
* Calculate the greatest common divisor (GCD) of two Num values using the Euclidean algorithm.
271+
*
272+
* @param a - First operand.
273+
* @param b - Second operand.
274+
* @returns GCD(a, b) as a Num.
275+
*/
276+
export function gcd(a: Num, b: Num): Num {
277+
a = a < Zero ? -a : a;
278+
b = b < Zero ? -b : b;
279+
while (b !== Zero) {
280+
[a, b] = [b, a % b];
281+
}
282+
return a;
283+
}

0 commit comments

Comments
 (0)