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) 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/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, 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": { 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 } }); +});