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