Created
January 16, 2018 23:52
-
-
Save KoryNunn/8d789a94e4c3fe6f21e42b2a07c57ec8 to your computer and use it in GitHub Desktop.
crel with type class and id parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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