Comparing version 0.0.1 to 0.0.2
0.0.2 / 2013-01-31 | ||
================== | ||
* add base64 encode and urlsafe base64 encode | ||
* add html escape() | ||
* update makefile | ||
0.0.1 / 2012-11-13 | ||
@@ -3,0 +10,0 @@ ================== |
@@ -35,1 +35,51 @@ /*! | ||
}; | ||
/** | ||
* Base64 encode string. | ||
* | ||
* @param {String|Buffer} s | ||
* @param {Boolean} [urlsafe=false] Encode string s using a URL-safe alphabet, | ||
* which substitutes - instead of + and _ instead of / in the standard Base64 alphabet. | ||
* @return {String} base64 encode format string. | ||
*/ | ||
exports.base64encode = function (s, urlsafe) { | ||
if (!Buffer.isBuffer(s)) { | ||
s = new Buffer(s); | ||
} | ||
var encode = s.toString('base64'); | ||
if (urlsafe) { | ||
encode = encode.replace(/\+/g, '-').replace(/\//g, '_'); | ||
} | ||
return encode; | ||
}; | ||
/** | ||
* Base64 string decode. | ||
* | ||
* @param {String} encode, base64 encoding string. | ||
* @param {Boolean} [urlsafe=false] Decode string s using a URL-safe alphabet, | ||
* which substitutes - instead of + and _ instead of / in the standard Base64 alphabet. | ||
* @return {String} plain text. | ||
*/ | ||
exports.base64decode = function (encode, urlsafe) { | ||
if (urlsafe) { | ||
encode = encode.replace(/\-/g, '+').replace(/_/g, '/'); | ||
} | ||
encode = new Buffer(encode, 'base64'); | ||
return encode.toString(); | ||
}; | ||
/** | ||
* Escape the given string of `html`. | ||
* | ||
* @param {String} html | ||
* @return {String} | ||
* @public | ||
*/ | ||
exports.escape = function (html) { | ||
return String(html) | ||
.replace(/&(?!\w+;)/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"'); | ||
}; |
{ | ||
"name": "utility", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A collection of useful utilities.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,2 +25,10 @@ utility [![Build Status](https://secure.travis-ci.org/fengmk2/utility.png)](http://travis-ci.org/fengmk2/utility) | ||
// base64 encode | ||
utils.base64encode('你好¥'); // '5L2g5aW977+l' | ||
utils.base64decode('5L2g5aW977+l') // '你好¥' | ||
// urlsafe base64 encode | ||
utils.base64encode('你好¥', true); // '5L2g5aW977-l' | ||
utils.base64decode('5L2g5aW977-l', true); // '你好¥' | ||
// empty function | ||
@@ -31,2 +39,5 @@ process.nextTick(utils.noop); | ||
} | ||
// html escape | ||
utils.escape('<script/>"& &'); // '<script/>"& &' | ||
``` | ||
@@ -33,0 +44,0 @@ |
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
5856
76
66