genie

A JavaScript library committed to improving user experience by empowering users to interact with web apps using the keyboard (better than cryptic shortcuts).

http://www.github.com/kentcdodds/genie

Note: This documentation was generated with autodocs. Both autodocs and this documentation are still under development. Any issues you find about content can be assigned to genie issues any docs related issues can be assigned to autodocs issues.

addContext addContext

Adds the new context to genie's current context. Genie's context will maintain uniqueness, so don't worry about overloading genie's context with duplicates.

Signature

function addContext(newContext) { /*...*/ }
function addContext(newContext) {
  if (newContext && newContext.length) {
    _previousContext = _context;
    _addUniqueItems(_context, newContext);
  }
  return _context;
}
Name Type(s) Description
newContext string|Array.<string>

The context to add

returns Array

Genie's new context

addPathContext addPathContext

Add a path context to genie's pathContexts

Signature

function addPathContext(pathContexts) { /*...*/ }
function addPathContext(pathContexts) {
  _each(pathContexts, function(pathContext) {
    if (pathContext.paths) {
      pathContext.paths = _arrayify(pathContext.paths);
    }

    if (pathContext.regexes) {
      pathContext.regexes = _arrayify(pathContext.regexes);
    }

    if (pathContext.contexts) {
      pathContext.contexts = _arrayify(pathContext.contexts);
    }
  });
  _addUniqueItems(_pathContexts, pathContexts);
  return _pathContexts;
}
Name Type(s) Description
pathContexts Array.<PathContext>

The path context to add

returns Array.<PathContext>

The new path contexts

context context

Set's then returns genie's current context. If no context is provided, simply acts as getter. If a context is provided, genie's previous context is set to the context before it is assigned to the given context.

Signature

function context(newContext) { /*...*/ }
function context(newContext) {
  if (!_isUndefined(newContext)) {
    _previousContext = _context;
    if (!_isArray(newContext)) {
      newContext = [newContext];
    }
    _context = newContext;
  }
  return _context;
}
Name Type(s) Description
newContext string|Array.<string>

The context to set genie's context to.

returns Array.<string>

The new context

deregisterWish deregisterWish

Deregisters the given wish. Removes it from the registry and from the _enteredMagicWords map. This will delete an _enteredMagicWords listing if this is the only wish in the list.

Signature

function deregisterWish(wish) { /*...*/ }
function deregisterWish(wish) {
  var indexOfWish = _wishes.indexOf(wish);
  if (!indexOfWish) {
    _each(_wishes, function(aWish, index) {
      // the given parameter could be an id.
      if (wish === aWish.id || wish.id === aWish.id) {
        indexOfWish = index;
        wish = aWish;
        return false;
      }
    });
  }

  _wishes.splice(indexOfWish, 1);
  _removeWishIdFromEnteredMagicWords(wish.id);
  return wish;
}
Name Type(s) Description
wish object|string

The wish to deregister

returns wish

The deregistered wish

getWish getWish

Get a specific wish by an id. If the id is an array, returns an array of wishes with the same order as the given array. Note: If the id does not correspond to a registered wish, it will be undefined

Signature

function getWish(id) { /*...*/ }
function getWish(id) {
  if (_isArray(id)) {
    var wishes = [];
    _each(_getWishIndexById(id), function(index) {
      wishes.push(_wishes[index]);
    });
    return wishes;
  } else {
    var index = _getWishIndexById(id);
    if (index > -1) {
      return _wishes[index];
    } else {
      return null;
    }
  }
}
Name Type(s) Description
id wishIds

The id(s) to get wishes for

returns wish|Array.<wish>|null

The wish object(s)

getWishesInContext getWishesInContext

Get wishes in a specific context. If no context is provided, all wishes are returned. Think of this as, if genie were in the given context, what would be returned if I called genie.getMatchingWishes()?

Signature

function getWishesInContext(context) { /*...*/ }
function getWishesInContext(context) {
  context = context || _defaultContext;
  var wishesInContext = [];
  _each(_wishes, function(wish) {
    if (_contextIsDefault(context) ||
      _contextIsDefault(wish.context) ||
      _wishInThisContext(wish, context)) {
      wishesInContext.push(wish);
    }
  });
  return wishesInContext;
}
Name Type(s) Description
context string|Array.<string>

The context(s) to check wishes against.

returns Array.<wish>

The wish's which are in the given context.

mergeWishes mergeWishes

Merges the given wishes with genie's current wishes. Iterates through the wishes: If the wish does not have an action, and the wish's id is registered with genie, genie will assign the registered wish's action to the new wish's action property. Next, if the new wish has an action, it is registered with genie based on its wishId

Signature

function mergeWishes(wishes) { /*...*/ }
function mergeWishes(wishes) {
  _each(wishes, function(newWish) {
    var wishIndex = -1;
    var existingWish = null;
    _each(_wishes, function(aWish, aWishIndex) {
      if (aWish.id === newWish.id) {
        existingWish = aWish;
        wishIndex = aWishIndex;
        return false;
      }
    });
    if (!newWish.action && existingWish) {
      newWish.action = existingWish.action;
    }
    if (newWish.action) {
      _wishes[wishIndex] = newWish;
    }
  });
  return _wishes;
}
Name Type(s) Description
wishes Array.<wish>

