Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

codetalker

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

codetalker

Encoding 53-bit Integers using character sets of arbitrary length and order

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Codetalker

BaseN encoding of 53-bit Integers using character sets of arbitrary length and order

NPM version Downloads Build Status

Install

$ npm install codetalker

Usage

This module aims to keep things very simple.

Require and acquire the default export.

const Codetalker = require('codetalker').default;

Create a new instance of Codetalker, passing in your character set.

const base38 = '0123456789abcdefghijklmnopqrstuvwxyz_-'
const talker = new Codetalker(base38);

Encode some integers.

const encoded = talker.encode(302087868);

Decode some strings.

const encoded = '3uxc4s';
const decoded = talker.decode(encoded);

Translate either.

const _99 = talker.translate(talker.translate(99));
const _string = talker.translate(talker.translate('string'));

Yep. That's it.

Errors

The Codetalker constructor will throw a TypeError if given a character Array that contains non-characters, and an Error if your set contains duplicates.

const talker = new Codetalker(['a', 'b', 'c', 4]);
// TypeError: Codetalker character sets must only contain characters.
const talker2 = new Codetalker('aabbcc');
// Error: Codetalker character sets must not contain duplicates.

The encode method will throw a RangeError if the given integer is greater than MAX_SAFE_INT (9007199254740991), or less than zero.

talker.encode(9007199254740992);
// RangeError: 9007199254740992 exceeds maximum 53-bit integer size of 9007199254740991.

talker.encode(-51);
// RangeError: Integer input should not be negative.

The decode method will throw a TypeError if the given input is an Array, and contains a non-character value, and an Error if the given input contains a character not found in the character set. It will also throw a RangeError if the decoded string produces a value which exceeds MAX_SAFE_INT (9007199254740991).

const talker = new Codetalker('qwertyuiopasdfghjklzxcvbnm$');

talker.decode(['q', 'w', 3]);
// TypeError: Iterable must contain only characters.

talker.decode('***');
// Error: * does not exist in character set.

talker.decode('$$$$$$$$$$$$')
// RangeError: Decoded integer exceeds logical encoding size.

The translate method will throw a TypeError if the given value is not a Number, String, or Array.

talker.translate([1, 2, 3, 4]);
// TypeError: Translation requires a Number, String, or Array type.

Notes

While expected as a String value, characters sets (constructor) and values to be decoded (decode, translate) are permitted as an Array of characters.

Strings are easier on the eyes.

Unicode is a no-go.

License

MIT


Enjoy!

Colin 'Oka' Hall-Coates

oka.io | @Okahyphen

Keywords

FAQs

Package last updated on 28 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc