0.1.0 • Published 8 years ago

wwl-js-filter-registry v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

wwl-node-filter-registry

The filter registry is a collection wrapper to white- and blacklist it's entries. By calling get and passing a key, the registry will filter it's entries based on the regular expressions listed in the whitelist and blacklist. Additionally, you can pass filter keys on top.

A whitelist/blacklist for one entry can have multiple regex statements.

Methods

methodreturnsdesc
register(id, data, filters = null, options = null)ampersand-stateRegister your data entry. id and data is required. Additionally you can pass a filters array and an options object. You can use options to pass meta information for later use.
unregister(id)nullRemove entry for id from registry.
whitelistEntry: (id, regex = null)nullWhitelist entry. If regex is null, /.*/ will be used.
blacklistEntry: (id, regex = null)nullBlacklist entry. If regex is null, /.*/ will be used.
resetEntryWhitelist: (id)nullReset whitelist.
resetEntryBlacklist: (id)nullReset blacklist.
get: (key, filters = null, useAnd = false)arrayampersand-stateReturns an array of key matching entries. Additionally the list can be filtered by the passed array of strings. (see isFilterIncluded)
isIncluded: (id, key, filters = [], useAnd = false)booleanFull passing check for one entry based on the passed key. Additionally you can extend the request by passing an array of filters. (see isFilterIncluded)
isWhitelisted: (id, key)booleanCheck if an entry is whitelisted based on the passed key.
isBlacklisted: (id, key)booleanCheck if an entry is blacklisted based on the passed key.
isFilterIncluded: (id, filters = [], useAnd = false)booleanCheck if an entry passes the filters array conditions. By default it will be true if one filters string matches (useAnd = false). If you set useAnd to true, the entry needs to have every of the passed filters in it's own filters list.

Examples

Listing

r = new (require('filter_registry'))()

# register objects
r.register 'test1', { my: 'data' }
r.register 'test2', { my: 'data' }
r.register 'test3', { my: 'data' }
r.register 'test4', { my: 'data' }
r.register 'test5', { my: 'data' }

# define their visibility

r.whitelistEntry('test1') # will always show up
r.blacklistEntry('test2') # will never show up

r.whitelistEntry('test3', /^some\/.*/)
r.whitelistEntry('test4', /.*other$/)

r.whitelistEntry('test5')
r.blacklistEntry('test5', /^some\/.*/)

# get data

r.get('xyz')          # test1, test2, test5
r.get('some')         # test1, test2, test5
r.get('some/other')   # test1, test2, test3, test4

Filtering

r = new (require('filter_registry'))()

# register objects
r.register 'test1', { my: 'data' }
r.register 'test2', { my: 'data' }, []
r.register 'test3', { my: 'data' }, ['a']
r.register 'test4', { my: 'data' }, ['b']
r.register 'test5', { my: 'data' }, ['a', 'b']

r.whitelistEntry('test1')
r.whitelistEntry('test2')
r.whitelistEntry('test3')
r.whitelistEntry('test4')
r.whitelistEntry('test5')

# Use filters
r.get('xyz')                    # 1, 2, 3, 4, 5
r.get('xyz', [])                # 1, 2, 3, 4, 5
r.get('xyz', ['a'])             # 3, 5
r.get('xyz', ['a'], false)      # 3, 5
r.get('xyz', ['a'], true)       # 3, 5
r.get('xyz', ['a', 'b'])        # 3, 5
r.get('xyz', ['a', 'b'], false) # 3, 5
r.get('xyz', ['a', 'b'], true)  # 5