All files / boundless/packages/boundless-utils-object-intersection index.js

100% Statements 4/4
100% Branches 2/2
100% Functions 2/2
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                              2x 2x 1x     2x        
/**
 * Returns a new object that is an intersection of the keys between the first and second object arguments.
 *
 * ## Example Usage
 *
 * ```js
 * import intersect from 'boundless-utils-object-intersection';
 *
 * const obj1 = {foo: 'bar', bar: 'baz', baz: 'fizz'};
 * const obj2 = {bar: 'x'};
 *
 * intersect(obj1, obj2); // returns `{bar: 'baz'}`
 * ```
 */
export default function getIntersection(obj1, obj2) {
    return Object.keys(obj2).reduce((childProps, key) => {
        if (key in obj1) {
            childProps[key] = obj1[key];
        }
 
        return childProps;
 
    }, {});
}