addon-sdk/source/lib/sdk/util/rules.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 const { Class } = require('../core/heritage');
michael@0 12 const { MatchPattern } = require('./match-pattern');
michael@0 13 const { on, off, emit } = require('../event/core');
michael@0 14 const { method } = require('../lang/functional');
michael@0 15 const objectUtil = require('./object');
michael@0 16 const { EventTarget } = require('../event/target');
michael@0 17 const { List, addListItem, removeListItem } = require('./list');
michael@0 18
michael@0 19 // Should deprecate usage of EventEmitter/compose
michael@0 20 const Rules = Class({
michael@0 21 implements: [
michael@0 22 EventTarget,
michael@0 23 List
michael@0 24 ],
michael@0 25 add: function(...rules) [].concat(rules).forEach(function onAdd(rule) {
michael@0 26 addListItem(this, rule);
michael@0 27 emit(this, 'add', rule);
michael@0 28 }, this),
michael@0 29 remove: function(...rules) [].concat(rules).forEach(function onRemove(rule) {
michael@0 30 removeListItem(this, rule);
michael@0 31 emit(this, 'remove', rule);
michael@0 32 }, this),
michael@0 33 get: function(rule) {
michael@0 34 let found = false;
michael@0 35 for (let i in this) if (this[i] === rule) found = true;
michael@0 36 return found;
michael@0 37 },
michael@0 38 // Returns true if uri matches atleast one stored rule
michael@0 39 matchesAny: function(uri) !!filterMatches(this, uri).length,
michael@0 40 toString: function() '[object Rules]'
michael@0 41 });
michael@0 42 exports.Rules = Rules;
michael@0 43
michael@0 44 function filterMatches(instance, uri) {
michael@0 45 let matches = [];
michael@0 46 for (let i in instance) {
michael@0 47 if (new MatchPattern(instance[i]).test(uri)) matches.push(instance[i]);
michael@0 48 }
michael@0 49 return matches;
michael@0 50 }

mercurial