yomichan/ext/mixed/js/object-property-accessor.js

244 lines
8.8 KiB
JavaScript
Raw Normal View History

2020-03-08 22:24:35 +00:00
/*
* Copyright (C) 2016-2020 Yomichan Authors
2020-03-08 22:24:35 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Class used to get and set generic properties of an object by using path strings.
*/
class ObjectPropertyAccessor {
constructor(target, setter=null) {
this._target = target;
this._setter = (typeof setter === 'function' ? setter : null);
}
getProperty(pathArray, pathLength) {
let target = this._target;
const ii = typeof pathLength === 'number' ? Math.min(pathArray.length, pathLength) : pathArray.length;
for (let i = 0; i < ii; ++i) {
const key = pathArray[i];
if (!ObjectPropertyAccessor.hasProperty(target, key)) {
2020-03-13 22:17:29 +00:00
throw new Error(`Invalid path: ${ObjectPropertyAccessor.getPathString(pathArray.slice(0, i + 1))}`);
2020-03-08 22:24:35 +00:00
}
target = target[key];
}
return target;
}
setProperty(pathArray, value) {
if (pathArray.length === 0) {
throw new Error('Invalid path');
}
const target = this.getProperty(pathArray, pathArray.length - 1);
const key = pathArray[pathArray.length - 1];
if (!ObjectPropertyAccessor.isValidPropertyType(target, key)) {
2020-03-13 22:17:29 +00:00
throw new Error(`Invalid path: ${ObjectPropertyAccessor.getPathString(pathArray)}`);
2020-03-08 22:24:35 +00:00
}
if (this._setter !== null) {
this._setter(target, key, value, pathArray);
} else {
target[key] = value;
}
}
static getPathString(pathArray) {
const regexShort = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
let pathString = '';
let first = true;
for (let part of pathArray) {
switch (typeof part) {
case 'number':
if (Math.floor(part) !== part || part < 0) {
throw new Error('Invalid index');
}
part = `[${part}]`;
break;
case 'string':
if (!regexShort.test(part)) {
const escapedPart = part.replace(/["\\]/g, '\\$&');
part = `["${escapedPart}"]`;
} else {
if (!first) {
part = `.${part}`;
}
}
break;
default:
throw new Error(`Invalid type: ${typeof part}`);
}
pathString += part;
first = false;
}
return pathString;
}
static getPathArray(pathString) {
const pathArray = [];
2020-03-15 16:26:38 +00:00
let state = 'empty';
2020-03-08 22:24:35 +00:00
let quote = 0;
let value = '';
let escaped = false;
for (const c of pathString) {
const v = c.codePointAt(0);
switch (state) {
2020-03-15 16:26:38 +00:00
case 'empty': // Empty
case 'id-start': // Expecting identifier start
2020-03-08 22:24:35 +00:00
if (v === 0x5b) { // '['
2020-03-15 16:26:38 +00:00
if (state === 'id-start') {
2020-03-08 22:24:35 +00:00
throw new Error(`Unexpected character: ${c}`);
}
2020-03-15 16:26:38 +00:00
state = 'open-bracket';
2020-03-08 22:24:35 +00:00
} else if (
(v >= 0x41 && v <= 0x5a) || // ['A', 'Z']
(v >= 0x61 && v <= 0x7a) || // ['a', 'z']
v === 0x5f // '_'
) {
2020-03-15 16:26:38 +00:00
state = 'id';
2020-03-08 22:24:35 +00:00
value += c;
} else {
throw new Error(`Unexpected character: ${c}`);
}
break;
2020-03-15 16:26:38 +00:00
case 'id': // Identifier
2020-03-08 22:24:35 +00:00
if (
(v >= 0x41 && v <= 0x5a) || // ['A', 'Z']
(v >= 0x61 && v <= 0x7a) || // ['a', 'z']
(v >= 0x30 && v <= 0x39) || // ['0', '9']
v === 0x5f // '_'
) {
value += c;
} else if (v === 0x5b) { // '['
pathArray.push(value);
value = '';
2020-03-15 16:26:38 +00:00
state = 'open-bracket';
2020-03-08 22:24:35 +00:00
} else if (v === 0x2e) { // '.'
pathArray.push(value);
value = '';
2020-03-15 16:26:38 +00:00
state = 'id-start';
2020-03-08 22:24:35 +00:00
} else {
throw new Error(`Unexpected character: ${c}`);
}
break;
2020-03-15 16:26:38 +00:00
case 'open-bracket': // Open bracket
2020-03-08 22:24:35 +00:00
if (v === 0x22 || v === 0x27) { // '"' or '\''
quote = v;
2020-03-15 16:26:38 +00:00
state = 'string';
2020-03-08 22:24:35 +00:00
} else if (v >= 0x30 && v <= 0x39) { // ['0', '9']
2020-03-15 16:26:38 +00:00
state = 'number';
2020-03-08 22:24:35 +00:00
value += c;
} else {
throw new Error(`Unexpected character: ${c}`);
}
break;
2020-03-15 16:26:38 +00:00
case 'string': // Quoted string
2020-03-08 22:24:35 +00:00
if (escaped) {
value += c;
escaped = false;
} else if (v === 0x5c) { // '\\'
escaped = true;
} else if (v !== quote) {
value += c;
} else {
2020-03-15 16:26:38 +00:00
state = 'close-bracket';
2020-03-08 22:24:35 +00:00
}
break;
2020-03-15 16:26:38 +00:00
case 'number': // Number
2020-03-08 22:24:35 +00:00
if (v >= 0x30 && v <= 0x39) { // ['0', '9']
value += c;
} else if (v === 0x5d) { // ']'
pathArray.push(Number.parseInt(value, 10));
value = '';
2020-03-15 16:26:38 +00:00
state = 'next';
2020-03-08 22:24:35 +00:00
} else {
throw new Error(`Unexpected character: ${c}`);
}
break;
2020-03-15 16:26:38 +00:00
case 'close-bracket': // Expecting closing bracket after quoted string
2020-03-08 22:24:35 +00:00
if (v === 0x5d) { // ']'
pathArray.push(value);
value = '';
2020-03-15 16:26:38 +00:00
state = 'next';
2020-03-08 22:24:35 +00:00
} else {
throw new Error(`Unexpected character: ${c}`);
}
break;
2020-03-15 16:26:38 +00:00
case 'next': // Expecting . or [
2020-03-08 22:24:35 +00:00
if (v === 0x5b) { // '['
2020-03-15 16:26:38 +00:00
state = 'open-bracket';
2020-03-08 22:24:35 +00:00
} else if (v === 0x2e) { // '.'
2020-03-15 16:26:38 +00:00
state = 'id-start';
2020-03-08 22:24:35 +00:00
} else {
throw new Error(`Unexpected character: ${c}`);
}
break;
}
}
switch (state) {
2020-03-15 16:26:38 +00:00
case 'empty':
case 'next':
2020-03-08 22:24:35 +00:00
break;
2020-03-15 16:26:38 +00:00
case 'id':
2020-03-08 22:24:35 +00:00
pathArray.push(value);
value = '';
break;
default:
throw new Error('Path not terminated correctly');
}
return pathArray;
}
static hasProperty(object, property) {
switch (typeof property) {
case 'string':
return (
typeof object === 'object' &&
object !== null &&
!Array.isArray(object) &&
Object.prototype.hasOwnProperty.call(object, property)
);
case 'number':
return (
Array.isArray(object) &&
property >= 0 &&
property < object.length &&
property === Math.floor(property)
);
default:
return false;
}
}
static isValidPropertyType(object, property) {
switch (typeof property) {
case 'string':
return (
typeof object === 'object' &&
object !== null &&
!Array.isArray(object)
);
case 'number':
return (
Array.isArray(object) &&
property >= 0 &&
property === Math.floor(property)
);
default:
return false;
}
}
}