Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

uber / deck.gl / 13758

17 Sep 2019 - 21:06 coverage increased (+2.9%) to 82.762%
13758

Pull #3621

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
fix CI
Pull Request #3621: Support spring transition in UniformTransitionManager

3406 of 4621 branches covered (73.71%)

Branch coverage included in aggregate %.

59 of 63 new or added lines in 6 files covered. (93.65%)

499 existing lines in 85 files now uncovered.

7200 of 8194 relevant lines covered (87.87%)

4274.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.75
/modules/json/src/json-converter.js
1
// Converts JSON to props ("hydrating" classes, resolving enums and functions etc).
2
// Lightly processes `json` props, transform string values, and extract `views` and `layers`
3
// See: https://github.com/uber/deck.gl/blob/master/dev-docs/RFCs/v6.1/json-layers-rfc.md
4
//
5
// NOTES:
6
// * This is intended to provide minimal necessary processing required to support
7
//   existing deck.gl props via JSON. This is not an implementation of alternate JSON schemas.
8
// * Optionally, error checking could be applied, but ideally should leverage
9
//   non-JSON specific mechanisms like prop types.
10

11
import assert from './utils/assert';
12
import JSONConfiguration from './json-configuration';
13
import {instantiateClass} from './helpers/instantiate-class';
14
import parseJSON from './helpers/parse-json';
15

16
const isObject = value => value && typeof value === 'object';
13×
17

18
export default class JSONConverter {
19
  constructor(props) {
20
    this.log = console; // eslint-disable-line
1×
21
    this.configuration = {};
2×
22
    this.onJSONChange = () => {};
1×
23
    this.json = null;
3×
24
    this.convertedJson = null;
1×
25
    this.setProps(props);
2×
26
  }
27

28
  finalize() {}
29

30
  setProps(props) {
31
    // HANDLE CONFIGURATION PROPS
32
    if ('configuration' in props) {
Branches [[1, 1]] missed. 1×
33
      // Accept object or `JSONConfiguration`
34
      this.configuration =
2×
35
        props.configuration instanceof JSONConfiguration
Branches [[2, 0]] missed.
36
          ? props.configuration
37
          : new JSONConfiguration(props.configuration);
38
    }
39

40
    if ('onJSONChange' in props) {
Branches [[3, 0]] missed. 1×
41
      this.onJSONChange = props.onJSONChange;
198×
42
    }
43
  }
44

45
  convert(json) {
46
    // Use shallow equality to ensure we only convert same json once
47
    if (!json || json === this.json) {
Branches [[4, 0]] missed. 4×
48
      return this.convertedJson;
4×
49
    }
50
    // Save json for shallow diffing
51
    this.json = json;
4×
52

53
    // Accept JSON strings by parsing them
54
    const parsedJSON = parseJSON(json);
4×
55

56
    // Convert the JSON
57
    let convertedJson = convertJSON(parsedJSON, this.configuration);
4×
58

59
    convertedJson = this.configuration.postProcessConvertedJson(convertedJson);
4×
60

61
    this.convertedJson = convertedJson;
4×
62
    return convertedJson;
4×
63
  }
64

65
  // DEPRECATED: Backwards compatibility
66
  convertJson(json) {
67
    return this.convert(json);
4×
68
  }
69
}
70

71
function convertJSON(json, configuration) {
72
  // Fixup configuration
UNCOV
73
  configuration = new JSONConfiguration(configuration);
!
74
  return convertJSONRecursively(json, '', configuration);
3×
75
}
76

77
// Converts JSON to props ("hydrating" classes, resolving enums and functions etc).
78
function convertJSONRecursively(json, key, configuration) {
UNCOV
79
  if (Array.isArray(json)) {
!
80
    return json.map((element, i) => convertJSONRecursively(element, String(i), configuration));
3×
81
  }
82

83
  // If object.type is in configuration, instantitate
84
  if (isClassInstance(json, configuration)) {
3×
85
    return convertClassInstance(json, configuration);
3×
86
  }
87

88
  if (isObject(json)) {
3×
89
    return convertPlainObject(json, configuration);
3×
90
  }
91

92
  // Single value
UNCOV
93
  if (typeof json === 'string') {
!
94
    return convertString(json, key, configuration);
3×
95
  }
96

97
  // Return unchanged (number, boolean, ...)
98
  return json;
3×
99
}
100

101
// Returns true if an object has a `type` field
102
function isClassInstance(json, configuration) {
103
  const {typeKey} = configuration;
117×
104
  return isObject(json) && Boolean(json[typeKey]);
24×
105
}
106

107
function convertClassInstance(json, configuration) {
108
  // Extract the class type field
109
  const {typeKey} = configuration;
48×
110
  const type = json[typeKey];
93×
111

112
  // Prepare a props object and ensure all values have been converted
113
  let props = {...json};
12×
114
  delete props[typeKey];
81×
115

116
  props = convertPlainObject(props, configuration);
12×
117

118
  return instantiateClass(type, props, configuration);
69×
119
}
120

121
// Plain JS object, convert each key and return.
122
function convertPlainObject(json, configuration) {
123
  assert(isObject(json));
21×
124

125
  const result = {};
48×
126
  for (const key in json) {
93×
127
    const value = json[key];
93×
128
    result[key] = convertJSONRecursively(value, key, configuration);
12×
129
  }
130
  return result;
12×
131
}
132

133
// Convert one string value in an object
134
// TODO - hard to convert without type hint
135
// TODO - Define a syntax for functions so we don't need to sniff types?
136
// if (json.indexOf('@@: ') === 0)
137
// if (typeHint === function)
138
// parseExpressionString(propValue, configuration, isAccessor);
139

140
// TODO - We could also support string syntax for hydrating other types, like regexps...
141
// But no current use case
142
function convertString(string, key, configuration) {
143
  if (configuration.constants[string]) {
12×
144
    return configuration.constants[string];
12×
145
  }
146
  if (configuration.enumerations[string]) {
Branches [[12, 0]] missed. 12×
147
    // TODO - look up
148
    return string;
12×
149
  }
150
  if (configuration.convertFunction) {
Branches [[13, 1]] missed. 24×
151
    return configuration.convertFunction(string, key, configuration);
24×
152
  }
153
  return string;
24×
154
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC