baseframe-js
Version:
A suite of useful Javascript plugins and functions to help with Front-end Development on websites
21 lines (20 loc) • 811 B
JavaScript
import $ from 'cash-dom';
const getCssAttr = (styleStr, find, rm) => {
const styleProp = styleStr.indexOf(find) !== -1 ? styleStr.split(find)[1] : null;
if (!styleProp)
return null;
const rmIndex = styleProp.indexOf(rm);
return rmIndex !== -1 ? styleProp.substring(0, rmIndex) : styleProp;
};
const hyperScript = (selector, props = {}) => {
const tagName = selector.split(/(\#|\.)/)[0], className = getCssAttr(selector, '.', '#'), idName = getCssAttr(selector, '#', '.'), baseObj = { class: className, id: idName };
let text = '';
if (props.text) {
text = props.text;
$.extend(props, { text: null });
}
const $elem = $(`<${tagName}>`).attr(Object.assign(baseObj, props));
text && $elem.text(text);
return $elem;
};
export default hyperScript;