-
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
1 parent
c3b77bb
commit 35f58de
Showing
15 changed files
with
1,351 additions
and
0 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 |
---|---|---|
|
@@ -128,3 +128,9 @@ dist | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
# Deno files | ||
npm/ | ||
|
||
# VSCode | ||
.vscode/launch.json |
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,4 @@ | ||
{ | ||
"deno.enable": true, | ||
"deno.lint": true, | ||
} |
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,2 +1,9 @@ | ||
# ts-alis-build | ||
|
||
Alis Build utils for Javascript/Typescript | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @alis-build/utils | ||
``` |
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,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"); | ||
}, | ||
}); |
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,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" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 }; |
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,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); | ||
}; |
Oops, something went wrong.