Skip to content

Commit de533b3

Browse files
committed
fixes
1 parent 44f78c8 commit de533b3

File tree

7 files changed

+42
-41
lines changed

7 files changed

+42
-41
lines changed

modules/json/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// @deck.gl/json: top-level exports
66

77
// Generic JSON converter, usable by other wrapper modules
8-
export {JSONConverter} from './json-converter';
9-
export {JSONConfiguration} from './json-configuration';
8+
export {JSONConverter, type JSONConverterProps} from './json-converter';
9+
export {JSONConfiguration, type JSONConfigurationProps} from './json-configuration';
1010

1111
// Transports
1212
export {Transport} from './transports/transport';

modules/json/src/json-converter.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
// SPDX-License-Identifier: MIT
33
// Copyright (c) vis.gl contributors
44

5-
import {JSONConfiguration} from './json-configuration';
5+
import {JSONConfiguration, JSONConfigurationProps} from './json-configuration';
66
import {FUNCTION_IDENTIFIER, CONSTANT_IDENTIFIER, FUNCTION_KEY} from './syntactic-sugar';
77
import {instantiateClass} from './helpers/instantiate-class';
88
import {executeFunction} from './helpers/execute-function';
9-
import {assert} from './utils/assert';
109
import {parseJSON} from './helpers/parse-json';
1110

1211
function isObject(value: unknown): value is Record<string, unknown> {
13-
return !!value && typeof value === 'object';
12+
return Boolean(value) && typeof value === 'object';
1413
}
1514

