Skip to content

Commit b1cfa77

Browse files
Merge pull request #100 from hiradimir/master
support null value include in @scopes
2 parents 61604c8 + f141e73 commit b1cfa77

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

lib/utils/object.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ export function deepAssign(target: any, ...sources: any[]): any {
4646
} else if (sourceValue instanceof Date) {
4747

4848
targetValue = new Date(sourceValue);
49-
} else {
49+
} else if (sourceValue === null) {
5050

51+
targetValue = null;
52+
} else {
5153
deepAssign(targetValue, sourceValue);
5254
}
5355
} else {

test/models/ShoeWithScopes.ts

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export const SHOE_SCOPES = {
1616
red: {
1717
where: {primaryColor: 'red'}
1818
},
19+
noImg: {
20+
where: {img: null}
21+
},
1922
manufacturerWithScope: {
2023
include: [() => Manufacturer.scope('brandOnly')]
2124
},

test/specs/scopes.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe('scopes', () => {
7171

7272
expect(yellowShoes).to.be.empty;
7373
})
74+
.then(() => ShoeWithScopes.scope('noImg').findAll())
75+
.then(noImgShoes => {
76+
77+
expect(noImgShoes).to.be.not.empty;
78+
})
7479
);
7580

7681
it('should not consider default scope due to unscoped call', () =>

test/specs/utils/object.spec.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe('utils', () => {
1212
const childSourceF = {};
1313
const childSourceA = {f: childSourceF};
1414
const childSourceB = {};
15-
const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim};
16-
const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden'};
15+
const source1 = {a: childSourceA, b: childSourceB, c: 1, d: 'd', over: 'ride', regex: /reg/gim, notNull: null};
16+
const source2 = {e: 'für elisa', g: () => null, arr: [{h: 1}, {}, 'e'], over: 'ridden', nullable: null, notNull: 'notNull'};
1717
const sourceKeys = [].concat(Object.keys(source1), Object.keys(source2));
1818

1919
it('should not be undefined', () => {
@@ -61,7 +61,7 @@ describe('utils', () => {
6161
sourceKeys
6262
.forEach(key => {
6363

64-
if (typeof copy[key] === 'object') {
64+
if (typeof copy[key] === 'object' && copy[key] !== null) {
6565

6666
expect(copy[key]).not.to.equal(source1[key] || source2[key]);
6767
expect(copy[key]).to.eql(source1[key] || source2[key]);
@@ -100,6 +100,15 @@ describe('utils', () => {
100100
});
101101
});
102102

103+
it('should have copy of nullable', () => {
104+
const copy = deepAssign({}, source1, source2);
105+
106+
expect(copy.nullable).to.equals(null);
107+
expect(copy.notNull).to.not.equals(null);
108+
109+
});
110+
111+
103112
});
104113

105114
});

0 commit comments

Comments
 (0)