js: Use ES6 object literal shorthand syntax.

Generated by ESLint.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-07-20 13:18:43 -07:00
parent b2745f6e41
commit 96dcc0ce6e
189 changed files with 1328 additions and 1326 deletions

View File

@@ -1,6 +1,6 @@
const ls = {
// parse JSON without throwing an error.
parseJSON: function (str) {
parseJSON(str) {
try {
return JSON.parse(str);
} catch (err) {
@@ -9,26 +9,26 @@ const ls = {
},
// check if the datestamp is from before now and if so return true.
isExpired: function (stamp) {
isExpired(stamp) {
return new Date(stamp) < new Date();
},
// return the localStorage key that is bound to a version of a key.
formGetter: function (version, name) {
formGetter(version, name) {
return "ls__" + version + "__" + name;
},
// create a formData object to put in the data, a signature that it was
// created with this library, and when it expires (if ever).
formData: function (data, expires) {
formData(data, expires) {
return {
data: data,
data,
__valid: true,
expires: new Date().getTime() + expires,
};
},
getData: function (version, name) {
getData(version, name) {
const key = this.formGetter(version, name);
let data = localStorage.getItem(key);
data = ls.parseJSON(data);
@@ -45,7 +45,7 @@ const ls = {
},
// set the wrapped version of the data into localStorage.
setData: function (version, name, data, expires) {
setData(version, name, data, expires) {
const key = this.formGetter(version, name);
const val = this.formData(data, expires);
@@ -53,14 +53,14 @@ const ls = {
},
// remove the key from localStorage and from memory.
removeData: function (version, name) {
removeData(version, name) {
const key = this.formGetter(version, name);
localStorage.removeItem(key);
},
// Remove keys which match a regex.
removeDataRegex: function (version, regex) {
removeDataRegex(version, regex) {
const key_regex = new RegExp(this.formGetter(version, regex));
const keys = Object.keys(localStorage).filter((key) => key_regex.test(key));
@@ -71,7 +71,7 @@ const ls = {
// migrate from an older version of a data src to a newer one with a
// specified callback function.
migrate: function (name, v1, v2, callback) {
migrate(name, v1, v2, callback) {
const old = this.getData(v1, name);
this.removeData(v1, name);
@@ -96,14 +96,14 @@ const localstorage = function () {
// `expires` should be a Number that represents the number of ms from
// now that this should expire in.
// this allows for it to either be set only once or permanently.
setExpiry: function (expires, isGlobal) {
setExpiry(expires, isGlobal) {
_data.expires = expires;
_data.expiresIsGlobal = isGlobal || false;
return this;
},
get: function (name) {
get(name) {
const data = ls.getData(_data.VERSION, name);
if (data) {
@@ -111,7 +111,7 @@ const localstorage = function () {
}
},
set: function (name, data) {
set(name, data) {
if (typeof _data.VERSION !== "undefined") {
ls.setData(_data.VERSION, name, data, _data.expires);
@@ -129,26 +129,26 @@ const localstorage = function () {
},
// remove a key with a given version.
remove: function (name) {
remove(name) {
ls.removeData(_data.VERSION, name);
},
// Remove keys which match the pattern given by name.
removeRegex: function (name) {
removeRegex(name) {
ls.removeDataRegex(_data.VERSION, name);
},
migrate: function (name, v1, v2, callback) {
migrate(name, v1, v2, callback) {
return ls.migrate(name, v1, v2, callback);
},
};
// set a new master version for the LocalStorage instance.
Object.defineProperty(prototype, "version", {
get: function () {
get() {
return _data.VERSION;
},
set: function (version) {
set(version) {
_data.VERSION = version;
},
});