diff --git a/src/index.js b/src/index.js index 01d3d69..d37d12f 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,7 @@ const getClassesAndId = selector => { * @return {Function} - explained above */ module.exports = createElement => { + /* eslint-disable complexity */ if (typeof createElement !== 'function') { throw new TypeError('Expected createElement to be a function') } @@ -49,6 +50,10 @@ module.exports = createElement => { // case: div('.hello', ['hi']) childrenToPass = attrsToPass attrsToPass = {} + } else if (typeof attrsToPass !== 'object') { + // case: div('.hello', 7) + childrenToPass = [attrsToPass] + attrsToPass = {} } if (Array.isArray(classesAndId)) { @@ -59,9 +64,12 @@ module.exports = createElement => { attrsToPass = classesAndId } - if (typeof classesAndId === 'string') { + if (typeof classesAndId === 'string' && (classesAndId.indexOf('.') === 0 || classesAndId.indexOf('#') === 0)) { // case: div('.hello') objectAssign(attrsToPass, getClassesAndId(classesAndId)) + } else if (classesAndId !== undefined && typeof classesAndId !== 'object') { + // case: div(2342374) + childrenToPass = [classesAndId] } return createElement(tagOrComponent, attrsToPass, ...childrenToPass) diff --git a/tests/test.js b/tests/test.js index 1cb7682..e3f469d 100644 --- a/tests/test.js +++ b/tests/test.js @@ -118,6 +118,66 @@ test('dscript fns pass empty children array by default', t => { t.truthy(spanCalled) }) +test('it passes single child when class and attrs are provided', t => { + let spanCalled = false + + const {span} = dscript((tagName, attrs, ...children) => { + if (tagName === 'span') { + t.deepEqual(children, [789]) + spanCalled = true + } + }) + + span('.hello', {}, 789) + + t.truthy(spanCalled) +}) + +test('it passes single child when class is provied', t => { + let spanCalled = false + + const {span} = dscript((tagName, attrs, ...children) => { + if (tagName === 'span') { + t.deepEqual(children, [789]) + spanCalled = true + } + }) + + span('.hello', 789) + + t.truthy(spanCalled) +}) + +test('it passes single child when only non-string child is passed', t => { + let spanCalled = false + + const {span} = dscript((tagName, attrs, ...children) => { + if (tagName === 'span') { + t.deepEqual(children, [789]) + spanCalled = true + } + }) + + span(789) + + t.truthy(spanCalled) +}) + +test('it passes single child when only string child is passed', t => { + let spanCalled = false + + const {span} = dscript((tagName, attrs, ...children) => { + if (tagName === 'span') { + t.deepEqual(children, ['hello']) + spanCalled = true + } + }) + + span('hello') + + t.truthy(spanCalled) +}) + test('dscript fn can handle no attrs, but selector and chilren', t => { let spanCalled = false