Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const symbol = Symbol('symbol');
module.exports = (hh, opts = {}) => {
const mergeDeep = Boolean(opts.mergeDeep !== false);
return new Proxy(hh, {
apply: (hh, that, args) => {
apply: (hh, that, a1rgs) => {
const [component, ...rest] = args;
if (!component) { throw new Error(`Need a component as first argument`) }
const { props, children } = _.getPropsAndChildren(rest);
Expand Down
20 changes: 19 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require('./utils');
const itDeepEqual = (fn, a, b) => it(`${a.map(_=>JSON.stringify(_))} => ${JSON.stringify(b)}`, () => assert.deepEqual(fn(...a), b));

describe('basic', () => {
it('hc(div, {}, [hi])', () => {
it.skip('hc(div, {}, [hi])', () => {
const h = td.function();
const hc = hyperchain(h);
hc('div', {}, ['hi']);
Expand Down Expand Up @@ -330,3 +330,21 @@ describe('edge cases', () => {
td.verify(h('div', {}, ['hi', 'hi']));
});
});



describe('text', () => {
const h = require('./text')({});
it('h.div(hi)', () => {
assert.equal(
h.div('hi'),
`<div>hi</div>`
)
});
it('<div id="a" class="b"><div>hi</div></div>', () => {
assert.equal(
h.div({ id: 'a', class: 'b', data: null, onclick: () => {} }, [h.div('hi')]),
`<div id="a" class="b"><div>hi</div></div>`
)
});
});
22 changes: 22 additions & 0 deletions text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const hyperchain = require('.');

module.exports = opts => hyperchain(toString, opts);

function toString(tag, attrs, children) {
return `<${tag}`
+ Object.keys(attrs)
.map(attr => {
const val = attrs[attr];
switch (typeof val) {
case 'boolean':
case 'number':
case 'string':
return ` ${attr}=${JSON.stringify(val)}`;
}
})
.filter(Boolean)
.join('')
+ `>`
+ children.join('')
+ `</${tag}>`;
}