Skip to content

Instantly share code, notes, and snippets.

@@ -6,14 +6,3 @@ function generateReadedReport(readedList, recommendList) {
for (let readedBook of readedList.books) {
- let currentPoint = 0;
- if (readedBook.times === 0) {
- currentPoint = 0;
- } else if (!recommendList.map(recommend => recommend.asin).includes(readedBook.asin)) {
- currentPoint = 1;
- } else if (readedBook.times === 1) {
- currentPoint = 3;
- } else if (readedBook.times === 2) {
@haru01
haru01 / config.json
Created November 24, 2018 23:47
refactoring2nd created by haru01 - https://repl.it/@haru01/refactoring2nd
{
"testRegex": ".*-test\\.js$",
"testEnvironment": "node"
}
const randomDiceTotal = () => {
const first = Math.floor(Math.random() * 6) + 1
const second = Math.floor(Math.random() * 6) + 1
return first + second
}
const createJugeChoHan = (diceTotal = randomDiceTotal) => (players) => {
const choHan = { 0: '丁', 1: '半' }[diceTotal() % 2]
return players.map((player) => {
@haru01
haru01 / chohan.take5.test.js
Last active November 7, 2018 02:21
classを使っていない時
const randomDiceTotal = () => {
const first = Math.floor(Math.random() * 6) + 1
const second = Math.floor(Math.random() * 6) + 1
return first + second
}
const judgeChoHan = (players, diceTotal = randomDiceTotal) => {
const choHan = { 0: '丁', 1: '半' }[diceTotal() % 2]
return players.map((player) => {
@haru01
haru01 / chohan.take4.test.js
Last active November 7, 2018 02:19
コンストラクタ インジェクションで差し替え可能案
const randomDiceTotal = () => {
const first = Math.floor(Math.random() * 6) + 1
const second = Math.floor(Math.random() * 6) + 1
return first + second
}
class ChoHan {
// コンストラクタ インジェクションで差し替え可能
constructor(diceTotal = randomDiceTotal) {
this.diceTotal = diceTotal
@haru01
haru01 / chohan.take3.test.js
Last active November 7, 2018 02:09
メソッドオーバライド
class ChoHan {
judge(players) {
const choHan = { 0: '丁', 1: '半' }[this._total() % 2]
return players.map((player) => {
if (player.choHan === choHan) {
return { ...player, result: 'win' }
} else {
return { ...player, result: 'lose' }
}
@haru01
haru01 / chohan.take2.test.js
Last active November 7, 2018 02:04
メソッド分離して引数でコントロール
class ChoHan {
// ランダムな振る舞いをするメソッドと
judge(players) {
const first = Math.floor(Math.random() * 6) + 1
const second = Math.floor(Math.random() * 6) + 1
return this._judge(players, first + second)
}
// ランダムな振る舞いしないメソッドに分離し、引数でコントロール
_judge(players, total) {
class ChoHan {
judge(players) {
// 2つのサイコロがランダムで、テストを難しくしている箇所。 どうやって分離する?
const first = Math.floor(Math.random() * 6) + 1
const second = Math.floor(Math.random() * 6) + 1
const choHan = { 0: '丁', 1: '半' }[(first + second) % 2]
return players.map((player) => {
if (player.choHan === choHan) {
return { ...player, result: 'win' }
inc = (n) => n + 1
inc(2)
square = (n) => n * n
[1,2,3].map(inc) // [ 2, 3, 4 ]
[1,2,3].map(square) // [ 1, 4, 9 ]
compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);
incSquare = compose(inc, square)
incSquare(3) // 10
import xs from 'xstream';
import delay from 'xstream/extra/delay';
const humanCProx = xs.create();
const humanA = humanCProx.map(increment).compose(delay(100));
const humanB = humanA.map(increment).compose(delay(100));
const humanC = humanB.map(increment).startWith(1).compose(delay(100));
humanCProx.imitate(humanC);
function increment(n) {