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: "use strict"; Created Jan. 16, 2017, 3:30 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 => ext.i18n.getMessage("global_" + id),
21 { 23 runAsync: callback =>
22 return ext.i18n.getMessage("global_" + id);
23 },
24 runAsync: function(callback)
25 { 24 {
26 if (document.readyState == "loading") 25 if (document.readyState == "loading")
27 { 26 {
28 // Make sure to not run asynchronous actions before all 27 // Make sure to not run asynchronous actions before all
29 // scripts loaded. This caused issues on Opera in the past. 28 // scripts loaded. This caused issues on Opera in the past.
30 let onDOMContentLoaded = function() 29 let onDOMContentLoaded = () =>
31 { 30 {
32 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); 31 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
33 callback(); 32 callback();
34 }; 33 };
35 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); 34 document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
36 } 35 }
37 else 36 else
38 { 37 {
39 setTimeout(callback, 0); 38 setTimeout(callback, 0);
40 } 39 }
41 }, 40 },
42 get appLocale() 41 get appLocale()
43 { 42 {
44 var locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); 43 let locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
45 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); 44 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true});
46 return this.appLocale; 45 return this.appLocale;
47 }, 46 },
48 generateChecksum: function(lines) 47 // We cannot calculate MD5 checksums yet :-(
49 { 48 generateChecksum: lines => null,
50 // We cannot calculate MD5 checksums yet :-(
51 return null;
52 },
53 checkLocalePrefixMatch: function(prefixes) 49 checkLocalePrefixMatch: function(prefixes)
54 { 50 {
55 if (!prefixes) 51 if (!prefixes)
56 return null; 52 return null;
57 53
58 var list = prefixes.split(","); 54 for (let prefix of prefixes.split(","))
59 for (var i = 0; i < list.length; i++) 55 if (new RegExp("^" + prefix + "\\b").test(this.appLocale))
60 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) 56 return prefix;
61 return list[i];
62 57
63 return null; 58 return null;
64 }, 59 },
65 60
66 chooseFilterSubscription: function(subscriptions) 61 chooseFilterSubscription: subscriptions =>
67 { 62 {
68 var selectedItem = null; 63 let selectedItem = null;
69 var selectedPrefix = null; 64 let selectedPrefix = null;
70 var matchCount = 0; 65 let matchCount = 0;
71 for (var i = 0; i < subscriptions.length; i++) 66 for (let subscription of subscriptions)
72 { 67 {
73 var subscription = subscriptions[i];
74 if (!selectedItem) 68 if (!selectedItem)
75 selectedItem = subscription; 69 selectedItem = subscription;
76 70
77 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 71 let prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes"));
78 if (prefix) 72 if (prefix)
79 { 73 {
80 if (!selectedPrefix || selectedPrefix.length < prefix.length) 74 if (!selectedPrefix || selectedPrefix.length < prefix.length)
81 { 75 {
82 selectedItem = subscription; 76 selectedItem = subscription;
83 selectedPrefix = prefix; 77 selectedPrefix = prefix;
84 matchCount = 1; 78 matchCount = 1;
85 } 79 }
86 else if (selectedPrefix && selectedPrefix.length == prefix.length) 80 else if (selectedPrefix && selectedPrefix.length == prefix.length)
87 { 81 {
88 matchCount++; 82 matchCount++;
89 83
90 // If multiple items have a matching prefix of the same length: 84 // If multiple items have a matching prefix of the same length:
91 // Select one of the items randomly, probability should be the same 85 // Select one of the items randomly, probability should be the same
92 // for all items. So we replace the previous match here with 86 // for all items. So we replace the previous match here with
93 // probability 1/N (N being the number of matches). 87 // probability 1/N (N being the number of matches).
94 if (Math.random() * matchCount < 1) 88 if (Math.random() * matchCount < 1)
95 { 89 {
96 selectedItem = subscription; 90 selectedItem = subscription;
97 selectedPrefix = prefix; 91 selectedPrefix = prefix;
98 } 92 }
99 } 93 }
100 } 94 }
101 } 95 }
102 return selectedItem; 96 return selectedItem;
103 }, 97 },
104 98
105 getDocLink: function(linkID) 99 getDocLink: linkID =>
106 { 100 {
107 var Prefs = require("prefs").Prefs; 101 let Prefs = require("prefs").Prefs;
108 var docLink = Prefs.documentation_link; 102 let docLink = Prefs.documentation_link;
109 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); 103 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale );
110 }, 104 },
111 105
112 yield: function() 106 yield: () =>
113 { 107 {
114 } 108 }
115 }; 109 };
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