@foxglove/message-definition
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -5,2 +5,25 @@ "use strict"; | ||
/** | ||
* Deep equality comparison of two DefaultValue values. Two default values are considered equal | ||
* if they are both arrays and all their elements are equal, or if they are both primitives and | ||
* equal. | ||
* | ||
* @param lhs Left-hand side of the comparison. | ||
* @param rhs Right-hand side of the comparison. | ||
* @returns True if the two default values are equal, false otherwise. | ||
*/ | ||
function defaultValuesEqual(lhs, rhs) { | ||
if (Array.isArray(lhs) && Array.isArray(rhs)) { | ||
if (lhs.length !== rhs.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < lhs.length; i++) { | ||
if (lhs[i] !== rhs[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return lhs === rhs; | ||
} | ||
/** | ||
* Compares two MessageDefinitionField objects for equality. Two fields are considered equal if all | ||
@@ -24,3 +47,3 @@ * their properties are equal. Assumes default values (False) for boolean properties that are undefined. | ||
lhs.arrayUpperBound === rhs.arrayUpperBound && | ||
lhs.defaultValue === rhs.defaultValue); | ||
defaultValuesEqual(lhs.defaultValue, rhs.defaultValue)); | ||
} | ||
@@ -27,0 +50,0 @@ exports.isMsgDefFieldEqual = isMsgDefFieldEqual; |
{ | ||
"name": "@foxglove/message-definition", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Defines common types for message definition schemas (ROS .msg, Protobuf, FlatBuffers, IDL, PX4 ULog, JSON Schema, etc)", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -63,2 +63,38 @@ import { isMsgDefEqual } from "./compare"; | ||
}); | ||
it("should return true for matching array default values", () => { | ||
const definition1: MessageDefinition = { | ||
name: "definition", | ||
definitions: [ | ||
{ type: "string", isArray: true, name: "field1", defaultValue: ["a", "b"] }, | ||
{ type: "number", isArray: true, name: "field2", defaultValue: [1, 2] }, | ||
], | ||
}; | ||
const definition2: MessageDefinition = { | ||
name: "definition", | ||
definitions: [ | ||
{ type: "string", isArray: true, name: "field1", defaultValue: ["a", "b"] }, | ||
{ type: "number", isArray: true, name: "field2", defaultValue: [1, 2] }, | ||
], | ||
}; | ||
expect(isMsgDefEqual(definition1, definition2)).toBe(true); | ||
}); | ||
it("should return false for different array default values", () => { | ||
const definition1: MessageDefinition = { | ||
name: "definition", | ||
definitions: [ | ||
{ type: "string", isArray: true, name: "field1", defaultValue: ["a", "b"] }, | ||
{ type: "number", isArray: true, name: "field2", defaultValue: [1, 2] }, | ||
], | ||
}; | ||
const definition2: MessageDefinition = { | ||
name: "definition", | ||
definitions: [ | ||
{ type: "string", isArray: true, name: "field1", defaultValue: ["a", "b"] }, | ||
{ type: "number", isArray: true, name: "field2", defaultValue: [1, 3] }, | ||
], | ||
}; | ||
expect(isMsgDefEqual(definition1, definition2)).toBe(false); | ||
}); | ||
}); |
@@ -1,4 +0,28 @@ | ||
import { MessageDefinition, MessageDefinitionField } from "./types"; | ||
import { DefaultValue, MessageDefinition, MessageDefinitionField } from "./types"; | ||
/** | ||
* Deep equality comparison of two DefaultValue values. Two default values are considered equal | ||
* if they are both arrays and all their elements are equal, or if they are both primitives and | ||
* equal. | ||
* | ||
* @param lhs Left-hand side of the comparison. | ||
* @param rhs Right-hand side of the comparison. | ||
* @returns True if the two default values are equal, false otherwise. | ||
*/ | ||
function defaultValuesEqual(lhs: DefaultValue, rhs: DefaultValue): boolean { | ||
if (Array.isArray(lhs) && Array.isArray(rhs)) { | ||
if (lhs.length !== rhs.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < lhs.length; i++) { | ||
if (lhs[i] !== rhs[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return lhs === rhs; | ||
} | ||
/** | ||
* Compares two MessageDefinitionField objects for equality. Two fields are considered equal if all | ||
@@ -26,3 +50,3 @@ * their properties are equal. Assumes default values (False) for boolean properties that are undefined. | ||
lhs.arrayUpperBound === rhs.arrayUpperBound && | ||
lhs.defaultValue === rhs.defaultValue | ||
defaultValuesEqual(lhs.defaultValue, rhs.defaultValue) | ||
); | ||
@@ -29,0 +53,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29520
635
0