@rniv/base-n
Version:
## Installation
91 lines (59 loc) • 1.59 kB
Markdown
```(shellscript)
npm i -S @rniv/base-n
```
```(javascript)
const {BaseN} = required('@rniv/base-n')
const bs16 = new BaseN('0123456789abcdef')
// n = {{symbols.length}} (base)
```
encode method will return base n string of given number.
Syntax
> encode(n: number): string
```(javascript)
console.log(bs16.encode(100)) // output 64
```
decode method will return decimal value of given base n string
Syntax
> decode(s: string): number
```(javascript)
console.log(bs16.decode('1f')) // output 31
```
add method will return base n string. i.e, sum of given two base n strings
Syntax
> add(s1: string, s2: string): string
```(javascript)
console.log(bs16.add('1f', '1f')) // output 3e
```
subtract method will return base n string. i.e, subtraction of given two base n strings
**Note** By default subtract will return absolute value
Syntax
> subtract(s1: string, s2: string, abs=true): string
>
> **Note**: subtract method "abs" parameter defaults to true
```(javascript)
console.log(bs16.subtract('1ff', '10')) // output 1ef
console.log(bs16.subtract('10', '1ff')) // output 1ef
console.log(bs16.subtract('10', '1ff', false)) // output -1ef
```
compare method will return 1|0|-1
Syntax
>compare(s1: string, s2:string): number
Example:
> s1 > s2 => 1
>
> s1 = s2 => 0
>
> s1 < s2 => -1
```(javascript)
console.log(bs16.compare('1ff', '10')) // output 1
console.log(bs16.compare('1ff', '1ff')) // output 0
console.log(bs16.compare('1f', '1ff')) // -1
```