Comparing version 0.1.7 to 0.2.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.2.0"></a> | ||
# [0.2.0](https://github.com/pixelass/abcq/compare/v0.1.7...v0.2.0) (2016-09-10) | ||
### Features | ||
* **method:** allow decoding ([b271fa3](https://github.com/pixelass/abcq/commit/b271fa3)) | ||
<a name="0.1.7"></a> | ||
@@ -7,0 +17,0 @@ ## [0.1.7](https://github.com/pixelass/abcq/compare/v0.1.6...v0.1.7) (2016-09-09) |
@@ -40,3 +40,3 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
* @module abcQ | ||
* @overview Number to character combination coverter | ||
* @overview Number / character combination encoder / decoder | ||
* | ||
@@ -51,2 +51,3 @@ * @author Gregor Adams <greg@pixelass.com> | ||
* @type {String} | ||
* @private | ||
*/ | ||
@@ -133,10 +134,10 @@ var alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
value: function generate() { | ||
return this.charsAt(++this.counter); | ||
return this.encode(++this.counter); | ||
} | ||
/** | ||
* Method to cenvert a number into a combination of characters | ||
* Method to encode a number into a combination of characters | ||
* | ||
* This method does not affect any other method. This method can be called multiple times before | ||
* calling `generate` | ||
* This method does not affect any other method. | ||
* This method can be called multiple times before calling `generate` | ||
* | ||
@@ -151,21 +152,17 @@ * @param {Number} i A number greater than `-1`. Given a list of `"ab" | ||
* @return {String} Returns the character combination of the number | ||
* @example | ||
* @example | ||
* const shortid = new abcQ({ | ||
* chars: 'ab' | ||
* }) | ||
* console.log(shortid.charAt(0)) | ||
* console.log(shortid.encode(0)) | ||
* // -> "a" | ||
* console.log(shortid.charAt(9)) | ||
* console.log(shortid.encode(9)) | ||
* // -> "abb" | ||
* console.log(shortid.generate()) | ||
* // -> "a" | ||
* console.log(shortid.generate()) | ||
* // -> "b" | ||
*/ | ||
}, { | ||
key: 'charsAt', | ||
value: function charsAt(i) { | ||
key: 'encode', | ||
value: function encode(i) { | ||
/* | ||
* check if the number is smaller than 0. | ||
* Check if the number is smaller than 0. | ||
* Then return `null` or continue | ||
@@ -182,25 +179,63 @@ */ | ||
* ### Example | ||
* - 'ab': 1; `slots = [1,0] -> "b"` | ||
* - 'ab': 2; `slots = [2,1] -> "aa"` | ||
* - 'ab': 9; `slots = [9,5,3] -> "abb"` | ||
* - 'ab': 1; `slots = [1, 0] -> "b"` | ||
* - 'ab': 2; `slots = [2, 1] -> "aa"` | ||
* - 'ab': 9; `slots = [9 ,5, 2] -> "abb"` | ||
*/ | ||
var nextSlot = ~~(i / this.chars.length); | ||
/* | ||
* Combine all previous slots. | ||
* @todo refactor to `do` when jsdoc supports it | ||
* @example | ||
* const previousSlots = do { | ||
* if (nextSlot > 0) { | ||
* this.charsAt(nextSlot) | ||
* } else { | ||
* '' | ||
* } | ||
* } | ||
*/ | ||
var previousSlots = nextSlot ? this.charsAt(nextSlot - 1) : ''; | ||
/* convert the current slot */ | ||
/* Combine and return all slots. */ | ||
var previousSlots = nextSlot ? this.encode(nextSlot - 1) : ''; | ||
var currentSlot = this.chars[i % this.chars.length]; | ||
return previousSlots + currentSlot; | ||
} | ||
/** | ||
* Method to decode a combination of characters into a number | ||
* | ||
* This method does not affect any other method. | ||
* This method can be called multiple times before calling `generate` | ||
* | ||
* @param {String} str Character combination to decode. Must contain only valid | ||
* characters, Given a list of `"ab" | ||
* the following will be returned | ||
* - "a" -> 0 | ||
* - "b" -> 1 | ||
* - "aa" -> 2 | ||
* - "ab" -> 3 | ||
* - ... | ||
* @return {Number} Returns the index of the input string | ||
* @example | ||
* const shortid = new abcQ({ | ||
* chars: 'ab' | ||
* }) | ||
* console.log(shortid.decode('a')) | ||
* // -> o | ||
* console.log(shortid.decode('abb')) | ||
* // -> 9 | ||
*/ | ||
}, { | ||
key: 'decode', | ||
value: function decode(str) { | ||
/* | ||
* Check if the string contains invalid characters. | ||
* Then return `null` or continue | ||
*/ | ||
if (str.replace(new RegExp('[' + this.chars + ']', 'g'), '') !== '') { | ||
return null; | ||
} | ||
/* Build the index for the given string */ | ||
var i = 0; | ||
var counter = str.length; | ||
/* For every slot add the result. | ||
* Adds all slots to return a `1`-based index | ||
*/ | ||
while (counter--) { | ||
var pow = Math.pow(this.chars.length, str.length - 1 - counter); | ||
i += (this.chars.indexOf(str[counter]) + 1) * pow; | ||
} | ||
/* Subtract `1` to switch back to a `0`-based index */ | ||
return i - 1; | ||
} | ||
}]); | ||
@@ -207,0 +242,0 @@ |
@@ -5,4 +5,4 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
},{"./":2}],2:[function(require,module,exports){ | ||
"use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function t(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",abcQ=function(){function t(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];_classCallCheck(this,t);var r={chars:alphabet,counter:-1};this.options=Object.assign(r,e),this.chars=this.options.chars,this.counter=this.options.counter}return _createClass(t,[{key:"generate",value:function(){return this.charsAt(++this.counter)}},{key:"charsAt",value:function(t){if(t<0)return null;var e=~~(t/this.chars.length),r=e?this.charsAt(e-1):"",n=this.chars[t%this.chars.length];return r+n}}]),t}();exports.default=abcQ; | ||
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",abcQ=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];_classCallCheck(this,e);var n={chars:alphabet,counter:-1};this.options=Object.assign(n,t),this.chars=this.options.chars,this.counter=this.options.counter}return _createClass(e,[{key:"generate",value:function(){return this.encode(++this.counter)}},{key:"encode",value:function(e){if(e<0)return null;var t=~~(e/this.chars.length),n=t?this.encode(t-1):"",r=this.chars[e%this.chars.length];return n+r}},{key:"decode",value:function(e){if(""!==e.replace(new RegExp("["+this.chars+"]","g"),""))return null;for(var t=0,n=e.length;n--;){var r=Math.pow(this.chars.length,e.length-1-n);t+=(this.chars.indexOf(e[n])+1)*r}return t-1}}]),e}();exports.default=abcQ; | ||
},{}]},{},[1]); |
@@ -1,1 +0,2 @@ | ||
module.exports = require("./lib").default; | ||
/* global exports */ | ||
exports = require("./lib").default; |
102
lib/index.js
@@ -14,3 +14,3 @@ 'use strict'; | ||
* @module abcQ | ||
* @overview Number to character combination coverter | ||
* @overview Number / character combination encoder / decoder | ||
* | ||
@@ -25,2 +25,3 @@ * @author Gregor Adams <greg@pixelass.com> | ||
* @type {String} | ||
* @private | ||
*/ | ||
@@ -47,3 +48,2 @@ var alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
*/ | ||
function abcQ() { | ||
@@ -110,10 +110,10 @@ var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
value: function generate() { | ||
return this.charsAt(++this.counter); | ||
return this.encode(++this.counter); | ||
} | ||
/** | ||
* Method to cenvert a number into a combination of characters | ||
* Method to encode a number into a combination of characters | ||
* | ||
* This method does not affect any other method. This method can be called multiple times before | ||
* calling `generate` | ||
* This method does not affect any other method. | ||
* This method can be called multiple times before calling `generate` | ||
* | ||
@@ -128,21 +128,17 @@ * @param {Number} i A number greater than `-1`. Given a list of `"ab" | ||
* @return {String} Returns the character combination of the number | ||
* @example | ||
* @example | ||
* const shortid = new abcQ({ | ||
* chars: 'ab' | ||
* }) | ||
* console.log(shortid.charAt(0)) | ||
* console.log(shortid.encode(0)) | ||
* // -> "a" | ||
* console.log(shortid.charAt(9)) | ||
* console.log(shortid.encode(9)) | ||
* // -> "abb" | ||
* console.log(shortid.generate()) | ||
* // -> "a" | ||
* console.log(shortid.generate()) | ||
* // -> "b" | ||
*/ | ||
}, { | ||
key: 'charsAt', | ||
value: function charsAt(i) { | ||
key: 'encode', | ||
value: function encode(i) { | ||
/* | ||
* check if the number is smaller than 0. | ||
* Check if the number is smaller than 0. | ||
* Then return `null` or continue | ||
@@ -159,25 +155,63 @@ */ | ||
* ### Example | ||
* - 'ab': 1; `slots = [1,0] -> "b"` | ||
* - 'ab': 2; `slots = [2,1] -> "aa"` | ||
* - 'ab': 9; `slots = [9,5,3] -> "abb"` | ||
* - 'ab': 1; `slots = [1, 0] -> "b"` | ||
* - 'ab': 2; `slots = [2, 1] -> "aa"` | ||
* - 'ab': 9; `slots = [9 ,5, 2] -> "abb"` | ||
*/ | ||
var nextSlot = ~ ~(i / this.chars.length); | ||
var nextSlot = ~~(i / this.chars.length); | ||
/* | ||
* Combine all previous slots. | ||
* @todo refactor to `do` when jsdoc supports it | ||
* @example | ||
* const previousSlots = do { | ||
* if (nextSlot > 0) { | ||
* this.charsAt(nextSlot) | ||
* } else { | ||
* '' | ||
* } | ||
* } | ||
*/ | ||
var previousSlots = nextSlot ? this.charsAt(nextSlot - 1) : ''; | ||
/* convert the current slot */ | ||
/* Combine and return all slots. */ | ||
var previousSlots = nextSlot ? this.encode(nextSlot - 1) : ''; | ||
var currentSlot = this.chars[i % this.chars.length]; | ||
return previousSlots + currentSlot; | ||
} | ||
/** | ||
* Method to decode a combination of characters into a number | ||
* | ||
* This method does not affect any other method. | ||
* This method can be called multiple times before calling `generate` | ||
* | ||
* @param {String} str Character combination to decode. Must contain only valid | ||
* characters, Given a list of `"ab" | ||
* the following will be returned | ||
* - "a" -> 0 | ||
* - "b" -> 1 | ||
* - "aa" -> 2 | ||
* - "ab" -> 3 | ||
* - ... | ||
* @return {Number} Returns the index of the input string | ||
* @example | ||
* const shortid = new abcQ({ | ||
* chars: 'ab' | ||
* }) | ||
* console.log(shortid.decode('a')) | ||
* // -> o | ||
* console.log(shortid.decode('abb')) | ||
* // -> 9 | ||
*/ | ||
}, { | ||
key: 'decode', | ||
value: function decode(str) { | ||
/* | ||
* Check if the string contains invalid characters. | ||
* Then return `null` or continue | ||
*/ | ||
if (str.replace(new RegExp('[' + this.chars + ']', 'g'), '') !== '') { | ||
return null; | ||
} | ||
/* Build the index for the given string */ | ||
var i = 0; | ||
var counter = str.length; | ||
/* For every slot add the result. | ||
* Adds all slots to return a `1`-based index | ||
*/ | ||
while (counter--) { | ||
var pow = Math.pow(this.chars.length, str.length - 1 - counter); | ||
i += (this.chars.indexOf(str[counter]) + 1) * pow; | ||
} | ||
/* Subtract `1` to switch back to a `0`-based index */ | ||
return i - 1; | ||
} | ||
}]); | ||
@@ -184,0 +218,0 @@ |
{ | ||
"name": "abcq", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -9,5 +9,8 @@ "repository": "git@github.com:pixelass/abcq.git", | ||
"babel": "babel src --out-dir lib", | ||
"prebuild": "NODE_ENV=production", | ||
"build": "npm run babel; node build", | ||
"create-index": "echo 'module.exports = require(\"./lib\").default;' > index.js", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"create-index": "echo '/* global exports */\nexports = require(\"./lib\").default;' > index.js", | ||
"docs": "rm -rf docs && mkdir docs && jsdoc -c jsdoc.json", | ||
"lint": "eslint src", | ||
"precommit": "npm test", | ||
@@ -17,4 +20,5 @@ "prepublish": "npm run create-index; npm run build", | ||
"prerelease": "npm test", | ||
"pretest": "npm run build; standard src", | ||
"release": "standard-version --no-verify" | ||
"pretest": "NODE_ENV=test;npm run build; npm run lint", | ||
"release": "standard-version --no-verify", | ||
"test": "babel-node ./node_modules/istanbul/lib/cli.js cover _mocha -- ./src/spec" | ||
}, | ||
@@ -25,2 +29,4 @@ "keywords": [], | ||
"devDependencies": { | ||
"babel-cli": "^6.14.0", | ||
"babel-plugin-istanbul": "^2.0.1", | ||
"babel-preset-es2015": "^6.14.0", | ||
@@ -30,11 +36,17 @@ "babel-preset-stage-0": "^6.5.0", | ||
"browserify": "^13.1.0", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.12", | ||
"cz-conventional-changelog": "^1.2.0", | ||
"errorify": "^0.3.1", | ||
"eslint": "^3.4.0", | ||
"eslint-config-standard": "^6.0.0", | ||
"eslint-plugin-promise": "^2.0.1", | ||
"eslint-plugin-standard": "^2.0.0", | ||
"hopsdoc": "^0.1.7", | ||
"husky": "^0.11.7", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"jsdoc": "^3.4.1", | ||
"log": "^1.4.0", | ||
"standard": "^8.0.0", | ||
"mocha": "^3.0.2", | ||
"standard-version": "^2.4.0", | ||
"uglify-js": "^2.7.3", | ||
"uglifyify": "^3.0.2", | ||
@@ -41,0 +53,0 @@ "watchify": "^3.7.0" |
@@ -10,2 +10,3 @@ # abcq | ||
[![Travis](https://img.shields.io/travis/pixelass/abcq.svg)](https://travis-ci.org/pixelass/abcq) | ||
[![Coveralls branch](https://img.shields.io/coveralls/pixelass/abcq.svg)](https://coveralls.io/github/pixelass/abcq) | ||
[![David](https://img.shields.io/david/pixelass/abcq.svg)](https://david-dm.org/pixelass/abcq) | ||
@@ -22,3 +23,3 @@ [![David](https://img.shields.io/david/dev/pixelass/abcq.svg)](https://david-dm.org/pixelass/abcq#info=devDependencies&view=table) | ||
* Convert numbers to character combinations. | ||
* Count by character cobination | ||
* Count by character combination | ||
* Create unique ids | ||
@@ -40,4 +41,6 @@ * Create simple hashes | ||
// -> b | ||
shortid.charsAt(1234567890) | ||
shortid.encode(1234567890) | ||
// -> clRjXk | ||
shortid.decode('clRjXk') | ||
// -> 1234567890 | ||
``` | ||
@@ -58,3 +61,3 @@ | ||
// -> 🦄💖💖🦄💖 | ||
unicornLove.charsAt(8) | ||
unicornLove.encode(8) | ||
// -> 🦄💖🦄 | ||
@@ -113,8 +116,16 @@ ``` | ||
### `charsAt` | ||
### `encode` | ||
```js | ||
const abc = new abcQ() | ||
abc.charsAt(1234567890) | ||
abc.encode(1234567890) | ||
// -> clRjXk | ||
``` | ||
### `decode` | ||
```js | ||
const abc = new abcQ() | ||
abc.decode('clRjXk') | ||
// -> 1234567890 | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30799
13
582
127
23