UNPKG

diffusion

Version:

Diffusion JavaScript client

75 lines (66 loc) 2.19 kB
var util = require('topics/topic-path-utils'); /** * A {@link TopicSelector} is a value that identifies one or more topics. * @param {TopicSelector.Type} type - The type of this selector. * @param {String} prefix - The maximum topic path prefix from this selector. * @param {String} expression - The original expression of this selector. * @class TopicSelector */ function TopicSelector(type, prefix, expression) { /* * The type of this selector. */ this.type = type; /* * The maximum topic path prefix from this selector. */ this.prefix = prefix; /* * The original expression of this selector. */ this.expression = expression; } /** * @readonly * @enum {String} */ TopicSelector.Prefix = { /** Prefix used for {@link TopicSelector.Type.PATH} expressions. */ PATH : '>', /** Prefix used for {@link TopicSelector.Type.SPLIT_PATH_PATTERN} expressions. */ SPLIT_PATH_PATTERN : '?', /** Prefix used for {@link TopicSelector.Type.FULL_PATH_PATTERN} expressions. */ FULL_PATH_PATTERN : '*', /** Prefix used for {@link TopicSelector.Type.SELECTOR_SET} expressions. */ SELECTOR_SET : '#' }; /** * Topic Selector type. * * @readonly * @enum {TopicSelector.Prefix} */ TopicSelector.Type = { /** A selector that selects a single topic. */ PATH : TopicSelector.Prefix.PATH, /** A selector that is a split-path pattern. */ SPLIT_PATH_PATTERN : TopicSelector.Prefix.SPLIT_PATH_PATTERN, /** A selector that is a full-path pattern. */ FULL_PATH_PATTERN : TopicSelector.Prefix.FULL_PATH_PATTERN, /** A composite of multiple selectors. */ SELECTOR_SET : TopicSelector.Prefix.SELECTOR_SET }; /** * Evaluate this selector against a topic path. * * @param {String} topicPath - The topic path * @returns {Boolean} If this selector selects the topic path */ TopicSelector.prototype.selects = function(topicPath) { var canonical = util.canonicalise(topicPath); return canonical.indexOf(this.prefix) === 0 && this.doSelects(canonical); }; TopicSelector.prototype.toString = function() { return this.expression; }; module.exports = TopicSelector;