-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from jwsloan/merge-by-name
feat: Add mergeByName
- Loading branch information
Showing
6 changed files
with
123 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// https://github.com/KyleAMathews/deepmerge#arraymerge | ||
|
||
const merge = require('deepmerge') | ||
|
||
function findMatchingIndex (sourceItem, target) { | ||
if (sourceItem.hasOwnProperty('name')) { | ||
return target | ||
.filter(targetItem => targetItem.hasOwnProperty('name')) | ||
.findIndex(targetItem => sourceItem.name === targetItem.name) | ||
} | ||
} | ||
|
||
function mergeByName (target, source, options) { | ||
const destination = target.slice() | ||
|
||
source.forEach(sourceItem => { | ||
const matchingIndex = findMatchingIndex(sourceItem, target) | ||
if (matchingIndex > -1) { | ||
destination[matchingIndex] = merge(target[matchingIndex], sourceItem, options) | ||
} else { | ||
destination.push(sourceItem) | ||
} | ||
}) | ||
|
||
return destination | ||
} | ||
|
||
module.exports = mergeByName |
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,76 @@ | ||
const branchArrayMerge = require('../../lib/mergeArrayByName') | ||
const YAML = require('js-yaml') | ||
|
||
describe('mergeArrayByName', () => { | ||
it('works', () => { | ||
const target = [{ | ||
name: 'master', | ||
shouldChange: 'did not change', | ||
shouldKeep: 'kept' | ||
}, { | ||
name: 'develop' | ||
}] | ||
|
||
const source = [{ | ||
name: 'master', | ||
shouldChange: 'did change' | ||
}, { | ||
name: 'added' | ||
}] | ||
|
||
const merged = branchArrayMerge(target, source) | ||
|
||
expect(merged).toEqual([{ | ||
name: 'master', | ||
shouldChange: 'did change', | ||
shouldKeep: 'kept' | ||
}, { | ||
name: 'develop' | ||
}, { | ||
name: 'added' | ||
}]) | ||
}) | ||
|
||
it('works in a realistic scenario', () => { | ||
const target = YAML.safeLoad(` | ||
branches: | ||
- name: master | ||
protection: | ||
required_pull_request_reviews: | ||
required_approving_review_count: 1 | ||
dismiss_stale_reviews: false | ||
require_code_owner_reviews: true | ||
dismissal_restrictions: {} | ||
required_status_checks: | ||
strict: true | ||
contexts: [] | ||
enforce_admins: false | ||
restrictions: | ||
`) | ||
|
||
const source = YAML.safeLoad(` | ||
branches: | ||
- name: master | ||
protection: | ||
required_pull_request_reviews: | ||
required_approving_review_count: 2 | ||
`) | ||
|
||
const expected = [{ | ||
name: 'master', | ||
protection: | ||
{ required_pull_request_reviews: | ||
{ required_approving_review_count: 2, | ||
dismiss_stale_reviews: false, | ||
require_code_owner_reviews: true, | ||
dismissal_restrictions: {} }, | ||
required_status_checks: { strict: true, contexts: [] }, | ||
enforce_admins: false, | ||
restrictions: null } | ||
}] | ||
|
||
const merged = branchArrayMerge(target.branches, source.branches) | ||
|
||
expect(merged).toEqual(expected) | ||
}) | ||
}) |