Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/reflect/scalar.js

raw
1 "use strict";
2 // Copyright 2021-2026 Buf Technologies, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.scalarEquals = scalarEquals;
17 exports.scalarZeroValue = scalarZeroValue;
18 exports.isScalarZeroValue = isScalarZeroValue;
19 const proto_int64_js_1 = require("../proto-int64.js");
20 const descriptors_js_1 = require("../descriptors.js");
21 /**
22 * Returns true if both scalar values are equal.
23 *
24 * For float and double, values are compared following IEEE semantics: -0
25 * equals 0, and NaN does not equal NaN. This is value equality, not identity.
26 *
27 * It deliberately differs from isScalarZeroValue, which treats -0 as distinct
28 * from 0. A value can equal the zero value without being a zero value
29 * (scalarEquals(DOUBLE, -0, 0) is true while isScalarZeroValue(DOUBLE, -0) is
30 * false) so this function must not be used to derive implicit presence.
31 */
32 function scalarEquals(type, a, b) {
33 if (a === b) {
34 // This correctly matches equal values except BYTES and (possibly) 64-bit integers.
35 return true;
36 }
37 // Special case BYTES - we need to compare each byte individually
38 if (type == descriptors_js_1.ScalarType.BYTES) {
39 if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array)) {
40 return false;
41 }
42 if (a.length !== b.length) {
43 return false;
44 }
45 for (let i = 0; i < a.length; i++) {
46 if (a[i] !== b[i]) {
47 return false;
48 }
49 }
50 return true;
51 }
52 // Special case 64-bit integers - we support number, string and bigint representation.
53 switch (type) {
54 case descriptors_js_1.ScalarType.UINT64:
55 case descriptors_js_1.ScalarType.FIXED64:
56 case descriptors_js_1.ScalarType.INT64:
57 case descriptors_js_1.ScalarType.SFIXED64:
58 case descriptors_js_1.ScalarType.SINT64:
59 // Loose comparison will match between 0n, 0 and "0".
60 return a == b;
61 }
62 // Anything that hasn't been caught by strict comparison or special cased
63 // BYTES and 64-bit integers is not equal.
64 return false;
65 }
66 /**
67 * Returns the zero value for the given scalar type, the value a field of this
68 * type has when unset: 0 for numeric types, "" for strings, false for
69 * booleans, and an empty Uint8Array for bytes. For 64-bit integer types, the
70 * result is "0" when longAsString is true, otherwise 0n.
71 *
72 * This is the type's zero value, not a proto2 custom field default. For float
73 * and double it is +0; isScalarZeroValue treats only +0, not -0, as this value.
74 */
75 function scalarZeroValue(type, longAsString) {
76 switch (type) {
77 case descriptors_js_1.ScalarType.STRING:
78 return "";
79 case descriptors_js_1.ScalarType.BOOL:
80 return false;
81 case descriptors_js_1.ScalarType.DOUBLE:
82 case descriptors_js_1.ScalarType.FLOAT:
83 return 0.0;
84 case descriptors_js_1.ScalarType.INT64:
85 case descriptors_js_1.ScalarType.UINT64:
86 case descriptors_js_1.ScalarType.SFIXED64:
87 case descriptors_js_1.ScalarType.FIXED64:
88 case descriptors_js_1.ScalarType.SINT64:
89 return (longAsString ? "0" : proto_int64_js_1.protoInt64.zero);
90 case descriptors_js_1.ScalarType.BYTES:
91 return new Uint8Array(0);
92 default:
93 // Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
94 // We do not use individual cases to save a few bytes code size.
95 return 0;
96 }
97 }
98 /**
99 * Returns true if the value is the zero value for the given scalar type: `0`
100 * for numeric types, `false` for booleans, `""` for strings, and an empty
101 * Uint8Array for bytes.
102 *
103 * This is the implicit-presence default check. A singular field with implicit
104 * presence is treated as unset, and omitted from the wire, when its value is
105 * the zero value. With explicit presence, or in repeated and map fields,
106 * presence is structural and this function does not apply.
107 *
108 * Note that -0 is NOT a zero value for float and double: under implicit
109 * presence, +0 is omitted from the wire but -0 is written, following the
110 * proto3 specification. As a result this can disagree with scalarEquals, which
111 * compares by value and treats -0 as equal to 0.
112 */
113 function isScalarZeroValue(type, value) {
114 switch (type) {
115 case descriptors_js_1.ScalarType.BOOL:
116 return value === false;
117 case descriptors_js_1.ScalarType.STRING:
118 return value === "";
119 case descriptors_js_1.ScalarType.BYTES:
120 return value instanceof Uint8Array && !value.byteLength;
121 case descriptors_js_1.ScalarType.DOUBLE:
122 case descriptors_js_1.ScalarType.FLOAT:
123 // Object.is distinguishes -0 from 0.
124 return Object.is(value, 0);
125 default:
126 // Loose comparison matches 0n, 0 and "0".
127 return value == 0;
128 }
129 }