pax_global_header00006660000000000000000000000064134377607340014530gustar00rootroot0000000000000052 comment=f99208bff69de2d8d11acfc9b1332d572fe2f4dd base62.js-2.0.1/000077500000000000000000000000001343776073400132255ustar00rootroot00000000000000base62.js-2.0.1/.codeclimate.yml000066400000000000000000000006221343776073400162770ustar00rootroot00000000000000--- engines: duplication: enabled: true config: languages: - ruby - javascript - python - php eslint: enabled: true fixme: enabled: true checks: similar-code: config: threshold: 50 ratings: paths: - "**.inc" - "**.js" - "**.jsx" - "**.module" - "**.php" - "**.py" - "**.rb" exclude_paths: - base62.js - test/ - benchmark/ base62.js-2.0.1/.eslintignore000066400000000000000000000000201343776073400157200ustar00rootroot00000000000000**/*{.,-}min.js base62.js-2.0.1/.eslintrc000066400000000000000000000100031343776073400150430ustar00rootroot00000000000000ecmaFeatures: modules: true jsx: true env: amd: true browser: true es6: true jquery: true node: true parserOptions: sourceType: module # http://eslint.org/docs/rules/ rules: # Possible Errors comma-dangle: [2, never] no-cond-assign: 2 no-console: 0 no-constant-condition: 2 no-control-regex: 2 no-debugger: 2 no-dupe-args: 2 no-dupe-keys: 2 no-duplicate-case: 2 no-empty: 2 no-empty-character-class: 2 no-ex-assign: 2 no-extra-boolean-cast: 2 no-extra-parens: 0 no-extra-semi: 2 no-func-assign: 2 no-inner-declarations: [2, functions] no-invalid-regexp: 2 no-irregular-whitespace: 2 no-negated-in-lhs: 2 no-obj-calls: 2 no-regex-spaces: 2 no-sparse-arrays: 2 no-unexpected-multiline: 2 no-unreachable: 2 use-isnan: 2 valid-jsdoc: 0 valid-typeof: 2 # Best Practices accessor-pairs: 2 block-scoped-var: 0 complexity: [2, 6] consistent-return: 0 curly: 0 default-case: 0 dot-location: 0 dot-notation: 0 eqeqeq: 2 guard-for-in: 2 no-alert: 2 no-caller: 2 no-case-declarations: 2 no-div-regex: 2 no-else-return: 0 no-empty-label: 2 no-empty-pattern: 2 no-eq-null: 2 no-eval: 2 no-extend-native: 2 no-extra-bind: 2 no-fallthrough: 2 no-floating-decimal: 0 no-implicit-coercion: 0 no-implied-eval: 2 no-invalid-this: 0 no-iterator: 2 no-labels: 0 no-lone-blocks: 2 no-loop-func: 2 no-magic-number: 0 no-multi-spaces: 0 no-multi-str: 0 no-native-reassign: 2 no-new-func: 2 no-new-wrappers: 2 no-new: 2 no-octal-escape: 2 no-octal: 2 no-proto: 2 no-redeclare: 2 no-return-assign: 2 no-script-url: 2 no-self-compare: 2 no-sequences: 0 no-throw-literal: 0 no-unused-expressions: 2 no-useless-call: 2 no-useless-concat: 2 no-void: 2 no-warning-comments: 0 no-with: 2 radix: 2 vars-on-top: 0 wrap-iife: 2 yoda: 0 # Strict strict: 0 # Variables init-declarations: 0 no-catch-shadow: 2 no-delete-var: 2 no-label-var: 2 no-shadow-restricted-names: 2 no-shadow: 0 no-undef-init: 2 no-undef: 0 no-undefined: 0 no-unused-vars: 0 no-use-before-define: 0 # Node.js and CommonJS callback-return: 2 global-require: 2 handle-callback-err: 2 no-mixed-requires: 0 no-new-require: 0 no-path-concat: 2 no-process-exit: 2 no-restricted-modules: 0 no-sync: 0 # Stylistic Issues array-bracket-spacing: 0 block-spacing: 0 brace-style: 0 camelcase: 0 comma-spacing: 0 comma-style: 0 computed-property-spacing: 0 consistent-this: 0 eol-last: 0 func-names: 0 func-style: 0 id-length: 0 id-match: 0 indent: 0 jsx-quotes: 0 key-spacing: 0 linebreak-style: 0 lines-around-comment: 0 max-depth: 0 max-len: 0 max-nested-callbacks: 0 max-params: 0 max-statements: [2, 30] new-cap: 0 new-parens: 0 newline-after-var: 0 no-array-constructor: 0 no-bitwise: 0 no-continue: 0 no-inline-comments: 0 no-lonely-if: 0 no-mixed-spaces-and-tabs: 0 no-multiple-empty-lines: 0 no-negated-condition: 0 no-nested-ternary: 0 no-new-object: 0 no-plusplus: 0 no-restricted-syntax: 0 no-spaced-func: 0 no-ternary: 0 no-trailing-spaces: 0 no-underscore-dangle: 0 no-unneeded-ternary: 0 object-curly-spacing: 0 one-var: 0 operator-assignment: 0 operator-linebreak: 0 padded-blocks: 0 quote-props: 0 quotes: 0 require-jsdoc: 0 semi-spacing: 0 semi: 0 sort-vars: 0 space-after-keywords: 0 space-before-blocks: 0 space-before-function-paren: 0 space-before-keywords: 0 space-in-parens: 0 space-infix-ops: 0 space-return-throw-case: 0 space-unary-ops: 0 spaced-comment: 0 wrap-regex: 0 # ECMAScript 6 arrow-body-style: 0 arrow-parens: 0 arrow-spacing: 0 constructor-super: 0 generator-star-spacing: 0 no-arrow-condition: 0 no-class-assign: 0 no-const-assign: 0 no-dupe-class-members: 0 no-this-before-super: 0 no-var: 0 object-shorthand: 0 prefer-arrow-callback: 0 prefer-const: 0 prefer-reflect: 0 prefer-spread: 0 prefer-template: 0 require-yield: 0 base62.js-2.0.1/.gitignore000066400000000000000000000001741343776073400152170ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results node_modules npm-debug.log build .lock-wscript .ideabase62.js-2.0.1/.travis.yml000066400000000000000000000001201343776073400153270ustar00rootroot00000000000000language: node_js node_js: - "9.10" - "8.7" - "7.10" - "6.10" - "6.3" base62.js-2.0.1/CODE_OF_CONDUCT.md000066400000000000000000000062331343776073400160300ustar00rootroot00000000000000# Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at andrewnez@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] [homepage]: http://contributor-covenant.org [version]: http://contributor-covenant.org/version/1/4/ base62.js-2.0.1/CONTRIBUTING.md000066400000000000000000000007011343776073400154540ustar00rootroot00000000000000# Contributing * Fork the project. * Make your feature addition or bug fix. * Add documentation if necessary. * Add tests for it. This is important so I don't break it in a future version unintentionally. * Send a pull request. Bonus points for topic branches. ## Code of Conduct Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms. base62.js-2.0.1/LICENSE000066400000000000000000000020421343776073400142300ustar00rootroot00000000000000Copyright (c) 2016 Andrew Nesbitt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. base62.js-2.0.1/Readme.md000066400000000000000000000067701343776073400147560ustar00rootroot00000000000000# [Base62.js](http://libraries.io/npm/base62) [![build status](https://secure.travis-ci.org/base62/base62.js.svg)](http://travis-ci.org/base62/base62.js) [![npm version](https://badge.fury.io/js/base62.svg)](http://badge.fury.io/js/base62) [![devDependency Status](https://david-dm.org/base62/base62.js/dev-status.svg?theme=shields.io)](https://david-dm.org/base62/base62.js#info=devDependencies) [![Gitter chat](http://img.shields.io/badge/gitter-base62/base62.js-brightgreen.svg)](https://gitter.im/base62/base62.js) 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 ```shell npm install base62 ``` alternatively using Yarn: ```shell yarn add base62 ``` ## Usage For backwards compatibility, Base62.js exposes v1.x's API by default – see [Legacy API](#legacy-api) below. For efficiency, v2.x's modernized API allows selectively importing individual modules instead: ```javascript 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: ```javascript 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. ```javascript 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: ```javascript 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](http://github.com/base62/base62.js). Please report issues or feature requests in [GitHub Issues](http://github.com/base62/base62.js.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) 2016 Andrew Nesbitt. See [LICENSE](https://github.com/base62/base62.js/blob/master/LICENSE) for details. base62.js-2.0.1/benchmark/000077500000000000000000000000001343776073400151575ustar00rootroot00000000000000base62.js-2.0.1/benchmark/benchmarks.js000066400000000000000000000035641343776073400176420ustar00rootroot00000000000000var base62 = require('../lib/ascii'); var base62custom = require('../lib/custom'); var encode = base62.encode; var decode = base62.decode; var encodeCustom = base62custom.encode; var decodeCustom = base62custom.decode; var indexCharset = base62custom.indexCharset; var intResult, strResult, now, delta, i; var charset = indexCharset('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'); function performanceNow() { var t = process.hrtime(); return t[0] * 1000 + t[1] / 1000000; } // decode with default charset (ASCII) now = performanceNow(); for (intResult = 0, i = 0; i < 1000000; i++) { intResult += decode('00thing'); } delta = performanceNow() - now; console.log('|', 'decoding with default charset (1000000x)', '|', intResult === 432635954000000 ? 'correct' : 'incorrect', '|', delta.toFixed(2), 'ms', '|'); // encode with default charset (ASCII) now = performanceNow(); for (strResult = '', i = 0; i < 1000000; i++) { strResult = encode(i); } delta = performanceNow() - now; console.log('|', 'encoding with default charset (1000000x)', '|', strResult === '4c91' ? 'correct' : 'incorrect', '|', delta.toFixed(2), 'ms', '|'); // decode with custom charset now = performanceNow(); for (intResult = 0, i = 0; i < 1000000; i++) { intResult += decodeCustom('00thing', charset); } delta = performanceNow() - now; console.log('|', 'decoding with custom charset (1000000x)', '|', intResult === 823118800000000 ? 'correct' : 'incorrect', '|', delta.toFixed(2), 'ms', '|'); // encode with custom charset now = performanceNow(); for (strResult = '', i = 0; i < 1000000; i++) { strResult = encodeCustom(i, charset); } delta = performanceNow() - now; console.log('|', 'encoding with custom charset (1000000x)', '|', strResult === '4C91' ? 'correct' : 'incorrect', '|', delta.toFixed(2), 'ms', '|'); base62.js-2.0.1/benchmark/benchmarks_legacy.js000066400000000000000000000032511343776073400211570ustar00rootroot00000000000000var base62 = require("../"); var now = 0, deltaTime = 0, i = 0, intResult = 0, strResult = 0; function performanceNow(){ var t = process.hrtime(); return t[0] * 1000 + t[1] / 1000000; } console.log('running legacy benchmarks'); //decode with default charset now = performanceNow(); for (intResult = 0, i = 0; i < 1000000; i++) { intResult += base62.decode('00thing'); } deltaTime = performanceNow() - now; console.log('|', 'decoding with default charset (1000000x)', '|', intResult === 432635954000000 ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|'); //encode with default charset now = performanceNow(); for (strResult = '', i = 0; i < 1000000; i++) { strResult = base62.encode(i); } deltaTime = performanceNow() - now; console.log('|', 'encoding with default charset (1000000x)', '|', strResult === '4c91' ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|'); //decode with custom charset base62.setCharacterSet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'); now = performanceNow(); for (intResult = 0, i = 0; i < 1000000; i++) { intResult += base62.decode('00thing'); } deltaTime = performanceNow() - now; console.log('|', 'decoding with custom charset (1000000x)', '|', intResult === 823118800000000 ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|'); //encode with custom charset now = performanceNow(); for (strResult = '', i = 0; i < 1000000; i++) { strResult = base62.encode(i); } deltaTime = performanceNow() - now; console.log('|', 'encoding with custom charset (1000000x)', '|', strResult === '4C91' ? 'correct' : 'incorrect', '|', deltaTime.toFixed(2), 'ms', '|'); base62.js-2.0.1/index.d.ts000066400000000000000000000003221343776073400151230ustar00rootroot00000000000000declare namespace base62 { export function setCharacterSet(characters: string): void export function encode(number: number): string export function decode(string: string): number } export = base62 base62.js-2.0.1/lib/000077500000000000000000000000001343776073400137735ustar00rootroot00000000000000base62.js-2.0.1/lib/ascii.js000066400000000000000000000014611343776073400154230ustar00rootroot00000000000000"use strict"; var CHARSET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); // NB: does not validate input exports.encode = function encode(int) { if (int === 0) { return CHARSET[0]; } var res = ""; while (int > 0) { res = CHARSET[int % 62] + res; int = Math.floor(int / 62); } return res; }; exports.decode = function decode(str) { var res = 0, length = str.length, i, char; for (i = 0; i < length; i++) { char = str.charCodeAt(i); if (char < 58) { // 0-9 char = char - 48; } else if (char < 91) { // A-Z char = char - 29; } else { // a-z char = char - 87; } res += char * Math.pow(62, length - i - 1); } return res; }; base62.js-2.0.1/lib/custom.js000066400000000000000000000020051343776073400156400ustar00rootroot00000000000000"use strict"; // NB: does not validate input exports.encode = function encode(int, charset) { let byCode = charset.byCode; if (int === 0) { return byCode[0]; } var res = "", max = charset.length; while (int > 0) { res = byCode[int % max] + res; int = Math.floor(int / max); } return res; }; exports.decode = function decode(str, charset) { var byChar = charset.byChar, res = 0, length = str.length, max = charset.length, i, char; for (i = 0; i < length; i++) { char = str[i]; res += byChar[char] * Math.pow(max, (length - i - 1)); } return res; }; // NB: does not validate input exports.indexCharset = function indexCharset(str) { var byCode = {}, byChar = {}, length = str.length, i, char; for (i = 0; i < length; i++) { char = str[i]; byCode[i] = char; byChar[char] = i; } return { byCode: byCode, byChar: byChar, length: length }; }; base62.js-2.0.1/lib/legacy.js000066400000000000000000000016011343776073400155730ustar00rootroot00000000000000"use strict"; var base62 = require("./ascii"); var base62custom = require("./custom"); // v1.x API var Base62 = { // NB: mutable singleton encode: base62.encode, decode: base62.decode, setCharacterSet: function(charset) { if(charset.length !== 62) { throw Error("You must supply 62 characters."); } var uniq = {}; // poor man's `Set` var i, char; for(i = 0; i < charset.length; i++) { char = charset[i]; if(uniq[char]) { throw Error("You must use unique characters."); } uniq[char] = true; } charset = base62custom.indexCharset(charset); Base62.encode = function(value) { return base62custom.encode(value, charset) }; Base62.decode = function(value) { return base62custom.decode(value, charset) }; } }; module.exports = Base62; base62.js-2.0.1/package.json000066400000000000000000000013351343776073400155150ustar00rootroot00000000000000{ "author": "Andrew Nesbitt (http://nesbitt.io)", "name": "base62", "description": "JavaScript Base62 encode/decoder", "keywords": [ "base-62", "encoder", "decoder" ], "license": "MIT", "version": "2.0.1", "repository": { "type": "git", "url": "git+https://github.com/base62/base62.js.git" }, "bugs": { "url": "https://github.com/base62/base62.js/issues" }, "homepage": "https://github.com/base62/base62.js", "main": "./lib/legacy.js", "engines": { "node": ">=6.0.0" }, "scripts": { "test": "mocha", "benchmark": "node benchmark/benchmarks.js; echo; node benchmark/benchmarks_legacy.js" }, "devDependencies": { "mocha": "~6.0.2" } } base62.js-2.0.1/test/000077500000000000000000000000001343776073400142045ustar00rootroot00000000000000base62.js-2.0.1/test/test_ascii.js000066400000000000000000000023611343776073400166730ustar00rootroot00000000000000/* global describe, it */ "use strict"; var base62 = require("../lib/ascii"); var assert = require("assert"); var encode = base62.encode; var decode = base62.decode; var assertSame = assert.strictEqual; describe("Base62 codec (ASCII)", function() { it("should encode numbers", function() { assertSame(encode(0), "0"); assertSame(encode(7), "7"); assertSame(encode(16), "g"); assertSame(encode(61), "Z"); assertSame(encode(65), "13"); assertSame(encode(999), "g7"); assertSame(encode(9999), "2Bh"); assertSame(encode(238327), "ZZZ"); assertSame(encode(10000000000001), "2Q3rKTOF"); assertSame(encode(10000000000002), "2Q3rKTOG"); }); it("should decode strings", function() { assertSame(decode("0"), 0); assertSame(decode("7"), 7); assertSame(decode("g"), 16); assertSame(decode("Z"), 61); assertSame(decode("13"), 65); assertSame(decode("0013"), 65); // ignore leading zeros assertSame(decode("g7"), 999); assertSame(decode("2Bh"), 9999); assertSame(decode("ZZZ"), 238327); assertSame(decode("2Q3rKTOF"), 10000000000001); assertSame(decode("2Q3rKTOH"), 10000000000003); }); }); base62.js-2.0.1/test/test_custom.js000066400000000000000000000032361343776073400171170ustar00rootroot00000000000000/* global describe, it */ "use strict"; var base62 = require("../lib/custom"); var assert = require("assert"); var encode = base62.encode; var decode = base62.decode; var assertSame = assert.strictEqual; var charset = "9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-=~"; charset = base62.indexCharset(charset); describe("Base62 codec (custom character set)", function() { it("should encode numbers", function() { assertSame(encode(0, charset), "9"); assertSame(encode(7, charset), "2"); assertSame(encode(16, charset), "G"); assertSame(encode(999, charset), "G2"); assertSame(encode(9999, charset), "7bH"); assertSame(encode(238327, charset), "~~~"); }); it("should decode strings", function() { assertSame(decode("9", charset), 0); assertSame(decode("2", charset), 7); assertSame(decode("G", charset), 16); assertSame(decode("G2", charset), 999); assertSame(decode("7bH", charset), 9999); assertSame(decode("~~~", charset), 238327); }); }); describe("arbitrary-length charsets (e.g. Base66)", function() { var ascii = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var charset = ascii + "äöüß"; charset = base62.indexCharset(charset); it("should encode numbers", function() { assertSame(encode(10, charset), "A"); assertSame(encode(65, charset), "ß"); assertSame(encode(70, charset), "14"); }); it("should decode strings", function() { assertSame(decode("A", charset), 10); assertSame(decode("ß", charset), 65); assertSame(decode("14", charset), 70); }); }); base62.js-2.0.1/test/test_legacy.js000066400000000000000000000051011343776073400170420ustar00rootroot00000000000000"use strict"; var Base62 = require("../"); var assert = require("assert"); describe("encode", function() { it("should encode a number to a Base62 string", function() { assert.equal(Base62.encode(0), '0'); assert.equal(Base62.encode(999), 'g7'); assert.equal(Base62.encode(65), '13'); //test big numbers assert.equal(Base62.encode(10000000000001), "2Q3rKTOF"); assert.equal(Base62.encode(10000000000002), "2Q3rKTOG"); }); }); describe("decode", function() { it("should decode a number from a Base62 string", function() { assert.equal(Base62.decode('0'), 0); assert.equal(Base62.decode('g7'), 999); assert.equal(Base62.decode('13'), 65); //zero padded strings assert.equal(Base62.decode('0013'), 65); //test big numbers assert.equal(Base62.decode("2Q3rKTOF"), 10000000000001); assert.equal(Base62.decode("2Q3rKTOH"), 10000000000003); }); }); describe("setCharacterSequence", function(){ it("should update the character sequence", function(){ Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); //Test default character set is not intact assert.notEqual(Base62.encode(999), 'g7'); //Test new character set test cases var testCases = { "G7": 999, "Lxf7": 5234233, "qx": 3283, "29": 133, "1S": 90, "3k": 232, "4I": 266, "2X": 157, "1E": 76, "1L": 83 }; Object.keys(testCases).forEach(function(base62String){ assert.equal(Base62.encode(testCases[base62String]), base62String); assert.equal(Base62.decode(base62String), testCases[base62String]); }); }); it("should throw exceptions on invalid strings", function(){ var errorCheck = function(err) { if ( (err instanceof Error) && /value/.test(err) ) { return true; } }; assert.throws(function(){ Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy"); }, /You must supply 62 characters/); assert.throws(function(){ Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;"); }, /You must supply 62 characters/); assert.throws(function(){ Base62.setCharacterSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzz"); }, /You must use unique characters/); }); });