gm-storage
Version:
An ES6 Map wrapper for the synchronous userscript storage API
37 lines (33 loc) • 1.04 kB
JavaScript
/* gm-storage 4.1.1. @copyright 2020 chocolateboy. @license MIT */
;
var gmStorageBase = require('./gm-storage-base.cjs');
const OPTIONS = { ...gmStorageBase.OPTIONS, canonical: true };
const toString = {}.toString;
const isPlainObject = (value) => {
return toString.call(value) === "[object Object]" && (value.constructor || Object) === Object;
};
const canonicalize = (_key, value) => {
if (isPlainObject(value)) {
const keys = Object.keys(value).sort();
const sorted = {};
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
sorted[key] = value[key];
}
return sorted;
} else {
return value;
}
};
const { parse } = JSON;
const stringify = (value) => JSON.stringify(value, canonicalize);
class JSONKeyStore extends gmStorageBase.GMStorageBase {
stringify;
constructor(options = OPTIONS) {
super(options);
this.stringify = options.canonical ? stringify : JSON.stringify;
}
parse = parse;
}
exports.JSONKeyStore = JSONKeyStore;
exports.stringify = stringify;