Array of wish objects

returns Array.<wish>

All of genie's wishes

overrideMatchingAlgorithm overrideMatchingAlgorithm

This will override the matching algorithm (#getMatchingWishes) You wont need to change how you interface with #getMatchingWishes at all by using this.

Signature

function overrideMatchingAlgorithm(fn) { /*...*/ }
function overrideMatchingAlgorithm(fn) {
  genie.getMatchingWishes = _passThrough(function(magicWord) {
    return fn(_wishes, magicWord, _context, _enteredMagicWords);
  }, []);
}
Name Type(s) Description
fn *

{Function} The new function. Should accept wishes array, magicWord string, and enteredMagicWords object.

registerWish registerWish

Note: This is actually assigned to the genie variable, so it is called like so: genie({wish: object});

Creates and registers a new wish with the given pseudo wish(es).

Signature

function registerWish(wish) { /*...*/ }
function registerWish(wish) {
  if (_isArray(wish)) {
    var wishesRegistered = [];
    _each(wish, function(w) {
      wishesRegistered.push(registerWish(w));
    });
    return wishesRegistered;
  } else {
    var newWish = _createWish(wish);
    var existingWishIndex = _getWishIndexById(newWish.id);
    if (existingWishIndex < 0) {
      _wishes.push(newWish);
    } else {
      _wishes[existingWishIndex] = newWish;
    }

    return newWish;
  }
}
Name Type(s) Description
wish object|Array.<object>

pseudo wish(es)

returns wish|Array.<wish>

The registered wish or array of wishes.

removeContext removeContext

Removes the given context

Signature

function removeContext(contextToRemove) { /*...*/ }
function removeContext(contextToRemove) {
  if (contextToRemove && contextToRemove.length) {
    _previousContext = _context;
    _removeItems(_context, contextToRemove);
    if (_isEmpty(context)) {
      _context = _defaultContext;
    }
  }
  return _context;
}
Name Type(s) Description
contextToRemove string|Array.<string>

The context to remove

returns Array.<string>

Genie's new context

removePathContext removePathContext

Removes the given path contexts from genie's path contexts

Signature

function removePathContext(pathContext) { /*...*/ }
function removePathContext(pathContext) {
  _removeItems(_pathContexts, pathContext);
  return _pathContexts;
}
Name Type(s) Description
pathContext Array.<PathContext>

The path context object to remove. Must be equal to the object that was added previously. No support for ids etc.

returns Array.<PathContext>

Genie's new path context

reset reset

Sets genie's options to the default options

Signature

function reset() { /*...*/ }
function reset() {
  var oldOptions = options();
  options({
    wishes: [],
    noWishMerge: true,
    previousId: 0,
    enteredMagicWords: {},
    context: _defaultContext,
    previousContext: _defaultContext,
    enabled: true
  });
  return oldOptions;
}
Name Type(s) Description
returns GenieOptions

Genie's old options

restoreContext restoreContext

Changes context to _defaultContext

Signature

function restoreContext() { /*...*/ }
function restoreContext() {
  return context(_defaultContext);
}
Name Type(s) Description
returns Array.<string>

The new context

restoreMatchingAlgorithm restoreMatchingAlgorithm

This will set the matching algorithm back to the original

Signature

function restoreMatchingAlgorithm() { /*...*/ }
function restoreMatchingAlgorithm() {
  var oldMatchingAlgorithm = genie.getMatchingWishes;
  genie.getMatchingWishes = _originalMatchingAlgorithm;
  return oldMatchingAlgorithm;
}
Name Type(s) Description
returns Function

The old matching algorithm

revertContext revertContext

Changes genie's context to _previousContext

Signature

function revertContext() { /*...*/ }
function revertContext() {
  return context(_previousContext);
}
Name Type(s) Description
returns Array.<string>

The new context

context

The context of a wish

Name Type Description
any Array.<string>
all Array.<string>
none Array.<string>

wish

The wish object

Name Type Description
id string

Unique identifier for the wish.

context context

The context of the wish. Can be given as a string or array. In which case it is assigned to the wish's context.any property.

data {timesMade:{total:number, magicWords:{string:*

Any data you wish to associate with the wish. Genie adds a 'timesMade' property with total and magicWords properties to keep track of how often a wish is made with a given magicWord.

magicWords Array.<string>

Used to match this wish on genie.getMatchingWishes

action WishAction

The action to be performed when genie.makeWish is called with this wish.

wishIds

Wish Ids

Name Type Description

PathContext

A path context

Name Type Description
regexes Array.<RegExp>
paths Array.<string>
contexts Array.<string>

GenieOptions

Name Type Description
wishes Array.<wish>

All wishes registered with genie

previousId number

The number used to generate an id for a newly registered wish

enteredMagicWords object

An exploded object of letters to wishes and letters.

context Array.<string>

an array of all of genie's current contexts

previousContext Array.<string>

genie's most recent context

enabled boolean

whether genie is enabled

returnOnDisabled boolean

whether genie will return an empty object when it is disabled.