Skip to content

Instantly share code, notes, and snippets.

@KoryNunn
Created January 16, 2018 23:52
Show Gist options
  • Save KoryNunn/8d789a94e4c3fe6f21e42b2a07c57ec8 to your computer and use it in GitHub Desktop.
Save KoryNunn/8d789a94e4c3fe6f21e42b2a07c57ec8 to your computer and use it in GitHub Desktop.
crel with type class and id parsing
var crel = require('crel');
function fancyCrel(){
var args = Array.prototype.slice.call(arguments);
console.log(args);
var type = args.shift();
var settings = {};
if(args[0] && typeof args[0] === 'object'){
settings = args.shift();
}
if(typeof type === 'string'){
var typeParts = type.split(/(?=\.|#)/g);
var type = typeParts.shift();
typeParts.forEach(function(part){
var partType = part.charAt(0);
var partValue = part.slice(1);
if(partType === '#'){
settings.id = partValue
}
if(partType === '.'){
settings.class = ((settings.class || '') + ' ' + partValue).trim();
}
});
}
return crel.apply(null, [type, settings].concat(args));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment