Node.js / JavaScript useful code snippets
'use strict';
var log = require('./log');
exports.slug = function(name) {
return name || name.
replace(/^[-\s_]*[\[\(]*[\s\d]+[\]\)]*[-\s_]*/g, ''). // remove eventual leading index / number in name
replace(/^[-\s_]+|[-\s_]+$/g, ''). // remove leading and trailing spaces, dashes, underscores
replace(/[-\s_]+/g, config.slugSeparator); // join by defined slug separator
};
exports.json = function(object) {
if (object && object instanceof Error) {
object = {'name': object.name, 'message': object.message};
}
return JSON.stringify(object, null, config.debug.on ? config.debug.indent : 0);
};
process.on('uncaughtException', function(err) {
log.error(err); // at least log something, not really able to send an answer (which request?)...
});