Skip to content

Commit

Permalink
chore: deploy to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
ctison committed Dec 10, 2023
1 parent 9995a0d commit c2efcae
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 26 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ jobs:
- name: Install cross
run: cargo install cross --git https://github.com/cross-rs/cross

- uses: oven-sh/setup-bun@v1
- name: Install npm packages
run: bun install --frozen-lockfile

- name: Build cross platforms
run: |
cross build --release --locked --target aarch64-unknown-linux-gnu
Expand All @@ -62,17 +66,17 @@ jobs:
mv ./target/x86_64-unknown-linux-musl/release/base-converter ./tmp/base-converter-x86_64-unknown-linux-musl
mv ./target/x86_64-pc-windows-gnu/release/base-converter ./tmp/base-converter-x86_64-pc-windows-gnu
# - name: Install wasm-pack
# run: curl -fL --tlsv1.2 --proto '=https' https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# - name: Build wasm
# run: wasm-pack build --target web --scope ctison . -F wasm
- name: Build wasm
run: bun run build

# - name: Publish on npmjs.com
# working-directory: pkg/
# env:
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
# run: npm publish
- name: Publish on npmjs.com
working-directory: pkg/
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
cp ../.npmrc .
npm version --no-git-tag-version '${{ needs.version.outputs.version }}'
npm publish
- name: Publish on crates.io
env:
Expand All @@ -93,5 +97,5 @@ jobs:
body: |
# Rust Doc
https://docs.rs/base-converter/${{ steps.version.outputs.version }}/base_converter/
# ## NPM Package
# https://www.npmjs.com/package/base-converter/v/${{ steps.version.outputs.version }}
# NPM Package
https://www.npmjs.com/package/@ctison/base-converter/v/${{ steps.version.outputs.version }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
/target/
/pkg/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/*
52 changes: 45 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,62 @@ Convert a number from any base to any other base from your terminal!

Pre-built binaries are available in [Releases](https://github.com/ctison/base-converter/releases).

## Usage
NPM package (wasm) available at https://www.npmjs.com/package/@ctison/base-converter.

## CLI Usage

```sh
$ base-converter <NUMBER> <FROM_BASE> <TO_BASE>
```

## Examples

Convert 51966 from base 10 to base 16

```sh
# Convert 51966 from base 10 to base 16
$ base-converter 51966 0123456789 0123456789ABCDEF
CAFE
```

Convert 42 from base 10 to base 2

```sh
# Convert 42 from base 10 to base 2
$ base-converter 42 0123456789 🦀🚀
🚀🦀🚀🦀🚀🦀
```

## NPM usage

```sh
$ npm add @ctison/base-converter
```

```ts
/**
* Checks if a base is valid by throwing an error if not.
* @param {string} base
*/
export function checkBase(base: string): void
/**
* Convert a number from any base to an decimal number.
* @param {string} nbr
* @param {string} fromBase
* @returns {number}
*/
export function baseToDecimal(nbr: string, fromBase: string): number
/**
* Convert an decimal number to any base.
* @param {number} nbr
* @param {string} toBase
* @returns {string}
*/
export function decimal_to_base(nbr: number, toBase: string): string
/**
* Convert a number from any base to any base.
* @param {string} nbr
* @param {string} fromBase
* @param {string} toBase
* @returns {string}
*/
export function baseToBase(
nbr: string,
fromBase: string,
toBase: string
): string
```
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"private": true,
"scripts": {
"build": "wasm-pack build --scope ctison --release . -F wasm"
},
"devDependencies": {
"wasm-pack": "0.12.1"
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Convert numbers from base to base.

#[cfg(feature = "wasm")]
#[allow(non_snake_case)]
pub mod wasm;

use anyhow::{ensure, Result};
Expand Down
14 changes: 7 additions & 7 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ use wasm_bindgen::prelude::*;

/// Checks if a base is valid by throwing an error if not.
#[wasm_bindgen]
pub fn check_base(base: &str) -> Result<(), JsError> {
pub fn checkBase(base: &str) -> Result<(), JsError> {
crate::check_base(base).map_err(|err| JsError::new(&format!("{}", err)))
}

/// Convert a number from any base to an decimal number.
#[wasm_bindgen]
pub fn base_to_decimal(nbr: &str, from_base: &str) -> Result<usize, JsError> {
crate::base_to_decimal(nbr, from_base).map_err(|err| JsError::new(&format!("{}", err)))
pub fn baseToDecimal(nbr: &str, fromBase: &str) -> Result<usize, JsError> {
crate::base_to_decimal(nbr, fromBase).map_err(|err| JsError::new(&format!("{}", err)))
}

/// Convert an decimal number to any base.
#[wasm_bindgen]
pub fn decimal_to_base(nbr: usize, to_base: &str) -> Result<String, JsError> {
crate::decimal_to_base(nbr, to_base).map_err(|err| JsError::new(&format!("{}", err)))
pub fn decimal_to_base(nbr: usize, toBase: &str) -> Result<String, JsError> {
crate::decimal_to_base(nbr, toBase).map_err(|err| JsError::new(&format!("{}", err)))
}

/// Convert a number from any base to any base.
#[wasm_bindgen]
pub fn base_to_base(nbr: &str, from_base: &str, to_base: &str) -> Result<String, JsError> {
crate::base_to_base(nbr, from_base, to_base).map_err(|err| JsError::new(&format!("{}", err)))
pub fn baseToBase(nbr: &str, fromBase: &str, toBase: &str) -> Result<String, JsError> {
crate::base_to_base(nbr, fromBase, toBase).map_err(|err| JsError::new(&format!("{}", err)))
}

0 comments on commit c2efcae

Please sign in to comment.