An open API service indexing awesome lists of open source software.

https://github.com/r48n34/chinese-commercial-code-convertor

Simple Typescript code for convert Chinese Commercial Code.
https://github.com/r48n34/chinese-commercial-code-convertor

ccc npm

Last synced: 5 months ago
JSON representation

Simple Typescript code for convert Chinese Commercial Code.

Awesome Lists containing this project

README

          

# πŸ’‘ Chinese Commercial Code Convertor

Simple Typescript code for convert Chinese Commercial Code.

- πŸ’ͺ🏼 Support both `CN` and `TW` standard
- πŸ’Ό Zero dependency
- πŸ‘” Tested works on `Node.js`, `React` and other web applications


## πŸ’» Install
```bash
npm i chinese-commercial-code-convertor
yarn add chinese-commercial-code-convertor
```

## πŸ› οΈ Usage
```ts
import { numberToCode, codeToNumber } from "chinese-commercial-code-convertor"

// Convert Number To Chinese Code
console.log(numberToCode(1234)); // "ε§Ή"
console.log(numberToCode(36, {lang: "tw"})); // "δΉ…", Will auto convert 36 to 0036

console.log(numberToCode("0155")); // "作"
console.log(numberToCode("0123 0123 0155")); // "δΌ‹δΌ‹δ½œ"
console.log(numberToCode(["0144", "0155"])); // 低作"
console.log(numberToCode([144, 155])); // 低作"

// Convert Chinese Code To Number
console.log(codeToNumber("ε§Ή")) // "1234"
console.log(codeToNumber("低作")) // "0144 0155"

console.log(codeToNumber(["低", "作"])) // "0144 0155"
console.log(codeToNumber(["低", "劝", "作"])) // "0144 -1 0155"
```

## Utils

### `numberToCode()`
Convert your number input to chinese string.
```ts
numberToCode(
input: string | number | number[] | string[],
inputOptions?: Options,
): string
```

### `codeToNumber()`
Convert your chinese string input to number string.
```ts
codeToNumber(
input: string | string[],
inputOptions?: Options,
): string
```

### Options
The Option input is a object which included the following items:
```ts
interface Options {
// Default: "tw"
lang?: "tw" | "cn";

// Default at numberToCode: ""
// Default at codeToNumber: "-1"
notFoundReturn?: string;
}
```

#### `lang`
The lang is refer to which `Chinese Commercial Code` standard you prefer to return.

- tw: Taiwan - 台灣標準電報璼
- cn: China - 倧陆标准甡ζŠ₯码
- Default: `"tw"`

`Code Samples`
```ts
numberToCode(1234, { lang: "cn" }) // "ε§Ή"
codeToNumber("ε’₯", { lang: "cn" }) // "8134"
```

#### `notFoundReturn`
If you target code is not found in the list, the `notFoundReturn` string will be the return string. You can input your own `notFoundReturn`.

- Default at numberToCode: `""`
- Default at codeToNumber: `"-1"`

`Code Samples`
```ts
numberToCode("8260", { notFoundReturn: "*" }) // *
codeToNumber("低劝作", { notFoundReturn: "?" }) // "0144 ? 0155"
```

## πŸ’» Dev logs

### - 1.1.0
1. Allow `codeToNumber()` to input split string.

### - 1.0.0
1. Project init

## πŸ” License
MIT License

## πŸ“œ References
https://www.njstar.com/cms/chinese-commercial-telegraph-code-lookup