Skip to content

Instantly share code, notes, and snippets.

@octavian-nita
Created November 17, 2014 11:33
Show Gist options
  • Select an option

  • Save octavian-nita/2e9f82401e94c85e791c to your computer and use it in GitHub Desktop.

Select an option

Save octavian-nita/2e9f82401e94c85e791c to your computer and use it in GitHub Desktop.
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?)...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment