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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 'use strict';
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 "stability": "experimental"
michael@0 8 };
michael@0 9
michael@0 10 const { Class } = require('../core/heritage');
michael@0 11 const listNS = require('../core/namespace').ns();
michael@0 12 const { iteratorSymbol } = require('../util/iteration');
michael@0 13
michael@0 14 const listOptions = {
michael@0 15 /**
michael@0 16 * List constructor can take any number of element to populate itself.
michael@0 17 * @params {Object|String|Number} element
michael@0 18 * @example
michael@0 19 * List(1,2,3).length == 3 // true
michael@0 20 */
michael@0 21 initialize: function List() {
michael@0 22 listNS(this).keyValueMap = [];
michael@0 23
michael@0 24 for (let i = 0, ii = arguments.length; i < ii; i++)
michael@0 25 addListItem(this, arguments[i]);
michael@0 26 },
michael@0 27 /**
michael@0 28 * Number of elements in this list.
michael@0 29 * @type {Number}
michael@0 30 */
michael@0 31 get length() listNS(this).keyValueMap.length,
michael@0 32 /**
michael@0 33 * Returns a string representing this list.
michael@0 34 * @returns {String}
michael@0 35 */
michael@0 36 toString: function toString() 'List(' + listNS(this).keyValueMap + ')',
michael@0 37 /**
michael@0 38 * Custom iterator providing `List`s enumeration behavior.
michael@0 39 * We cant reuse `_iterator` that is defined by `Iterable` since it provides
michael@0 40 * iteration in an arbitrary order.
michael@0 41 * @see https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in
michael@0 42 * @param {Boolean} onKeys
michael@0 43 */
michael@0 44 __iterator__: function __iterator__(onKeys, onKeyValue) {
michael@0 45 let array = listNS(this).keyValueMap.slice(0),
michael@0 46 i = -1;
michael@0 47 for each(let element in array)
michael@0 48 yield onKeyValue ? [++i, element] : onKeys ? ++i : element;
michael@0 49 },
michael@0 50 };
michael@0 51 listOptions[iteratorSymbol] = function iterator() {
michael@0 52 return listNS(this).keyValueMap.slice(0)[iteratorSymbol]();
michael@0 53 };
michael@0 54 const List = Class(listOptions);
michael@0 55 exports.List = List;
michael@0 56
michael@0 57 function addListItem(that, value) {
michael@0 58 let list = listNS(that).keyValueMap,
michael@0 59 index = list.indexOf(value);
michael@0 60
michael@0 61 if (-1 === index) {
michael@0 62 try {
michael@0 63 that[that.length] = value;
michael@0 64 }
michael@0 65 catch (e) {}
michael@0 66 list.push(value);
michael@0 67 }
michael@0 68 }
michael@0 69 exports.addListItem = addListItem;
michael@0 70
michael@0 71 function removeListItem(that, element) {
michael@0 72 let list = listNS(that).keyValueMap,
michael@0 73 index = list.indexOf(element);
michael@0 74
michael@0 75 if (0 <= index) {
michael@0 76 list.splice(index, 1);
michael@0 77 try {
michael@0 78 for (let length = list.length; index < length; index++)
michael@0 79 that[index] = list[index];
michael@0 80 that[list.length] = undefined;
michael@0 81 }
michael@0 82 catch(e){}
michael@0 83 }
michael@0 84 }
michael@0 85 exports.removeListItem = removeListItem;
michael@0 86
michael@0 87 exports.listNS = listNS;

mercurial