16-
export type JSONConverterProps = JSONConfiguration & {
15+
export type JSONConverterProps = {
16+
configuration: JSONConfiguration | JSONConfigurationProps;
1717
onJSONChange: () => void;
1818
};
1919

@@ -43,14 +43,14 @@ export class JSONConverter {
4343
// eslint-disable-next-line @typescript-eslint/no-empty-function
4444
finalize() {}
4545

46-
setProps(props: JSONConverterProps) {
46+
setProps(props: JSONConverterProps | JSONConfiguration) {
4747
// HANDLE CONFIGURATION PROPS
4848
if ('configuration' in props) {
4949
// Accept object or `JSONConfiguration`
5050
this.configuration =
5151
props.configuration instanceof JSONConfiguration
5252
? props.configuration
53-
: new JSONConfiguration(props.config);
53+
: new JSONConfiguration(props.configuration);
5454
}
5555

5656
if ('onJSONChange' in props) {
@@ -72,6 +72,9 @@ export class JSONConverter {
7272

7373
// Accept JSON strings by parsing them
7474
const parsedJSON = parseJSON(json);
75+
if (!isObject(parsedJSON)) {
76+
throw new Error('JSONConverter: expected an object');
77+
}
7578

7679
// Convert the JSON
7780
let convertedJson = convertJSON(parsedJSON, this.configuration);
@@ -88,9 +91,9 @@ export class JSONConverter {
8891
}
8992
}
9093

91-
function convertJSON(json: unknown, configuration: JSONConfiguration) {
94+
function convertJSON(json: Record<string, unknown>, configuration: JSONConfiguration) {
9295
// Fixup configuration
93-
configuration = new JSONConfiguration(configuration);
96+
configuration = new JSONConfiguration(configuration.config);
9497
return convertJSONRecursively(json, '', configuration);
9598
}
9699

@@ -100,12 +103,12 @@ function convertJSONRecursively(json: unknown, key, configuration) {
100103
return json.map((element, i) => convertJSONRecursively(element, String(i), configuration));
101104
}
102105

103-
// If object.type is in configuration, instantiate
104-
if (isClassInstance(json, configuration)) {
105-
return convertClassInstance(json, configuration);
106-
}
107-
108106
if (isObject(json)) {
107+
// If object.type is in configuration, instantiate
108+
if (isClassInstance(json, configuration)) {
109+
return convertClassInstance(json, configuration);
110+
}
111+
109112
// If object.function is in configuration, convert object to function
110113
if (FUNCTION_KEY in json) {
111114
return convertFunctionObject(json, configuration);
@@ -123,15 +126,15 @@ function convertJSONRecursively(json: unknown, key, configuration) {
123126
}
124127

125128
/** Returns true if an object has a `type` field */
126-
function isClassInstance(json: unknown, configuration: JSONConfiguration) {
127-
const {typeKey} = configuration;
129+
function isClassInstance(json: Record<string, unknown>, configuration: JSONConfiguration) {
130+
const {typeKey} = configuration.config;
128131
const isClass = isObject(json) && Boolean(json[typeKey]);
129132
return isClass;
130133
}
131134

132-
function convertClassInstance(json: unknown, configuration: JSONConfiguration) {
135+
function convertClassInstance(json: Record<string, unknown>, configuration: JSONConfiguration) {
133136
// Extract the class type field
134-
const {typeKey} = configuration;
137+
const {typeKey} = configuration.config;
135138
const type = json[typeKey];
136139

137140
// Prepare a props object and ensure all values have been converted
@@ -144,9 +147,9 @@ function convertClassInstance(json: unknown, configuration: JSONConfiguration) {
144147
}
145148

146149
/** Plain JS object, embed functions. */
147-
function convertFunctionObject(json, configuration:JSONConfiguration) {
150+
function convertFunctionObject(json, configuration: JSONConfiguration) {
148151
// Extract the target function field
149-
const {functionKey} = configuration;
152+
const {functionKey} = configuration.config;
150153
const targetFunction = json[functionKey];
151154

152155
// Prepare a props object and ensure all values have been converted
@@ -165,8 +168,7 @@ function convertPlainObject(json: unknown, configuration: JSONConfiguration) {
165168
}
166169

167170
const result = {};
168-
for (const key in json) {
169-
const value = json[key];
171+
for (const [key, value] of Object.entries(json)) {
170172
result[key] = convertJSONRecursively(value, key, configuration);
171173
}
172174
return result;
@@ -175,20 +177,20 @@ function convertPlainObject(json: unknown, configuration: JSONConfiguration) {
175177
/** Convert one string value in an object
176178
* @todo We could also support string syntax for hydrating other types, like regexps... But no current use case
177179
*/
178-
function convertString(string, key, configuration: JSONConfiguration) {
180+
function convertString(string: string, key: string, configuration: JSONConfiguration) {
179181
// Here the JSON value is supposed to be treated as a function
180182
if (string.startsWith(FUNCTION_IDENTIFIER) && configuration.convertFunction) {
181183
string = string.replace(FUNCTION_IDENTIFIER, '');
182184
return configuration.convertFunction(string, configuration);
183185
}
184186
if (string.startsWith(CONSTANT_IDENTIFIER)) {
185187
string = string.replace(CONSTANT_IDENTIFIER, '');
186-
if (configuration.constants[string]) {
187-
return configuration.constants[string];
188+
if (configuration.config.constants[string]) {
189+
return configuration.config.constants[string];
188190
}
189191
// enum
190192
const [enumVarName, enumValName] = string.split('.');
191-
return configuration.enumerations[enumVarName][enumValName];
193+
return configuration.config.enumerations[enumVarName][enumValName];
192194
}
193195
return string;
194196
}

modules/jupyter-widget/src/playground/create-deck.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function extractElements(library = {}, filter) {
4343
}
4444

4545
// Handle JSONConverter and loaders configuration
46-
const jsonConverterConfiguration = {
46+
const JSON_CONVERTER_CONFIGURATION = {
4747
classes: extractElements(deckExports, classesFilter),
4848
// Will be resolved as `<enum-name>.<enum-value>`
4949
enumerations: {
@@ -55,7 +55,7 @@ const jsonConverterConfiguration = {
5555
registerLoaders([CSVLoader]);
5656

5757
const jsonConverter = new JSONConverter({
58-
configuration: jsonConverterConfiguration
58+
configuration: JSON_CONVERTER_CONFIGURATION
5959
});
6060

6161
function addModuleToConverter(module, converter) {

test/modules/json/json-configuration-for-deck.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function calculateRadius({base, exponent}) {
1313
return Math.pow(base, exponent);
1414
}
1515

16-
export default {
16+
export const JSON_CONFIGURATION = {
1717
log,
1818
// a map of all layers that should be exposes as JSONLayers
1919
classes: Object.assign({MapView, FirstPersonView}, deckglLayers),

test/modules/json/json-configuration.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import test from 'tape-promise/tape';
66

77
import {JSONConfiguration} from '@deck.gl/json';
8-
import configuration from './json-configuration-for-deck';
8+
import {JSON_CONFIGURATION} from './json-configuration-for-deck';
99

1010
test('JSONConfiguration#import', t => {
1111
t.ok(JSONConfiguration, 'JSONConfiguration imported');
1212
t.end();
1313
});
1414

1515
test('JSONConfiguration#create', t => {
16-
const jsonConverter = new JSONConfiguration({configuration});
16+
const jsonConverter = new JSONConfiguration({configuration: JSON_CONFIGURATION});
1717
t.ok(jsonConverter, 'JSONConfiguration created');
1818
t.end();
1919
});

test/modules/json/json-converter.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {makeSpy} from '@probe.gl/test-utils';
88
import {COORDINATE_SYSTEM} from '@deck.gl/core';
99
import {MapController} from '@deck.gl/core';
1010
import {JSONConverter} from '@deck.gl/json';
11-
import configuration, {log, calculateRadius} from './json-configuration-for-deck';
11+
import {JSON_CONFIGURATION, log, calculateRadius} from './json-configuration-for-deck';
1212
import JSON_DATA from './data/deck-props.json';
1313
import COMPLEX_JSON from './data/complex-data.json';
1414

@@ -20,13 +20,13 @@ test('JSONConverter#import', t => {
2020
});
2121

2222
test('JSONConverter#create', t => {
23-
const jsonConverter = new JSONConverter({configuration});
23+
const jsonConverter = new JSONConverter({configuration: JSON_CONFIGURATION});
2424
t.ok(jsonConverter, 'JSONConverter created');
2525
t.end();
2626
});
2727

2828
test('JSONConverter#convert', t => {
29-
const jsonConverter = new JSONConverter({configuration});
29+
const jsonConverter = new JSONConverter({configuration: JSON_CONFIGURATION});
3030
t.ok(jsonConverter, 'JSONConverter created');
3131

3232
let deckProps = jsonConverter.convert(JSON_DATA);
@@ -52,7 +52,7 @@ test('JSONConverter#convert', t => {
5252
});
5353

5454
test('JSONConverter#merge', t => {
55-
const jsonConverter = new JSONConverter({configuration});
55+
const jsonConverter = new JSONConverter({configuration: JSON_CONFIGURATION});
5656
jsonConverter.mergeConfiguration({
5757
classes: {OrbitView}
5858
});
@@ -69,7 +69,7 @@ test('JSONConverter#merge', t => {
6969
});
7070

7171
test('JSONConverter#badConvert', t => {
72-
const jsonConverter = new JSONConverter({configuration});
72+
const jsonConverter = new JSONConverter({configuration: JSON_CONFIGURATION});
7373
t.ok(jsonConverter, 'JSONConverter created');
7474
const badData = JSON.parse(JSON.stringify(JSON_DATA));
7575
badData.layers[0]['@@type'] = 'InvalidLayer';
@@ -81,7 +81,7 @@ test('JSONConverter#badConvert', t => {
8181
});
8282

8383
test('JSONConverter#handleTypeAsKey', t => {
84-
const jsonConverter = new JSONConverter({configuration});
84+
const jsonConverter = new JSONConverter({configuration: JSON_CONFIGURATION});
8585
t.ok(jsonConverter, 'JSONConverter created');
8686
const complexData = JSON.parse(JSON.stringify(COMPLEX_JSON));
8787
const deckProps = jsonConverter.convert(complexData);

test/modules/json/json-render.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import test from 'tape-promise/tape';
66
import {Deck} from '@deck.gl/core';
77
import {JSONConverter} from '@deck.gl/json';
8-
import configuration from './json-configuration-for-deck';
8+
import {JSON_CONFIGURATION} from './json-configuration-for-deck';
99
import JSON_DATA from './data/deck-props.json';
1010
import {gl} from '@deck.gl/test-utils';
1111

1212
test('JSONConverter#render', t => {
13-
const jsonConverter = new JSONConverter({configuration});
14-
t.ok(jsonConverter, 'JSONConverter created');
13+
const jsonConverter = new JSONConverter({configuration: JSON_CONFIGURATION});
1514

1615
const deckProps = jsonConverter.convert(JSON_DATA);
1716
t.ok(deckProps, 'JSONConverter converted correctly');

0 commit comments

Comments
 (0)