-
Notifications
You must be signed in to change notification settings - Fork 6
NodeJS with CaffeineScript
Shane Brinkman-Davis Delamore edited this page May 8, 2017
·
2 revisions
As part of my combined ArtSuite and Caffeine ecosystems, I'm maintaining an ever-growing list of NPMs. As-of May 2017, there are 43! The only way one person can stay on top of this much code is by doing more with less. CaffeineScript is key to my ongoing strategy of extending my reach as a programmer.
If you are interested in streamlining your npm-code, see also: neptune-namespaces
Take this simple, well-written NPM library:
Compare in CaffeineScript vs the original source:
import &crypto
cryptoIntRand = (max) ->
[low, high] = randomBytes 2
256 * high + low
% max
digits = :0123456789
base62 = :0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz
base30 = :0123456789ABCDFHKLMNPQRSTUVWXYZ # 1-9, A-Z exclude(E, G, I, J, O) for human read
generate: generate = (length = 16, chars = base62) ->
key = ''
charsLen = chars.length
while key.length < length
key += chars[cryptoIntRand charsLen]
generateDigits: (length = 16) -> generate length, digits
generateBase30: (length = 16) -> generate length, base30
versus
var crypto = require("crypto");
var rn = function(max) {
var rnBytes = crypto.randomBytes(2);
var randomNum = rnBytes.readUInt8(0) * 256 + rnBytes.readUInt8(1);
return randomNum % max;
}
var digits = '0123456789'
var base62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'
var base30 = '0123456789ABCDFHKLMNPQRSTUVWXYZ' // 1-9, A-Z exclude(E, G, I, J, O) for human read
exports.generate = function(len, chars) {
len = len || 16;
chars = chars || base62;
var key = '';
var charsLen = chars.length;
for (var i=0; i<len; i++) {
key += chars[rn(charsLen)];
}
return key;
};
exports.generateDigits = function(len) {
len = len || 16;
return exports.generate(len, digits)
};
exports.generateBase30 = function(len) {
len = len || 16;
return exports.generate(len, base30)
}
- Home
- Get Started
- Benefits
- Highlights
- Productivity by Design
- CaffeineScript Design
- What is CaffeineScript Good For?
- Get the most out of JavaScript
- Language Comparison
- CHANGELOG
- Blocks Instead of Brackets
- Binary Line-Starts
- Everything Returns a Value
- Streamlined Modules
- Scopes and Variables
- Optional Commas
- Semantics
- Ambiguities