Home > bitTags

bitTags

BitTags is a project mainly written in JavaScript, it's free.

Bitmasking Javascript class that is used to associate objects with tags

h1. bitTags.js

Created by "Arlo Carreon":http://www.arlocarreon.com on 2011-03-16. MIT LIcense "arlo.mit-license.org":http://arlo.mit-license.org

h1. About

  • Bitmasking Javascript class that allows an API to associate objects with tags.
  • Useful for filtering down a big collection of objects using multiple filters.
  • Every set of Tags generates a unique ID, these IDs are assigned to your objects.
  • Use this class to compare your object's TagIDs with another set of tags.

Note: Tags are defined as an array of strings. e.g. ['tall','dark','handsome','male']

h2. Interface

  • getTagsID( tags[] ) RETURNS: the TagID (number) for a set of tags. TagIDs are assigned to your objects ** By asking for a TagID, each tag is entered into a global list.
  • matchAny( TagID | tags[], TagID | tags[] optional ) RETURNS: the number of similarities in both sets of tags matches any element of set a and b. the order of these parameters does not matter. you can pass two sets of tags, two sets of TagIDs or a mixture of both. ** IF you only pass 1 parameter, then that parameter is compared to the entire list.
  • matchAll( TagID | tags[], TagID | tags[] optional ) RETURNS: whether are not both tag sets are identical. the order of these parameters does not matter. you can pass two sets of tags, two sets of TagIDs or a mixture of both. IF you only pass 1 parameter, then that parameter is compared to the entire list.
  • matchNone( TagID | tags[], TagID | tags[] optional ) RETURNS: whether or not NONE of a match b. matches any element of set a and b. the order of these parameters does not matter. you can pass two sets of tags, two sets of TagIDs or a mixture of both. ** IF you only pass 1 parameter, then that parameter is compared to the entire list.

h1. Use Cases

// Initiate Class
var bt = new bitTags();

// Local Objects var people = { betty : new Person('betty'), justin : new Person('justin'), erick : new Person('erick'), arlo : new Person('arlo') }

// Assign tags people.betty.tags = bt.getTagsID(['tall','chinese','female']); people.justin.tags = bt.getTagsID(['short','vietnamese','male']); people.erick.tags = bt.getTagsID(['tall','dominican','male']); people.arlo.tags = bt.getTagsID(['tall','mexican','male']);

// Find Asians var asianList = ['japanese','chinese','vietnamese','korean','thai']; for(var key in people) { // Make our comparison if( bt.matchAny( asianList,person[key].tags ) ) alert(key+' is asian.'); } // *** Above outputs: betty is asian. justin is asian.

// Find Hispanics var hispList = ['mexican','cuban','puerto rican','dominican','argentinian']; for(var key in people) { // Make our comparison if( bt.matchAny( hispList,person[key].tags ) ) alert(key+' is hispanic.'); } // *** Above outputs: arlo is hispanic. erick is hispanic.

h1. Test Cases

// Create object
var bl = new bitTags();

// Register initial list bt.getTagsID(['a','b','c','d','e','f','g','h','i','j','k','l']);

// Test matchAny() bt.matchAny( ['a','b','c','d'], ['k','l','e','b','f','a'] ); //returns: 2 bt.matchAny( ['a','b','c','d'], ['k','c','e','b','f','a'] ); //returns: 3 bt.matchAny( ['a','b','c','d'], ['e','f','g','h','i','j'] ); //returns: 0 bt.matchAny( ['sunshine','b','cat','z'] ); //returns: 1

// Test matchAll() bt.matchAll( ['a','b','c','d'], ['a','b','c','d'] ); //returns: true bt.matchAll( ['a','b','c','d'], ['c','b','d','a'] ); //returns: true bt.matchAll( ['a','b','c','d'], ['a','e','c','d'] ); //returns: false bt.matchAll( ['a','b','c','d'], ['a','b','c','d','e'] ); //returns: false bt.matchAll( ['a','b','c','d'] ); //returns: false

// Test matchNone() bt.matchNone( ['a','b','c','d'], ['e','f','g','h'] ); //returns: true bt.matchNone( ['a','b','c','d'], ['a','b','c'] ); //returns: false bt.matchNone( ['a','b','c','d'], ['sunshine','b','cat','z'] ); //returns: false bt.matchNone( ['small','ball','3','$'] ); //returns: true

h1. License: "arlo.mit-license.org":http://arlo.mit-license.org

Previous:StaticLib