Generating coupon codes easily
To test the app, please follow the next steps:
git clone https://github.com/ajmasia/couponGenerator.git
git clone [email protected]:ajmasia/couponGenerator.git
Install app dependences:
npm i
Run app:
npm start
Build app
npm run build
Install app dependences:
npm run docker:install
Run app without logs
docker-compose up -d
Run app with logs
docker-compose up
Test the app in your browser http://localhost:3000
.
You can see the results both in the browser window and the browserconsole.
You can use two different types of algorithms to generate coupons codes, secuentila
and alphanumeric
. To do it you just have to configure the service through the config.ts
file. The algorithm type is selected in the algorithm property from the configuration file.
const config: IConfig = {
algorithm: 'alphanumeric',
}
Generate numerical coupons sequentially. You can optionally configure the following parameters: amount
, digits
and startWith
:
const config: IConfig = {
algorithm: 'secuential',
config: {
amount: 12,
digits: 8,
startWith: 345,
}
}
By default, undefined parameters have the following values:
const config: IConfig = {
algorithm: 'secuential',
config: {
amount: 5,
digits: 5,
startWith: 1,
}
}
Generate alphanumeric coupons. You can optionally configure the following parameters: amount
, digits
and pattern
:
const config: IConfig = {
algorithm: 'alphanumeric',
config: {
amount: 5,
digits: 8,
pattern: '#A!',
}
}
By default, undefined parameters have the following values:
const config: IConfig = {
algorithm: 'secuential',
config: {
amount: 5,
digits: 5,
startWith: '#A',
}
}
- '#' Use numerics values
- 'A' Use capital letters
- 'a' Used lowercase letters
- '!' Use special chars like !@#$ ...
You can combine these patterns. For example #a
will generate codes using numbers and lowercase letters.
All algorithms are stored in Algortuhms
folder. To add new one, yo need to a new file with the new algorithm name implemente the new class, following the same pattern as the existing ones:
class Secuential2 implements IAlgorithm {
public getCodes(config: any): string[] {
const { amount = 5, digits = 5, startWith = 1 } = config
const maxNumber = getHighestNumOf(digits)
if (startWith > maxNumber - amount + 1) {
return ['Error: Initial value it is higher than allowed']
}
const result: string[] = Array.from(Array(amount), (x, index) => {
const code: number = index + startWith
return padNumber(code.toString(), digits)
})
return result
}
}
export default Secuential2
After create the new file you need add it to the algorithms index file:
export { default as secuential } from './Secuential'
export { default as alphanumeric } from './Alphanumeric'
export { default as secuential2 } from './Alphanumeric'
From now on it can be used through the configuration file, passing all the configuration parameters through the config property:
const config: IConfig = {
algorithm: 'secuential2',
config: {
amount: 5,
digits: 5,
startWith: '#A',
}
}
This project use the next tecnologies trying to use SOLID principles:
- HTML5 to generate the result page
- Typescript like leanguage base.
- Webpack to build the app.
- Docker with Node 10 like container image.
- An some develop tools like tslint and babel
- Implement strategy pattern
- Refactor code and fix some type errors. Now we can custom the file config.
- Update documentation
-
This version includes the posibility to use two different algoritms defined on the conf file. Now you can select
secuential
oralphanumeric
algorithms:export default { algorithm: 'alphanumeric', amount: 5, digits: 5, initialValue: 1, pattern: '#A', }
-
Update documentation
-
This first versión generate secuentials coupons using a simple configuration file. It is necessary to configure the service by entering the neneeded values in
services/couponsGenerator/config.ts
:export default { algorithm: 'secuential', amount: 5, digits: 5, initialValue: 1, }
All this values al required.