Skip to content

Commit

Permalink
feat: add rmDecimals to utils, apply to planckToUnit (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat authored Nov 19, 2024
1 parent daa318b commit 77dc9b8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@w3ux/utils-source",
"license": "GPL-3.0-only",
"version": "1.1.1-beta.9",
"version": "1.1.1-beta.11",
"type": "module",
"scripts": {
"clear": "rm -rf node_modules dist tsconfig.tsbuildinfo",
Expand Down
6 changes: 6 additions & 0 deletions library/utils/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export const pageFromUri = (pathname: string, fallback: string) => {
*/
export const rmCommas = (val: string): string => val.replace(/,/g, "");

/**
* @name rmDecimals
* @summary Removes the decimal point and decimals from a string.
*/
export const rmDecimals = (str) => str.split(".")[0];

/**
* @name shuffle
* @summary Shuffle a set of objects.
Expand Down
6 changes: 4 additions & 2 deletions library/utils/src/unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SPDX-License-Identifier: GPL-3.0-only */

import type { MutableRefObject, RefObject } from "react";
import { AnyObject } from "./types";
import { rmCommas } from "./base";
import { rmCommas, rmDecimals } from "./base";
import { AnyJson } from "@w3ux/types";
import { AccountId } from "@polkadot-api/substrate-bindings";

Expand Down Expand Up @@ -31,7 +31,9 @@ export const planckToUnit = (
typeof val === "bigint"
? val
: BigInt(
typeof val === "number" ? Math.floor(val).toString() : rmCommas(val)
typeof val === "number"
? Math.floor(val).toString()
: rmDecimals(rmCommas(val))
);

const divisor = units === 0 ? 1n : BigInt(10) ** BigInt(units);
Expand Down
33 changes: 33 additions & 0 deletions library/utils/tests/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@ describe("Tests suite - rmCommas Function", () => {
});
});

describe("rmDecimals", () => {
test("should remove decimals from a string with a decimal point", () => {
expect(fn.rmDecimals("123.45")).toBe("123");
expect(fn.rmDecimals("45.6789")).toBe("45");
});

test("should return the original string if no decimal point exists", () => {
expect(fn.rmDecimals("678")).toBe("678");
expect(fn.rmDecimals("123")).toBe("123");
});

test("should handle empty strings", () => {
expect(fn.rmDecimals("")).toBe("");
});

test("should handle strings with multiple decimal points by removing everything after the first", () => {
expect(fn.rmDecimals("123.45.67")).toBe("123");
});

test("should handle strings with only a decimal point", () => {
expect(fn.rmDecimals(".")).toBe("");
});

test("should handle strings that start with a decimal point", () => {
expect(fn.rmDecimals(".123")).toBe("");
});

test("should handle strings with non-numeric characters", () => {
expect(fn.rmDecimals("abc.123")).toBe("abc");
expect(fn.rmDecimals("test.")).toBe("test");
});
});

describe("Test suite - shuffle Function", () => {
test("should shuffle an array of numbers", () => {
const inputArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Expand Down

0 comments on commit 77dc9b8

Please sign in to comment.