From 0d2b1d534ac76c7d9bd852611cda034cb4fe9342 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Thu, 22 Oct 2020 21:41:05 +0530 Subject: [PATCH 1/4] shallowMerge function --- app/functional/index.js | 18 +++++++++++++++- tests/unit/app/functional/index.unit.test.js | 22 +++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/functional/index.js b/app/functional/index.js index 054d156..16f38da 100644 --- a/app/functional/index.js +++ b/app/functional/index.js @@ -51,4 +51,20 @@ const omit = (values, valuesToOmit) => values.filter(value => !valuesToOmit.incl */ const uniqueCharacters = characters => [...new Set(characters.join(''))]; -module.exports = { sumOf, attachPrefix, attachSuffix, pipe, identity, omit, uniqueCharacters }; +/** + * Copies all enumerable own properties from one or more objects to a empty target object. + * @param {...object} sourceObjects + * @returns {object} + */ +const shallowMerge = (...sourceObjects) => Object.assign({}, ...sourceObjects); + +module.exports = { + sumOf, + attachPrefix, + attachSuffix, + pipe, + identity, + omit, + uniqueCharacters, + shallowMerge +}; diff --git a/tests/unit/app/functional/index.unit.test.js b/tests/unit/app/functional/index.unit.test.js index 0e4d96f..bf32bc5 100644 --- a/tests/unit/app/functional/index.unit.test.js +++ b/tests/unit/app/functional/index.unit.test.js @@ -7,7 +7,8 @@ const { pipe, identity, omit, - uniqueCharacters + uniqueCharacters, + shallowMerge } = require('../../../../app/functional'); test('Should be able to sum up', () => { @@ -54,3 +55,22 @@ test('Should be able to get unique characters', () => { 'F' ]); }); + +test('Should be able to shallow merge without mutating the source objects', () => { + const obj1 = { a: 1, b: 2, e: { f: [1, 2], g: { h: 10 } } }; + const obj2 = { b: 3 }; + const obj3 = { c: 4 }; + const obj4 = { e: { f: 10 } }; + expect(shallowMerge(obj1, obj2, obj3, obj4)).toStrictEqual({ + a: 1, + b: 3, + c: 4, + e: { + f: 10 + } + }); + expect(obj1).toStrictEqual({ a: 1, b: 2, e: { f: [1, 2], g: { h: 10 } } }); + expect(obj2).toStrictEqual({ b: 3 }); + expect(obj3).toStrictEqual({ c: 4 }); + expect(obj4).toStrictEqual({ e: { f: 10 } }); +}); From 37873fb3e6f4af9e90878891ef36a671d4b1e883 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Thu, 22 Oct 2020 21:41:23 +0530 Subject: [PATCH 2/4] code refactor --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 72fa2a8..5894ea5 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const { defaultCouponGenerationOption, defaultCouponEngineOption } = require('./ const randomInteger = require('./app/random-integer.js'); const Performance = require('./app/performance.js'); const { couponConfigValidator } = require('./app/validator/coupon-config-validator.js'); +const { shallowMerge } = require('./app/functional'); /** * The Coupon constructor. @@ -13,8 +14,7 @@ const { couponConfigValidator } = require('./app/validator/coupon-config-validat */ const Coupon = function (config) { const performance = new Performance(); - const { verbose, logPerformance, maxNumberOfCouponsToGenerate } = Object.assign( - {}, + const { verbose, logPerformance, maxNumberOfCouponsToGenerate } = shallowMerge( defaultCouponEngineOption, config ); @@ -36,7 +36,7 @@ const Coupon = function (config) { omitCharacters, format, characterSet: characterSetOption - } = Object.assign({}, defaultCouponGenerationOption, option); + } = shallowMerge(defaultCouponGenerationOption, option); try { const engine = new Engine({ randomInteger, From 207f8373099e72a18bd1ff34627072c81ebe88c9 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Fri, 23 Oct 2020 20:03:27 +0530 Subject: [PATCH 3/4] update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0fba765..a07113b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This is a simple coupon creation project using NodeJS. [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yusufshakeel/couponjs) -[![npm version](https://img.shields.io/badge/npm-0.8.15-blue.svg)](https://www.npmjs.com/package/couponjs) +[![npm version](https://img.shields.io/badge/npm-0.8.16-blue.svg)](https://www.npmjs.com/package/couponjs) [![Build Status](https://travis-ci.com/yusufshakeel/couponjs.svg?branch=master)](https://travis-ci.com/yusufshakeel/couponjs) [![Coverage Status](https://coveralls.io/repos/github/yusufshakeel/couponjs/badge.svg?branch=master)](https://coveralls.io/github/yusufshakeel/couponjs?branch=master) From 4e54f80c3ae5c9408ee3de7aea10ac84e1f2df22 Mon Sep 17 00:00:00 2001 From: Yusuf Shakeel Date: Fri, 23 Oct 2020 20:03:35 +0530 Subject: [PATCH 4/4] 0.8.16 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 42d6c1a..265b74b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.8.15", + "version": "0.8.16", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8126545..dd6b4e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "couponjs", - "version": "0.8.15", + "version": "0.8.16", "description": "This is a simple coupon creation project using NodeJS.", "main": "index.js", "scripts": {