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: Define and destructure backgroundPage more consistently, fix minor whitespace errors Created Jan. 18, 2017, 7:34 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
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 // We cannot calculate MD5 checksums yet :-(
Sebastian Noack 2017/01/18 11:24:27 Moving the comment here seems unrelated.
kzar 2017/01/18 11:46:38 Whoops, well spotted. Done.
51 generateChecksum(lines)
49 { 52 {
50 // 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 = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes"));
Sebastian Noack 2017/01/18 11:24:27 Is require("utils") even necessary? Couldn't we ju
kzar 2017/01/18 11:46:37 Yes I can't see why not, Done.
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 const {Prefs} = require("prefs");
Sebastian Noack 2017/01/18 11:24:28 In this case, perhaps just inline it: let docLi
kzar 2017/01/18 11:46:38 Done.
108 var docLink = Prefs.documentation_link; 108 let docLink = Prefs.documentation_link;
109 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); 109 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale );
110 }, 110 },
111 111
112 yield: function() 112 yield()
113 { 113 {
114 } 114 }
115 }; 115 };
OLDNEW

Powered by Google App Engine
This is Rietveld