Coder Social home page Coder Social logo

base62.js's Introduction

Base62.js

npm version

A JavaScript Base62 encode/decoder

What is Base62 encoding?

Base62 encoding converts numbers to ASCII strings (0-9, a-z and A-Z) and vice versa, which typically results in comparatively short strings. Such identifiers also tend to more readily identifiable by humans.

  • 999"g7"
  • 9999"2Bh"
  • 238327"ZZZ"

Installation

npm install base62

alternatively using Yarn:

yarn add base62

Usage

For backwards compatibility, Base62.js exposes v1.x's API by default – see Legacy API below. For efficiency, v2.x's modernized API allows selectively importing individual modules instead:

var base62 = require("base62/lib/ascii");

base62.encode(999);  // "g7"
base62.decode("g7"); // 999

This uses the default ASCII character set for encoding/decoding.

It's also possible to define a custom character set instead:

var base62 = require("base62/lib/custom");

var charset = "~9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-=";
charset = base62.indexCharset(charset);

base62.encode(999, charset);  // "F3"
base62.decode("F3", charset); // 999

Note that indexCharset typically expects the respective string to contain exactly 62 unique character, but does not validate this for efficieny. In fact, it's also possible to use characters sets with more than 62 characters in order to achieve shorter identifiers for large numbers.

Legacy API

Base62.js v1.x's API is maintained for backwards compatibility.

var base62 = require("base62");

base62.encode(999);  // "g7"
base62.decode("g7"); // 999

This uses the default ASCII character set for encoding/decoding.

It's also possible to define a custom character set instead:

var base62 = require("base62");

var charset = "~9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-=";
base62.setCharacterSet(charset);

base62.encode(999);  // "F3"
base62.decode("F3"); // 999

setCharacterSet ensures that the respective string contains exactly 62 unique characters.

Development

Source code is hosted on GitHub. Please report issues or feature requests in GitHub Issues.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Send me a pull request. Bonus points for topic branches.

Release Process for Maintainers

  • Update the version number in package.json.
  • Commit this change with the respective version number as commit message (e.g. "1.2.3").
  • Create an annotated tag, using the prefixed version number (e.g. git tag -am "1.2.3" v1.2.3).
  • Publish the new version: git push --tags origin master and npm publish.

Copyright

Copyright (c) 2024 Andrew Nesbitt. See LICENSE for details.

base62.js's People

Contributors

andrew avatar fnd avatar chalkers avatar kchapelier avatar greenkeeperio-bot avatar donavon avatar andyfusniak avatar dependabot-support avatar eiriksm avatar imshara avatar kishorkunal-raj avatar lmk123 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.