Commit 627752ea authored by Alary Franck's avatar Alary Franck

Soirée CodingDojo 2018-09-19 (KataPotter en cours de réalisation)

parent 464c601d
{
"root": true,
"parserOptions": {
"parser": 'babel-eslint',
"sourceType": "module",
"allowImportExportEverywhere": true
},
"extends": ["airbnb-base"],
"settings": {
"import/resolver": {
"webpack": {
"config": "webpack.config.js"
}
}
},
"globals": {
"expect": false,
"assert": false,
"describe": false,
"it": false,
},
"overrides": [
{
"files": "*-test.js",
"rules": {
"no-unused-expressions": "off"
}
}
],
}
node_modules
dist
# Coding dojo du 08-08-2018
## Installation de l'environnement
```bash
$ yarn install
```
## Lancement des tests
```bash
$ yarn test
```
## Build de l'application
```bash
$ yarn watch
```
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "coding-dojo",
"scripts": {
"watch": "webpack -d -w",
"test": "mocha-webpack --watch --webpack-config webpack.config.js --require test/setup.js \"src/**/*-test.js\""
},
"dependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"webpack": "^4.16.4",
"webpack-cli": "^3.1.0"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0",
"mocha-webpack": "^2.0.0-beta.0"
}
}
import { getUniqueCartItemsCount, getGroupedCartItems, getCartGroupsGrandTotal } from './potter'
console.log('A super JS app !');
//console.log(getUniqueCartItemsCount([1, 1, 2, 3, 4]));
console.log(getUniqueCartItemsCount([1, 1, 1, 1, 2, 2, 3, 4, 5]));
console.log(getGroupedCartItems([1, 1, 1, 1, 2, 2, 3, 4, 5]));
console.log(getGroupedCartItems([1, 2, 3, 4, 1, 2, 3, 5]));
console.log(getCartGroupsGrandTotal([ [ 1, 2, 3, 4, 5 ], [ 1, 2 ], [ 1 ], [ 1 ] ]));
console.log(getCartGroupsGrandTotal([ [ 1, 2, 3, 4], [ 1, 2 ], [ 1, 5 ], [ 1 ] ]));
\ No newline at end of file
import {getUniqueCartItemsCount, getGroupedCartItems, getCartGroupsGrandTotal} from './potter';
// Un panier [1, 1, 2, 3, 4, 5]
describe('Potter', () => {
describe('getUniqueCartItemsCount', () => {
it('should count unique cart item', () => {
expect(getUniqueCartItemsCount([])).to.deep.equal({});
expect(getUniqueCartItemsCount([1])).to.deep.equal({1: 1});
expect(getUniqueCartItemsCount([1, 1])).to.deep.equal({1: 2});
expect(getUniqueCartItemsCount([1, 2, 3])).to.deep.equal({1: 1, 2: 1, 3: 1});
})
});
describe('getGroupedCartItems', () => {
it('compute the total price', () => {
expect(getGroupedCartItems([])).to.deep.equal([]);
expect(getGroupedCartItems([1])).to.deep.equal([[1]]);
expect(getGroupedCartItems([1, 1])).to.deep.equal([[1], [1]]);
expect(getGroupedCartItems([1, 2, 3])).to.deep.equal([[1, 2, 3]]);
expect(getGroupedCartItems([1, 2, 2, 3])).to.deep.equal([[1, 2, 3], [2]]);
})
});
describe('getCartGroupsGrandTotal', () => {
it('compute the groups total price', () => {
expect(getCartGroupsGrandTotal([])).to.deep.equal(0);
expect(getCartGroupsGrandTotal([[1]])).to.be.equal(8);
expect(getCartGroupsGrandTotal([[1], [1]])).to.be.equal(16);
expect(getCartGroupsGrandTotal([[1, 2, 3]])).to.be.equal(3 * 8 * 0.9);
expect(getCartGroupsGrandTotal([[1, 2, 3], [2]])).to.be.equal(3 * 8 * 0.9 + 8);
expect(getCartGroupsGrandTotal([[1, 2, 3, 4], [1, 2, 3, 5]])).to.be.equal(4 * 8 * 0.8 * 2);
})
})
});
export const REGULAR_PRICE = 8;
export const DISCOUNTS = {
1: 0,
2: 5,
3: 10,
4: 20,
5: 25
};
const cart = [1, 1, 2, 2, 3, 3, 4, 5];
const grouped = {
1: [],
2: [],
3: [1, 2, 3], // 3 * 10 = 30
4: [],
5: [1, 2, 3, 4, 5] // 5 * 25 = 125 ==== 155
};
const grouped2 = {
1: [],
2: [],
3: [],
4: [1, 2, 3, 4, 1, 2, 3, 5],//8*20 = 160 ==== 160
5: []
};
export function getUniqueCartItemsCount(products) {
return products.reduce((acc, i) => {
if (acc[i]) {
++acc[i];
} else {
acc[i] = 1;
}
return acc;
}, {});
}
export function getGroupedCartItems(products) {
const counts = getUniqueCartItemsCount(products);
let currentGroup = [];
for (let bookNumber in counts) {
if (counts.hasOwnProperty(bookNumber)) {
for (let i = 0, count = counts[bookNumber]; i < count; ++i) {
if (!currentGroup[i]) {
currentGroup[i] = [];
}
currentGroup[i].push(1 * bookNumber);
}
}
}
return currentGroup;
}
export function getMaybeOptimizedGroupedCartItems(products) {
const counts = getUniqueCartItemsCount(products);
const maxGroupNumber = Object.values(counts).reduce((acc, i) => {
return i > acc ? i : acc;
}, 0);
const numDifferentItems = Object.keys(counts).length;
const numProducts = products.length;
const maxByGroup = numProducts / numDifferentItems;
const maxDiscount = Math.min(maxByGroup, Object.keys(DISCOUNTS).pop());
let ratio = numProducts / maxDiscount;
let ratioCeiled = Math.ceil(ratio);
var optimumDiscount = numProducts / ratioCeiled;
let currentGroup = [];
for (let bookNumber in counts) {
if (counts.hasOwnProperty(bookNumber)) {
for (let i = 0, count = counts[bookNumber]; i < count; ++i) {
if (!currentGroup[i]) {
currentGroup[i] = [];
}
currentGroup[i].push(1 * bookNumber);
}
}
}
return currentGroup;
}
export function getCartGroupsGrandTotal(groups) {
return groups.reduce((acc, groupItems) => {
let discount = 1 - DISCOUNTS[groupItems.length] / 100;
return acc + REGULAR_PRICE * groupItems.length * discount;
}, 0);
}
export function getGrandTotal(products) {
var groups = getGroupedCartItems(products);
return getCartGroupsGrandTotal(groups);
}
const { expect, assert } = require('chai');
global.expect = expect;
global.assert = assert;
const path = require('path');
module.exports = {
mode: 'development',
entry: {
app: path.resolve(__dirname, 'src/app.js'),
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
};
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment