0.2.0 ā€¢ Published 6 years ago

sycamore.js v0.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

sycamore.js

šŸŒ² A decision tree based chat library

Usage

import Sycamore from 'sycamore.js'

// import the data
import data from './data.json'

// or

// create the data array
const data = [
	{
		id: 'intro',
		type: 'message',
		text: 'Hey!',
		next: 'compliment'
	},
	{
		id: 'fact',
		type: 'message',
		text: 'I am not a robot, I am a real human.',
		next: 'color'
	},
	{
		id: 'compliment',
		type: 'message',
		text: 'You look great today!',
		next: 'animal',
	},
	{
		id: 'animal',
		type: 'question',
		question: 'Which animal do you like more?',
		answers: [
			{
				text: 'Cat',
				next: 'age',
				callback: catCallback
			},
			{
				text: 'Dog',
				next: 'age'
			}
		]
	},
	{
		id: 'age',
		type: 'question',
		question: 'How old are you?',
		answers: [
			{
				text: 'Under 15',
				next: 'color'
			},
			{
				text: '15 - 30',
				next: 'color'
			},
			{
				text: '31 - 45',
				next: 'color'
			},
			{
				text: '46 - 60',
				next: 'color'
			},
			{
				text: 'Over 60',
				next: 'color'
			}
		]
	},
	{
		id: 'color',
		type: 'question',
		question: 'Which colour is your favourite?',
		answers: [
			{
				text: 'red'
			},
			{
				text: 'blue'
			},
			{
				text: 'green'
			},
			{
				text: 'yellow'
			},
			{
				text: 'purple'
			},
			{
				text: 'orange'
			}
		]
	}
]

const someFunction = () => {
	// this is the callback function for the answer 'Cat'
}

// create instance of sycamore with data
const sycamore = new Sycamore(data)

// initiate and start with the first question in the data
sycamore.init()

// or

// initiate and start with a specific question by it's ID
sycamore.init('age')

Events

// when Sycamore is emulating writing and sending the question
sycamore.on('typing', (wait) => {
	// passed the wait time in milliseconds
	
	// start a 'typing' animation for example
})

// sycamore emits a message to the user
sycamore.on('message', (obj) => {

})

// sycamore emits a question and will wait for the answer() method to be called in response
sycamore.on('question', (obj) => {
	// stop the 'typing' animation
	// do something with obj.question to display the question
	// iterate over and display each obj.answers.text
})

// when there is a delay between questions/messages this will fire
sycamore.on('delay', (delay) => {

})

// sycamore emits the 'answered' event when the answer() method is called
sycamore.on('answered', (qa) => {
	//qa obj contains the question and answer
})

// after each question is answered, sycamore will emit an updated event with the current collected data
sycamore.on('update', (data) => {

})

// once the tree reaches an end, the finished event will be emitted with the final collected data
sycamore.on('finished', (data) => {
	// can display or store the data array here
})

Methods

// send back the answer to the previous asked question
sycamore.answer(answer)

Options

OptionTypeDefaultDescription
speedint5A value between 1 - 10. 5 is an average typing speed. If number is below 1 or above 10 then it will be ignored and default will be used.
delayint0A millisecond value greater or equal to 0, which specifies the delay time after the answer() method is called before the next question is asked
delayMinMaxarrayAn array of 2 numbers only to determine the range of randomised delay after each answer() (ex. 1500, 3000)
firstQuestionstringSupply a question ID string to determine that is the first question
const options = {
	speed: 5, // Determine the speed of the typing, from 1 - 10
	delay: 1000, // Delay after question is answered before next is asked, millisecond >= 0
	delayMinMax: [1500, 3000], // Array containing only 2 numbers, first index must be lower than second
	firstQuestion: 'age' // The first question can be set in the options or passed as the first parameter to the init method
}

const sycamore = new Sycamore(data, options)
0.2.0

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

1.0.0

6 years ago