Code Monkey home page Code Monkey 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 andyfusniak avatar chalkers avatar dependabot-support avatar donavon avatar eiriksm avatar fnd avatar greenkeeperio-bot avatar imshara avatar kchapelier avatar kishorkunal-raj avatar lmk123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

base62.js's Issues

Is it possible to disable install-stats.js?

base62.js is one of dependency in my project. :)

I have one problem with install-stats.js in base62/scripts.
Internal network case, because install-stats.js tries to connect google analytics, so npm install takes too long time, and fails to connect in the end.
This makes build time to be increased.

Could you add timeout for this process or give any option to disable it?

Where is the tag for 1.2.8?

Hi, I see that a version 1.2.8 is listed on npm but I cannot find the tag here. Could you push it by chance? Thanks.

Converting base 62 to base 16

I'm trying to convert base 62 to base 16. When I do the following..

Base62.decode('4u8LPK581OHn7kRqRqP9ks').toString(16)

I get 93669fd54as300000000000000000000. I'm expecting A12D08BC6D93BC4E8EA847434C960416. You can see a php based converter here http://convertxy.com/index.php/numberbases/ return the correct value.

Is the difference due to JS using scientific notation for these large numbers? In other words, Base62.decode('4u8LPK581OHn7kRqRqP9ks') returns 1.9592937146177773e+38. Does that munge the conversion to base 16 via toString(16)?

Thanks.

Install problem

Your recent change to the package json, specifically the postinstall where you're trying to scrape stats is causing install failures

"postinstall": "TID=UA-265870-43 node scripts/install-stats.js"

The repository doesn't mention a license

I would like to use base62 in my project, but you haven't included a license, so I can't. Could you pick one? (MIT or similar would be great.)

Thanks in advance!

Base16 to Base64

What would be the proper way to convert base16 strings into base64 strings, and vice versa ?

For example: (b16) 0xF216E209761BB32986723 -> (b64) ????

modular ES6 variant

As discussed elsewhere, I found myself accidentally re-implementing Base62.js in modular ES6: https://github.com/FND/base62
Now that I've recognized my folly, rather than imposing yet another npm package on the world, it seems worth first considering to merge our efforts instead.

My goal there was to create an efficient library that works in any JavaScript engine (i.e. browsers as well as Node, Nashorn etc.). The only expectation is either ES2015 support or DIY ES5 transpiling - which doesn't have to be painful these days.

ES6 helps not just with readability and hipster cred, but modular exports also avoid including unused bits (think tree shaking).

Preliminary benchmarking (I'm not much of an authority there) suggests that my version is at least on par with the current implementation.

I realize that in its current form, my version's API and aforementioned expectations aren't compatible with Base62.js v1.x, but that's not necessarily a bad thing and still up for debate.

Let me know what you think. (I've tried writing concise and readable code as well as useful commit messages.)

Update NPM

I just used NPM to download the latest version, 0.1.1 and it doesn't give me the latest version from github.

doesnt round trip even 10 places

var Base62=require('base62');
var maxv= Base62.decode("ZZZZZZZZZZ");
var mmaxv = Base62.encode(maxv);
console.log("maxv=" +maxv+" mmaxv="+mmaxv);


>   maxv=839299365868340200 mmaxv=10000000000

`TID` is not recognized command.

@andrew I just now saw that you have released a patch to fix this error by adding a fallback. This error was happening to me in windows, but passed in linux. Why not use cross-env? This package will enable to use same command line environment variables.

Thank you for immediately fixing the issue!

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.