Skip to content

Commit 6e8d1ce

Browse files
committed
Making the method Utils.toText() accept objects
1 parent 7e0ed75 commit 6e8d1ce

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/utils.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,19 @@ var utils = module.exports = {
7373
},
7474

7575
/**
76-
* Make sure only strings and numbers are output to html
77-
* output empty string is value is not string or number
76+
* Make sure only strings, booleans, numbers and
77+
* objects are output to html. otherwise, ouput empty string.
7878
*/
7979
toText: function (value) {
8080
/* jshint eqeqeq: false */
81-
return (typeof value === 'string' ||
82-
typeof value === 'boolean' ||
83-
(typeof value === 'number' && value == value)) // deal with NaN
84-
? value
85-
: ''
81+
var type = typeof value
82+
return (type === 'string' ||
83+
type === 'boolean' ||
84+
(type === 'number' && value == value)) // deal with NaN
85+
? value
86+
: type === 'object' && value !== null
87+
? JSON.stringify(value)
88+
: ''
8689
},
8790

8891
/**

test/unit/specs/directives.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ describe('UNIT: Directives', function () {
3232
assert.strictEqual(dir.el.textContent, 'true')
3333
})
3434

35+
it('should work with objects', function () {
36+
dir.update({foo:"bar"})
37+
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
38+
})
39+
3540
it('should be empty with other stuff', function () {
3641
dir.update(null)
3742
assert.strictEqual(dir.el.textContent, '')
3843
dir.update(undefined)
3944
assert.strictEqual(dir.el.textContent, '')
40-
dir.update({a:123})
41-
assert.strictEqual(dir.el.textContent, '')
4245
dir.update(function () {})
4346
assert.strictEqual(dir.el.textContent, '')
4447
})
@@ -65,13 +68,16 @@ describe('UNIT: Directives', function () {
6568
assert.strictEqual(dir.el.textContent, 'true')
6669
})
6770

71+
it('should work with objects', function () {
72+
dir.update({foo:"bar"})
73+
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
74+
})
75+
6876
it('should be empty with other stuff', function () {
6977
dir.update(null)
7078
assert.strictEqual(dir.el.innerHTML, '')
7179
dir.update(undefined)
7280
assert.strictEqual(dir.el.innerHTML, '')
73-
dir.update({a:123})
74-
assert.strictEqual(dir.el.innerHTML, '')
7581
dir.update(function () {})
7682
assert.strictEqual(dir.el.innerHTML, '')
7783
})

test/unit/specs/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ describe('UNIT: Utils', function () {
110110
})
111111

112112
it('should output empty string if value is not string or number', function () {
113-
assert.strictEqual(txt({}), '')
114-
assert.strictEqual(txt([]), '')
115113
assert.strictEqual(txt(undefined), '')
116114
assert.strictEqual(txt(null), '')
117115
assert.strictEqual(txt(NaN), '')
118116
})
119117

118+
it('should stringify value if is object', function () {
119+
assert.strictEqual(txt({foo:"bar"}), '{"foo":"bar"}')
120+
})
121+
120122
})
121123

122124
describe('extend', function () {

0 commit comments

Comments
 (0)