Просмотр исходного кода

Making the method Utils.toText() accept objects

Chris Grant 12 лет назад
Родитель
Сommit
6e8d1ced8f
3 измененных файлов с 24 добавлено и 13 удалено
  1. 10 7
      src/utils.js
  2. 10 4
      test/unit/specs/directives.js
  3. 4 2
      test/unit/specs/utils.js

+ 10 - 7
src/utils.js

@@ -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)
+                    : ''
     },
 
     /**

+ 10 - 4
test/unit/specs/directives.js

@@ -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, '')
         })
@@ -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, '')
         })

+ 4 - 2
test/unit/specs/utils.js

@@ -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 () {