-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
diff_object + tests 👉ui/state_management
- Loading branch information
Showing
4 changed files
with
117 additions
and
97 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
75 changes: 75 additions & 0 deletions
75
src/legacy/ui/public/state_management/utils/diff_object.ts
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,75 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { keys, isFunction, difference, filter, union, pick, each, assign, isEqual } from 'lodash'; | ||
|
||
export interface IDiffObject { | ||
removed: string[]; | ||
added: string[]; | ||
changed: string[]; | ||
keys: string[]; | ||
} | ||
|
||
/** | ||
* Filter the private vars | ||
* @param {string} key The keys | ||
* @returns {boolean} | ||
*/ | ||
const filterPrivateAndMethods = function(obj: Record<string, any>) { | ||
return function(key: string) { | ||
if (isFunction(obj[key])) return false; | ||
if (key.charAt(0) === '$') return false; | ||
return key.charAt(0) !== '_'; | ||
}; | ||
}; | ||
|
||
export function applyDiff(target: Record<string, any>, source: Record<string, any>) { | ||
const diff: IDiffObject = { | ||
removed: [], | ||
added: [], | ||
changed: [], | ||
keys: [], | ||
}; | ||
|
||
const targetKeys = keys(target).filter(filterPrivateAndMethods(target)); | ||
const sourceKeys = keys(source).filter(filterPrivateAndMethods(source)); | ||
|
||
// Find the keys to be removed | ||
diff.removed = difference(targetKeys, sourceKeys); | ||
|
||
// Find the keys to be added | ||
diff.added = difference(sourceKeys, targetKeys); | ||
|
||
// Find the keys that will be changed | ||
diff.changed = filter(sourceKeys, key => !isEqual(target[key], source[key])); | ||
|
||
// Make a list of all the keys that are changing | ||
diff.keys = union(diff.changed, diff.removed, diff.added); | ||
|
||
// Remove all the keys | ||
each(diff.removed, key => { | ||
delete target[key]; | ||
}); | ||
|
||
// Assign the changed to the source to the target | ||
assign(target, pick(source, diff.changed)); | ||
// Assign the added to the source to the target | ||
assign(target, pick(source, diff.added)); | ||
|
||
return diff; | ||
} |
This file was deleted.
Oops, something went wrong.