Comments (7)
Yeah I think it should be fairly simple to do some variants for different systems like that. I guess it'd be just a matter of putting a wrapper around each function. I'll give it a look when I can.
from tiny-hashes.
I'm already using some of this functions on my website and have wrapped them in a function like this one:
(function(name, root) {
'use strict';
(typeof define !== 'function' || !define.amd
? typeof module == 'undefined' || !module.exports
? function (deps, factory) { root[name] = factory(); } // Browser
: function (deps, factory) { module.exports = factory(); } // CommonJs
: define // AMD
)
/*define*/(/*name, */[], function factory() {
... code ...
return sha1;
});
}('sha1', this));
If you like the idea, I could make a PR.
P.S. Closure-Compiler removes all the comments (including 'use strict'), removes names of expresion functions (ex. factory), renames variables and does some other optimisations.
from tiny-hashes.
Well, if you know what's necessary to make it work in as little code as possible, then a PR would be great! I have to admit I don't really know enough about either AMD or commonJS so I'd have to look it up anyway.
Not sure exactly how to fit it in with the build process, open to suggestions.
from tiny-hashes.
AMD and CommonJS use different approaches to building JS modules and managing dependencies.
AMD is commonly used in Browsers, with the help of script loaders (like RequireJS, curlJS and many others).
CommonJS is implemented in environments like Node.js and Mozilla Addons.
AMD example:
// md5.js
define([/* dependencies */], function factory(/* dependencies are passed as arguments */) {
var md5 = function () {...}
return md5; // return and object that represents the module
});
// main.js
require('md5', function callback(md5){
alert(md5('this loads md5.js async'))
})
CommonJS example:
// md5.js
var crypto = require('crypto'); // load dependencies
var md5 = function() {...}
// This is not a global scope!
module.exports = md5; // Only what we export gets outside this scope
// main.js
var md5 = require('./md5');
console.log(md5('md5.js was loaded synchronously'))
There is a concept of UMD (Universal Module Definition).
Modules that follow UMD can be loaded either way, in Node.js, or using RequireJS in a browser, or just by a simple <script>
tag.
One important consideration about AMD is to keep global scope clean (don't export anything to global scope). But if it is not and AMD environment, typically you would export one object/function to global scope, with module's name.
from tiny-hashes.
Take a look at my first try here ☺
The resulting min files are about ~130 bytes bigger.
from tiny-hashes.
Ooh yeah that looks pretty nice. I like how small you've managed to make it. Wonder if there's any nice way to make it share code rather than having completely separate source files. Could get a little messy though...
from tiny-hashes.
There is a easy way for AMD and CommonJS, but would require a little more code for Browsers to export hash functions one by one, or could just export one object with hash functions as properties.
// jsCrypt.js file
(function(root, undefined){
// shared and specific stuff here
// maybe utf8encode = function(s){return unescape(encodeURI(s))}
var jc = {
md5: function(){...},
sha1: function(){...},
sha256: function(){...}
}
typeof define != 'function' || !define.amd
? typeof module == (undefined+'') || !module.exports
? (root.jsCrypt = jc) // Export one object or inject contents of jc into root
: (module.exports = jc)
: define(function () { return jc})
;
}
(this));
// main.js file
var jsCrypt = require('jsCrypt');
alert(jsCrypt.sha1('hello'));
In my repo I play with testem to run tests in browsers, with requireJS for AMD. I'm not sure yet what would be the best way to write tests once and run then in any environment.
from tiny-hashes.
Related Issues (2)
- License? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tiny-hashes.