Skip to content

Instantly share code, notes, and snippets.

@bclinkinbeard
Forked from ThomasBurleson/gist:6445197
Last active December 22, 2015 08:29
Show Gist options
  • Select an option

  • Save bclinkinbeard/6445373 to your computer and use it in GitHub Desktop.

Select an option

Save bclinkinbeard/6445373 to your computer and use it in GitHub Desktop.
CommonJS format
var supplant = require('utils/supplant');
function rhex(n) {
var s = '', j = 0;
for (; j < 4; j++) {
s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + hex_chr[(n >> (j * 8)) & 0x0F];
}
return s;
}
function hex(x) {
for (var i = 0; i < x.length; i++) {
x[i] = rhex(x[i]);
}
return x.join('');
}
module.exports = function () {
// Build an instance of the md5 object with encrypt() features only...
return {
encrypt: function (s) {
var results = hex(s);
console.log(supplant('md5::encrypt({0}) = {1}', [ s, results]));
return results;
}
};
};
@ThomasBurleson
Copy link

But aren't the functions hex() and rhex() global; with this implementation ?

@bclinkinbeard
Copy link
Author

Nope, Browserify wraps the file's contents in an IIFE and only module.exports gets exposed.

@mbilokonsky
Copy link

Seems like they wouldn't be if this is loaded into a node-style context, right? That's the point of the "module.exports" object. All of this stays encapsulated, if I understand correctly.

If you just include this via a <script> tag, of course, then it would be global. But Browserify, as I understand it, basically uses node-style module loading to compile this before you include it, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment