Skip to content

Commit

Permalink
feat(children): add support for single child
Browse files Browse the repository at this point in the history
Closes #16

Before, `div('hello world')` was invalid.

Now, dscript knows how to handle single children.

A potential gotcha can occur:
`div('#Awesome app')` will still try to be handled as a class/id
notation.
  • Loading branch information
dustinspecker committed Apr 17, 2016
1 parent 9d99b1a commit 091933b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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)) {
Expand All @@ -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)
Expand Down
60 changes: 60 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 091933b

Please sign in to comment.