@gov.nasa.jpl.honeycomb/tag-tracker
v0.0.6
Published
Class for adding, tracking, and querying objects with associated tags.
Readme
tag-tracker
Manager for associating tags with objects and quickly querying objects that match a set of tags or boolean operations with tags.
Use
import { TagTracker, compileExpression } from '@gov.nasa.jpl.honeycomb/tag-tracker';
// Add tags
const objA = {};
const objB = {};
tagTracker.addTag(objA, ['tag-a', 'tab-b']);
tagTracker.addTag(objB, ['tag-b', 'tab-c']);
// Get the objects with the given tags
tagTracker.getObjects('tag-b && !tag-c');
// or
const expr = compileExpression('tag-b && !tag-c');
tagTracker.getObjects(expr);API
Functions
compileExpression
compileExpression( expression : String ) : FunctionPrebuilds the expression evaluation function to use with the getObjects function. This can be faster than building the function on the fly.
TagTracker
Class for adding, tracking, and querying sets of tags on objects.
extends EventDispatcher
addTag
addTag( obj : Object, tag : String | Array<String> ) : BooleanAssociates a tag or set of tags with an object.
Returns true for a tag if it was added successfully or false if the tag was already associated with an object.
!> Tags cannot contain character that may be included in an expression or white, including !, &, <, >, |, =, or ~.
removeTag
removeTag( obj : Object, tag : String | Array<String> ) : BooleanRemoves a tag or st of tags from an object.
Returns true for a tag if it was removed successfully or false if the tag wasn't already associated with an object.
hasTag
hasTag( obj : Object, tag : String ) : BooleanReturns true if the object has the given tag.
filter
filter(
obj : Object | Array<Object>,
tagOrExp : String | Function,
flatten : Boolean = false
) : Array<Object>Filters the given tree of objects to a flattened list of objects if
flatten is true. obj is expected to have an array of children on
it if flatten is true.
Returns the filtered result.
getObjects
getObjects( tagOrExp : String | Function ) : Array<Object> | nullReturns the set of objects that matches the given tag or expression. An expression may be a string or pre-compoiled expression function. For example:
tracker.getObjects('tag-a && tag-b && !tag-c || tag-d');If the expression is null then all objects are returned.
getTags
getTags( obj : Object ) : Array<String>Returns the set of tags associated with an object. Returns null if there are no tags.
removeObject
removeObject( obj : Object ) : voidRemove an object an all associated tags from the tracker.
getAllTags
getAllTags( ) : Array<String>Returns an array of all currently tracked tag names
