Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lodash #155

Open
wants to merge 4 commits into
base: parcel-build
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jobs:
test:
executor:
name: node/default
tag: lts
steps:
- checkout
- node/install-packages
Expand Down
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/
module.exports = {
"extends": ["standard", "plugin:compat/recommended"],
"extends": ["eslint:recommended"],
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2021,
"sourceType": "module"
},
"env": {
Expand Down
10 changes: 2 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ module.exports = {
collectCoverage: true,
coverageDirectory: '<rootDir>/test/unit-test-coverage',
testMatch: ['<rootDir>/src/__tests__/**/*.test.js'],
setupFilesAfterEnv: ['jest-expect-message'],
coveragePathIgnorePatterns: ['<rootDir>/src/__tests__'],
transformIgnorePatterns: [
"node_modules/(?!(lodash-es)/)"
],
moduleNameMapper: {
'^lodash-es$': 'lodash'
}
setupFilesAfterEnv: ['./jest.setup.js', 'jest-expect-message'],
coveragePathIgnorePatterns: ['<rootDir>/src/__tests__']
};
3 changes: 3 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const structuredClone = require('@ungap/structured-clone').default;

global.structuredClone = structuredClone;
4,330 changes: 1,999 additions & 2,331 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 4 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,21 @@
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/plugin-transform-modules-commonjs": "^7.23.0",
"@babel/preset-env": "^7.6.3",
"@babel/preset-env": "^7.24.0",
"@parcel/reporter-bundle-analyzer": "^2.10.2",
"@ungap/structured-clone": "^1.2.0",
"babel-jest": "^29.7.0",
"codecov": "^3.6.5",
"concurrently": "^8.2.2",
"eslint": "^6.5.1",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-compat": "^3.6.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint": "^8.57.0",
"jest": "^24.9.0",
"jest-cli": "^24.9.0",
"jest-expect-message": "^1.0.2",
"live-server": "^1.2.2",
"parcel": "^2.10.2"
},
"dependencies": {
"@swc/helpers": "^0.5.3",
"lodash-es": "^4.17.21"
"@swc/helpers": "^0.5.3"
},
"targets": {
"acdl": {
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/scenarios/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { merge } from 'lodash-es';
import { mergeWith } from '../../utils/mergeWith';

const testData = require('../testData');
const DataLayerManager = require('../../dataLayerManager');
const DataLayer = { Manager: DataLayerManager };
const merge = (target, ...sources) => {
return sources.reduce((acc, source) => {
return mergeWith(acc, structuredClone(source));
}, target);
};
let adobeDataLayer;

const clearDL = function() {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/scenarios/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const clearDL = function() {
});
};

const createEventListener = function(dl, eventName, callback, eventData) {
const createEventListener = function(dl, eventName, callback) {
dl.addEventListener(eventName, function(eventData) {
expect(eventData, 'data layer object as an argument of callback').toEqual(eventData);
callback();
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Functions', () => {
const mockCallback2 = jest.fn();
const mockCallback3 = jest.fn();

adobeDataLayer.addEventListener('adobeDataLayer:event', function(eventData) {
adobeDataLayer.addEventListener('adobeDataLayer:event', function() {
mockCallback1();
});

Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/scenarios/state.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { isEmpty } from 'lodash-es';

const DataLayerManager = require('../../dataLayerManager');
const DataLayer = { Manager: DataLayerManager };
const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;

let adobeDataLayer;

const clearDL = function() {
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/scenarios/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ describe('Utils', () => {
customMerge(objectInitial, objectSource);
expect(objectInitial).toEqual(objectFinal);
});
test('merge into null object', () => {
const objectInitial = null;
const objectSource = { prop1: 'foo' };
const objectFinal = null;
customMerge(objectInitial, objectSource);
expect(objectInitial).toEqual(objectFinal);
});
});

describe('dataMatchesContraints', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/testData.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { merge } from 'lodash-es';
import { mergeWith } from '../utils/mergeWith';
const merge = (target, ...sources) => {
return sources.reduce((acc, source) => {
return mergeWith(acc, structuredClone(source));
}, target);
};

const carousel1 = {
component: {
Expand Down
27 changes: 14 additions & 13 deletions src/dataLayerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { cloneDeep, get } from 'lodash-es';
import { get } from './utils/get.js';

const version = require('../version.json').version;
const Item = require('./item');
const Listener = require('./listener');
Expand Down Expand Up @@ -61,7 +62,7 @@ module.exports = function(config) {
_dataLayer.version = version;
_state = {};
_listenerManager = ListenerManager(DataLayerManager);
};
}

/**
* Updates the state with the item.
Expand All @@ -71,7 +72,7 @@ module.exports = function(config) {
*/
function _updateState(item) {
_state = customMerge(_state, item.data);
};
}

/**
* Augments the data layer Array Object, overriding: push() and adding getState(), addEventListener and removeEventListener.
Expand All @@ -82,12 +83,12 @@ module.exports = function(config) {
/**
* Pushes one or more items to the data layer.
*
* @param {...ItemConfig} var_args The items to add to the data layer.
* @param {...ItemConfig} args The items to add to the data layer.
* @returns {Number} The length of the data layer following push.
*/
_dataLayer.push = function(var_args) { /* eslint-disable-line camelcase */
const pushArguments = arguments;
const filteredArguments = arguments;
_dataLayer.push = function(...args) {
const pushArguments = args;
const filteredArguments = args;

Object.keys(pushArguments).forEach(function(key) {
const itemConfig = pushArguments[key];
Expand Down Expand Up @@ -128,9 +129,9 @@ module.exports = function(config) {
*/
_dataLayer.getState = function(path) {
if (path) {
return get(cloneDeep(_state), path);
return get(structuredClone(_state), path);
}
return cloneDeep(_state);
return structuredClone(_state);
};

/**
Expand Down Expand Up @@ -178,7 +179,7 @@ module.exports = function(config) {

_processItem(eventListenerItem);
};
};
}

/**
* Processes all items that already exist on the stack.
Expand All @@ -189,7 +190,7 @@ module.exports = function(config) {
for (let i = 0; i < _preLoadedItems.length; i++) {
_dataLayer.push(_preLoadedItems[i]);
}
};
}

/**
* Processes an item pushed to the stack.
Expand Down Expand Up @@ -260,7 +261,7 @@ module.exports = function(config) {
};

typeProcessors[item.type](item);
};
}

/**
* Logs error for invalid item pushed to the data layer.
Expand All @@ -273,7 +274,7 @@ module.exports = function(config) {
'because it does not have a valid format: ' +
JSON.stringify(item.config);
console.error(message);
};
}

return DataLayerManager;
};
21 changes: 17 additions & 4 deletions src/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { isPlainObject, isEmpty, omit, find } from 'lodash-es';

const dataMatchesContraints = require('./utils/dataMatchesContraints');
const ITEM_CONSTRAINTS = require('./itemConstraints');
const CONSTANTS = require('./constants');

const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;
function isPlainObject(obj) {
if (typeof obj !== 'object' || obj === null) return false;
let proto = obj;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(obj) === proto;
}

/**
* Constructs a data layer item.
*
Expand All @@ -31,13 +39,18 @@ module.exports = function(itemConfig, index) {
const _valid = !!_type;

function getType() {
return find(Object.keys(ITEM_CONSTRAINTS), key => dataMatchesContraints(_config, ITEM_CONSTRAINTS[key])) ||
return Object.keys(ITEM_CONSTRAINTS).find(key => dataMatchesContraints(_config, ITEM_CONSTRAINTS[key])) ||
(typeof _config === 'function' && CONSTANTS.itemType.FCTN) ||
(isPlainObject(_config) && CONSTANTS.itemType.DATA);
}

function getData() {
const data = omit(_config, Object.keys(ITEM_CONSTRAINTS.event));
const data = Object.keys(_config)
.filter(key => !Object.keys(ITEM_CONSTRAINTS.event).includes(key))
.reduce((obj, key) => {
obj[key] = _config[key];
return obj;
}, {});
if (!isEmpty(data)) {
return data;
}
Expand Down
4 changes: 2 additions & 2 deletions src/listenerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { cloneDeep } from 'lodash-es';
import { cloneDeepWith } from './utils/mergeWith';

const constants = require('./constants');
const listenerMatch = require('./utils/listenerMatch');
Expand Down Expand Up @@ -110,7 +110,7 @@ module.exports = function(dataLayerManager) {
*/
function _callHandler(listener, item) {
if (listenerMatch(listener, item)) {
const callbackArgs = [cloneDeep(item.config)];
const callbackArgs = [cloneDeepWith(item.config)];
listener.handler.apply(_dataLayerManager.getDataLayer(), callbackArgs);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ancestorRemoved.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { has, get } from 'lodash-es';
import { has, get } from './get.js';

/**
* Checks if the object contains an ancestor that is set to null or undefined.
Expand Down
14 changes: 7 additions & 7 deletions src/utils/customMerge.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { cloneDeepWith, isObject, isArray, reject, mergeWith, isNull } from 'lodash-es';
const { cloneDeepWith, mergeWith } = require('./mergeWith.js');

/**
* Merges the source into the object and sets objects as 'undefined' if they are 'undefined' in the source object.
Expand All @@ -21,10 +21,10 @@ import { cloneDeepWith, isObject, isArray, reject, mergeWith, isNull } from 'lod
*/
module.exports = function(object, source) {
const makeOmittingCloneDeepCustomizer = function(predicate) {
return function omittingCloneDeepCustomizer(value, key, object, stack) {
if (isObject(value)) {
if (isArray(value)) {
return reject(value, predicate).map(item => cloneDeepWith(item, omittingCloneDeepCustomizer));
return function omittingCloneDeepCustomizer(value) {
if (value === Object(value)) {
if (Array.isArray(value)) {
return value.filter(item => !predicate(item)).map(item => cloneDeepWith(item, omittingCloneDeepCustomizer));
}

const clone = {};
Expand All @@ -39,7 +39,7 @@ module.exports = function(object, source) {
};
};

const customizer = function(objValue, srcValue, key, object) {
const customizer = function(_, srcValue) {
if (typeof srcValue === 'undefined' || srcValue === null) {
return null;
}
Expand All @@ -52,7 +52,7 @@ module.exports = function(object, source) {
mergeWith(object, source, customizer);

// Remove null or undefined objects
object = omitDeep(object, isNull);
object = omitDeep(object, v => v === null || v === undefined);

return object;
};
6 changes: 2 additions & 4 deletions src/utils/dataMatchesContraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { find, includes } from 'lodash-es';

module.exports = function(data, constraints) {
// Go through all constraints and find one which does not match the data
const foundObjection = find(Object.keys(constraints), key => {
const foundObjection = Object.keys(constraints).find(key => {
const type = constraints[key].type;
const supportedValues = key && constraints[key].values;
const mandatory = !constraints[key].optional;
const value = data[key];
const valueType = typeof value;
const incorrectType = type && valueType !== type;
const noMatchForSupportedValues = supportedValues && !includes(supportedValues, value);
const noMatchForSupportedValues = supportedValues && !supportedValues.includes(value);
if (mandatory) {
return !value || incorrectType || noMatchForSupportedValues;
} else {
Expand Down
Loading