From a124ce50bb152b101631e522de8013ab52cceb2a Mon Sep 17 00:00:00 2001 From: Simon Sinclair Date: Tue, 13 Jul 2021 23:15:54 +0100 Subject: [PATCH 1/5] Add getSymbolValue --- src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.ts b/src/index.ts index 2ab0505..f6f3d37 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,10 @@ export const getSymbol = (index: number): string => { return SYMBOLS.charAt(index); }; +export const getSymbolValue = (symbol: string): number => { + return '0123456789abcdefghjkmnpqrstvwxyz'.search(symbol); +}; + const n32 = (number: number): string => { if (number < 0) throw new Error('n32 expects an absolute number.'); if (number < 32) return getSymbol(number); From 8e972637320c11abd53c0af25669b3552d5d0e26 Mon Sep 17 00:00:00 2001 From: Simon Sinclair Date: Tue, 13 Jul 2021 23:16:07 +0100 Subject: [PATCH 2/5] Add n32decode --- src/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/index.ts b/src/index.ts index f6f3d37..afdb88b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,4 +26,18 @@ const n32 = (number: number): string => { return m; }; +export const n32decode = (n32: string): number => { + const BASE = 32; + let n = n32.length; + let s = 0; + + while (n > 0) { + n--; + const symbol = n32[n32.length - n - 1]; + s = getSymbolValue(symbol) * BASE ** n + s; + } + + return s; +}; + export default n32; From a2fd5a6d1b58d6b3049b9ed49a6598bc88188dc2 Mon Sep 17 00:00:00 2001 From: Simon Sinclair Date: Tue, 13 Jul 2021 23:16:55 +0100 Subject: [PATCH 3/5] Expand n32 value string assertions --- __tests__/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/__tests__/index.ts b/__tests__/index.ts index 2316831..414b7ab 100644 --- a/__tests__/index.ts +++ b/__tests__/index.ts @@ -5,6 +5,7 @@ describe('n32', () => { expect(n32(0)).toBe('0'); expect(n32(31)).toBe('z'); expect(n32(32)).toBe('10'); + expect(n32(1035280)).toBe('zk0g'); expect(n32(Number.MAX_SAFE_INTEGER)).toBe('7zzzzzzzzzz'); }); it('throws an error', () => { From a51422cee5a8875ab505321b55c389be30d3bf38 Mon Sep 17 00:00:00 2001 From: Simon Sinclair Date: Tue, 13 Jul 2021 23:18:09 +0100 Subject: [PATCH 4/5] Add getSymbolValue test suite --- __tests__/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/__tests__/index.ts b/__tests__/index.ts index 414b7ab..07569e6 100644 --- a/__tests__/index.ts +++ b/__tests__/index.ts @@ -1,4 +1,4 @@ -import n32, { getSymbol } from '../dist'; +import n32, { n32decode, getSymbol, getSymbolValue } from '../dist'; describe('n32', () => { it('returns a valid string', () => { @@ -26,3 +26,12 @@ describe('getSymbol', () => { ); }); }); + +describe('getSymbolValue', () => { + it('returns a valid symbol value', () => { + expect(getSymbolValue('0')).toBe(0); + expect(getSymbolValue('9')).toBe(9); + expect(getSymbolValue('a')).toBe(10); + expect(getSymbolValue('z')).toBe(31); + }); +}); From 8b6316bebbbc1eb8f744b5dcabca46e8ac4e3ea6 Mon Sep 17 00:00:00 2001 From: Simon Sinclair Date: Tue, 13 Jul 2021 23:18:20 +0100 Subject: [PATCH 5/5] Add n32decode test suite --- __tests__/index.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/__tests__/index.ts b/__tests__/index.ts index 07569e6..bdb4a62 100644 --- a/__tests__/index.ts +++ b/__tests__/index.ts @@ -13,6 +13,16 @@ describe('n32', () => { }); }); +describe('n32decode', () => { + it('returns a valid number', () => { + expect(n32decode('0')).toBe(0); + expect(n32decode('z')).toBe(31); + expect(n32decode('10')).toBe(32); + expect(n32decode('0zk0g')).toBe(1035280); + expect(n32decode('7zzzzzzzzzz')).toBe(Number.MAX_SAFE_INTEGER); + }); +}); + describe('getSymbol', () => { it('returns a valid symbol', () => { expect(getSymbol(0)).toBe('0');