“MediaWiki:Gadget-LocalObjectStorage.js”的版本间的差异

来自萌娘文库
跳转至: 导航搜索
(同步小工具)
1个标签Bot
第1行: 第1行:
/* [[zh:MediaWiki:Gadget-LocalObjectStorage.js]] */
+
/* [[MediaWiki:Gadget-LocalObjectStorage.js/es6.js]] */
 
/* eslint-disable */
 
/* eslint-disable */
 
"use strict";
 
"use strict";

2022年3月28日 (一) 12:46的版本

/* [[MediaWiki:Gadget-LocalObjectStorage.js/es6.js]] */
/* eslint-disable */
"use strict";
(function () {
    var __spreadArray = function (to, from) {
        for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
            to[j] = from[i];
        return to;
    };
    var builtinTransformations = [
        {
            type: "undefined",
            match: function (t) { return typeof t === "undefined"; },
            encode: function () { return "undefined"; },
            decode: function () { return undefined; },
        },
        {
            type: "bigint",
            match: function (t) { return typeof t === "bigint"; },
            encode: function (b) { return "" + b; },
            decode: function (b) { return BigInt(b); },
        },
        {
            type: "date",
            match: function (t) { return t instanceof Date; },
            encode: function (d) { return d.toISOString(); },
            decode: function (d) { return new Date(d); },
        },
        {
            type: "set",
            match: function (t) { return t instanceof Set; },
            encode: function (s) { return JSON.stringify(__spreadArray([], s.values())); },
            decode: function (s) { return new Set(JSON.parse(s)); },
        },
        {
            type: "map",
            match: function (t) { return t instanceof Map; },
            encode: function (m) { return JSON.stringify(__spreadArray([], m.entries())); },
            decode: function (m) { return new Map(JSON.parse(m)); },
        },
        {
            type: "regexp",
            match: function (t) { return t instanceof RegExp; },
            encode: function (r) { return "" + r; },
            decode: function (r) { return new RegExp(r.slice(1, r.length - 1)); },
        },
    ];
    var LocalObjectStorage = /** @class */ (function () {
        function LocalObjectStorage(prefix) {
            if (prefix === void 0) { prefix = ""; }
            if (prefix === "default") {
                throw new Error("LocalObjectStorage can't accept prefix \"" + prefix + "\".");
            }
            if (prefix.includes("/")) {
                throw new Error("LocalObjectStorage can't accept prefix \"" + prefix + "\" including \"/\".");
            }
            this._keyPrefix = "AnnTool-localObjectStorage/" + ((prefix === null || prefix === void 0 ? void 0 : prefix.length) > 0 ? prefix + "/" : "");
        }
        LocalObjectStorage.prototype._getAllKeys = function () {
            var _this = this;
            return Object.keys(localStorage).filter(function (key) { return key.startsWith(_this._keyPrefix); });
        };
        Object.defineProperty(LocalObjectStorage.prototype, "length", {
            get: function () {
                return this._getAllKeys().length;
            },
            enumerable: false,
            configurable: true
        });
        LocalObjectStorage.prototype.getItem = function (key, fallback) {
            var value = localStorage.getItem("" + this._keyPrefix + key);
            if (value === null) {
                return fallback || value;
            }
            for (var _i = 0, _a = builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list); _i < _a.length; _i++) {
                var _b = _a[_i], type = _b.type, decode = _b.decode;
                if (type.includes("|")) {
                    console.error("LocalObjectStorage can't accept type name \"" + type + "\" including \"|\", skip...");
                    continue;
                }
                if (type === "JSON") {
                    console.error("LocalObjectStorage can't accept type name \"" + type + "\", skip...");
                    continue;
                }
                if (value.startsWith(type + "|")) {
                    try {
                        return decode(value.replace(type + "|", ""));
                    }
                    catch (e) {
                        console.error("LocalObjectStorage can's transform value of key \"" + key + "\" to type \"" + type + "\" and skip...");
                    }
                }
            }
            try {
                return JSON.parse(value.replace("JSON|", ""));
            }
            catch (e) {
                console.error("LocalObjectStorage can's transform value of key \"" + key + "\" to JSON and return raw value...");
                return value;
            }
        };
        LocalObjectStorage.prototype.setItem = function (key, value) {
            for (var _i = 0, _a = builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list); _i < _a.length; _i++) {
                var _b = _a[_i], type = _b.type, match = _b.match, encode = _b.encode;
                if (type.includes("|")) {
                    console.error("LocalObjectStorage can't accept type name \"" + type + "\" including \"|\", skip...");
                    continue;
                }
                if (type === "JSON") {
                    console.error("LocalObjectStorage can't accept type name \"" + type + "\", skip...");
                    continue;
                }
                if (match(value)) {
                    try {
                        localStorage.setItem("" + this._keyPrefix + key, type + "|" + encode(value));
                        return;
                    }
                    catch (e) {
                        console.error("LocalObjectStorage can's transform value of key \"" + key + "\" from type \"" + type + "\" and skip...");
                    }
                }
            }
            try {
                localStorage.setItem("" + this._keyPrefix + key, "JSON|" + JSON.stringify(value));
                return;
            }
            catch (e) {
                console.error("LocalObjectStorage can's transform value of key \"" + key + "\" from JSON and store raw value...");
                localStorage.setItem("" + this._keyPrefix + key, value);
            }
        };
        LocalObjectStorage.prototype.removeItem = function (key) {
            localStorage.removeItem("" + this._keyPrefix + key);
        };
        LocalObjectStorage.prototype.clear = function () {
            this._getAllKeys().forEach(function (key) {
                localStorage.removeItem(key);
            });
            this.length = 0;
        };
        LocalObjectStorage.prototype.key = function (index) {
            return this._getAllKeys()[index];
        };
        return LocalObjectStorage;
    }());
    var externalTransformations = [];
    LocalObjectStorage.plugins = {
        transformations: {
            get list() {
                return externalTransformations;
            },
            add: function (_a) {
                var type = _a.type, match = _a.match, decode = _a.decode, encode = _a.encode;
                if (type.includes("|")) {
                    console.error("LocalObjectStorage can't accept type name \"" + type + "\" including \"|\", skip...");
                    return false;
                }
                if (type === "JSON") {
                    console.error("LocalObjectStorage can't accept type name \"" + type + "\", skip...");
                    return false;
                }
                if (builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list).filter(function (_a) {
                    var eType = _a.type;
                    return eType === type;
                }).length > 0) {
                    console.error("LocalObjectStorage can't accept duplicated type name \"" + type + "\", skip...");
                    return false;
                }
                if (typeof match !== "function" || typeof decode !== "function" || typeof encode !== "function") {
                    console.error("LocalObjectStorage can't accept broken transformation [ type: \"" + type + "\", match: " + typeof match + ", decode: " + typeof decode + ", encode: " + typeof encode + " ], skip...");
                    return false;
                }
                externalTransformations.push({ type: type, match: match, decode: decode, encode: encode });
                return true;
            },
        },
    };
    window.LocalObjectStorage = LocalObjectStorage;
})();