text-commander

2.0.0 • Public • Published

text-commander

tiny library for text-based interactions (e.g. MUDs, text-based adventure games, etc)

install

npm install text-commander

use

var cmdr = require('text-commander')([
  {
    '/look at {thing}': (obj) => {
      return `looking at ${obj.thing}!`
    }
  },
  {
    '/use {thing} on {otherThing}': (obj) => {
      return `using ${obj.thing} on ${obj.otherThing}!`
    }
  }
])
 
console.log(cmdr('/look at sun'))
// looking at sun!
console.log(cmdr('/use glove on lever'))
// using glove on lever!

(see templateer, on which this is built)

protip you can use es6 destructuring syntax to make your functions a little nicer. e.g.,

var cmdr = require('text-commander')([
  {'link {thing} with {otherThing}': ({thing, otherThing}) => {
    // do stuff...
    // return stuff...
  }}
])

api

var cmdr = require('text-commander')([ {'some {template}': ({template}) => { }}, ... ])

text-commander takes a list of objects { templateString: function }. when templateString is matched, function will execute on the the template match. (see templateer, on which this is built)

cmdr('some command')

will try to match each template string, in order, executing the appropriate function if a match is found.

cmdr will pass the match object as arguments to the appropriate function, and return whatever that function returns.

if no match is found, cmdr will return undefined.

license

BSD

Package Sidebar

Install

npm i text-commander

Weekly Downloads

2

Version

2.0.0

License

BSD-3-Clause

Last publish

Collaborators

  • elsehow