Skip to content

Instantly share code, notes, and snippets.

@sebs
Forked from felixge/str.js
Created May 30, 2010 13:49
Show Gist options
  • Save sebs/419041 to your computer and use it in GitHub Desktop.
Save sebs/419041 to your computer and use it in GitHub Desktop.
var sys = require('sys')
, str = exports;
// generates those big random strings
str.uuid = function() {
var uuid = '';
for (i = 0; i < 32; i++) {
uuid += Math.floor(Math.random() * 16).toString(16);
}
return uuid;
};
// custom / simplified flavor of sprintf
str.sprintf = function() {
var args = Array.prototype.slice.call(arguments)
, str = args.shift();
return str.replace(/%[so]/g, function(m, i, s) {
var arg = args.shift();
if (m == '%o') {
return sys.inspect(arg);
}
if (!arg) {
return '';
}
return arg.toString();
});
};
// quotes a string to be used as a bash argument
str.bashQuote = function(arg) {
if (arg) {
// cast to string
arg = ''+arg;
}
arg = (arg || '').replace(/[^\\]'/g, function(m, i, s) {
return m.substr(0, 1)+'\\'+m.substr(1, 1);
});
return "'"+arg+"'";
};
str.camelize = function(s) {
return s.replace(/(^|_)([a-z0-9])/g, function(m, underscore, character) {
return character.toUpperCase();
});
};
str.underscore = function(s) {
return s.replace(/(^|[a-z0-9])([A-Z0-9])/g, function(m, before, character) {
return (before)
? before+'_'+character.toLowerCase()
: character.toLowerCase();
});
};
require('../common');
var str = require('transloadit/str')
, sys = require('sys');
function test(test) {
test();
}
test(function bashQuote() {
assert.equal
( str.bashQuote('foo bar')
, "'foo bar'"
);
assert.equal
( str.bashQuote("foo' bar")
, "'foo\\' bar'"
);
assert.equal
( str.bashQuote(undefined)
, "''"
);
assert.equal
( str.bashQuote("foo\\' bar")
, "'foo\\' bar'"
);
assert.equal
( str.bashQuote(5)
, "'5'"
);
});
test(function sprintf() {
var TEST_OBJ = {hey: 'you'};
assert.equal
( str.sprintf('%s loves %s', 'foo', 'bar')
, 'foo loves bar'
);
assert.equal
( str.sprintf('%s', TEST_OBJ)
, TEST_OBJ.toString()
);
assert.equal
( str.sprintf('%o', {hey: 'you'})
, sys.inspect({hey: 'you'})
);
assert.equal
( str.sprintf('%s', 5)
, '5'
);
assert.equal
( str.sprintf('%s', undefined)
, ''
);
assert.equal
( str.sprintf('%s', null)
, ''
);
});
test(function uuid() {
var generated = {}
, uuid;
for (var i = 0; i < 10; i++) {
uuid = str.uuid();
assert.equal(uuid.length, 32);
if (uuid in generated) {
throw new Error('not so random, huh?');
}
}
});
test(function camelize() {
assert.equal(str.camelize('foo'), 'Foo');
assert.equal(str.camelize('foo_bar'), 'FooBar');
assert.equal(str.camelize('foo_bar_la'), 'FooBarLa');
assert.equal(str.camelize('s3_store'), 'S3Store');
assert.equal(str.camelize('go_5ive'), 'Go5ive');
});
test(function underscore() {
assert.equal(str.underscore('Foo'), 'foo');
assert.equal(str.underscore('FooBar'), 'foo_bar');
assert.equal(str.underscore('FooBarLa'), 'foo_bar_la');
assert.equal(str.underscore('S3Store'), 's3_store');
assert.equal(str.underscore('Go5ive'), 'go_5ive');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment