1.0.2 • Published 5 years ago

@rniv/base-n v1.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

Base N

Installation

npm i -S @rniv/base-n

Getting started

const {BaseN} = required('@rniv/base-n')
const bs16 = new BaseN('0123456789abcdef')
// n = {{symbols.length}} (base)

Encoding

encode method will return base n string of given number.

Syntax

encode(n: number): string

console.log(bs16.encode(100)) // output 64

Decoding

decode method will return decimal value of given base n string

Syntax

decode(s: string): number

console.log(bs16.decode('1f')) // output 31

Add

add method will return base n string. i.e, sum of given two base n strings

Syntax

add(s1: string, s2: string): string

console.log(bs16.add('1f', '1f')) // output 3e

Subtract

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

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

compare method will return 1|0|-1

Syntax

compare(s1: string, s2:string): number

Example:

s1 > s2 => 1

s1 = s2 => 0

s1 < s2 => -1

console.log(bs16.compare('1ff', '10')) // output 1
console.log(bs16.compare('1ff', '1ff')) // output 0
console.log(bs16.compare('1f', '1ff')) // -1