Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit f08d13f1d6b757e3fa229fe0bdb5097b5de1d70b 0 parents
@alanclarke authored
1  .npmignore
@@ -0,0 +1 @@
+/node_modules/
22 LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2012 alan clarke
+
+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.
35 README.md
@@ -0,0 +1,35 @@
+# int-encoder
+
+a simple utitlity to encode and decode ints from a predefined alphabet
+
+## Getting Started
+Install the module with: `npm install int-encoder`
+
+```javascript
+var int_encoder = require('int-encoder');
+
+int_encoder.encode(12345678); // "ZXP0"
+int_encoder.decode('ZXP0'); // 12345678
+
+//invoke custom alphabet option
+int_encoder.alphabet('0123456789abcdef') //hex alphabet
+
+int_encoder.encode(255); // "ff"
+int_encoder.decode('ff'); // 255
+
+//check what alphabet is being used
+int_encoder.alphabet() //0123456789abcdef
+
+
+
+```
+
+## Contributing
+In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt).
+
+## Release History
+_(Nothing yet)_
+
+## License
+Copyright (c) 2012 alan clarke
+Licensed under the MIT license.
39 grunt.js
@@ -0,0 +1,39 @@
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ pkg: '<json:package.json>',
+ test: {
+ files: ['test/**/*.js']
+ },
+ lint: {
+ files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
+ },
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true
+ },
+ globals: {
+ exports: true
+ }
+ }
+ });
+
+ // Default task.
+ grunt.registerTask('default', 'lint test');
+
+};
52 lib/int-encoder.js
@@ -0,0 +1,52 @@
+/*
+ * int-encoder
+ * https://github.com/alanclarke/int-encoder
+ *
+ * Copyright (c) 2012 alan clarke
+ * Licensed under the MIT license.
+ */
+
+
+(function(){
+ module.exports = (function(alphabet) {
+ alphabet = (alphabet || 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
+ var re = {
+ is_alphabet: new RegExp('^['+alphabet+']*$'),
+ is_int: /^\d*$/
+ };
+ return {
+ encode:function(value){
+ if(!re.is_int.test(value)){
+ throw new Error("int-encoder:encode parameter must be a positive integer");
+ }
+ var result = '';
+ if(value===0){ return alphabet[0]; }
+ while(value>0){
+ var v = value % alphabet.length;
+ result = alphabet[v] + result;
+ value -= v;
+ value = Math.floor(value/alphabet.length);
+ }
+ return result;
+ },
+ decode:function(str){
+ if(!re.is_alphabet.test(str)){
+ throw new Error("int-encoder:decode parameter must be a string made up exclusively from characters from the alphabet option ("+alphabet+")");
+ }
+ var result = 0;
+ for( var i =0; i < str.length; i++ ) {
+ result += (alphabet.indexOf(str[i]) * Math.pow(alphabet.length,str.length - 1 - i ))
+ }
+ return result;
+ },
+ alphabet:function(_alphabet){
+ if(_alphabet){
+ alphabet = _alphabet;
+ re.is_alphabet = new RegExp('^['+alphabet+']+$');
+ } else {
+ return alphabet;
+ }
+ }
+ };
+ })();
+})();
35 package.json
@@ -0,0 +1,35 @@
+{
+ "name": "int-encoder",
+ "description": "a simple utitlity to encode and decode ints from a predefined alphabet",
+ "version": "0.1.0",
+ "homepage": "https://github.com/alanclarke/int-encoder",
+ "author": {
+ "name": "alan clarke",
+ "email": "hi@alz.so",
+ "url": "alz.so"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/alanclarke/int-encoder.git"
+ },
+ "bugs": {
+ "url": "https://github.com/alanclarke/int-encoder/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/alanclarke/int-encoder/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "lib/int-encoder",
+ "engines": {
+ "node": ">= 0.6.0"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.11"
+ },
+ "keywords": []
+}
56 test/int-encoder_test.js
@@ -0,0 +1,56 @@
+var int_encoder = require('../lib/int-encoder.js');
+
+/*
+ ======== A Handy Little Nodeunit Reference ========
+ https://github.com/caolan/nodeunit
+
+ Test methods:
+ test.expect(numAssertions)
+ test.done()
+ Test assertions:
+ test.ok(value, [message])
+ test.equal(actual, expected, [message])
+ test.notEqual(actual, expected, [message])
+ test.deepEqual(actual, expected, [message])
+ test.notDeepEqual(actual, expected, [message])
+ test.strictEqual(actual, expected, [message])
+ test.notStrictEqual(actual, expected, [message])
+ test.throws(block, [error], [message])
+ test.doesNotThrow(block, [error], [message])
+ test.ifError(value)
+*/
+
+
+exports['encode'] = {
+ setUp: function(done) {
+ // setup here
+ done();
+ },
+ 'encoder': function(test) {
+
+ var test_values = [0, 1, 2, 3, 62, 100,1000,354324523453245];
+
+ test_values.forEach(function(i){
+ var encoded = int_encoder.encode( i );
+ test.ok( i.toString().length >= encoded.toString().length, 'encoded string is equal to or longer than int' );
+ test.equal(i,int_encoder.decode(encoded),'decoded string is same as input');
+ });
+ test.done();
+ },
+ 'errors':function(test){
+
+ test.throws(function(){
+ int_encoder.encode('not an int');
+ }, Error, 'encode should only accept ints');
+
+ test.throws(function(){
+ int_encoder.encode(-1);
+ }, Error, 'encode should only accept ints greater or equal to zero');
+
+ test.throws(function(){
+ int_encoder.decode('(*)&^*&(^');
+ }, Error, 'decode should only accept strings made of defined alphabet');
+
+ test.done();
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.