From 37b31a4752e6d3ed37badba8b19cc69f26d5cafd Mon Sep 17 00:00:00 2001 From: Danny Banks Date: Fri, 6 Sep 2019 10:48:32 -0700 Subject: [PATCH] fix(utils): handle 0 values --- __tests__/utils/resolveObject.test.js | 10 ++++++++++ lib/utils/resolveObject.js | 16 ++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/__tests__/utils/resolveObject.test.js b/__tests__/utils/resolveObject.test.js index 8e894bda9..701c40767 100644 --- a/__tests__/utils/resolveObject.test.js +++ b/__tests__/utils/resolveObject.test.js @@ -294,5 +294,15 @@ describe('utils', () => { ])); }); + it('should handle 0', () => { + GroupMessages.clear(PROPERTY_REFERENCE_WARNINGS); + var test = resolveObject({ + "test": { "value": "{zero.value}" }, + "zero": { "value": 0} + }); + expect(GroupMessages.fetchMessages(PROPERTY_REFERENCE_WARNINGS).length).toBe(0); + expect(test.test.value).toBe(0); + }); + }); }); diff --git a/lib/utils/resolveObject.js b/lib/utils/resolveObject.js index a653f874a..6658ee682 100644 --- a/lib/utils/resolveObject.js +++ b/lib/utils/resolveObject.js @@ -88,7 +88,7 @@ function compile_value(value, stack) { // Find what the value is referencing ref = selfRef(variable.trim(), updated_object); - if (ref) { + if (typeof ref !== 'undefined') { if (typeof ref === 'string') { to_ret = value.replace(match, ref); @@ -150,24 +150,24 @@ function selfRef(str, obj) { context = current_context.join(options.separator); for (i = 0; i < array.length; i++) { - if (ref[array[i]]) { + // Check for undefined as 0 is a valid, truthy value + if (typeof ref[array[i]] !== 'undefined') { ref = ref[array[i]]; } else { - ref = obj; + // set the reference as undefined if we don't find anything + ref = undefined; break; } } - // If the reference doesn't change then it means - // we didn't find it in the object - if (ref === obj) { + if (typeof ref === 'undefined') { GroupMessages.add( PROPERTY_REFERENCE_WARNINGS, "Reference doesn't exist: " + context + " tries to reference " + str + ", which is not defined" ); + } else { + return ref; } - - return ref; }