Skip to content

Commit

Permalink
Rewrite module rewriting, allow custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge committed Sep 2, 2018
1 parent afc9651 commit a8d5b42
Show file tree
Hide file tree
Showing 37 changed files with 549 additions and 156 deletions.
123 changes: 66 additions & 57 deletions dist/index.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ var mapObject = function (obj, fn) { return Object.keys(obj).map(
{}
); };

var deepifyKeys = function (obj) { return mapObject(obj,
var deepifyKeys = function (obj, modules) { return mapObject(obj,
function (key, val) {
var dashIndex = key.indexOf('-');
if (dashIndex > -1) {
if (dashIndex > -1 && modules[key.slice(0, dashIndex)] !== undefined) {
var moduleData = {};
moduleData[key.slice(dashIndex + 1)] = val;
return ( obj = {}, obj[key.slice(0, dashIndex)] = moduleData, obj )
Expand All @@ -70,14 +70,7 @@ var deepifyKeys = function (obj) { return mapObject(obj,
}
); };

var flatifyKeys = function (obj) { return mapObject(obj,
function (mod, data) { return !object(data) ? (( obj = {}, obj[mod] = data, obj )) : mapObject(
flatifyKeys(data),
function (key, val) { return (( obj = {}, obj[(mod + "-" + key)] = val, obj ))
var obj; }
)
var obj; }
); };


var omit = function (key, obj) { return mapObject(obj,
function (mod, data) { return mod !== key ? (( obj = {}, obj[mod] = data, obj )) : {}
Expand Down Expand Up @@ -107,42 +100,43 @@ var considerSvg = function (vnode$$1) { return !svg(vnode$$1) ? vnode$$1 :
}
); };

var considerData = function (data) {
return !data.data ? data : mapObject(data, function (mod, data) {
var key = mod === 'data' ? 'dataset' : mod;
return (( obj = {}, obj[key] = data, obj ))
var obj;
})
var rewrites = {
for: 'attrs',
role: 'attrs',
tabindex: 'attrs',
'aria-*': 'attrs',
key: null
};

var considerAria = function (data) { return data.attrs || data.aria ? omit('aria',
assign(data, {
attrs: extend(data.attrs, data.aria ? flatifyKeys({ aria: data.aria }) : {})
})
) : data; };

var considerProps = function (data) { return mapObject(data,
function (key, val) { return object(val) ? ( obj = {}, obj[key] = val, obj ) :
{ props: ( obj$1 = {}, obj$1[key] = val, obj$1 ) }
var rewriteModules = function (data, modules) { return mapObject(data, function (key, val) {
var inner = {};
inner[key] = val;
if (rewrites[key] && modules[rewrites[key]] !== undefined) {
return ( obj = {}, obj[rewrites[key]] = inner, obj )
var obj;
var obj$1; }
); };

var rewritesMap = { for: 1, role: 1, tabindex: 1 };

var considerAttrs = function (data) { return mapObject(data,
function (key, data) { return !(key in rewritesMap) ? ( obj = {}, obj[key] = data, obj ) : {
attrs: extend(data.attrs, ( obj$1 = {}, obj$1[key] = data, obj$1 ))
}
if (rewrites[key] === null) {
return {}
}
var keys = Object.keys(rewrites);
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
if (k.charAt(k.length - 1) === '*' && key.indexOf(k.slice(0, -1)) === 0 && modules[rewrites[k]] !== undefined) {
return ( obj$1 = {}, obj$1[rewrites[k]] = inner, obj$1 )
var obj$1;
}
var obj;
var obj$1; }
); };

var considerKey = function (data) {
return 'key' in data ? omit('key', data) : data
};
}
if (modules[key] !== undefined) {
return ( obj$2 = {}, obj$2[modules[key] ? modules[key] : key] = val, obj$2 )
var obj$2;
}
if (modules.props !== undefined) {
return { props: inner }
}
return inner
}); };

var sanitizeData = function (data) { return considerProps(considerAria(considerData(considerAttrs(considerKey(deepifyKeys(data)))))); };
var sanitizeData = function (data, modules) { return considerSvg(rewriteModules(deepifyKeys(data, modules), modules)); };

var sanitizeText = function (children) { return children.length > 1 || !text(children[0]) ? undefined : children[0]; };

Expand All @@ -153,26 +147,41 @@ var sanitizeChildren = function (children) { return reduceDeep(children, functio
}
, []); };

var createElement = function (sel, data) {
var children = [], len = arguments.length - 2;
while ( len-- > 0 ) children[ len ] = arguments[ len + 2 ];
var defaultModules = {
attrs: '',
props: '',
class: '',
data: 'dataset',
style: '',
hook: '',
on: ''
};

var createElementWithModules = function (modules) {
return function (sel, data) {
var children = [], len = arguments.length - 2;
while ( len-- > 0 ) children[ len ] = arguments[ len + 2 ];

if (fun(sel)) {
return sel(data || {}, children)
if (fun(sel)) {
return sel(data || {}, children)
}
var text$$1 = sanitizeText(children, modules);
return considerSvg({
sel: sel,
data: data ? sanitizeData(data, modules) : {},
children: text$$1 ? undefined : sanitizeChildren(children),
text: text$$1,
elm: undefined,
key: data ? data.key : undefined
})
}
var text$$1 = sanitizeText(children);
return considerSvg({
sel: sel,
data: data ? sanitizeData(data) : {},
children: text$$1 ? undefined : sanitizeChildren(children),
text: text$$1,
elm: undefined,
key: data ? data.key : undefined
})
};

var createElement = createElementWithModules(defaultModules);

var index = {
createElement: createElement
createElement: createElement,
createElementWithModules: createElementWithModules
};

export { createElement };export default index;
export { createElementWithModules, createElement };export default index;
122 changes: 66 additions & 56 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ var mapObject = function (obj, fn) { return Object.keys(obj).map(
{}
); };

var deepifyKeys = function (obj) { return mapObject(obj,
var deepifyKeys = function (obj, modules) { return mapObject(obj,
function (key, val) {
var dashIndex = key.indexOf('-');
if (dashIndex > -1) {
if (dashIndex > -1 && modules[key.slice(0, dashIndex)] !== undefined) {
var moduleData = {};
moduleData[key.slice(dashIndex + 1)] = val;
return ( obj = {}, obj[key.slice(0, dashIndex)] = moduleData, obj )
Expand All @@ -76,14 +76,7 @@ var deepifyKeys = function (obj) { return mapObject(obj,
}
); };

var flatifyKeys = function (obj) { return mapObject(obj,
function (mod, data) { return !object(data) ? (( obj = {}, obj[mod] = data, obj )) : mapObject(
flatifyKeys(data),
function (key, val) { return (( obj = {}, obj[(mod + "-" + key)] = val, obj ))
var obj; }
)
var obj; }
); };


var omit = function (key, obj) { return mapObject(obj,
function (mod, data) { return mod !== key ? (( obj = {}, obj[mod] = data, obj )) : {}
Expand Down Expand Up @@ -113,42 +106,43 @@ var considerSvg = function (vnode$$1) { return !svg(vnode$$1) ? vnode$$1 :
}
); };

var considerData = function (data) {
return !data.data ? data : mapObject(data, function (mod, data) {
var key = mod === 'data' ? 'dataset' : mod;
return (( obj = {}, obj[key] = data, obj ))
var obj;
})
var rewrites = {
for: 'attrs',
role: 'attrs',
tabindex: 'attrs',
'aria-*': 'attrs',
key: null
};

var considerAria = function (data) { return data.attrs || data.aria ? omit('aria',
assign(data, {
attrs: extend(data.attrs, data.aria ? flatifyKeys({ aria: data.aria }) : {})
})
) : data; };

var considerProps = function (data) { return mapObject(data,
function (key, val) { return object(val) ? ( obj = {}, obj[key] = val, obj ) :
{ props: ( obj$1 = {}, obj$1[key] = val, obj$1 ) }
var rewriteModules = function (data, modules) { return mapObject(data, function (key, val) {
var inner = {};
inner[key] = val;
if (rewrites[key] && modules[rewrites[key]] !== undefined) {
return ( obj = {}, obj[rewrites[key]] = inner, obj )
var obj;
var obj$1; }
); };

var rewritesMap = { for: 1, role: 1, tabindex: 1 };

var considerAttrs = function (data) { return mapObject(data,
function (key, data) { return !(key in rewritesMap) ? ( obj = {}, obj[key] = data, obj ) : {
attrs: extend(data.attrs, ( obj$1 = {}, obj$1[key] = data, obj$1 ))
}
if (rewrites[key] === null) {
return {}
}
var keys = Object.keys(rewrites);
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
if (k.charAt(k.length - 1) === '*' && key.indexOf(k.slice(0, -1)) === 0 && modules[rewrites[k]] !== undefined) {
return ( obj$1 = {}, obj$1[rewrites[k]] = inner, obj$1 )
var obj$1;
}
var obj;
var obj$1; }
); };

var considerKey = function (data) {
return 'key' in data ? omit('key', data) : data
};
}
if (modules[key] !== undefined) {
return ( obj$2 = {}, obj$2[modules[key] ? modules[key] : key] = val, obj$2 )
var obj$2;
}
if (modules.props !== undefined) {
return { props: inner }
}
return inner
}); };

var sanitizeData = function (data) { return considerProps(considerAria(considerData(considerAttrs(considerKey(deepifyKeys(data)))))); };
var sanitizeData = function (data, modules) { return considerSvg(rewriteModules(deepifyKeys(data, modules), modules)); };

var sanitizeText = function (children) { return children.length > 1 || !text(children[0]) ? undefined : children[0]; };

Expand All @@ -159,27 +153,43 @@ var sanitizeChildren = function (children) { return reduceDeep(children, functio
}
, []); };

var createElement = function (sel, data) {
var children = [], len = arguments.length - 2;
while ( len-- > 0 ) children[ len ] = arguments[ len + 2 ];
var defaultModules = {
attrs: '',
props: '',
class: '',
data: 'dataset',
style: '',
hook: '',
on: ''
};

var createElementWithModules = function (modules) {
return function (sel, data) {
var children = [], len = arguments.length - 2;
while ( len-- > 0 ) children[ len ] = arguments[ len + 2 ];

if (fun(sel)) {
return sel(data || {}, children)
if (fun(sel)) {
return sel(data || {}, children)
}
var text$$1 = sanitizeText(children, modules);
return considerSvg({
sel: sel,
data: data ? sanitizeData(data, modules) : {},
children: text$$1 ? undefined : sanitizeChildren(children),
text: text$$1,
elm: undefined,
key: data ? data.key : undefined
})
}
var text$$1 = sanitizeText(children);
return considerSvg({
sel: sel,
data: data ? sanitizeData(data) : {},
children: text$$1 ? undefined : sanitizeChildren(children),
text: text$$1,
elm: undefined,
key: data ? data.key : undefined
})
};

var createElement = createElementWithModules(defaultModules);

var index = {
createElement: createElement
createElement: createElement,
createElementWithModules: createElementWithModules
};

exports.createElementWithModules = createElementWithModules;
exports.createElement = createElement;
exports['default'] = index;
4 changes: 2 additions & 2 deletions src/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export const mapObject = (obj, fn) => Object.keys(obj).map(
{}
)

export const deepifyKeys = (obj) => mapObject(obj,
export const deepifyKeys = (obj, modules) => mapObject(obj,
(key, val) => {
const dashIndex = key.indexOf('-')
if (dashIndex > -1) {
if (dashIndex > -1 && modules[key.slice(0, dashIndex)] !== undefined) {
const moduleData = {
[key.slice(dashIndex + 1)]: val
}
Expand Down
Loading

0 comments on commit a8d5b42

Please sign in to comment.