ES6ify /gr-storage/*
Bug: Issue 6179
Change-Id: I456468d84472328d0ceb7a2db480c631e8cbf95d
diff --git a/polygerrit-ui/app/elements/shared/gr-storage/gr-storage.js b/polygerrit-ui/app/elements/shared/gr-storage/gr-storage.js
index 77d1c05..2e7f17c 100644
--- a/polygerrit-ui/app/elements/shared/gr-storage/gr-storage.js
+++ b/polygerrit-ui/app/elements/shared/gr-storage/gr-storage.js
@@ -15,10 +15,10 @@
'use strict';
// Date cutoff is one day:
- var DRAFT_MAX_AGE = 24 * 60 * 60 * 1000;
+ const DRAFT_MAX_AGE = 24 * 60 * 60 * 1000;
// Clean up old entries no more frequently than one day.
- var CLEANUP_THROTTLE_INTERVAL = 24 * 60 * 60 * 1000;
+ const CLEANUP_THROTTLE_INTERVAL = 24 * 60 * 60 * 1000;
Polymer({
is: 'gr-storage',
@@ -27,7 +27,7 @@
_lastCleanup: Number,
_storage: {
type: Object,
- value: function() {
+ value() {
return window.localStorage;
},
},
@@ -37,42 +37,43 @@
},
},
- getDraftComment: function(location) {
+ getDraftComment(location) {
this._cleanupDrafts();
return this._getObject(this._getDraftKey(location));
},
- setDraftComment: function(location, message) {
- var key = this._getDraftKey(location);
- this._setObject(key, {message: message, updated: Date.now()});
+ setDraftComment(location, message) {
+ const key = this._getDraftKey(location);
+ this._setObject(key, {message, updated: Date.now()});
},
- eraseDraftComment: function(location) {
- var key = this._getDraftKey(location);
+ eraseDraftComment(location) {
+ const key = this._getDraftKey(location);
this._storage.removeItem(key);
},
- getPreferences: function() {
+ getPreferences() {
return this._getObject('localPrefs');
},
- savePreferences: function(localPrefs) {
+ savePreferences(localPrefs) {
this._setObject('localPrefs', localPrefs || null);
},
- _getDraftKey: function(location) {
- var range = location.range ? location.range.start_line + '-' +
- location.range.start_character + '-' + location.range.end_character +
- '-' + location.range.end_line : null;
- var key = ['draft', location.changeNum, location.patchNum, location.path,
- location.line || ''].join(':');
+ _getDraftKey(location) {
+ const range = location.range ?
+ `${location.range.start_line}-${location.range.start_character}` +
+ `-${location.range.end_character}-${location.range.end_line}` :
+ null;
+ let key = ['draft', location.changeNum, location.patchNum, location.path,
+ location.line || ''].join(':');
if (range) {
key = key + ':' + range;
}
return key;
},
- _cleanupDrafts: function() {
+ _cleanupDrafts() {
// Throttle cleanup to the throttle interval.
if (this._lastCleanup &&
Date.now() - this._lastCleanup < CLEANUP_THROTTLE_INTERVAL) {
@@ -80,9 +81,9 @@
}
this._lastCleanup = Date.now();
- var draft;
- for (var key in this._storage) {
- if (key.indexOf('draft:') === 0) {
+ let draft;
+ for (const key in this._storage) {
+ if (key.startsWith('draft:')) {
draft = this._getObject(key);
if (Date.now() - draft.updated > DRAFT_MAX_AGE) {
this._storage.removeItem(key);
@@ -91,13 +92,13 @@
}
},
- _getObject: function(key) {
- var serial = this._storage.getItem(key);
+ _getObject(key) {
+ const serial = this._storage.getItem(key);
if (!serial) { return null; }
return JSON.parse(serial);
},
- _setObject: function(key, obj) {
+ _setObject(key, obj) {
if (this._exceededQuota) { return; }
try {
this._storage.setItem(key, JSON.stringify(obj));
diff --git a/polygerrit-ui/app/elements/shared/gr-storage/gr-storage_test.html b/polygerrit-ui/app/elements/shared/gr-storage/gr-storage_test.html
index 6d77c55..4171939 100644
--- a/polygerrit-ui/app/elements/shared/gr-storage/gr-storage_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-storage/gr-storage_test.html
@@ -31,43 +31,44 @@
</test-fixture>
<script>
- suite('gr-storage tests', function() {
- var element;
+ suite('gr-storage tests', () => {
+ let element;
function mockStorage(opt_quotaExceeded) {
return {
- getItem: function(key) { return this[key]; },
- removeItem: function(key) { delete this[key]; },
- setItem: function(key, value) {
+ getItem(key) { return this[key]; },
+ removeItem(key) { delete this[key]; },
+ setItem(key, value) {
+ // eslint-disable-next-line no-throw-literal
if (opt_quotaExceeded) { throw {code: 22}; /* Quota exceeded */ }
this[key] = value;
},
};
}
- setup(function() {
+ setup(() => {
element = fixture('basic');
element._storage = mockStorage();
});
- test('storing, retrieving and erasing drafts', function() {
- var changeNum = 1234;
- var patchNum = 5;
- var path = 'my_source_file.js';
- var line = 123;
- var location = {
- changeNum: changeNum,
- patchNum: patchNum,
- path: path,
- line: line,
+ test('storing, retrieving and erasing drafts', () => {
+ const changeNum = 1234;
+ const patchNum = 5;
+ const path = 'my_source_file.js';
+ const line = 123;
+ const location = {
+ changeNum,
+ patchNum,
+ path,
+ line,
};
// The key is in the expected format.
- var key = element._getDraftKey(location);
+ const key = element._getDraftKey(location);
assert.equal(key, ['draft', changeNum, patchNum, path, line].join(':'));
// There should be no draft initially.
- var draft = element.getDraftComment(location);
+ const draft = element.getDraftComment(location);
assert.isNotOk(draft);
// Setting the draft stores it under the expected key.
@@ -82,24 +83,24 @@
assert.isNotOk(element._storage.getItem(key));
});
- test('automatically removes old drafts', function() {
- var changeNum = 1234;
- var patchNum = 5;
- var path = 'my_source_file.js';
- var line = 123;
- var location = {
- changeNum: changeNum,
- patchNum: patchNum,
- path: path,
- line: line,
+ test('automatically removes old drafts', () => {
+ const changeNum = 1234;
+ const patchNum = 5;
+ const path = 'my_source_file.js';
+ const line = 123;
+ const location = {
+ changeNum,
+ patchNum,
+ path,
+ line,
};
- var key = element._getDraftKey(location);
+ const key = element._getDraftKey(location);
// Make sure that the call to cleanup doesn't get throttled.
element._lastCleanup = 0;
- var cleanupSpy = sinon.spy(element, '_cleanupDrafts');
+ const cleanupSpy = sinon.spy(element, '_cleanupDrafts');
// Create a message with a timestamp that is a second behind the max age.
element._storage.setItem(key, JSON.stringify({
@@ -108,7 +109,7 @@
}));
// Getting the draft should cause it to be removed.
- var draft = element.getDraftComment(location);
+ const draft = element.getDraftComment(location);
assert.isTrue(cleanupSpy.called);
assert.isNotOk(draft);
@@ -117,18 +118,18 @@
cleanupSpy.restore();
});
- test('_getDraftKey', function() {
- var changeNum = 1234;
- var patchNum = 5;
- var path = 'my_source_file.js';
- var line = 123;
- var location = {
- changeNum: changeNum,
- patchNum: patchNum,
- path: path,
- line: line,
+ test('_getDraftKey', () => {
+ const changeNum = 1234;
+ const patchNum = 5;
+ const path = 'my_source_file.js';
+ const line = 123;
+ const location = {
+ changeNum,
+ patchNum,
+ path,
+ line,
};
- var expectedResult = 'draft:1234:5:my_source_file.js:123';
+ let expectedResult = 'draft:1234:5:my_source_file.js:123';
assert.equal(element._getDraftKey(location), expectedResult);
location.range = {
start_character: 1,
@@ -140,21 +141,21 @@
assert.equal(element._getDraftKey(location), expectedResult);
});
- test('exceeded quota disables storage', function() {
+ test('exceeded quota disables storage', () => {
element._storage = mockStorage(true);
assert.isFalse(element._exceededQuota);
- var changeNum = 1234;
- var patchNum = 5;
- var path = 'my_source_file.js';
- var line = 123;
- var location = {
- changeNum: changeNum,
- patchNum: patchNum,
- path: path,
- line: line,
+ const changeNum = 1234;
+ const patchNum = 5;
+ const path = 'my_source_file.js';
+ const line = 123;
+ const location = {
+ changeNum,
+ patchNum,
+ path,
+ line,
};
- var key = element._getDraftKey(location);
+ const key = element._getDraftKey(location);
element.setDraftComment(location, 'my comment');
assert.isTrue(element._exceededQuota);
assert.isNotOk(element._storage.getItem(key));