Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the method Utils.toText() accept objects #92

Merged
merged 1 commit into from
Feb 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ var utils = module.exports = {
},

/**
* Make sure only strings and numbers are output to html
* output empty string is value is not string or number
* Make sure only strings, booleans, numbers and
* objects are output to html. otherwise, ouput empty string.
*/
toText: function (value) {
/* jshint eqeqeq: false */
return (typeof value === 'string' ||
typeof value === 'boolean' ||
(typeof value === 'number' && value == value)) // deal with NaN
? value
: ''
var type = typeof value
return (type === 'string' ||
type === 'boolean' ||
(type === 'number' && value == value)) // deal with NaN
? value
: type === 'object' && value !== null
? JSON.stringify(value)
: ''
},

/**
Expand Down
14 changes: 10 additions & 4 deletions test/unit/specs/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ describe('UNIT: Directives', function () {
assert.strictEqual(dir.el.textContent, 'true')
})

it('should work with objects', function () {
dir.update({foo:"bar"})
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
})

it('should be empty with other stuff', function () {
dir.update(null)
assert.strictEqual(dir.el.textContent, '')
dir.update(undefined)
assert.strictEqual(dir.el.textContent, '')
dir.update({a:123})
assert.strictEqual(dir.el.textContent, '')
dir.update(function () {})
assert.strictEqual(dir.el.textContent, '')
})
Expand All @@ -65,13 +68,16 @@ describe('UNIT: Directives', function () {
assert.strictEqual(dir.el.textContent, 'true')
})

it('should work with objects', function () {
dir.update({foo:"bar"})
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
})

it('should be empty with other stuff', function () {
dir.update(null)
assert.strictEqual(dir.el.innerHTML, '')
dir.update(undefined)
assert.strictEqual(dir.el.innerHTML, '')
dir.update({a:123})
assert.strictEqual(dir.el.innerHTML, '')
dir.update(function () {})
assert.strictEqual(dir.el.innerHTML, '')
})
Expand Down
6 changes: 4 additions & 2 deletions test/unit/specs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ describe('UNIT: Utils', function () {
})

it('should output empty string if value is not string or number', function () {
assert.strictEqual(txt({}), '')
assert.strictEqual(txt([]), '')
assert.strictEqual(txt(undefined), '')
assert.strictEqual(txt(null), '')
assert.strictEqual(txt(NaN), '')
})

it('should stringify value if is object', function () {
assert.strictEqual(txt({foo:"bar"}), '{"foo":"bar"}')
})

})

describe('extend', function () {
Expand Down