Skip to content

Commit 6606ac4

Browse files
committed
Merge pull request #92 from duckbox/dev
Making the method Utils.toText() accept objects
2 parents 1946a51 + 6e8d1ce commit 6606ac4

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
@@ -61,13 +61,16 @@ describe('UNIT: Directives', function () {
6161
assert.strictEqual(dir.el.textContent, 'true')
6262
})
6363

64+
it('should work with objects', function () {
65+
dir.update({foo:"bar"})
66+
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
67+
})
68+
6469
it('should be empty with other stuff', function () {
6570
dir.update(null)
6671
assert.strictEqual(dir.el.textContent, '')
6772
dir.update(undefined)
6873
assert.strictEqual(dir.el.textContent, '')
69-
dir.update({a:123})
70-
assert.strictEqual(dir.el.textContent, '')
7174
dir.update(function () {})
7275
assert.strictEqual(dir.el.textContent, '')
7376
})
@@ -94,13 +97,16 @@ describe('UNIT: Directives', function () {
9497
assert.strictEqual(dir.el.textContent, 'true')
9598
})
9699

100+
it('should work with objects', function () {
101+
dir.update({foo:"bar"})
102+
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
103+
})
104+
97105
it('should be empty with other stuff', function () {
98106
dir.update(null)
99107
assert.strictEqual(dir.el.innerHTML, '')
100108
dir.update(undefined)
101109
assert.strictEqual(dir.el.innerHTML, '')
102-
dir.update({a:123})
103-
assert.strictEqual(dir.el.innerHTML, '')
104110
dir.update(function () {})
105111
assert.strictEqual(dir.el.innerHTML, '')
106112
})

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)