Skip to content

Commit dd9c25b

Browse files
authored
Merge pull request #12507 from jfayot/fix_11674
Replaced defaultValue with ??
2 parents 5eaa228 + 02bedea commit dd9c25b

File tree

506 files changed

+3432
-4990
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

506 files changed

+3432
-4990
lines changed

Apps/Sandcastle/gallery/3D Tiles 1.1 CDB Yemen.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,7 @@
301301
const propertyValue = feature.getProperty(propertyId);
302302
const property = metadataClass.properties[propertyId];
303303

304-
const propertyType = Cesium.defaultValue(
305-
property.componentType,
306-
property.type,
307-
);
304+
const propertyType = property.componentType ?? property.type;
308305
tableHtmlScratch += `<tr style='font-family: monospace;' title='${property.description}'><th>${property.name}</th><th><b>${property.id}</b></th><td>${propertyType}</td><td>${propertyValue}</td></tr>`;
309306
}
310307
tableHtmlScratch +=

Apps/Sandcastle/gallery/3D Tiles Point Cloud Styling.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
const styles = [];
5151
function addStyle(name, style) {
52-
style.pointSize = Cesium.defaultValue(style.pointSize, 5.0);
52+
style.pointSize = style.pointSize ?? 5.0;
5353
styles.push({
5454
name: name,
5555
style: style,

Apps/Sandcastle/gallery/Imagery Layers Manipulation.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@
269269
try {
270270
const imageryProvider = await Promise.resolve(imageryProviderPromise);
271271
const layer = new Cesium.ImageryLayer(imageryProvider);
272-
layer.alpha = Cesium.defaultValue(alpha, 0.5);
273-
layer.show = Cesium.defaultValue(show, true);
272+
layer.alpha = alpha ?? 0.5;
273+
layer.show = show ?? true;
274274
layer.name = name;
275275
imageryLayers.add(layer);
276276
Cesium.knockout.track(layer, ["alpha", "show", "name"]);

Apps/Sandcastle/gallery/development/3D Models.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@
152152
});
153153

154154
async function createModel(url, height, heading, pitch, roll) {
155-
height = Cesium.defaultValue(height, 0.0);
156-
heading = Cesium.defaultValue(heading, 0.0);
157-
pitch = Cesium.defaultValue(pitch, 0.0);
158-
roll = Cesium.defaultValue(roll, 0.0);
155+
height = height ?? 0.0;
156+
heading = heading ?? 0.0;
157+
pitch = pitch ?? 0.0;
158+
roll = roll ?? 0.0;
159159
const hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
160160

161161
const origin = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height);

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
- `Camera.getPickRay` was erroneous returning a result in camera coordinates. It is now returned in world coordinates as stated in the documentation. The result can be transformed using `Camera.inverseViewMatrix` to achieve the previous behavior.
1010

11+
#### Deprecated :hourglass_flowing_sand:
12+
13+
- `defaultValue` function and `defaultValue.EMPTY_OBJECT` have been deprecated, and will be removed in 1.134. Use respectively the nullish coalescing operator [??](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) and `Frozen.EMPTY_OBJECT` instead. A new `Frozen.EMPTY_ARRAY` frozen value has been added for the same purpose as `Frozen.EMPTY_OBJECT`. See [Coding Guide](https://github.com/CesiumGS/cesium/tree/main/Documentation/Contributors/CodingGuide#default-parameter-values).
14+
1115
#### Fixes :wrench:
1216

1317
- Fixed broken Entity Tracking [sandcastle](https://sandcastle.cesium.com/?src=Entity%20tracking.html). [#12467](https://github.com/CesiumGS/cesium/pull/12467)

Documentation/Contributors/CodingGuide/README.md

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ To some extent, this guide can be summarized as _make new code similar to existi
5151

5252
- Directory names are `PascalCase`, e.g., `Source/Scene`.
5353
- Constructor functions are `PascalCase`, e.g., `Cartesian3`.
54-
- Functions are `camelCase`, e.g., `defaultValue()`, `Cartesian3.equalsEpsilon()`.
55-
- Files end in `.js` and have the same name as the JavaScript identifier, e.g., `Cartesian3.js` and `defaultValue.js`.
54+
- Functions are `camelCase`, e.g., `binarySearch()`, `Cartesian3.equalsEpsilon()`.
55+
- Files end in `.js` and have the same name as the JavaScript identifier, e.g., `Cartesian3.js` and `binarySearch.js`.
5656
- Variables, including class properties, are `camelCase`, e.g.,
5757

5858
```javascript
@@ -438,39 +438,30 @@ const p = new Cartesian3(1.0, 2.0, 3.0);
438438

439439
### Default Parameter Values
440440

441-
If a _sensible_ default exists for a function parameter or class property, don't require the user to provide it. Use Cesium's `defaultValue` to assign a default value. For example, `height` defaults to zero in `Cartesian3.fromRadians`:
441+
If a _sensible_ default exists for a function parameter or class property, don't require the user to provide it. Use the nullish coalescing operator [`??`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) to assign a default value. For example, `height` defaults to zero in `Cartesian3.fromRadians`:
442442

443443
```javascript
444444
Cartesian3.fromRadians = function (longitude, latitude, height) {
445-
height = defaultValue(height, 0.0);
445+
height = height ?? 0.0;
446446
// ...
447447
};
448448
```
449449
450-
- :speedboat: Don't use `defaultValue` if it could cause an unnecessary function call or memory allocation, e.g.,
450+
- :speedboat: `??` operator can also be used with a memory allocations in the right hand side, as it will be allocated only if the left hand side is either `null` or `undefined`, and therefore has no negative impact on performances, e.g.,
451451
452452
```javascript
453-
this._mapProjection = defaultValue(
454-
options.mapProjection,
455-
new GeographicProjection(),
456-
);
453+
this._mapProjection = options.mapProjection ?? new GeographicProjection();
457454
```
458455
459-
is better written as
456+
- If an `options` parameter is optional and never modified afterwards, use `Frozen.EMPTY_OBJECT` or `Frozen.EMPTY_ARRAY`, this could prevent from unnecessary memory allocations in code critical path, e.g.,
460457
461458
```javascript
462-
this._mapProjection = defined(options.mapProjection)
463-
? options.mapProjection
464-
: new GeographicProjection();
465-
```
466-
467-
- If an `options` parameter is optional, use `defaultValue.EMPTY_OBJECT`, e.g.,
459+
function BaseLayerPickerViewModel(options) {
460+
options = options ?? Frozen.EMPTY_OBJECT;
468461

469-
```javascript
470-
function DebugModelMatrixPrimitive(options) {
471-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
472-
this.length = defaultValue(options.length, 10000000.0);
473-
this.width = defaultValue(options.width, 2.0);
462+
const globe = options.globe;
463+
const imageryProviderViewModels =
464+
options.imageryProviderViewModels ?? Frozen.EMPTY_ARRAY;
474465
// ...
475466
}
476467
```
@@ -594,9 +585,9 @@ result = Cartesian3.add(result, v2, result);
594585
595586
```javascript
596587
function Cartesian3(x, y, z) {
597-
this.x = defaultValue(x, 0.0);
598-
this.y = defaultValue(y, 0.0);
599-
this.z = defaultValue(z, 0.0);
588+
this.x = x ?? 0.0;
589+
this.y = y ?? 0.0;
590+
this.z = z ?? 0.0;
600591
}
601592
```
602593
@@ -771,7 +762,7 @@ Public properties that can be read or written without extra processing can simpl
771762
772763
```javascript
773764
function Model(options) {
774-
this.show = defaultValue(options.show, true);
765+
this.show = options.show ?? true;
775766
}
776767
```
777768
@@ -827,9 +818,7 @@ When the overhead of getter/setter functions is prohibitive or reference-type se
827818
828819
```javascript
829820
function Model(options) {
830-
this.modelMatrix = Matrix4.clone(
831-
defaultValue(options.modelMatrix, Matrix4.IDENTITY),
832-
);
821+
this.modelMatrix = Matrix4.clone(options.modelMatrix ?? Matrix4.IDENTITY);
833822
this._modelMatrix = Matrix4.clone(this.modelMatrix);
834823
}
835824

Documentation/Contributors/DocumentationGuide/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ function Cartesian3(x, y) {
291291
* @type {number}
292292
* @default 0.0
293293
*/
294-
this.x = defaultValue(x, 0.0);
294+
this.x = x ?? 0.0;
295295

296296
// ...
297297
```

Specs/Cesium3DTilesTester.js

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Cartesian3,
33
Color,
4-
defaultValue,
4+
Frozen,
55
defined,
66
JulianDate,
77
ImageBasedLighting,
@@ -120,15 +120,9 @@ const defaultIbl = new ImageBasedLighting({
120120
});
121121

122122
Cesium3DTilesTester.loadTileset = async function (scene, url, options) {
123-
options = defaultValue(options, {});
124-
options.cullRequestsWhileMoving = defaultValue(
125-
options.cullRequestsWhileMoving,
126-
false,
127-
);
128-
options.imageBasedLighting = defaultValue(
129-
options.imageBasedLighting,
130-
defaultIbl,
131-
);
123+
options = options ?? {};
124+
options.cullRequestsWhileMoving = options.cullRequestsWhileMoving ?? false;
125+
options.imageBasedLighting = options.imageBasedLighting ?? defaultIbl;
132126
options.environmentMapOptions = {
133127
enabled: false, // disable other diffuse lighting by default
134128
...options.environmentMapOptions,
@@ -170,10 +164,10 @@ Cesium3DTilesTester.tileDestroys = function (scene, url, options) {
170164

171165
Cesium3DTilesTester.generateBatchedTileBuffer = function (options) {
172166
// Procedurally generate the tile array buffer for testing purposes
173-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
174-
const magic = defaultValue(options.magic, [98, 51, 100, 109]);
175-
const version = defaultValue(options.version, 1);
176-
const featuresLength = defaultValue(options.featuresLength, 1);
167+
options = options ?? Frozen.EMPTY_OBJECT;
168+
const magic = options.magic ?? [98, 51, 100, 109];
169+
const version = options.version ?? 1;
170+
const featuresLength = options.featuresLength ?? 1;
177171
const featureTableJson = {
178172
BATCH_LENGTH: featuresLength,
179173
};
@@ -207,12 +201,12 @@ Cesium3DTilesTester.generateBatchedTileBuffer = function (options) {
207201

208202
Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
209203
// Procedurally generate the tile array buffer for testing purposes
210-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
211-
const magic = defaultValue(options.magic, [105, 51, 100, 109]);
212-
const version = defaultValue(options.version, 1);
204+
options = options ?? Frozen.EMPTY_OBJECT;
205+
const magic = options.magic ?? [105, 51, 100, 109];
206+
const version = options.version ?? 1;
213207

214-
const gltfFormat = defaultValue(options.gltfFormat, 1);
215-
const gltfUri = defaultValue(options.gltfUri, "model.gltf");
208+
const gltfFormat = options.gltfFormat ?? 1;
209+
const gltfUri = options.gltfUri ?? "model.gltf";
216210
const gltfUriByteLength = gltfUri.length;
217211

218212
const featureTableJson = options.featureTableJson;
@@ -222,7 +216,7 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
222216
featureTableJsonString = JSON.stringify(featureTableJson);
223217
}
224218
} else {
225-
const featuresLength = defaultValue(options.featuresLength, 1);
219+
const featuresLength = options.featuresLength ?? 1;
226220
featureTableJsonString = JSON.stringify({
227221
INSTANCES_LENGTH: featuresLength,
228222
POSITION: new Array(featuresLength * 3).fill(0),
@@ -231,10 +225,7 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
231225
featureTableJsonString = padStringToByteAlignment(featureTableJsonString, 8);
232226
const featureTableJsonByteLength = featureTableJsonString.length;
233227

234-
const featureTableBinary = defaultValue(
235-
options.featureTableBinary,
236-
new Uint8Array(0),
237-
);
228+
const featureTableBinary = options.featureTableBinary ?? new Uint8Array(0);
238229
const featureTableBinaryByteLength = featureTableBinary.length;
239230

240231
const batchTableJson = options.batchTableJson;
@@ -245,10 +236,7 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
245236
batchTableJsonString = padStringToByteAlignment(batchTableJsonString, 8);
246237
const batchTableJsonByteLength = batchTableJsonString.length;
247238

248-
const batchTableBinary = defaultValue(
249-
options.batchTableBinary,
250-
new Uint8Array(0),
251-
);
239+
const batchTableBinary = options.batchTableBinary ?? new Uint8Array(0);
252240
const batchTableBinaryByteLength = batchTableBinary.length;
253241

254242
const headerByteLength = 32;
@@ -300,9 +288,9 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
300288

301289
Cesium3DTilesTester.generatePointCloudTileBuffer = function (options) {
302290
// Procedurally generate the tile array buffer for testing purposes
303-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
304-
const magic = defaultValue(options.magic, [112, 110, 116, 115]);
305-
const version = defaultValue(options.version, 1);
291+
options = options ?? Frozen.EMPTY_OBJECT;
292+
const magic = options.magic ?? [112, 110, 116, 115];
293+
const version = options.version ?? 1;
306294
let featureTableJson = options.featureTableJson;
307295
if (!defined(featureTableJson)) {
308296
featureTableJson = {
@@ -315,10 +303,8 @@ Cesium3DTilesTester.generatePointCloudTileBuffer = function (options) {
315303

316304
let featureTableJsonString = JSON.stringify(featureTableJson);
317305
featureTableJsonString = padStringToByteAlignment(featureTableJsonString, 4);
318-
const featureTableJsonByteLength = defaultValue(
319-
options.featureTableJsonByteLength,
320-
featureTableJsonString.length,
321-
);
306+
const featureTableJsonByteLength =
307+
options.featureTableJsonByteLength ?? featureTableJsonString.length;
322308

323309
const featureTableBinary = new ArrayBuffer(12); // Enough space to hold 3 floats
324310
const featureTableBinaryByteLength = featureTableBinary.byteLength;
@@ -356,10 +342,10 @@ Cesium3DTilesTester.generatePointCloudTileBuffer = function (options) {
356342

357343
Cesium3DTilesTester.generateCompositeTileBuffer = function (options) {
358344
// Procedurally generate the tile array buffer for testing purposes
359-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
360-
const magic = defaultValue(options.magic, [99, 109, 112, 116]);
361-
const version = defaultValue(options.version, 1);
362-
const tiles = defaultValue(options.tiles, []);
345+
options = options ?? Frozen.EMPTY_OBJECT;
346+
const magic = options.magic ?? [99, 109, 112, 116];
347+
const version = options.version ?? 1;
348+
const tiles = options.tiles ?? Frozen.EMPTY_ARRAY;
363349
const tilesLength = tiles.length;
364350

365351
let i;
@@ -393,20 +379,20 @@ Cesium3DTilesTester.generateCompositeTileBuffer = function (options) {
393379

394380
Cesium3DTilesTester.generateVectorTileBuffer = function (options) {
395381
// Procedurally generate the tile array buffer for testing purposes
396-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
397-
const magic = defaultValue(options.magic, [118, 99, 116, 114]);
398-
const version = defaultValue(options.version, 1);
382+
options = options ?? Frozen.EMPTY_OBJECT;
383+
const magic = options.magic ?? [118, 99, 116, 114];
384+
const version = options.version ?? 1;
399385

400386
let featureTableJsonString;
401387
let featureTableJsonByteLength = 0;
402-
const defineFeatureTable = defaultValue(options.defineFeatureTable, true);
388+
const defineFeatureTable = options.defineFeatureTable ?? true;
403389
if (defineFeatureTable) {
404-
const defineRegion = defaultValue(options.defineRegion, true);
390+
const defineRegion = options.defineRegion ?? true;
405391
const featureTableJson = {
406392
REGION: defineRegion ? [-1.0, -1.0, 1.0, 1.0, -1.0, 1.0] : undefined,
407-
POLYGONS_LENGTH: defaultValue(options.polygonsLength, 0),
408-
POLYLINES_LENGTH: defaultValue(options.polylinesLength, 0),
409-
POINTS_LENGTH: defaultValue(options.pointsLength, 0),
393+
POLYGONS_LENGTH: options.polygonsLength ?? 0,
394+
POLYLINES_LENGTH: options.polylinesLength ?? 0,
395+
POINTS_LENGTH: options.pointsLength ?? 0,
410396
POLYGON_BATCH_IDS: options.polygonBatchIds,
411397
POLYLINE_BATCH_IDS: options.polylineBatchIds,
412398
POINT_BATCH_IDS: options.pointBatchIds,
@@ -446,19 +432,19 @@ Cesium3DTilesTester.generateVectorTileBuffer = function (options) {
446432

447433
Cesium3DTilesTester.generateGeometryTileBuffer = function (options) {
448434
// Procedurally generate the tile array buffer for testing purposes
449-
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
450-
const magic = defaultValue(options.magic, [103, 101, 111, 109]);
451-
const version = defaultValue(options.version, 1);
435+
options = options ?? Frozen.EMPTY_OBJECT;
436+
const magic = options.magic ?? [103, 101, 111, 109];
437+
const version = options.version ?? 1;
452438

453439
let featureTableJsonString;
454440
let featureTableJsonByteLength = 0;
455-
const defineFeatureTable = defaultValue(options.defineFeatureTable, true);
441+
const defineFeatureTable = options.defineFeatureTable ?? true;
456442
if (defineFeatureTable) {
457443
const featureTableJson = {
458-
BOXES_LENGTH: defaultValue(options.boxesLength, 0),
459-
CYLINDERS_LENGTH: defaultValue(options.cylindersLength, 0),
460-
ELLIPSOIDS_LENGTH: defaultValue(options.ellipsoidsLength, 0),
461-
SPHERES_LENGTH: defaultValue(options.spheresLength, 0),
444+
BOXES_LENGTH: options.boxesLength ?? 0,
445+
CYLINDERS_LENGTH: options.cylindersLength ?? 0,
446+
ELLIPSOIDS_LENGTH: options.ellipsoidsLength ?? 0,
447+
SPHERES_LENGTH: options.spheresLength ?? 0,
462448
BOX_BATCH_IDS: options.boxBatchIds,
463449
CYLINDER_BATCH_IDS: options.cylinderBatchIds,
464450
ELLIPSOID_BATCH_IDS: options.ellipsoidBatchIds,

0 commit comments

Comments
 (0)