Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for .version, .encode and .validate tests;

  • Loading branch information...
commit ac856d4f8a25b8a79c94cca2ca1f2bf43dc33a53 1 parent 587aff4
RGBboy authored
Showing with 58 additions and 0 deletions.
  1. +2 −0  index.js
  2. +56 −0 lib/urlsafe-base64.js
View
2  index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/urlsafe-base64');
View
56 lib/urlsafe-base64.js
@@ -0,0 +1,56 @@
+/*!
+ * urlsafe-base64
+ * Copyright(c) 2013 RGBboy <me@rgbboy.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module Dependencies
+ */
+
+// None yet!
+
+/**
+ * Library version.
+ */
+
+exports.version = '0.0.1';
+
+/**
+ * .encode
+ *
+ * return an encoded Buffer as URL Safe Base64
+ *
+ * Note: This function encodes to the RFC 4648 Spec where '+' is encoded
+ * as '-' and '/' is encoded as '_'. The padding character '=' is
+ * removed.
+ *
+ * @param {Buffer} buffer
+ * @return {String}
+ * @api public
+ */
+
+exports.encode = function encode(buffer) {
+
+ return buffer.toString('base64')
+ .replace(/\+/g, '-') // Convert '+' to '-'
+ .replace(/\//g, '_') // Convert '/' to '_'
+ .replace(/=+$/, ''); // Remove ending '='
+
+};
+
+/**
+ * .validate
+ *
+ * Validates a string if it is URL Safe Base64 encoded.
+ *
+ * @param {String}
+ * @return {Boolean}
+ * @api public
+ */
+
+exports.validate = function validate(base64) {
+
+ return /^[A-Za-z0-9\-_]+$/.test(base64);
+
+};
Please sign in to comment.
Something went wrong with that request. Please try again.