From f141e7318be32f08ba96265daf7d2e13eae896bc Mon Sep 17 00:00:00 2001 From: hiradimir Date: Wed, 16 Aug 2017 23:25:32 +0900 Subject: [PATCH] allow deepAssign null value --- lib/utils/object.ts | 4 +++- test/models/ShoeWithScopes.ts | 3 +++ test/specs/scopes.spec.ts | 5 +++++ test/specs/utils/object.spec.ts | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/utils/object.ts b/lib/utils/object.ts index 941c8860..54fb9d18 100644 --- a/lib/utils/object.ts +++ b/lib/utils/object.ts @@ -46,8 +46,10 @@ export function deepAssign(target: any, ...sources: any[]): any { } else if (sourceValue instanceof Date) { targetValue = new Date(sourceValue); - } else { + } else if (sourceValue === null) { + targetValue = null; + } else { deepAssign(targetValue, sourceValue); } } else { diff --git a/test/models/ShoeWithScopes.ts b/test/models/ShoeWithScopes.ts index e10e62a3..bb662900 100644 --- a/test/models/ShoeWithScopes.ts +++ b/test/models/ShoeWithScopes.ts @@ -16,6 +16,9 @@ export const SHOE_SCOPES = { red: { where: {primaryColor: 'red'} }, + noImg: { + where: {img: null} + }, manufacturerWithScope: { include: [() => Manufacturer.scope('brandOnly')] }, diff --git a/test/specs/scopes.spec.ts b/test/specs/scopes.spec.ts index 8254d547..36a2725e 100644 --- a/test/specs/scopes.spec.ts +++ b/test/specs/scopes.spec.ts @@ -71,6 +71,11 @@ describe('scopes', () => { expect(yellowShoes).to.be.empty; }) + .then(() => ShoeWithScopes.scope('noImg').findAll()) + .then(noImgShoes => { + + expect(noImgShoes).to.be.not.empty; + }) ); it('should not consider default scope due to unscoped call', () => diff --git a/test/specs/utils/object.spec.ts b/test/specs/utils/object.spec.ts index e13bb978..1363d0b7 100644 --- a/test/specs/utils/object.spec.ts +++ b/test/specs/utils/object.spec.ts @@ -12,8 +12,8 @@ describe('utils', () => { const childSourceF = {}; const childSourceA = {f: childSourceF}; const childSourceB = {}; - const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim}; - const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden'}; + const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim, notNull: null}; + const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden', nullable: null, notNull: 'notNull'}; const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2)); it('should not be undefined', () => { @@ -61,7 +61,7 @@ describe('utils', () => { sourceKeys .forEach(key => { - if (typeof copy[key] === 'object') { + if (typeof copy[key] === 'object' && copy[key] !== null) { expect(copy[key]).not.to.equal(source1[key] || source2[key]); expect(copy[key]).to.eql(source1[key] || source2[key]); @@ -100,6 +100,15 @@ describe('utils', () => { }); }); + it('should have copy of nullable', () => { + const copy = deepAssign({}, source1, source2); + + expect(copy.nullable).to.equals(null); + expect(copy.notNull).to.not.equals(null); + + }); + + }); });