Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
refactor(lodash): merge
Browse files Browse the repository at this point in the history
disable some tests we don't want to comply to
  • Loading branch information
Haroenv committed May 10, 2019
1 parent bb3d82e commit f4f73b5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var isEqual = require('lodash/isEqual');
var isUndefined = require('lodash/isUndefined');
var isFunction = require('lodash/isFunction');

var merge = require('lodash/merge');

var merge = require('../functions/merge');
var defaultsPure = require('../functions/defaultsPure');
var find = require('../functions/find');
var valToNumber = require('../functions/valToNumber');
Expand Down
3 changes: 1 addition & 2 deletions src/SearchResults/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';

var merge = require('lodash/merge');

var isFunction = require('lodash/isFunction');

var merge = require('../functions/merge');
var defaultsPure = require('../functions/defaultsPure');
var orderBy = require('../functions/orderBy');
var compact = require('../functions/compact');
Expand Down
45 changes: 45 additions & 0 deletions src/functions/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';


function clone(value) {
if (typeof value === 'object' && value !== null) {
return merge(Array.isArray(value) ? [] : {}, value);
}
return value;
}

// NOTE: this behaves like lodash/merge, but does mutate functions if they are a source
function merge() {
var sources = Array.prototype.slice.call(arguments);
var target = sources.shift();

return sources.reduce(function(acc, source) {
if (acc === source) {
return acc;
}

if (source === undefined) {
return acc;
}

if (acc === undefined) {
return clone(source);
}

if (typeof acc !== 'object' && typeof acc !== 'function') {
return clone(source);
}

if (typeof source !== 'object' && typeof source !== 'function') {
return acc;
}

Object.keys(source).forEach(function(key) {
acc[key] = merge(acc[key], source[key]);
});

return acc;
}, target);
}

module.exports = merge;
16 changes: 0 additions & 16 deletions src/functions/mergePure.js

This file was deleted.

13 changes: 8 additions & 5 deletions src/requestBuilder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var merge = require('lodash/merge');
var merge = require('./functions/merge');

var requestBuilder = {
/**
Expand Down Expand Up @@ -72,7 +72,7 @@ var requestBuilder = {
additionalParams.numericFilters = numericFilters;
}

return merge(state.getQueryParams(), additionalParams);
return merge({}, state.getQueryParams(), additionalParams);
},

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ var requestBuilder = {
additionalParams.facetFilters = facetFilters;
}

return merge(state.getQueryParams(), additionalParams);
return merge({}, state.getQueryParams(), additionalParams);
},

/**
Expand Down Expand Up @@ -310,8 +310,11 @@ var requestBuilder = {
if (typeof maxFacetHits === 'number') {
searchForFacetSearchParameters.maxFacetHits = maxFacetHits;
}
var queries = merge(requestBuilder._getHitsSearchParams(stateForSearchForFacetValues), searchForFacetSearchParameters);
return queries;
return merge(
{},
requestBuilder._getHitsSearchParams(stateForSearchForFacetValues),
searchForFacetSearchParameters
);
}
};

Expand Down
24 changes: 14 additions & 10 deletions test/spec/functions/mergePure.js → test/spec/functions/merge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const merge = require('../../../src/functions/mergePure');
const merge = require('../../../src/functions/merge');

it('should merge `source` into `object`', function() {
var names = {
Expand All @@ -24,7 +24,7 @@ it('should merge `source` into `object`', function() {
expect(merge(names, ages, heights)).toEqual(expected);
});

it('should merge sources containing circular references', function() {
it.skip('should merge sources containing circular references', function() {
var object = {
foo: {a: 1},
bar: {a: 2}
Expand Down Expand Up @@ -69,15 +69,18 @@ it('should merge first source object properties to function', function() {
expect(actual.prop).toBeInstanceOf(Function);
});

// TODO: differs from lodash, but seems to make more sense to me
it('should merge first and second source object properties to function', function() {
var fn = function() {};
var object = {prop: {}};
var object = {prop: {dogs: 'out'}};
var actual = merge({prop: fn}, {prop: fn}, object);

expect(actual).toEqual(object);
expect(actual.prop).toBe(fn);
expect(actual.prop.dogs).toBe('out');
});

it('should not merge onto function values of sources', function() {
// TODO: if a source was a function, it _will_ get added properties (no big deal for us)
it.skip('should not merge onto function values of sources', function() {
var source1 = {a: function() {}};
var source2 = {a: {b: 2}};
var expected = {a: {b: 2}};
Expand All @@ -100,7 +103,8 @@ it('should merge onto non-plain `object` values', function() {
expect(object.a).toBe(1);
});

it('should treat sparse array sources as dense', function() {
// TODO: different from lodash, but seems not necessary
it.skip('should treat sparse array sources as dense', function() {
var array = [1];
array[2] = 3;

Expand All @@ -118,7 +122,7 @@ it('should assign `null` values', function() {
expect(actual.a).toBe(null);
});

it('should assign non array/buffer/typed-array/plain-object source values directly', function() {
it.skip('should assign non array/object source values directly', function() {
function Foo() {}

/* eslint-disable no-new-wrappers */
Expand Down Expand Up @@ -195,8 +199,7 @@ it('should not overwrite existing values with `undefined` values of array source

expect(actual).toEqual(expected);

array = [1, , 3]; // eslint-disable-line no-sparse-arrays
array[1] = undefined;
array = [1, undefined, 3];

actual = merge([4, 5, 6], array);
expect(actual).toEqual(expected);
Expand All @@ -221,7 +224,8 @@ it('should skip merging when `object` and `source` are the same value', function
expect(pass).toBe(true);
});

it('should convert values to arrays when merging arrays of `source`', function() {
// NOT: we don't want this behavior
it.skip('should convert values to arrays when merging arrays of `source`', function() {
var object = {a: {'1': 'y', 'b': 'z', 'length': 2}};
var actual = merge(object, {a: ['x']});

Expand Down

0 comments on commit f4f73b5

Please sign in to comment.