From 6abc0265732d0d414cf04ba7f548b6e01b5776d1 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:09:52 +0900 Subject: [PATCH 01/28] add nb.js --- books/refactoring-javascript/ch07/nb.js | 119 ++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 books/refactoring-javascript/ch07/nb.js diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js new file mode 100644 index 0000000..724abbc --- /dev/null +++ b/books/refactoring-javascript/ch07/nb.js @@ -0,0 +1,119 @@ +var easy = "easy"; +var medium = "medium"; +var hard = "hard"; + +imagine = ["c", "cmaj7", "f", "am", "dm", "g", "e7"]; +somewhereOverTheRainbow = ["c", "em", "f", "g", "am"]; +tooManyCooks = ["c", "g", "f"]; +iWillFollowYouIntoTheDark = ["f", "dm", "bb", "c", "a", "bbm"]; +babyOneMoreTime = ["cm", "g", "bb", "eb", "fm", "ab"]; +creep = ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"]; +paperBag = [ + "bm7", + "e", + "c", + "g", + "b7", + "f", + "em", + "a", + "cmaj7", + "em7", + "a7", + "f7", + "b" +]; +toxic = ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"]; +bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; + +var songs = []; +var labels = []; +var allChords = []; +var labelCounts = []; +var labelProbabilities = []; +var chordCountsInLabels = {}; +var probabilityOfChordsInLabels = {}; + +function train(chords, label) { + var index; + songs.push([label, chords]); + labels.push(label); + for (index = 0; index < chords.length; index++) { + if (!allChords.includes(chords[index])) { + allChords.push(chords[index]); + } + } + if (Object.keys(labelCounts).includes(label)) { + labelCounts[label] = labelCounts[label] + 1; + } else { + labelCounts[label] = 1; + } +} + +function setLabelProbabilities() { + Object.keys(labelCounts).forEach(function(label) { + var numberOfSongs = songs.length; + labelProbabilities[label] = labelCounts[label] / numberOfSongs; + }); +} + +function setChordCountsInLabels() { + songs.forEach(function(song) { + if (chordCountsInLabels[song[0]] === undefined) { + chordCountsInLabels[song[0]] = {}; + } + song[1].forEach(function(chord) { + if (chordCountsInLabels[song[0]][chord] > 0) { + chordCountsInLabels[song[0]][chord] += 1; + } else { + chordCountsInLabels[song[0]][chord] = 1; + } + }); + }); +} + +function setProbabilityOfChordsInLabels() { + probabilityOfChordsInLabels = chordCountsInLabels; + Object.keys(probabilityOfChordsInLabels).forEach(function(difficulty) { + Object.keys(probabilityOfChordsInLabels[difficulty]).forEach(function( + chord + ) { + probabilityOfChordsInLabels[difficulty][chord] /= songs.length; + }); + }); +} + +train(imagine, easy); +train(somewhereOverTheRainbow, easy); +train(tooManyCooks, easy); +train(iWillFollowYouIntoTheDark, medium); +train(babyOneMoreTime, medium); +train(creep, medium); +train(paperBag, hard); +train(toxic, hard); +train(bulletproof, hard); + +setLabelProbabilities(); +setChordCountsInLabels(); +setProbabilityOfChordsInLabels(); + +function classify(chords) { + var smoothing = 1.01; + var classified = {}; + console.log(labelProbabilities); + Object.keys(labelProbabilities).forEach(function(difficulty) { + var first = labelProbabilities[difficulty] + smoothing; + chords.forEach(function(chord) { + var probabilityOfChordInLabel = + probabilityOfChordsInLabels[difficulty][chord]; + if (probabilityOfChordInLabel) { + first = first * (probabilityOfChordInLabel + smoothing); + } + }); + classified[difficulty] = first; + }); + console.log(classified); +} + +classify(["d", "g", "e", "dm"]); +classify(["f#m7", "a", "dadd9", "dmaj7", "bm", "bm7", "d", "f#m"]); From ac0741fc2cf97fd26ada06fc73d9be0d4a320aa5 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:14:45 +0900 Subject: [PATCH 02/28] refactor iteration statement --- books/refactoring-javascript/ch07/nb.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 724abbc..74e6968 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -35,14 +35,13 @@ var chordCountsInLabels = {}; var probabilityOfChordsInLabels = {}; function train(chords, label) { - var index; songs.push([label, chords]); labels.push(label); - for (index = 0; index < chords.length; index++) { - if (!allChords.includes(chords[index])) { - allChords.push(chords[index]); + chords.forEach(chord => { + if (!allChords.includes(chord)) { + allChords.push(chord); } - } + }); if (Object.keys(labelCounts).includes(label)) { labelCounts[label] = labelCounts[label] + 1; } else { From 91d454223bc1b551a466ce2e2a86ab35aa59dca7 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:21:49 +0900 Subject: [PATCH 03/28] use Set() --- books/refactoring-javascript/ch07/nb.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 74e6968..9ea334b 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -28,7 +28,7 @@ bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; var songs = []; var labels = []; -var allChords = []; +var allChords = new Set(); var labelCounts = []; var labelProbabilities = []; var chordCountsInLabels = {}; @@ -37,11 +37,7 @@ var probabilityOfChordsInLabels = {}; function train(chords, label) { songs.push([label, chords]); labels.push(label); - chords.forEach(chord => { - if (!allChords.includes(chord)) { - allChords.push(chord); - } - }); + chords.forEach(chord => allChords.add(chord)); if (Object.keys(labelCounts).includes(label)) { labelCounts[label] = labelCounts[label] + 1; } else { From aac4a0fa37e2e2b52218f2f68f81f045335a73db Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:24:17 +0900 Subject: [PATCH 04/28] use object instead of array --- books/refactoring-javascript/ch07/nb.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 9ea334b..c15c17a 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -29,8 +29,8 @@ bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; var songs = []; var labels = []; var allChords = new Set(); -var labelCounts = []; -var labelProbabilities = []; +var labelCounts = {}; +var labelProbabilities = {}; var chordCountsInLabels = {}; var probabilityOfChordsInLabels = {}; From 50293fc7c843955689e331b681170d0e95b0a9bc Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:25:51 +0900 Subject: [PATCH 05/28] remove dead code --- books/refactoring-javascript/ch07/nb.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index c15c17a..ed953a9 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -27,7 +27,6 @@ toxic = ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"]; bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; var songs = []; -var labels = []; var allChords = new Set(); var labelCounts = {}; var labelProbabilities = {}; @@ -36,7 +35,6 @@ var probabilityOfChordsInLabels = {}; function train(chords, label) { songs.push([label, chords]); - labels.push(label); chords.forEach(chord => allChords.add(chord)); if (Object.keys(labelCounts).includes(label)) { labelCounts[label] = labelCounts[label] + 1; From 4a98792db25ced48496423ab92ea7a519835d90e Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:29:24 +0900 Subject: [PATCH 06/28] change array injection to object injection --- books/refactoring-javascript/ch07/nb.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index ed953a9..873fed2 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -34,7 +34,10 @@ var chordCountsInLabels = {}; var probabilityOfChordsInLabels = {}; function train(chords, label) { - songs.push([label, chords]); + songs.push({ + label, + chords + }); chords.forEach(chord => allChords.add(chord)); if (Object.keys(labelCounts).includes(label)) { labelCounts[label] = labelCounts[label] + 1; @@ -52,14 +55,14 @@ function setLabelProbabilities() { function setChordCountsInLabels() { songs.forEach(function(song) { - if (chordCountsInLabels[song[0]] === undefined) { - chordCountsInLabels[song[0]] = {}; + if (chordCountsInLabels[song.label] === undefined) { + chordCountsInLabels[song.label] = {}; } - song[1].forEach(function(chord) { - if (chordCountsInLabels[song[0]][chord] > 0) { - chordCountsInLabels[song[0]][chord] += 1; + song.chords.forEach(function(chord) { + if (chordCountsInLabels[song.label][chord] > 0) { + chordCountsInLabels[song.label][chord] += 1; } else { - chordCountsInLabels[song[0]][chord] = 1; + chordCountsInLabels[song.label][chord] = 1; } }); }); From 0f8cbb6dc6a86670b4bd44c32daaa0a02daba06c Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:36:52 +0900 Subject: [PATCH 07/28] use Map() --- books/refactoring-javascript/ch07/nb.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 873fed2..76698a2 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -95,7 +95,7 @@ setProbabilityOfChordsInLabels(); function classify(chords) { var smoothing = 1.01; - var classified = {}; + var classified = new Map(); console.log(labelProbabilities); Object.keys(labelProbabilities).forEach(function(difficulty) { var first = labelProbabilities[difficulty] + smoothing; @@ -106,7 +106,7 @@ function classify(chords) { first = first * (probabilityOfChordInLabel + smoothing); } }); - classified[difficulty] = first; + classified.set(difficulty, first); }); console.log(classified); } From 8d79159099657abdb06477da74e4ec57ff1fae29 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:43:54 +0900 Subject: [PATCH 08/28] use Map() for labelCounts --- books/refactoring-javascript/ch07/nb.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 76698a2..c2b9411 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -28,7 +28,7 @@ bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; var songs = []; var allChords = new Set(); -var labelCounts = {}; +var labelCounts = new Map(); var labelProbabilities = {}; var chordCountsInLabels = {}; var probabilityOfChordsInLabels = {}; @@ -39,17 +39,17 @@ function train(chords, label) { chords }); chords.forEach(chord => allChords.add(chord)); - if (Object.keys(labelCounts).includes(label)) { - labelCounts[label] = labelCounts[label] + 1; + if (Array.from(labelCounts.keys()).includes(label)) { + labelCounts.set(label, labelCounts.get(label) + 1); } else { - labelCounts[label] = 1; + labelCounts.set(label, 1); } } function setLabelProbabilities() { - Object.keys(labelCounts).forEach(function(label) { + labelCounts.forEach(function(_count, label) { var numberOfSongs = songs.length; - labelProbabilities[label] = labelCounts[label] / numberOfSongs; + labelProbabilities[label] = labelCounts.get(label) / numberOfSongs; }); } From 92dc24fcf92c7c5614e4c8b982edab883a9ac6f2 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:51:43 +0900 Subject: [PATCH 09/28] use Map() for labelProbabilities --- books/refactoring-javascript/ch07/nb.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index c2b9411..c45868a 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -29,7 +29,7 @@ bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; var songs = []; var allChords = new Set(); var labelCounts = new Map(); -var labelProbabilities = {}; +var labelProbabilities = new Map(); var chordCountsInLabels = {}; var probabilityOfChordsInLabels = {}; @@ -48,8 +48,7 @@ function train(chords, label) { function setLabelProbabilities() { labelCounts.forEach(function(_count, label) { - var numberOfSongs = songs.length; - labelProbabilities[label] = labelCounts.get(label) / numberOfSongs; + labelProbabilities.set(label, labelCounts.get(label) / songs.length); }); } @@ -97,8 +96,8 @@ function classify(chords) { var smoothing = 1.01; var classified = new Map(); console.log(labelProbabilities); - Object.keys(labelProbabilities).forEach(function(difficulty) { - var first = labelProbabilities[difficulty] + smoothing; + labelProbabilities.forEach(function(_probabilities, difficulty) { + var first = labelProbabilities.get(difficulty) + smoothing; chords.forEach(function(chord) { var probabilityOfChordInLabel = probabilityOfChordsInLabels[difficulty][chord]; From ac352c5307ceda69ff0b8f9647b1e33735bf32d3 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 14:56:15 +0900 Subject: [PATCH 10/28] use Map() for global var --- books/refactoring-javascript/ch07/nb.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index c45868a..52da523 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -30,8 +30,8 @@ var songs = []; var allChords = new Set(); var labelCounts = new Map(); var labelProbabilities = new Map(); -var chordCountsInLabels = {}; -var probabilityOfChordsInLabels = {}; +var chordCountsInLabels = new Map(); +var probabilityOfChordsInLabels = new Map(); function train(chords, label) { songs.push({ @@ -54,14 +54,14 @@ function setLabelProbabilities() { function setChordCountsInLabels() { songs.forEach(function(song) { - if (chordCountsInLabels[song.label] === undefined) { - chordCountsInLabels[song.label] = {}; + if (chordCountsInLabels.get(song.label) === undefined) { + chordCountsInLabels.set(song.label, {}); } song.chords.forEach(function(chord) { - if (chordCountsInLabels[song.label][chord] > 0) { - chordCountsInLabels[song.label][chord] += 1; + if (chordCountsInLabels.get(song.label)[chord] > 0) { + chordCountsInLabels.get(song.label)[chord] += 1; } else { - chordCountsInLabels[song.label][chord] = 1; + chordCountsInLabels.get(song.label)[chord] = 1; } }); }); @@ -69,11 +69,11 @@ function setChordCountsInLabels() { function setProbabilityOfChordsInLabels() { probabilityOfChordsInLabels = chordCountsInLabels; - Object.keys(probabilityOfChordsInLabels).forEach(function(difficulty) { - Object.keys(probabilityOfChordsInLabels[difficulty]).forEach(function( + probabilityOfChordsInLabels.forEach(function(_chords, difficulty) { + Object.keys(probabilityOfChordsInLabels.get(difficulty)).forEach(function( chord ) { - probabilityOfChordsInLabels[difficulty][chord] /= songs.length; + probabilityOfChordsInLabels.get(difficulty)[chord] /= songs.length; }); }); } @@ -99,8 +99,9 @@ function classify(chords) { labelProbabilities.forEach(function(_probabilities, difficulty) { var first = labelProbabilities.get(difficulty) + smoothing; chords.forEach(function(chord) { - var probabilityOfChordInLabel = - probabilityOfChordsInLabels[difficulty][chord]; + var probabilityOfChordInLabel = probabilityOfChordsInLabels.get( + difficulty + )[chord]; if (probabilityOfChordInLabel) { first = first * (probabilityOfChordInLabel + smoothing); } From e35904078fe3ac76ceaf1015098d9c5e7b16b899 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 15:21:01 +0900 Subject: [PATCH 11/28] npm install mocha, wish --- package-lock.json | 860 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 ++ 2 files changed, 883 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e789b8a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,860 @@ +{ + "name": "javascript-playground", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ansi-colors": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.3.tgz", + "integrity": "sha512-LEHHyuhlPY3TmuUYMh2oz89lTShfvgbmzaBcxve9t/9Wuy7Dwf4yoAKcND7KFT1HAQfqZ12qtc+DUrBMeKF9nw==" + }, + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=" + }, + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "requires": { + "color-convert": "^1.9.0" + } + }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "requires": { + "sprintf-js": "~1.0.2" + } + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==" + }, + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==" + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + }, + "dependencies": { + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, + "cliui": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-4.1.0.tgz", + "integrity": "sha512-4FG+RSG9DL7uEwRUZXZn3SS34DiDPfzP0VOiEwtUWlE+AR2EIg+hSyvrIgUUfhdgR/UkAeW2QHgeP+hWrXs7jQ==", + "requires": { + "string-width": "^2.1.1", + "strip-ansi": "^4.0.0", + "wrap-ansi": "^2.0.0" + } + }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, + "cross-spawn": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "requires": { + "nice-try": "^1.0.4", + "path-key": "^2.0.1", + "semver": "^5.5.0", + "shebang-command": "^1.2.0", + "which": "^1.2.9" + } + }, + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "requires": { + "ms": "^2.1.1" + } + }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=" + }, + "define-properties": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", + "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "requires": { + "object-keys": "^1.0.12" + } + }, + "diff": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==" + }, + "emoji-regex": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", + "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==" + }, + "end-of-stream": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz", + "integrity": "sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==", + "requires": { + "once": "^1.4.0" + } + }, + "es-abstract": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", + "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", + "requires": { + "es-to-primitive": "^1.2.0", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "is-callable": "^1.1.4", + "is-regex": "^1.0.4", + "object-keys": "^1.0.12" + } + }, + "es-to-primitive": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", + "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + }, + "esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==" + }, + "execa": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", + "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", + "requires": { + "cross-spawn": "^6.0.0", + "get-stream": "^4.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, + "find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "requires": { + "locate-path": "^3.0.0" + } + }, + "flat": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz", + "integrity": "sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==", + "requires": { + "is-buffer": "~2.0.3" + } + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" + }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "requires": { + "pump": "^3.0.0" + } + }, + "glob": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", + "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "growl": { + "version": "1.10.5", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", + "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==" + }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "requires": { + "function-bind": "^1.1.1" + } + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + }, + "has-symbols": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", + "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=" + }, + "he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==" + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "invert-kv": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", + "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==" + }, + "is-buffer": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz", + "integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==" + }, + "is-callable": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", + "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==" + }, + "is-date-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", + "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=" + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" + }, + "is-regex": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", + "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", + "requires": { + "has": "^1.0.1" + } + }, + "is-stream": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" + }, + "is-symbol": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", + "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", + "requires": { + "has-symbols": "^1.0.0" + } + }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" + }, + "js-yaml": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, + "lcid": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", + "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", + "requires": { + "invert-kv": "^2.0.0" + } + }, + "locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "requires": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" + } + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, + "log-symbols": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.2.0.tgz", + "integrity": "sha512-VeIAFslyIerEJLXHziedo2basKbMKtTw3vfn5IzG0XTjhAVEJyNHnL2p7vc+wBDSdQuUpNw3M2u6xb9QsAY5Eg==", + "requires": { + "chalk": "^2.0.1" + } + }, + "map-age-cleaner": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", + "integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==", + "requires": { + "p-defer": "^1.0.0" + } + }, + "mem": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", + "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", + "requires": { + "map-age-cleaner": "^0.1.1", + "mimic-fn": "^2.0.0", + "p-is-promise": "^2.0.0" + } + }, + "mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==" + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.2.0.tgz", + "integrity": "sha512-qwfFgY+7EKAAUAdv7VYMZQknI7YJSGesxHyhn6qD52DV8UcSZs5XwCifcZGMVIE4a5fbmhvbotxC0DLQ0oKohQ==", + "requires": { + "ansi-colors": "3.2.3", + "browser-stdout": "1.3.1", + "debug": "3.2.6", + "diff": "3.5.0", + "escape-string-regexp": "1.0.5", + "find-up": "3.0.0", + "glob": "7.1.3", + "growl": "1.10.5", + "he": "1.2.0", + "js-yaml": "3.13.1", + "log-symbols": "2.2.0", + "minimatch": "3.0.4", + "mkdirp": "0.5.1", + "ms": "2.1.1", + "node-environment-flags": "1.0.5", + "object.assign": "4.1.0", + "strip-json-comments": "2.0.1", + "supports-color": "6.0.0", + "which": "1.3.1", + "wide-align": "1.1.3", + "yargs": "13.2.2", + "yargs-parser": "13.0.0", + "yargs-unparser": "1.5.0" + } + }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + }, + "nice-try": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", + "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" + }, + "node-environment-flags": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.5.tgz", + "integrity": "sha512-VNYPRfGfmZLx0Ye20jWzHUjyTW/c+6Wq+iLhDzUI4XmhrDd9l/FozXV3F2xOaXjvp0co0+v1YSR3CMP6g+VvLQ==", + "requires": { + "object.getownpropertydescriptors": "^2.0.3", + "semver": "^5.7.0" + } + }, + "npm-run-path": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", + "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", + "requires": { + "path-key": "^2.0.0" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" + }, + "object-keys": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==" + }, + "object.assign": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz", + "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==", + "requires": { + "define-properties": "^1.1.2", + "function-bind": "^1.1.1", + "has-symbols": "^1.0.0", + "object-keys": "^1.0.11" + } + }, + "object.getownpropertydescriptors": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz", + "integrity": "sha1-h1jIRvW0B62rDyNuCYbxSwUcqhY=", + "requires": { + "define-properties": "^1.1.2", + "es-abstract": "^1.5.1" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1" + } + }, + "os-locale": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", + "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", + "requires": { + "execa": "^1.0.0", + "lcid": "^2.0.0", + "mem": "^4.0.0" + } + }, + "p-defer": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", + "integrity": "sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=" + }, + "p-finally": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", + "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=" + }, + "p-is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", + "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" + }, + "p-limit": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", + "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", + "requires": { + "p-try": "^2.0.0" + } + }, + "p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "requires": { + "p-limit": "^2.0.0" + } + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, + "path-key": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" + }, + "pump": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", + "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=" + }, + "require-main-filename": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==" + }, + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + }, + "set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=" + }, + "shebang-command": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", + "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", + "requires": { + "shebang-regex": "^1.0.0" + } + }, + "shebang-regex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", + "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" + }, + "signal-exit": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", + "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" + }, + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "requires": { + "ansi-regex": "^3.0.0" + } + }, + "strip-eof": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" + }, + "strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" + }, + "supports-color": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.0.0.tgz", + "integrity": "sha512-on9Kwidc1IUQo+bQdhi8+Tijpo0e1SS6RoGo2guUwn5vdaxw8RXOF9Vb2ws+ihWOmh4JnCJOvaziZWP1VABaLg==", + "requires": { + "has-flag": "^3.0.0" + } + }, + "which": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", + "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", + "requires": { + "isexe": "^2.0.0" + } + }, + "which-module": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", + "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=" + }, + "wide-align": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", + "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", + "requires": { + "string-width": "^1.0.2 || 2" + } + }, + "wish": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/wish/-/wish-1.0.1.tgz", + "integrity": "sha1-fy0zSGSUex/f8EixMdACh8r3nZw=" + }, + "wrap-ansi": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", + "requires": { + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "^2.0.0" + } + } + } + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + }, + "y18n": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", + "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" + }, + "yargs": { + "version": "13.2.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.2.2.tgz", + "integrity": "sha512-WyEoxgyTD3w5XRpAQNYUB9ycVH/PQrToaTXdYXRdOXvEy1l19br+VJsc0vcO8PTGg5ro/l/GY7F/JMEBmI0BxA==", + "requires": { + "cliui": "^4.0.0", + "find-up": "^3.0.0", + "get-caller-file": "^2.0.1", + "os-locale": "^3.1.0", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^3.0.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^13.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==" + }, + "string-width": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", + "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", + "requires": { + "emoji-regex": "^7.0.1", + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^5.1.0" + } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "requires": { + "ansi-regex": "^4.1.0" + } + } + } + }, + "yargs-parser": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.0.0.tgz", + "integrity": "sha512-w2LXjoL8oRdRQN+hOyppuXs+V/fVAYtpcrRxZuF7Kt/Oc+Jr2uAcVntaUTNT6w5ihoWfFDpNY8CPx1QskxZ/pw==", + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + }, + "yargs-unparser": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-1.5.0.tgz", + "integrity": "sha512-HK25qidFTCVuj/D1VfNiEndpLIeJN78aqgR23nL3y4N0U/91cOAzqfHlF8n2BvoNDcZmJKin3ddNSvOxSr8flw==", + "requires": { + "flat": "^4.1.0", + "lodash": "^4.17.11", + "yargs": "^12.0.5" + }, + "dependencies": { + "get-caller-file": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", + "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==" + }, + "require-main-filename": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", + "integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=" + }, + "yargs": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", + "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", + "requires": { + "cliui": "^4.0.0", + "decamelize": "^1.2.0", + "find-up": "^3.0.0", + "get-caller-file": "^1.0.1", + "os-locale": "^3.0.0", + "require-directory": "^2.1.1", + "require-main-filename": "^1.0.1", + "set-blocking": "^2.0.0", + "string-width": "^2.0.0", + "which-module": "^2.0.0", + "y18n": "^3.2.1 || ^4.0.0", + "yargs-parser": "^11.1.1" + } + }, + "yargs-parser": { + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-11.1.1.tgz", + "integrity": "sha512-C6kB/WJDiaxONLJQnF8ccx9SEeoTTLek8RVbaOIsrAUS8VrBEXfmeSnCZxygc+XC2sNMBIwOOnfcxiynjHsVSQ==", + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + } + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b012984 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "javascript-playground", + "version": "1.0.0", + "description": "## Design Patterns", + "main": "index.js", + "scripts": { + "test": "mocha -w **/*.test.js -w" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/letsget23/javascript-playground.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/letsget23/javascript-playground/issues" + }, + "homepage": "https://github.com/letsget23/javascript-playground#readme", + "dependencies": { + "mocha": "^6.2.0", + "wish": "^1.0.1" + } +} From e1589c5e50425186208da6d53dd7080d8649cf89 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 15:21:11 +0900 Subject: [PATCH 12/28] add small test --- books/refactoring-javascript/ch07/nb.test.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 books/refactoring-javascript/ch07/nb.test.js diff --git a/books/refactoring-javascript/ch07/nb.test.js b/books/refactoring-javascript/ch07/nb.test.js new file mode 100644 index 0000000..3ba0eb1 --- /dev/null +++ b/books/refactoring-javascript/ch07/nb.test.js @@ -0,0 +1,7 @@ +var wish = require("wish"); + +describe("the file", function() { + it("works", function() { + with (true); + }); +}); From 5958a6fae13891c75339b19a89cf1745253d82f6 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 15:27:18 +0900 Subject: [PATCH 13/28] add minimal test --- books/refactoring-javascript/ch07/nb.js | 10 ++++++++-- books/refactoring-javascript/ch07/nb.test.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 52da523..96fa26d 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -95,7 +95,7 @@ setProbabilityOfChordsInLabels(); function classify(chords) { var smoothing = 1.01; var classified = new Map(); - console.log(labelProbabilities); + // console.log(labelProbabilities); labelProbabilities.forEach(function(_probabilities, difficulty) { var first = labelProbabilities.get(difficulty) + smoothing; chords.forEach(function(chord) { @@ -108,8 +108,14 @@ function classify(chords) { }); classified.set(difficulty, first); }); - console.log(classified); + + // console.log(classified); + return classified; } classify(["d", "g", "e", "dm"]); classify(["f#m7", "a", "dadd9", "dmaj7", "bm", "bm7", "d", "f#m"]); + +module.exports = { + classify +}; diff --git a/books/refactoring-javascript/ch07/nb.test.js b/books/refactoring-javascript/ch07/nb.test.js index 3ba0eb1..76506f5 100644 --- a/books/refactoring-javascript/ch07/nb.test.js +++ b/books/refactoring-javascript/ch07/nb.test.js @@ -1,7 +1,25 @@ +const { classify } = require("./nb"); var wish = require("wish"); describe("the file", function() { it("works", function() { with (true); }); + + it("classifies", function() { + var classified = classify([ + "f#m7", + "a", + "dadd9", + "dmaj7", + "bm", + "bm7", + "d", + "f#m" + ]); + // wish(classified.get("easy"), true); 로 특성화 테스트하여 값을 얻어봄 + wish(classified.get("easy") === 1.3433333333333333); + wish(classified.get("medium") === 1.5060259259259259); + wish(classified.get("hard") === 1.6884223991769547); + }); }); From aecc6397c19eeb6e1be7a2a0ac57f79587cc0345 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 15:31:18 +0900 Subject: [PATCH 14/28] add result test --- books/refactoring-javascript/ch07/nb.js | 3 ++- books/refactoring-javascript/ch07/nb.test.js | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 96fa26d..e5eca30 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -117,5 +117,6 @@ classify(["d", "g", "e", "dm"]); classify(["f#m7", "a", "dadd9", "dmaj7", "bm", "bm7", "d", "f#m"]); module.exports = { - classify + classify, + labelProbabilities }; diff --git a/books/refactoring-javascript/ch07/nb.test.js b/books/refactoring-javascript/ch07/nb.test.js index 76506f5..84607ca 100644 --- a/books/refactoring-javascript/ch07/nb.test.js +++ b/books/refactoring-javascript/ch07/nb.test.js @@ -1,4 +1,4 @@ -const { classify } = require("./nb"); +const { classify, labelProbabilities } = require("./nb"); var wish = require("wish"); describe("the file", function() { @@ -22,4 +22,10 @@ describe("the file", function() { wish(classified.get("medium") === 1.5060259259259259); wish(classified.get("hard") === 1.6884223991769547); }); + + it("label probabilities", function() { + wish(labelProbabilities.get("easy") === 0.3333333333333333); + wish(labelProbabilities.get("medium") === 0.3333333333333333); + wish(labelProbabilities.get("hard") === 0.3333333333333333); + }); }); From 0196b0c7717fd7b01a45a34115f2a9f4f8109817 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:05:43 +0900 Subject: [PATCH 15/28] extract functions --- books/refactoring-javascript/ch07/nb.js | 86 +++++++++++--------- books/refactoring-javascript/ch07/nb.test.js | 4 +- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index e5eca30..31f2dc3 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -2,29 +2,31 @@ var easy = "easy"; var medium = "medium"; var hard = "hard"; -imagine = ["c", "cmaj7", "f", "am", "dm", "g", "e7"]; -somewhereOverTheRainbow = ["c", "em", "f", "g", "am"]; -tooManyCooks = ["c", "g", "f"]; -iWillFollowYouIntoTheDark = ["f", "dm", "bb", "c", "a", "bbm"]; -babyOneMoreTime = ["cm", "g", "bb", "eb", "fm", "ab"]; -creep = ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"]; -paperBag = [ - "bm7", - "e", - "c", - "g", - "b7", - "f", - "em", - "a", - "cmaj7", - "em7", - "a7", - "f7", - "b" -]; -toxic = ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"]; -bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; +function setSongs() { + imagine = ["c", "cmaj7", "f", "am", "dm", "g", "e7"]; + somewhereOverTheRainbow = ["c", "em", "f", "g", "am"]; + tooManyCooks = ["c", "g", "f"]; + iWillFollowYouIntoTheDark = ["f", "dm", "bb", "c", "a", "bbm"]; + babyOneMoreTime = ["cm", "g", "bb", "eb", "fm", "ab"]; + creep = ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"]; + paperBag = [ + "bm7", + "e", + "c", + "g", + "b7", + "f", + "em", + "a", + "cmaj7", + "em7", + "a7", + "f7", + "b" + ]; + toxic = ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"]; + bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; +} var songs = []; var allChords = new Set(); @@ -78,20 +80,6 @@ function setProbabilityOfChordsInLabels() { }); } -train(imagine, easy); -train(somewhereOverTheRainbow, easy); -train(tooManyCooks, easy); -train(iWillFollowYouIntoTheDark, medium); -train(babyOneMoreTime, medium); -train(creep, medium); -train(paperBag, hard); -train(toxic, hard); -train(bulletproof, hard); - -setLabelProbabilities(); -setChordCountsInLabels(); -setProbabilityOfChordsInLabels(); - function classify(chords) { var smoothing = 1.01; var classified = new Map(); @@ -113,10 +101,28 @@ function classify(chords) { return classified; } -classify(["d", "g", "e", "dm"]); -classify(["f#m7", "a", "dadd9", "dmaj7", "bm", "bm7", "d", "f#m"]); +function trainAll() { + setSongs(); + train(imagine, easy); + train(somewhereOverTheRainbow, easy); + train(tooManyCooks, easy); + train(iWillFollowYouIntoTheDark, medium); + train(babyOneMoreTime, medium); + train(creep, medium); + train(paperBag, hard); + train(toxic, hard); + train(bulletproof, hard); + setLabelsAndProbabilities(); +} + +function setLabelsAndProbabilities() { + setLabelProbabilities(); + setChordCountsInLabels(); + setProbabilityOfChordsInLabels(); +} module.exports = { classify, - labelProbabilities + labelProbabilities, + trainAll }; diff --git a/books/refactoring-javascript/ch07/nb.test.js b/books/refactoring-javascript/ch07/nb.test.js index 84607ca..8b80613 100644 --- a/books/refactoring-javascript/ch07/nb.test.js +++ b/books/refactoring-javascript/ch07/nb.test.js @@ -1,7 +1,9 @@ -const { classify, labelProbabilities } = require("./nb"); +const { classify, labelProbabilities, trainAll } = require("./nb"); var wish = require("wish"); describe("the file", function() { + trainAll(); + it("works", function() { with (true); }); From 8c3cfe007ac40e6d4e747318679bef69bcafd0d4 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:09:48 +0900 Subject: [PATCH 16/28] extract global variables to function --- books/refactoring-javascript/ch07/nb.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 31f2dc3..87728c2 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -1,6 +1,8 @@ -var easy = "easy"; -var medium = "medium"; -var hard = "hard"; +function setDifficulties() { + easy = "easy"; + medium = "medium"; + hard = "hard"; +} function setSongs() { imagine = ["c", "cmaj7", "f", "am", "dm", "g", "e7"]; @@ -28,12 +30,14 @@ function setSongs() { bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; } -var songs = []; -var allChords = new Set(); -var labelCounts = new Map(); -var labelProbabilities = new Map(); -var chordCountsInLabels = new Map(); -var probabilityOfChordsInLabels = new Map(); +function setup() { + songs = []; + allChords = new Set(); + labelCounts = new Map(); + labelProbabilities = new Map(); + chordCountsInLabels = new Map(); + probabilityOfChordsInLabels = new Map(); +} function train(chords, label) { songs.push({ @@ -102,6 +106,8 @@ function classify(chords) { } function trainAll() { + setDifficulties(); + setup(); setSongs(); train(imagine, easy); train(somewhereOverTheRainbow, easy); From b996ff5fff17dce8dd89beadbcd8270cb1f48b0c Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:28:39 +0900 Subject: [PATCH 17/28] extract classifier object --- books/refactoring-javascript/ch07/nb.js | 74 +++++++++++++------------ 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 87728c2..3d8bd06 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -1,3 +1,14 @@ +var classifier = { + setup: function() { + this.songs = []; + this.allChords = new Set(); + this.labelCounts = new Map(); + this.labelProbabilities = new Map(); + this.chordCountsInLabels = new Map(); + this.probabilityOfChordsInLabels = new Map(); + } +}; + function setDifficulties() { easy = "easy"; medium = "medium"; @@ -30,68 +41,63 @@ function setSongs() { bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; } -function setup() { - songs = []; - allChords = new Set(); - labelCounts = new Map(); - labelProbabilities = new Map(); - chordCountsInLabels = new Map(); - probabilityOfChordsInLabels = new Map(); -} - function train(chords, label) { - songs.push({ + classifier.songs.push({ label, chords }); - chords.forEach(chord => allChords.add(chord)); - if (Array.from(labelCounts.keys()).includes(label)) { - labelCounts.set(label, labelCounts.get(label) + 1); + chords.forEach(chord => classifier.allChords.add(chord)); + if (Array.from(classifier.labelCounts.keys()).includes(label)) { + classifier.labelCounts.set(label, classifier.labelCounts.get(label) + 1); } else { - labelCounts.set(label, 1); + classifier.labelCounts.set(label, 1); } } function setLabelProbabilities() { - labelCounts.forEach(function(_count, label) { - labelProbabilities.set(label, labelCounts.get(label) / songs.length); + classifier.labelCounts.forEach(function(_count, label) { + classifier.labelProbabilities.set( + label, + classifier.labelCounts.get(label) / classifier.songs.length + ); }); } function setChordCountsInLabels() { - songs.forEach(function(song) { - if (chordCountsInLabels.get(song.label) === undefined) { - chordCountsInLabels.set(song.label, {}); + classifier.songs.forEach(function(song) { + if (classifier.chordCountsInLabels.get(song.label) === undefined) { + classifier.chordCountsInLabels.set(song.label, {}); } song.chords.forEach(function(chord) { - if (chordCountsInLabels.get(song.label)[chord] > 0) { - chordCountsInLabels.get(song.label)[chord] += 1; + if (classifier.chordCountsInLabels.get(song.label)[chord] > 0) { + classifier.chordCountsInLabels.get(song.label)[chord] += 1; } else { - chordCountsInLabels.get(song.label)[chord] = 1; + classifier.chordCountsInLabels.get(song.label)[chord] = 1; } }); }); } function setProbabilityOfChordsInLabels() { - probabilityOfChordsInLabels = chordCountsInLabels; - probabilityOfChordsInLabels.forEach(function(_chords, difficulty) { - Object.keys(probabilityOfChordsInLabels.get(difficulty)).forEach(function( - chord - ) { - probabilityOfChordsInLabels.get(difficulty)[chord] /= songs.length; - }); + classifier.probabilityOfChordsInLabels = classifier.chordCountsInLabels; + classifier.probabilityOfChordsInLabels.forEach(function(_chords, difficulty) { + Object.keys(classifier.probabilityOfChordsInLabels.get(difficulty)).forEach( + function(chord) { + classifier.probabilityOfChordsInLabels.get(difficulty)[chord] /= + classifier.songs.length; + } + ); }); } function classify(chords) { var smoothing = 1.01; var classified = new Map(); - // console.log(labelProbabilities); - labelProbabilities.forEach(function(_probabilities, difficulty) { - var first = labelProbabilities.get(difficulty) + smoothing; + // console.log(classifier.labelProbabilities); + classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { + var first = classifier.labelProbabilities.get(difficulty) + smoothing; chords.forEach(function(chord) { - var probabilityOfChordInLabel = probabilityOfChordsInLabels.get( + var probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( difficulty )[chord]; if (probabilityOfChordInLabel) { @@ -106,8 +112,8 @@ function classify(chords) { } function trainAll() { + classifier.setup(); setDifficulties(); - setup(); setSongs(); train(imagine, easy); train(somewhereOverTheRainbow, easy); From 08ab94df436c393d2f654314b8394127561522a5 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:31:03 +0900 Subject: [PATCH 18/28] inline setup() function --- books/refactoring-javascript/ch07/nb.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 3d8bd06..ab18b68 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -1,12 +1,10 @@ var classifier = { - setup: function() { - this.songs = []; - this.allChords = new Set(); - this.labelCounts = new Map(); - this.labelProbabilities = new Map(); - this.chordCountsInLabels = new Map(); - this.probabilityOfChordsInLabels = new Map(); - } + songs: [], + allChords: new Set(), + labelCounts: new Map(), + labelProbabilities: new Map(), + chordCountsInLabels: new Map(), + probabilityOfChordsInLabels: new Map() }; function setDifficulties() { @@ -112,7 +110,6 @@ function classify(chords) { } function trainAll() { - classifier.setup(); setDifficulties(); setSongs(); train(imagine, easy); From 28299ca62cc808515c58ad52ff0a11da56225bbd Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:36:16 +0900 Subject: [PATCH 19/28] make dataset useful --- books/refactoring-javascript/ch07/nb.js | 93 ++++++++++++++++--------- 1 file changed, 61 insertions(+), 32 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index ab18b68..6d85751 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -1,3 +1,14 @@ +var songList = { + songs: [], + addSong: function(name, chords, difficulty) { + this.songs.push({ + name, + chords, + difficulty + }); + } +}; + var classifier = { songs: [], allChords: new Set(), @@ -14,29 +25,53 @@ function setDifficulties() { } function setSongs() { - imagine = ["c", "cmaj7", "f", "am", "dm", "g", "e7"]; - somewhereOverTheRainbow = ["c", "em", "f", "g", "am"]; - tooManyCooks = ["c", "g", "f"]; - iWillFollowYouIntoTheDark = ["f", "dm", "bb", "c", "a", "bbm"]; - babyOneMoreTime = ["cm", "g", "bb", "eb", "fm", "ab"]; - creep = ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"]; - paperBag = [ - "bm7", - "e", - "c", - "g", - "b7", - "f", - "em", - "a", - "cmaj7", - "em7", - "a7", - "f7", - "b" - ]; - toxic = ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"]; - bulletproof = ["d#m", "g#", "b", "f#", "g#m", "c#"]; + songList.addSong("imagine", ["c", "cmaj7", "f", "am", "dm", "g", "e7"], easy); + songList.addSong( + "somewhereOverTheRainbow", + ["c", "em", "f", "g", "am"], + easy + ); + songList.addSong("tooManyCooks", ["c", "g", "f"], easy); + songList.addSong( + "iWillFollowYouIntoTheDark", + ["f", "dm", "bb", "c", "a", "bbm"], + medium + ); + songList.addSong( + "babyOneMoreTime", + ["cm", "g", "bb", "eb", "fm", "ab"], + medium + ); + songList.addSong( + "creep", + ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"], + medium + ); + songList.addSong( + "paperBag", + [ + "bm7", + "e", + "c", + "g", + "b7", + "f", + "em", + "a", + "cmaj7", + "em7", + "a7", + "f7", + "b" + ], + hard + ); + songList.addSong( + "toxic", + ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"], + hard + ); + songList.addSong("bulletproof", ["d#m", "g#", "b", "f#", "g#m", "c#"], hard); } function train(chords, label) { @@ -112,15 +147,9 @@ function classify(chords) { function trainAll() { setDifficulties(); setSongs(); - train(imagine, easy); - train(somewhereOverTheRainbow, easy); - train(tooManyCooks, easy); - train(iWillFollowYouIntoTheDark, medium); - train(babyOneMoreTime, medium); - train(creep, medium); - train(paperBag, hard); - train(toxic, hard); - train(bulletproof, hard); + songList.songs.forEach(function(song) { + train(song.chords, song.difficulty); + }); setLabelsAndProbabilities(); } From a2c42c4941c994e3b26b39e49d68f217ca2a3a90 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:40:32 +0900 Subject: [PATCH 20/28] remove all global variables --- books/refactoring-javascript/ch07/nb.js | 36 ++++++++----------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 6d85751..34a9ad5 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -1,10 +1,11 @@ var songList = { songs: [], + difficulties: ["easy", "medium", "hard"], addSong: function(name, chords, difficulty) { this.songs.push({ name, chords, - difficulty + difficulty: this.difficulties[difficulty] }); } }; @@ -18,34 +19,20 @@ var classifier = { probabilityOfChordsInLabels: new Map() }; -function setDifficulties() { - easy = "easy"; - medium = "medium"; - hard = "hard"; -} - function setSongs() { - songList.addSong("imagine", ["c", "cmaj7", "f", "am", "dm", "g", "e7"], easy); - songList.addSong( - "somewhereOverTheRainbow", - ["c", "em", "f", "g", "am"], - easy - ); - songList.addSong("tooManyCooks", ["c", "g", "f"], easy); + songList.addSong("imagine", ["c", "cmaj7", "f", "am", "dm", "g", "e7"], 0); + songList.addSong("somewhereOverTheRainbow", ["c", "em", "f", "g", "am"], 0); + songList.addSong("tooManyCooks", ["c", "g", "f"], 0); songList.addSong( "iWillFollowYouIntoTheDark", ["f", "dm", "bb", "c", "a", "bbm"], - medium - ); - songList.addSong( - "babyOneMoreTime", - ["cm", "g", "bb", "eb", "fm", "ab"], - medium + 1 ); + songList.addSong("babyOneMoreTime", ["cm", "g", "bb", "eb", "fm", "ab"], 1); songList.addSong( "creep", ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"], - medium + 1 ); songList.addSong( "paperBag", @@ -64,14 +51,14 @@ function setSongs() { "f7", "b" ], - hard + 2 ); songList.addSong( "toxic", ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"], - hard + 2 ); - songList.addSong("bulletproof", ["d#m", "g#", "b", "f#", "g#m", "c#"], hard); + songList.addSong("bulletproof", ["d#m", "g#", "b", "f#", "g#m", "c#"], 2); } function train(chords, label) { @@ -145,7 +132,6 @@ function classify(chords) { } function trainAll() { - setDifficulties(); setSongs(); songList.songs.forEach(function(song) { train(song.chords, song.difficulty); From 68d983698bd480541a6c4e133d2bde7da5ad350f Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:44:48 +0900 Subject: [PATCH 21/28] remove data related code --- books/refactoring-javascript/ch07/nb.js | 58 +++----------------- books/refactoring-javascript/ch07/nb.test.js | 41 +++++++++++++- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 34a9ad5..ec9492e 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -1,4 +1,4 @@ -var songList = { +const songList = { songs: [], difficulties: ["easy", "medium", "hard"], addSong: function(name, chords, difficulty) { @@ -10,7 +10,7 @@ var songList = { } }; -var classifier = { +const classifier = { songs: [], allChords: new Set(), labelCounts: new Map(), @@ -19,48 +19,6 @@ var classifier = { probabilityOfChordsInLabels: new Map() }; -function setSongs() { - songList.addSong("imagine", ["c", "cmaj7", "f", "am", "dm", "g", "e7"], 0); - songList.addSong("somewhereOverTheRainbow", ["c", "em", "f", "g", "am"], 0); - songList.addSong("tooManyCooks", ["c", "g", "f"], 0); - songList.addSong( - "iWillFollowYouIntoTheDark", - ["f", "dm", "bb", "c", "a", "bbm"], - 1 - ); - songList.addSong("babyOneMoreTime", ["cm", "g", "bb", "eb", "fm", "ab"], 1); - songList.addSong( - "creep", - ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"], - 1 - ); - songList.addSong( - "paperBag", - [ - "bm7", - "e", - "c", - "g", - "b7", - "f", - "em", - "a", - "cmaj7", - "em7", - "a7", - "f7", - "b" - ], - 2 - ); - songList.addSong( - "toxic", - ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"], - 2 - ); - songList.addSong("bulletproof", ["d#m", "g#", "b", "f#", "g#m", "c#"], 2); -} - function train(chords, label) { classifier.songs.push({ label, @@ -111,13 +69,13 @@ function setProbabilityOfChordsInLabels() { } function classify(chords) { - var smoothing = 1.01; - var classified = new Map(); + const smoothing = 1.01; + const classified = new Map(); // console.log(classifier.labelProbabilities); classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { - var first = classifier.labelProbabilities.get(difficulty) + smoothing; + let first = classifier.labelProbabilities.get(difficulty) + smoothing; chords.forEach(function(chord) { - var probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( + const probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( difficulty )[chord]; if (probabilityOfChordInLabel) { @@ -132,7 +90,6 @@ function classify(chords) { } function trainAll() { - setSongs(); songList.songs.forEach(function(song) { train(song.chords, song.difficulty); }); @@ -148,5 +105,6 @@ function setLabelsAndProbabilities() { module.exports = { classify, labelProbabilities, - trainAll + trainAll, + songList }; diff --git a/books/refactoring-javascript/ch07/nb.test.js b/books/refactoring-javascript/ch07/nb.test.js index 8b80613..aee2e83 100644 --- a/books/refactoring-javascript/ch07/nb.test.js +++ b/books/refactoring-javascript/ch07/nb.test.js @@ -1,7 +1,46 @@ -const { classify, labelProbabilities, trainAll } = require("./nb"); +const { classify, labelProbabilities, trainAll, songList } = require("./nb"); var wish = require("wish"); describe("the file", function() { + songList.addSong("imagine", ["c", "cmaj7", "f", "am", "dm", "g", "e7"], 0); + songList.addSong("somewhereOverTheRainbow", ["c", "em", "f", "g", "am"], 0); + songList.addSong("tooManyCooks", ["c", "g", "f"], 0); + songList.addSong( + "iWillFollowYouIntoTheDark", + ["f", "dm", "bb", "c", "a", "bbm"], + 1 + ); + songList.addSong("babyOneMoreTime", ["cm", "g", "bb", "eb", "fm", "ab"], 1); + songList.addSong( + "creep", + ["g", "gsus4", "b", "bsus4", "c", "cmsus4", "cm6"], + 1 + ); + songList.addSong( + "paperBag", + [ + "bm7", + "e", + "c", + "g", + "b7", + "f", + "em", + "a", + "cmaj7", + "em7", + "a7", + "f7", + "b" + ], + 2 + ); + songList.addSong( + "toxic", + ["cm", "eb", "g", "cdim", "eb7", "d7", "db7", "ab", "gmaj7", "g7"], + 2 + ); + songList.addSong("bulletproof", ["d#m", "g#", "b", "f#", "g#m", "c#"], 2); trainAll(); it("works", function() { From fb3f008ebac174b073bcc064b2ee9fe507ffd631 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 20:59:31 +0900 Subject: [PATCH 22/28] use another idea for result --- books/refactoring-javascript/ch07/nb.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index ec9492e..8ed63f4 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -73,16 +73,21 @@ function classify(chords) { const classified = new Map(); // console.log(classifier.labelProbabilities); classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { - let first = classifier.labelProbabilities.get(difficulty) + smoothing; + const likelihoods = [ + classifier.labelProbabilities.get(difficulty) + smoothing + ]; chords.forEach(function(chord) { const probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( difficulty )[chord]; if (probabilityOfChordInLabel) { - first = first * (probabilityOfChordInLabel + smoothing); + likelihoods.push(probabilityOfChordInLabel + smoothing); } }); - classified.set(difficulty, first); + const totalLikelihood = likelihoods.reduce(function(total, index) { + return total * index; + }); + classified.set(difficulty, totalLikelihood); }); // console.log(classified); From 5a41ce44fcc6e0217351ff35ab4cf24210fa1edf Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 21:03:32 +0900 Subject: [PATCH 23/28] use .reduce --- books/refactoring-javascript/ch07/nb.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 8ed63f4..2bd077f 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -73,20 +73,17 @@ function classify(chords) { const classified = new Map(); // console.log(classifier.labelProbabilities); classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { - const likelihoods = [ - classifier.labelProbabilities.get(difficulty) + smoothing - ]; - chords.forEach(function(chord) { + const totalLikelihood = chords.reduce(function(total, chord) { const probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( difficulty )[chord]; if (probabilityOfChordInLabel) { - likelihoods.push(probabilityOfChordInLabel + smoothing); + return total * (probabilityOfChordInLabel + smoothing); + } else { + return total; } - }); - const totalLikelihood = likelihoods.reduce(function(total, index) { - return total * index; - }); + }, classifier.labelProbabilities.get(difficulty) + smoothing); + classified.set(difficulty, totalLikelihood); }); From 9b486490edc9c3063c2eca6bdf746c754839810d Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 21:08:05 +0900 Subject: [PATCH 24/28] make as method of object --- books/refactoring-javascript/ch07/nb.js | 49 ++++++++++---------- books/refactoring-javascript/ch07/nb.test.js | 4 +- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 2bd077f..287ea87 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -16,7 +16,29 @@ const classifier = { labelCounts: new Map(), labelProbabilities: new Map(), chordCountsInLabels: new Map(), - probabilityOfChordsInLabels: new Map() + probabilityOfChordsInLabels: new Map(), + classify: function(chords) { + const smoothing = 1.01; + const classified = new Map(); + // console.log(classifier.labelProbabilities); + classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { + const totalLikelihood = chords.reduce(function(total, chord) { + const probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( + difficulty + )[chord]; + if (probabilityOfChordInLabel) { + return total * (probabilityOfChordInLabel + smoothing); + } else { + return total; + } + }, classifier.labelProbabilities.get(difficulty) + smoothing); + + classified.set(difficulty, totalLikelihood); + }); + + // console.log(classified); + return classified; + } }; function train(chords, label) { @@ -68,29 +90,6 @@ function setProbabilityOfChordsInLabels() { }); } -function classify(chords) { - const smoothing = 1.01; - const classified = new Map(); - // console.log(classifier.labelProbabilities); - classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { - const totalLikelihood = chords.reduce(function(total, chord) { - const probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( - difficulty - )[chord]; - if (probabilityOfChordInLabel) { - return total * (probabilityOfChordInLabel + smoothing); - } else { - return total; - } - }, classifier.labelProbabilities.get(difficulty) + smoothing); - - classified.set(difficulty, totalLikelihood); - }); - - // console.log(classified); - return classified; -} - function trainAll() { songList.songs.forEach(function(song) { train(song.chords, song.difficulty); @@ -105,7 +104,7 @@ function setLabelsAndProbabilities() { } module.exports = { - classify, + classifier: classifier, labelProbabilities, trainAll, songList diff --git a/books/refactoring-javascript/ch07/nb.test.js b/books/refactoring-javascript/ch07/nb.test.js index aee2e83..9b8e774 100644 --- a/books/refactoring-javascript/ch07/nb.test.js +++ b/books/refactoring-javascript/ch07/nb.test.js @@ -1,4 +1,4 @@ -const { classify, labelProbabilities, trainAll, songList } = require("./nb"); +const { classifier, labelProbabilities, trainAll, songList } = require("./nb"); var wish = require("wish"); describe("the file", function() { @@ -48,7 +48,7 @@ describe("the file", function() { }); it("classifies", function() { - var classified = classify([ + var classified = classifier.classify([ "f#m7", "a", "dadd9", From 9778db3960bd95891bf9bb8c5f988224cdb91f5d Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 21:19:33 +0900 Subject: [PATCH 25/28] bind this --- books/refactoring-javascript/ch07/nb.js | 31 ++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 287ea87..3b229e3 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -21,20 +21,25 @@ const classifier = { const smoothing = 1.01; const classified = new Map(); // console.log(classifier.labelProbabilities); - classifier.labelProbabilities.forEach(function(_probabilities, difficulty) { - const totalLikelihood = chords.reduce(function(total, chord) { - const probabilityOfChordInLabel = classifier.probabilityOfChordsInLabels.get( - difficulty - )[chord]; - if (probabilityOfChordInLabel) { - return total * (probabilityOfChordInLabel + smoothing); - } else { - return total; - } - }, classifier.labelProbabilities.get(difficulty) + smoothing); + this.labelProbabilities.forEach( + function(_probabilities, difficulty) { + const totalLikelihood = chords.reduce( + function(total, chord) { + const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( + difficulty + )[chord]; + if (probabilityOfChordInLabel) { + return total * (probabilityOfChordInLabel + smoothing); + } else { + return total; + } + }.bind(this), + this.labelProbabilities.get(difficulty) + smoothing + ); - classified.set(difficulty, totalLikelihood); - }); + classified.set(difficulty, totalLikelihood); + }.bind(this) + ); // console.log(classified); return classified; From dda180425f9cc23616268588192c6bc7349d89cb Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 21:21:54 +0900 Subject: [PATCH 26/28] use arrow function --- books/refactoring-javascript/ch07/nb.js | 33 +++++++++++-------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 3b229e3..6773b50 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -17,29 +17,24 @@ const classifier = { labelProbabilities: new Map(), chordCountsInLabels: new Map(), probabilityOfChordsInLabels: new Map(), + smoothing: 1.01, classify: function(chords) { - const smoothing = 1.01; const classified = new Map(); // console.log(classifier.labelProbabilities); - this.labelProbabilities.forEach( - function(_probabilities, difficulty) { - const totalLikelihood = chords.reduce( - function(total, chord) { - const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( - difficulty - )[chord]; - if (probabilityOfChordInLabel) { - return total * (probabilityOfChordInLabel + smoothing); - } else { - return total; - } - }.bind(this), - this.labelProbabilities.get(difficulty) + smoothing - ); + this.labelProbabilities.forEach((_probabilities, difficulty) => { + const totalLikelihood = chords.reduce((total, chord) => { + const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( + difficulty + )[chord]; + if (probabilityOfChordInLabel) { + return total * (probabilityOfChordInLabel + this.smoothing); + } else { + return total; + } + }, this.labelProbabilities.get(difficulty) + this.smoothing); - classified.set(difficulty, totalLikelihood); - }.bind(this) - ); + classified.set(difficulty, totalLikelihood); + }); // console.log(classified); return classified; From 90558982dd3a85e844d24c6a401a9ba0c0bef6ee Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 21:28:32 +0900 Subject: [PATCH 27/28] use Array#map() --- books/refactoring-javascript/ch07/nb.js | 35 +++++++++++++------------ 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 6773b50..5707387 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -19,25 +19,26 @@ const classifier = { probabilityOfChordsInLabels: new Map(), smoothing: 1.01, classify: function(chords) { - const classified = new Map(); // console.log(classifier.labelProbabilities); - this.labelProbabilities.forEach((_probabilities, difficulty) => { - const totalLikelihood = chords.reduce((total, chord) => { - const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( - difficulty - )[chord]; - if (probabilityOfChordInLabel) { - return total * (probabilityOfChordInLabel + this.smoothing); - } else { - return total; - } - }, this.labelProbabilities.get(difficulty) + this.smoothing); - - classified.set(difficulty, totalLikelihood); - }); + return new Map( + Array.from(this.labelProbabilities.entries()).map( + labelWithProbability => { + const difficulty = labelWithProbability[0]; + const totalLikelihood = chords.reduce((total, chord) => { + const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( + difficulty + )[chord]; + if (probabilityOfChordInLabel) { + return total * (probabilityOfChordInLabel + this.smoothing); + } else { + return total; + } + }, this.labelProbabilities.get(difficulty) + this.smoothing); - // console.log(classified); - return classified; + return [difficulty, totalLikelihood]; + } + ) + ); } }; From d724371f4e800f6a98240f5ec7f1874aff47e531 Mon Sep 17 00:00:00 2001 From: letsget23 Date: Sun, 18 Aug 2019 21:39:43 +0900 Subject: [PATCH 28/28] inline the code --- books/refactoring-javascript/ch07/nb.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/books/refactoring-javascript/ch07/nb.js b/books/refactoring-javascript/ch07/nb.js index 5707387..21c0278 100644 --- a/books/refactoring-javascript/ch07/nb.js +++ b/books/refactoring-javascript/ch07/nb.js @@ -24,18 +24,20 @@ const classifier = { Array.from(this.labelProbabilities.entries()).map( labelWithProbability => { const difficulty = labelWithProbability[0]; - const totalLikelihood = chords.reduce((total, chord) => { - const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( - difficulty - )[chord]; - if (probabilityOfChordInLabel) { - return total * (probabilityOfChordInLabel + this.smoothing); - } else { - return total; - } - }, this.labelProbabilities.get(difficulty) + this.smoothing); - return [difficulty, totalLikelihood]; + return [ + difficulty, + chords.reduce((total, chord) => { + const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get( + difficulty + )[chord]; + if (probabilityOfChordInLabel) { + return total * (probabilityOfChordInLabel + this.smoothing); + } else { + return total; + } + }, this.labelProbabilities.get(difficulty) + this.smoothing) + ]; } ) );