Comparing version 0.0.2 to 0.0.3
59
munge.js
'use strict'; | ||
var munge = function (anyString) { | ||
var aMunged = new Array(); | ||
var encoder = { | ||
TYPES: ['ascii', 'utf8', 'random'], | ||
DEFAULT: 'random', | ||
ascii: function(str) { | ||
return str.charCodeAt(); | ||
}, | ||
utf8: function(str) { | ||
// toString(16) converts decimal (ascii) to hex | ||
var unicode = this.ascii(str).toString(16).toUpperCase(); | ||
// pad with leading zeros to ensure 4 bytes | ||
while (unicode.length < 4) | ||
unicode = '0' + unicode; | ||
return 'x' + unicode; | ||
}, | ||
random: function(str) { | ||
var type = this.TYPES[Math.floor(Math.random() * this.TYPES.length)]; | ||
return this[type](str); | ||
} | ||
}; | ||
/** | ||
* the one and only public function of this module. It takes any string and munges | ||
* it according to the options. By default it uses a random encoding. | ||
* | ||
* | ||
* @param {String} str any string to munge, for example 'spacemonkey@moon.com' | ||
* @param {Object} options for munging | ||
* @param options.encoding can be 'ascii', 'utf8' or 'random' (default) | ||
* @return {String} the munged string | ||
* @api public | ||
*/ | ||
var munge = function(str, options) { | ||
var aMunged = []; | ||
var aChars, i; | ||
if (anyString) { | ||
// initialize default options | ||
options = options || {}; | ||
aChars = anyString.split(''); | ||
if (options.encoding) { | ||
// validate encoding option | ||
if (encoder.TYPES.indexOf(options.encoding) < 0) | ||
throw Error('Invalid encoding option given: ' + options.encoding); | ||
} else | ||
options.encoding = encoder.DEFAULT; | ||
if (str) { | ||
aChars = str.split(''); | ||
for (i in aChars) | ||
aMunged[i] = '&#' + aChars[i].charCodeAt() + ';'; | ||
aMunged[i] = '&#' + encoder[options.encoding](aChars[i]) + ';'; | ||
} | ||
@@ -14,0 +63,0 @@ |
{ | ||
"name": "munge", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "a tiny node module to munge any strings. useful if wou want to obfuscate email addresses to valid, numeric html entities.", | ||
"main": "lib/munge.js", | ||
"main": "munge.js", | ||
"author": "Michael Heuberger <michael.heuberger@binarykitchen.com> (http://binarykitchen.com)", | ||
"keywords": ["munge", "spam", "obfuscation", "ascii"], | ||
"keywords": ["munge", "email", "spam", "obfuscation", "ascii", "utf8"], | ||
"devDependencies": { | ||
@@ -9,0 +9,0 @@ "nodeunit": "*" |
@@ -7,14 +7,18 @@ # munge | ||
## example | ||
## simple example | ||
by default, munge() encodes each char with a random encoding, either ascii or unicode, to make it more difficult for spammers. | ||
because of the random generator the example below does not always produce the same output: | ||
``` js | ||
var munge = require('munge'); | ||
var ANY_EMAIL = 'spacemonkey@moon.com'; | ||
var munge = require('munge'); | ||
console.log(munge(ANY_EMAIL)); | ||
console.log(munge('spacemonkey@moon.com')); | ||
``` | ||
should output | ||
might output something like: | ||
``` | ||
spacemonkey@moon.com | ||
spacemonkey@moon.com | ||
``` | ||
@@ -26,6 +30,34 @@ | ||
## more examples | ||
### ascii encoding only | ||
``` js | ||
var munge = require('munge'); | ||
console.log(munge('spacemonkey@moon.com', {encoding: 'ascii'})); | ||
``` | ||
should output the string with ascii encodings like that: | ||
``` | ||
spacemonkey@moon.com | ||
``` | ||
### utf8 encoding only | ||
``` js | ||
var munge = require('munge'); | ||
console.log(munge('spacemonkey@moon.com', {encoding: 'utf8'})); | ||
``` | ||
outputs the same blurb but in unicode: | ||
``` | ||
spacemonkey@moon.com | ||
``` | ||
## todo | ||
* express/jade integration | ||
* have it a piped stream instead | ||
* have it a piped stream instead (for larger strings; not sure if it makes sense here) | ||
@@ -32,0 +64,0 @@ ## license |
@@ -8,3 +8,3 @@ var testCase = require('nodeunit').testCase, | ||
return typeof(anything) == 'function'; | ||
}; | ||
} | ||
@@ -15,40 +15,80 @@ // boo, type checks are not enabled in nodeunit | ||
return typeof(anything) == 'string'; | ||
}; | ||
} | ||
module.exports = testCase({ | ||
'Loading munge (require)': function(t) { | ||
'loading munge (require)': function(t) { | ||
munge = require('../munge.js'); | ||
t.ok(munge, 'Munge module is loaded.'); | ||
t.ok(munge, 'munge module is loaded.'); | ||
t.done() | ||
}, | ||
'Checking type munge.email': function(t) { | ||
t.ok(isFunction(munge), 'Type of munge.email is a function.'); | ||
'checking type munge.email': function(t) { | ||
t.ok(isFunction(munge), 'type of munge.email is a function.'); | ||
t.done(); | ||
}, | ||
'Munge with weird parameters': function(t) { | ||
'munge with weird parameters': function(t) { | ||
var nothingMunged = munge(); | ||
t.strictEqual(nothingMunged, '', 'Empty parameter returns an empty string'); | ||
t.strictEqual(nothingMunged, '', 'empty parameter returns an empty string'); | ||
t.throws(function() { | ||
munge(new Object()); | ||
}, 'TypeError', 'Object parameter causes TypeError'); | ||
}, 'TypeError', 'first parameter as an object causes TypeError to be thrown.'); | ||
t.throws(function() { | ||
munge(null, {encoding: 'hex'}); | ||
}, 'Error', 'unsupported encoding parameter causes an error to be thrown.'); | ||
t.doesNotThrow(function() { | ||
munge(null, {encoding: 'ascii'}); | ||
}, 'Error', 'encoding option ascii should not throw an error.'); | ||
t.doesNotThrow(function() { | ||
munge(null, {encoding: 'utf8'}); | ||
}, 'Error', 'encoding option utf8 should not throw an error.'); | ||
t.doesNotThrow(function() { | ||
munge(null, {encoding: 'random'}); | ||
}, 'Error', 'encoding option random should not throw an error.'); | ||
t.done(); | ||
}, | ||
'Munging my own email address': function(t) { | ||
'munge my own email address by random': function(t) { | ||
var EMAIL_ADDRESS = 'spacemonkey@moon.com'; | ||
var mungedEmailAddress = munge(EMAIL_ADDRESS); | ||
t.ok(mungedEmailAddress, 'munged email address is not empty.'); | ||
t.ok(isString(mungedEmailAddress), 'munged email address is a string.'); | ||
t.done(); | ||
}, | ||
'ASCII munge my own email address': function(t) { | ||
var EMAIL_ADDRESS = 'spacemonkey@moon.com', | ||
MUNGED_EMAIL_ADDRESS = 'spacemonkey@moon.com'; | ||
var mungedEmailAddress = munge(EMAIL_ADDRESS); | ||
var mungedEmailAddress = munge(EMAIL_ADDRESS, {encoding: 'ascii'}); | ||
t.ok(mungedEmailAddress, 'Munged email address is not empty.'); | ||
t.ok(isString(mungedEmailAddress), 'Munged email address is a string.'); | ||
t.strictEqual(mungedEmailAddress, MUNGED_EMAIL_ADDRESS, 'Munged email address is correct.'); | ||
t.ok(mungedEmailAddress, 'ascii mungedd email address is not empty.'); | ||
t.ok(isString(mungedEmailAddress), 'ascii munged email address is a string.'); | ||
t.strictEqual(mungedEmailAddress, MUNGED_EMAIL_ADDRESS, 'ascii munged email address is correct.'); | ||
t.done(); | ||
}, | ||
'UTF8 munge my own email address': function(t) { | ||
var EMAIL_ADDRESS = 'spacemonkey@moon.com', | ||
MUNGED_EMAIL_ADDRESS = 'spacemonkey@moon.com'; | ||
var mungedEmailAddress = munge(EMAIL_ADDRESS, {encoding: 'utf8'}); | ||
t.ok(mungedEmailAddress, 'utf8 munged email address is not empty.'); | ||
t.ok(isString(mungedEmailAddress), 'utf8 munged email address is a string.'); | ||
t.strictEqual(mungedEmailAddress, MUNGED_EMAIL_ADDRESS, 'utf8 munged email address is correct.'); | ||
t.done(); | ||
} | ||
}); |
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
43205
116
64