Skip to content

Commit

Permalink
Merge pull request #28 from yusufshakeel/dev
Browse files Browse the repository at this point in the history
0.8.15
  • Loading branch information
yusufshakeel authored Oct 19, 2020
2 parents 29f75a0 + b81ba01 commit 5138cbf
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.14-blue.svg)](https://www.npmjs.com/package/couponjs)
[![npm version](https://img.shields.io/badge/npm-0.8.15-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)

Expand Down
17 changes: 7 additions & 10 deletions app/character-set-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { DEFAULT_OMIT_CHARACTERS } = require('./constants.js');
const characterSet = require('./character-set.js');
const { omit, uniqueCharacters } = require('./functional');

/**
* This will generate a string of unique characters based on the options provided.
Expand All @@ -11,16 +12,12 @@ const characterSet = require('./character-set.js');
*/
function characterSetBuilder(characterSetOptions, omitCharacters = DEFAULT_OMIT_CHARACTERS) {
const { builtIn = [], custom = [] } = characterSetOptions;
const charactersToOmit = [...new Set(omitCharacters.join(''))];
const charactersFromCharacterSetOptions = [
...new Set(
[...builtIn.map(characterSetName => characterSet(characterSetName)), ...custom].join('')
)
];
const charactersToUse = charactersFromCharacterSetOptions.filter(
character => !charactersToOmit.includes(character)
);
return charactersToUse.join('');
const charactersToOmit = uniqueCharacters(omitCharacters);
const charactersFromCharacterSetOptions = uniqueCharacters([
...builtIn.map(characterSetName => characterSet(characterSetName)),
...custom
]);
return omit(charactersFromCharacterSetOptions, charactersToOmit).join('');
}

module.exports = characterSetBuilder;
10 changes: 7 additions & 3 deletions app/engine.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { attachSuffix, attachPrefix, pipe, identity } = require('./functional');

const {
validateLength,
validateOmitCharacters,
Expand All @@ -19,7 +21,9 @@ const {
DEFAULT_CHARACTER_SET_OPTION,
UNDEFINED
} = require('./constants.js');

const Formatter = require('./formatter.js');

const characterSetBuilder = require('./character-set-builder.js');

/**
Expand Down Expand Up @@ -52,7 +56,7 @@ const Engine = function ({
validateOmitCharacters(omitCharacters);
validateCharacterSetOption(characterSetOption);

const formatter = format !== UNDEFINED ? new Formatter(format) : { format: coupon => coupon };
const formatter = format !== UNDEFINED ? new Formatter(format) : { format: identity };

const characters = characterSetBuilder(characterSetOption, omitCharacters).split('');
const charactersLength = characters.length;
Expand All @@ -73,8 +77,8 @@ const Engine = function ({
for (let i = 0; i < length; i++) {
generatedCouponCharacters.push(characters[randomInteger(0, charactersLength - 1)]);
}
const coupon = `${prefix}${generatedCouponCharacters.join('')}${suffix}`;
return formatter.format(coupon);
const coupon = generatedCouponCharacters.join('');
return pipe([attachPrefix(prefix), attachSuffix(suffix), formatter.format])(coupon);
}

/**
Expand Down
51 changes: 50 additions & 1 deletion app/functional/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
'use strict';

/**
* This will sum up the values.
* @param {number[]} values
* @returns {number}
*/
const sumOf = values => values.reduce((sum, size) => sum + size, 0);

module.exports = { sumOf };
/**
* This will attach prefix to the coupon.
* @param {string} prefix
* @returns {function(string): string}
*/
const attachPrefix = (prefix = '') => coupon => `${prefix}${coupon}`;

/**
* This will attach suffix to the coupon.
* @param {string} suffix
* @returns {function(string): string}
*/
const attachSuffix = (suffix = '') => coupon => `${coupon}${suffix}`;

/**
* This will take in an array of operators to operate on a value.
* @param {*} operators
* @returns {function(*=): *}
*/
const pipe = operators => value =>
operators.reduce((enrichedValue, operator) => operator(enrichedValue), value);

/**
* This will return the first argument it receives.
* @param {*} value
* @returns {*}
*/
const identity = value => value;

/**
* This will omit the specified values.
* @param {string[]} values
* @param {string[]} valuesToOmit
* @returns {string[]}
*/
const omit = (values, valuesToOmit) => values.filter(value => !valuesToOmit.includes(value));

/**
* This will return unique characters array.
* @param {string[]} characters
* @returns {string[]}
*/
const uniqueCharacters = characters => [...new Set(characters.join(''))];

module.exports = { sumOf, attachPrefix, attachSuffix, pipe, identity, omit, uniqueCharacters };
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "couponjs",
"version": "0.8.14",
"version": "0.8.15",
"description": "This is a simple coupon creation project using NodeJS.",
"main": "index.js",
"scripts": {
Expand Down
51 changes: 50 additions & 1 deletion tests/unit/app/functional/index.unit.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
'use strict';

const { sumOf } = require('../../../../app/functional');
const {
sumOf,
attachSuffix,
attachPrefix,
pipe,
identity,
omit,
uniqueCharacters
} = require('../../../../app/functional');

test('Should be able to sum up', () => {
expect(sumOf([1, 2, 3])).toBe(6);
});

test('Should be able to attach prefix', () => {
expect(attachPrefix('HELLO')('COUPON')).toBe('HELLOCOUPON');
expect(attachPrefix()('COUPON')).toBe('COUPON');
});

test('Should be able to attach suffix', () => {
expect(attachSuffix('WORLD')('COUPON')).toBe('COUPONWORLD');
expect(attachSuffix()('COUPON')).toBe('COUPON');
});

test('Should be able to attach prefix and suffix in that order using pipe function', () => {
expect(pipe([attachPrefix('HELLO'), attachSuffix('WORLD')])('COUPON')).toBe('HELLOCOUPONWORLD');
});

test('Should be able to return the first argument it receives using identity function', () => {
expect(identity('foo')).toBe('foo');
expect(identity(1)).toBe(1);
expect(identity(null)).toBe(null);
expect(identity(undefined)).toBe(undefined);
expect(identity({ foo: 'bar' })).toStrictEqual({ foo: 'bar' });
expect(identity(true)).toBeTruthy();
expect(identity(false)).toBeFalsy();
});

test('Should be able to omit values', () => {
const values = ['A', 'B', 'C', 'D', 'E', 'F'];
const valuesToOmit = ['A', 'B', 'F'];
expect(omit(values, valuesToOmit)).toStrictEqual(['C', 'D', 'E']);
});

test('Should be able to get unique characters', () => {
expect(uniqueCharacters(['ABC', 'CD', 'EB', 'ACF'])).toStrictEqual([
'A',
'B',
'C',
'D',
'E',
'F'
]);
});

0 comments on commit 5138cbf

Please sign in to comment.