From 887b736f39979c260495349629696243f349b92c Mon Sep 17 00:00:00 2001 From: harttle Date: Tue, 14 Aug 2018 00:01:17 +0800 Subject: [PATCH] test cases for stringify --- src/util/underscore.js | 14 ++++++++------ test/util/underscore.js | 6 ++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/util/underscore.js b/src/util/underscore.js index f77828b08..37f233ef0 100644 --- a/src/util/underscore.js +++ b/src/util/underscore.js @@ -11,12 +11,14 @@ function isString (value) { } function stringify (value) { - if (!isNil(value)) { - if (typeof value.to_liquid === 'function') { - return stringify(value.to_liquid()) - } else if (typeof value.toLiquid === 'function') { - return stringify(value.toLiquid()) - } + if (isNil(value)) { + return String(value) + } + if (typeof value.to_liquid === 'function') { + return stringify(value.to_liquid()) + } + if (typeof value.toLiquid === 'function') { + return stringify(value.toLiquid()) } if (isString(value)) { return value diff --git a/test/util/underscore.js b/test/util/underscore.js index 8cfa8b897..ca365df2b 100644 --- a/test/util/underscore.js +++ b/test/util/underscore.js @@ -44,6 +44,12 @@ describe('util/underscore', function () { it('should recursively call toLiquid()', function () { expect(_.stringify({toLiquid: () => ({toLiquid: () => 'foo'})})).to.equal('foo') }) + it('should return "null" for null', function () { + expect(_.stringify(null)).to.equal('null') + }) + it('should return "undefined" for undefined', function () { + expect(_.stringify(undefined)).to.equal('undefined') + }) }) describe('.forOwn()', function () { it('should iterate all properties', function () {