Skip to content

Instantly share code, notes, and snippets.

@johnrees
Last active October 7, 2020 14:24
Show Gist options
  • Save johnrees/07dba663862e34384ef6686f8057fe84 to your computer and use it in GitHub Desktop.
Save johnrees/07dba663862e34384ef6686f8057fe84 to your computer and use it in GitHub Desktop.
+------------------+
| what are your |
| favourite foods? |
+------------------+
| |
+--------+ +-----------+
| Fruit | | Vegetable |
+--------+ +-----------+
| | |
+-------+ +--------+ +----------+
| Apple | | Banana | | Broccoli |
+-------+ +--------+ +----------+
// 1. separate category nodes
{
nodes: {
chk1: {
$t: TYPES.Checklist,
text: "What are your favourite foods?"
},
cat1: {
$t: TYPES.ChecklistCategory,
text: "Fruit"
},
cat2: {
$t: TYPES.ChecklistCategory,
text: "Vegetable"
},
res1: {
$t: TYPES.Response,
text: "Apple"
},
res2: {
$t: TYPES.Response,
text: "Banana"
},
res3: {
$t: TYPES.Response,
text: "Broccoli"
}
},
edges: [
[null, chk1],
[chk1, cat1],
[chk1, cat2],
[cat1, res1],
[cat1, res2],
[cat2, res3],
]
}
// 2. store category values inside responses
{
nodes: {
chk1: {
$t: TYPES.Checklist,
text: "What are your favourite foods?"
},
res1: {
$t: TYPES.Response,
text: "Apple",
category: "Fruit"
},
res2: {
$t: TYPES.Response,
text: "Banana",
category: "Fruit"
},
res3: {
$t: TYPES.Response,
text: "Broccoli",
category: "Vegetable"
}
},
edges: [
[null, chk1],
[chk1, res1],
[chk1, res2],
[chk1, res3],
]
}
// 3. store category index inside responses
{
nodes: {
chk1: {
$t: TYPES.Checklist,
text: "What are your favourite foods?",
categories: ["Fruit", "Vegetable"]
},
res1: {
$t: TYPES.Response,
text: "Apple",
category: 0
},
res2: {
$t: TYPES.Response,
text: "Banana",
category: 0
},
res3: {
$t: TYPES.Response,
text: "Broccoli",
category: 1
}
},
edges: [
[null, chk1],
[chk1, res1],
[chk1, res2],
[chk1, res3],
]
}
// 4. Store categories and the count/number of responses they apply to
// in an object (keys have guaranteed order so it can be iterated)
// there are 2 fruits and 1 vegetable, so Fruit=2, Vegetable=1
{
nodes: {
chk1: {
$t: TYPES.Checklist,
text: "What are your favourite foods?",
categories: {
"Fruit": 2,
"Vegetable": 1,
}
},
res1: {
$t: TYPES.Response,
text: "Apple"
},
res2: {
$t: TYPES.Response,
text: "Banana"
},
res3: {
$t: TYPES.Response,
text: "Broccoli"
}
},
edges: [
[null, chk1],
[chk1, res1],
[chk1, res2],
[chk1, res3],
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment