From 19cfd7a4230121db61fdbda09430fc432feb5ec7 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 14 Jan 2021 18:43:20 +0000 Subject: [PATCH] Move telepath module to components --- client/src/components/Telepath/Telepath.js | 50 +++++++++++++++++++ .../entrypoints/admin/telepath/telepath.js | 49 +----------------- 2 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 client/src/components/Telepath/Telepath.js diff --git a/client/src/components/Telepath/Telepath.js b/client/src/components/Telepath/Telepath.js new file mode 100644 index 0000000000..cd0421341f --- /dev/null +++ b/client/src/components/Telepath/Telepath.js @@ -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; diff --git a/client/src/entrypoints/admin/telepath/telepath.js b/client/src/entrypoints/admin/telepath/telepath.js index e748e55384..71662add0c 100644 --- a/client/src/entrypoints/admin/telepath/telepath.js +++ b/client/src/entrypoints/admin/telepath/telepath.js @@ -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();