Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handling onverriding params in BaseRecord
* `field: null` hasn't been overriden when flat.set('field.0') * add `flat.merge()` * getPropertyByKey got new param: `skipArrayIndexes` * both `BaseRecord.save` and `upate` store flatten params * fix error with saving arrays of refrences when nulls are given
- Loading branch information
1 parent
9af1968
commit 3dd1747
Showing
17 changed files
with
199 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { expect } from 'chai' | ||
import { merge } from './merge' | ||
|
||
describe('merge', () => { | ||
it('removes nulled arrays when nested items were found', () => { | ||
const object1 = { status: 'draft', postImage: null, blogImageSizes: null } | ||
const object2 = { 'blogImageSizes.0': 4130, 'blogImageMimeTypes.0': 'image/jpeg' } | ||
|
||
expect(merge(object1, object2)).to.deep.equal({ | ||
status: 'draft', | ||
postImage: null, | ||
'blogImageSizes.0': 4130, | ||
'blogImageMimeTypes.0': 'image/jpeg', | ||
}) | ||
}) | ||
|
||
context('object with nested fields are given in the first argument', () => { | ||
const object1 = { status: { | ||
type: 'draft', | ||
updated: 'yesterday', | ||
tags: ['super'], | ||
} } | ||
|
||
it('flattens everything and changes just nested property when it was given nested', () => { | ||
const object2 = { 'status.type': 'newDraft' } | ||
|
||
expect(merge(object1, object2)).to.deep.equal({ | ||
'status.type': object2['status.type'], | ||
'status.updated': 'yesterday', | ||
'status.tags.0': 'super', | ||
}) | ||
}) | ||
|
||
it('changes entire record when 2 objects are given', () => { | ||
const object2 = { status: { | ||
type: 'newType', | ||
updated: 'today', | ||
} } | ||
|
||
expect(merge(object1, object2)).to.deep.equal({ | ||
'status.type': object2.status.type, | ||
'status.updated': 'today', | ||
}) | ||
}) | ||
}) | ||
|
||
describe('multiple parameters', () => { | ||
const object1 = { status: { type: 'draft' } } | ||
|
||
it('returns flatten object when one other argument is given', () => { | ||
expect(merge(object1)).to.deep.equal({ | ||
'status.type': 'draft', | ||
}) | ||
}) | ||
|
||
it('merges more then 2 arguments', () => { | ||
const object2 = { | ||
'status.type': 'status2', | ||
'status.age': '1 day', | ||
} | ||
const object3 = { | ||
'status.type': 'status3', | ||
names: [ | ||
'Wojtek', | ||
], | ||
} | ||
expect(merge(object1, object2, object3)).to.deep.equal({ | ||
'status.type': 'status2', | ||
'status.age': '1 day', | ||
'names.0': 'Wojtek', | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { flatten } from 'flat' | ||
import { FlattenParams } from './flat.types' | ||
import { set } from './set' | ||
|
||
/** | ||
* Merges params together and returns flatten result | ||
* | ||
* @param {any} params | ||
* @param {Array<any>} ...mergeParams | ||
* @memberof flat | ||
*/ | ||
export const merge = (params: any = {}, ...mergeParams: Array<any>): FlattenParams => { | ||
const flattenParams = flatten(params) | ||
|
||
// reverse because we merge from right | ||
return mergeParams.reverse().reduce((globalMemo, mergeParam) => ( | ||
Object.keys(mergeParam) | ||
.reduce((memo, key) => ( | ||
set(memo, key, mergeParam[key]) | ||
), globalMemo) | ||
), flattenParams as Record<string, any>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.