Skip to content

support null value include in @Scopes #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor Author

@hiradimir hiradimir Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

above L39 condition of
} else if (typeof sourceValue === 'object') {
is true if sourceValue = null.

image


and null value is occurring exception with below else sesion deepAssign

image


targetValue = null;
} else {
deepAssign(targetValue, sourceValue);
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions test/models/ShoeWithScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const SHOE_SCOPES = {
red: {
where: {primaryColor: 'red'}
},
noImg: {
where: {img: null}
},
manufacturerWithScope: {
include: [() => Manufacturer.scope('brandOnly')]
},
Expand Down
5 changes: 5 additions & 0 deletions test/specs/scopes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () =>
Expand Down
15 changes: 12 additions & 3 deletions test/specs/utils/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);

});


});

});
Expand Down