diff --git a/index.js b/index.js index c669086..47f9dd6 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/test.js b/test.js index 2e0bc65..2af78dc 100644 --- a/test.js +++ b/test.js @@ -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']); @@ -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'), + `
hi
` + ) + }); + it('
hi
', () => { + assert.equal( + h.div({ id: 'a', class: 'b', data: null, onclick: () => {} }, [h.div('hi')]), + `
hi
` + ) + }); +}); diff --git a/text.js b/text.js new file mode 100644 index 0000000..b05b81c --- /dev/null +++ b/text.js @@ -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('') + + ``; +}