Map-like objects

Many features in the async-tree library and the Origami expression language built-in functions accept map-like arguments.

A map-like object is any of the following:

  1. A JavaScript Map object
  2. An AsyncMap object (an asynchronous variant of Map)
  3. An object with the same interface as Map or AsyncMap: it may be of a different class, but nevertheless has the same methods and properties
  4. A JavaScript Array instance
  5. A JavaScript function
  6. A JavaScript Iterator instance
  7. A JavaScript Set instance
  8. A plain JavaScript object created with object literal syntax, new Object(), or Object.create(null).

The easiest way for you to write a function that can accept any map-like object as a parameter is to pass that parameter to Tree.from(). The from() function considers the types above and, if the input matches one of the above types, returns a Map or AsyncMap object. If necessary, from() will wrap the input with a map class like ObjectMap.

async function manipulateTree(maplike) {
  const map = Tree.from(maplike);
  if (map) {
    /* `map` is a sync or async map */
  }
}

Note: the Tree.from method accepts any JavaScript object, but the helper function Tree.isMaplike returns false for an object that isn’t one of the types listed above.