Move telepath module to components

pull/6931/head
Matt Westcott 2021-01-14 18:43:20 +00:00
rodzic 7c53261d16
commit 19cfd7a423
2 zmienionych plików z 51 dodań i 48 usunięć

Wyświetl plik

@ -0,0 +1,50 @@
/* eslint-disable dot-notation */
class Telepath {
constructor() {
this.constructors = {};
}
register(name, constructor) {
this.constructors[name] = constructor;
}
unpackAsDict(objData) {
const result = [];
for (const [key, val] of Object.entries(objData)) {
result[key] = this.unpack(val);
}
return result;
}
unpack(objData) {
if (objData === null || typeof(objData) !== 'object') {
/* primitive value - return unchanged */
return objData;
}
if (Array.isArray(objData)) {
/* unpack recursively */
return objData.map(item => this.unpack(item));
}
/* objData is an object / dict - look for special key names _type and _dict */
if ('_type' in objData) {
/* handle as a custom type */
const constructorId = objData['_type'];
const constructor = this.constructors[constructorId];
/* unpack arguments recursively */
const args = objData['_args'].map(arg => this.unpack(arg));
return new constructor(...args);
}
if ('_dict' in objData) {
return this.unpackAsDict(objData['_dict']);
}
/* no special key names found, so unpack objData as a plain dict */
return this.unpackAsDict(objData);
}
}
export default Telepath;

Wyświetl plik

@ -1,50 +1,3 @@
/* eslint-disable dot-notation */
class Telepath {
constructor() {
this.constructors = {};
}
register(name, constructor) {
this.constructors[name] = constructor;
}
unpackAsDict(objData) {
const result = [];
for (const [key, val] of Object.entries(objData)) {
result[key] = this.unpack(val);
}
return result;
}
unpack(objData) {
if (objData === null || typeof(objData) !== 'object') {
/* primitive value - return unchanged */
return objData;
}
if (Array.isArray(objData)) {
/* unpack recursively */
return objData.map(item => this.unpack(item));
}
/* objData is an object / dict - look for special key names _type and _dict */
if ('_type' in objData) {
/* handle as a custom type */
const constructorId = objData['_type'];
const constructor = this.constructors[constructorId];
/* unpack arguments recursively */
const args = objData['_args'].map(arg => this.unpack(arg));
return new constructor(...args);
}
if ('_dict' in objData) {
return this.unpackAsDict(objData['_dict']);
}
/* no special key names found, so unpack objData as a plain dict */
return this.unpackAsDict(objData);
}
}
import Telepath from '../../../components/Telepath/Telepath';
window.telepath = new Telepath();