Skip to content

Instantly share code, notes, and snippets.

@whatAboutJohn
Last active April 7, 2017 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatAboutJohn/c0acaa0634233d5ba9229edbb6defb57 to your computer and use it in GitHub Desktop.
Save whatAboutJohn/c0acaa0634233d5ba9229edbb6defb57 to your computer and use it in GitHub Desktop.
Script to map questions to answers on surveys.
/**
* @author John Molina <jmolina@pixnabilab.com>
* Creates a Survey class that gathers data based on chosen answers and returns
* a key object that will be mapped against a dictionary of answers.
* @class
* @param {Object} answers
*/
class Survey extends EventEmitter {
constructor(answers) {
super()
this.scope = this
this.answers = answers
/**
* Traversing question tree. Used to indicate which questions to ask
* @constant
* @type {Array}
*/
this.questionSteps = []
this.questions = {}
/**
* Tracks current question through setQuestion event
* @type {object}
*/
this.q = null
this.counter = 0
this.answer = null
this.on('setQuestion', this.setQuestion)
this.on('setAnswer', this.setAnswer)
}
setQuestion() {
if (!Object.is(this.q, this.questions[this.counter]))
this.q = this.questions[this.counter]
}
/**
* Set new answer
* @param {String} answer
*/
setAnswer(answer) {
this.answer = this.answers[answer]
}
/**
* Set new question
* @param {Number|String} key
*/
addQuestion(key) {
if (isFinite(key))
this.questions[key] = { value: null, collection: [] }
else
throw new Error('Wrong key assignment for question')
this.emit('setQuestion')
}
get handlers() {
const scope = this
return {
/**
* Update collection values, such as checkboxes
* @param {Number} i Index value of the current value in the collection
* @param {Number} v Add/Subtract current collection question value
* @return {Number} Updated question value
*/
multiples() {
const { q } = scope
try {
let cLength = q.collection.length
let cIntegers = q.collection.filter(Number.isInteger)
let total = cIntegers.reduce((v1, v2) => v1 + v2)
if (cIntegers.length > 1) ++total
q.value = total
} catch(e) {
q.value = 0
}
scope.emit('setQuestion')
return q
},
/**
* Update current question value
* @param {Number} v New value
* @return {Number} New assigned value to the current question
*/
update(v) {
scope.emit('setQuestion')
scope.q.value = parseInt(v)
return scope.q
}
}
}
/**
* Returns all questions objects as enum
* @return {Array.<questions>}
*/
*[Symbol.iterator]() {
for (let k of Object.keys(this.questions)) {
yield this.questions[k]
}
}
/**
* Collect all question values and return in dictionary format
* @return {String}
*/
constructAnswer() {
const answer = [...this].map(({ value }) => value - 1).join('-')
return this.emit('setAnswer', answer)
}
}
const answers = [
'First Answer',
'Second Answer',
'Third Answer',
'Fourth Answer'
]
/* Create delegations */
const delegations = {
/* Internet */
'0-0-0': answer[0],
'1-0-0': answer[1],
'0-1-0': answer[2],
'0-0-1': answer[2],
'1-1-0': answer[3]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment