Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/utils.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Patch Set: Addressed some more feedback Created Jan. 18, 2017, 11:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/url.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 var Utils = exports.Utils = { 18 "use strict";
19
20 let Utils = exports.Utils = {
19 systemPrincipal: null, 21 systemPrincipal: null,
20 getString: function(id) 22 getString(id)
21 { 23 {
22 return ext.i18n.getMessage("global_" + id); 24 return ext.i18n.getMessage("global_" + id);
23 }, 25 },
24 runAsync: function(callback) 26 runAsync(callback)
25 { 27 {
26 if (document.readyState == "loading") 28 if (document.readyState == "loading")
27 { 29 {
28 // Make sure to not run asynchronous actions before all 30 // Make sure to not run asynchronous actions before all
29 // scripts loaded. This caused issues on Opera in the past. 31 // scripts loaded. This caused issues on Opera in the past.
30 let onDOMContentLoaded = function() 32 let onDOMContentLoaded = () =>
31 { 33 {
32 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); 34 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
33 callback(); 35 callback();
34 }; 36 };
35 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); 37 document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
36 } 38 }
37 else 39 else
38 { 40 {
39 setTimeout(callback, 0); 41 setTimeout(callback, 0);
40 } 42 }
41 }, 43 },
42 get appLocale() 44 get appLocale()
43 { 45 {
44 var locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); 46 let locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
45 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); 47 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true});
46 return this.appLocale; 48 return this.appLocale;
47 }, 49 },
48 generateChecksum: function(lines) 50 generateChecksum(lines)
49 { 51 {
50 // We cannot calculate MD5 checksums yet :-( 52 // We cannot calculate MD5 checksums yet :-(
51 return null; 53 return null;
52 }, 54 },
53 checkLocalePrefixMatch: function(prefixes) 55 checkLocalePrefixMatch(prefixes)
54 { 56 {
55 if (!prefixes) 57 if (!prefixes)
56 return null; 58 return null;
57 59
58 var list = prefixes.split(","); 60 for (let prefix of prefixes.split(","))
59 for (var i = 0; i < list.length; i++) 61 if (new RegExp("^" + prefix + "\\b").test(this.appLocale))
60 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) 62 return prefix;
61 return list[i];
62 63
63 return null; 64 return null;
64 }, 65 },
65 66
66 chooseFilterSubscription: function(subscriptions) 67 chooseFilterSubscription(subscriptions)
67 { 68 {
68 var selectedItem = null; 69 let selectedItem = null;
69 var selectedPrefix = null; 70 let selectedPrefix = null;
70 var matchCount = 0; 71 let matchCount = 0;
71 for (var i = 0; i < subscriptions.length; i++) 72 for (let subscription of subscriptions)
72 { 73 {
73 var subscription = subscriptions[i];
74 if (!selectedItem) 74 if (!selectedItem)
75 selectedItem = subscription; 75 selectedItem = subscription;
76 76
77 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 77 let prefix = Utils.checkLocalePrefixMatch(subscription.getAttribute("prefi xes"));
78 if (prefix) 78 if (prefix)
79 { 79 {
80 if (!selectedPrefix || selectedPrefix.length < prefix.length) 80 if (!selectedPrefix || selectedPrefix.length < prefix.length)
81 { 81 {
82 selectedItem = subscription; 82 selectedItem = subscription;
83 selectedPrefix = prefix; 83 selectedPrefix = prefix;
84 matchCount = 1; 84 matchCount = 1;
85 } 85 }
86 else if (selectedPrefix && selectedPrefix.length == prefix.length) 86 else if (selectedPrefix && selectedPrefix.length == prefix.length)
87 { 87 {
88 matchCount++; 88 matchCount++;
89 89
90 // If multiple items have a matching prefix of the same length: 90 // If multiple items have a matching prefix of the same length:
91 // Select one of the items randomly, probability should be the same 91 // Select one of the items randomly, probability should be the same
92 // for all items. So we replace the previous match here with 92 // for all items. So we replace the previous match here with
93 // probability 1/N (N being the number of matches). 93 // probability 1/N (N being the number of matches).
94 if (Math.random() * matchCount < 1) 94 if (Math.random() * matchCount < 1)
95 { 95 {
96 selectedItem = subscription; 96 selectedItem = subscription;
97 selectedPrefix = prefix; 97 selectedPrefix = prefix;
98 } 98 }
99 } 99 }
100 } 100 }
101 } 101 }
102 return selectedItem; 102 return selectedItem;
103 }, 103 },
104 104
105 getDocLink: function(linkID) 105 getDocLink(linkID)
106 { 106 {
107 var Prefs = require("prefs").Prefs; 107 let docLink = require("prefs").Prefs.documentation_link;
108 var docLink = Prefs.documentation_link;
109 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); 108 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale );
110 }, 109 },
111 110
112 yield: function() 111 yield()
113 { 112 {
114 } 113 }
115 }; 114 };
OLDNEW
« no previous file with comments | « lib/url.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld