This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CombinatorGenerator = function (opts) { | |
function* combine(current, remainder) { | |
if (remainder.length === 0) { | |
if (current.length >= (opts.min || 0) && | |
current.length <= (opts.max || current.length)) { | |
yield(current); | |
} | |
} else { | |
combine(current.concat(remainder[0]), remainder.slice(1, remainder.length)) | |
combine(current, remainder.slice(1, remainder.length)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Combinator = function (opts) { | |
var combinations = []; | |
function combine(current, remainder) { | |
if (remainder.length === 0) { | |
if (current.length >= (opts.min || 0) && | |
current.length <= (opts.max || current.length)) | |
combinations.push(current); | |
} else { | |
combine(current.concat(remainder[0]), remainder.slice(1, remainder.length)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const choices = [ | |
[['VANILLA'], ['coconut flakes', 'pecans'], ['marshmallow']], | |
[['CHOCOLATE'], ['pecans'], ['chocolate']], | |
[['STRAWBERRY', 'VANILLA'], ['pineapple', 'coconut flakes'], ['marshmallow']], | |
[['STRAWBERRY'], ['pecans'], ['maple']], | |
[['VANILLA'], ['coconut flakes', 'pineapple'], ['chocolate']], | |
[['CHOCOLATE, STRAWBERRY'], ['pineapple', 'pecans'], ['butterscotch']], | |
]; | |
const keys = _.map(choices, c => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PouchDB = require('pouchdb'); | |
const db = new PouchDB('choices'); | |
// doc that defines the view | |
var ddoc = { | |
_id: '_design/choice', | |
views: { | |
by_key: { | |
map: function (doc) { | |
emit(doc.key, doc.taste); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash'); | |
const hash = require('./hash'); | |
const PouchDB = require('pouchdb'); | |
const db = new PouchDB('choices'); | |
db.allDocs({ | |
include_docs: true | |
}) | |
.then(docs => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const _ = require('lodash') | |
function hash(choice) { | |
var str = _.chain(choice) | |
.flatten() | |
.sortBy() | |
.join('|') | |
.value(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var PouchDB = require('pouchdb'); | |
var db = new PouchDB('choices'); | |
db.allDocs({ | |
include_docs: true | |
}) | |
.then(docs => { | |
_.each(docs.rows, r => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function run() { | |
var menu = { //... | |
} | |
var iceCreamChoices = new Combinator({ //... | |
}); | |
var toppingChoices = new Combinator({ //... | |
}); | |
var syrupChoices = new Combinator({ //... | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var count = 0; | |
_.each(iceCreamChoices, function(ic) { | |
_.each(toppingChoices, function(tp) { | |
_.each(syrupChoices, function(sy) { | |
//allChoices.push([ic,tp,sy]); | |
db.post({choice: [ic,tp,sy]}, function(err, doc){ | |
if (err) console.error(err); | |
else console.log(`stored ${++count}`); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var allChoices = []; | |
_.each(iceCreamChoices, function(ic) { | |
_.each(toppingChoices, function(tp) { | |
_.each(syrupChoices, function(sy) { | |
allChoices.push([ic,tp,sy]); | |
}) | |
}) | |
}) |