Skip to content

Commit

Permalink
feat(): Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
newtonnthiga committed Oct 15, 2024
1 parent c3b77bb commit 35f58de
Show file tree
Hide file tree
Showing 15 changed files with 1,351 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Deno files
npm/

# VSCode
.vscode/launch.json
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"deno.lint": true,
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# ts-alis-build

Alis Build utils for Javascript/Typescript

## Installation

```bash
npm install @alis-build/utils
```
28 changes: 28 additions & 0 deletions build_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";

await emptyDir("./npm");

await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
shims: {
deno: true,
},
package: {
name: "@alis-build/utils",
version: Deno.args[0],
description: "A collection of common utilities used at Alis Exchange.",
license: "APACHE-2.0",
repository: {
type: "git",
url: "git+https://github.com/alis-exchange/ts-alis-build.git",
},
bugs: {
url: "https://github.com/alis-exchange/ts-alis-build/issues",
},
},
postBuild() {
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");
},
});
15 changes: 15 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@alis-build/utils",
"version": "0.1.0",
"exports": "./mod.ts",
"tasks": {
"dev": "deno test --watch mod.ts"
},
"license": "MIT",
"imports": {
"@alis-build/google-common-protos": "npm:@alis-build/google-common-protos@^1.7.0",
"@std/assert": "jsr:@std/assert@^1.0.6",
"google-protobuf": "npm:google-protobuf@^3.21.4"
},
"nodeModulesDir": "auto"
}
136 changes: 136 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as money from "./money/index.ts";
import * as strings from "./strings/index.ts";
import * as time from "./time/index.ts";
import * as numbers from "./numbers/index.ts";

export { money, strings, time, numbers };
66 changes: 66 additions & 0 deletions money/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Money from "npm:@alis-build/google-common-protos@latest/google/type/money_pb.js";
import { modf } from "../numbers/index.ts";

/**
* Parse a google.type.Money instance into a number
*
* @param money {google.type.Money} - The money to parse
* @returns {number | null} The parsed number, or null if the input is nullish
*/
export const parse = (money: Money.Money): number | null => {
// If money is nullish, return null
if (!money) {
return null;
}

const units = money.getUnits();
const nanos = money.getNanos();

return units + nanos / 1e9;
};

/**
* Encode a number into a google.type.Money instance
*
* @param currency {string} - The currency code
* @param value {number} - The number to encode
* @returns {Money | null} The encoded Money instance, or null if the input is nullish
*/
export const encode = (currency: string, value: number): Money.Money | null => {
if (!currency || !value) {
return null;
}

const [units, nanos] = modf(value);

const rawNanos = nanos * 1e9;
const absNanosString = rawNanos?.toString()?.split(".")[0];
const absNanos = absNanosString ? parseInt(absNanosString) : 0;

const money = new Money.Money();
money.setCurrencyCode(currency);
money.setUnits(units);
money.setNanos(absNanos);

return money;
};

/**
* Takes a google.type.Money object and returns a string formatted as currency.
*
* @param money {google.type.Money} - The money object to format
* @returns {string}
*/
export const format = (money?: Money.Money): string => {
if (!money) {
return "0.0";
}

// Create a JS number formatter
const currencyFormatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: money.getCurrencyCode(),
});

return currencyFormatter.format(parse(money) ?? 0.0);
};
Loading

0 comments on commit 35f58de

Please sign in to comment.