Skip to content

Commit dbf130e

Browse files
authored
Time fromMsg() fix #601 (#602)
time.js fromMsg() - fixed invalid nanoseconds property access - improved Time#fromMsg() jsdoc time.d.ts fromMsg() - changed msg parameter type to builtin_interfaces.msg.Time - changed clockType parameter to optional - improved tsdoc - ignore eslint camelcase rule on some lines to avoid false negatives test-time.js - enhanced test to include Time#fromMsg() Fix #601
1 parent 3e4c97f commit dbf130e

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

lib/time.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,13 @@ class Time {
251251

252252
/**
253253
* Create a Time object from a message of builtin_interfaces/msg/Time
254-
* @param {object} msg - The message to be created from.
255-
* @param {ClockType} [clockType=Clock.ClockType.SYSTEM_TIME] - The type of the time object.
254+
* @param {object} msg - The builtin_interfaces.msg.Time message to be
255+
* created from.
256+
* @param {ClockType} [clockType=Clock.ClockType.ROS_TIME] - The type of the time object.
256257
* @return {Time} Return the created Time object.
257258
*/
258259
static fromMsg(msg, clockType = ClockType.ROS_TIME) {
259-
return new Time(msg.sec, msg.nanoseconds, clockType);
260+
return new Time(msg.sec, msg.nanosec, clockType);
260261
}
261262
}
262263

test/test-time.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ describe('rclnodejs Time/Clock testing', function() {
4545
time = new Time(0, '9223372036854775807');
4646
assert.strictEqual(time.nanoseconds, '9223372036854775807');
4747

48+
time = Time.fromMsg({ sec: 1, nanosec: 64 });
49+
assert.strictEqual(time.nanoseconds, 1000000064);
50+
assert.strictEqual(time.clockType, ClockType.ROS_TIME);
51+
4852
assert.throws(() => {
4953
new Time(1, 1, 'SYSTEM_TIME');
5054
}, TypeError);

test/types/main.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ duration1.gte(duration2);
219219
const time1 = new rclnodejs.Time(100, 100);
220220

221221
// $ExpectType Time
222-
const time2 = rclnodejs.Time.fromMsg(
223-
'helloworld',
224-
rclnodejs.ClockType.SYSTEM_TIME
225-
);
222+
const time2 = rclnodejs.Time.fromMsg({sec: 0, nanosec: 0});
223+
224+
// $ExpectType Time
225+
const time3 =
226+
rclnodejs.Time.fromMsg({sec: 0, nanosec: 0}, rclnodejs.ClockType.ROS_TIME);
226227

227228
// $ExpectType ClockType
228229
time1.clockType;

types/time.d.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Message } from 'rclnodejs';
1+
2+
// eslint-disable-next-line camelcase
3+
import { Message, builtin_interfaces } from 'rclnodejs';
24

35
declare module 'rclnodejs' {
46
/**
@@ -102,10 +104,11 @@ declare module 'rclnodejs' {
102104
/**
103105
* Create a Time object from a message of builtin_interfaces/msg/Time
104106
*
105-
* @param msg - The message to be created from.
106-
* @param clockType- The type of the time object.
107+
* @param msg - The Time message to be created from.
108+
* @param clockType - The type of the time object. Default is ClockType.ROS_TIME
107109
* @returns The new Time.
108110
*/
109-
static fromMsg(msg: Message, clockType: ClockType): Time;
111+
// eslint-disable-next-line camelcase
112+
static fromMsg(msg: builtin_interfaces.msg.Time, clockType?: ClockType): Time;
110113
}
111114
}

0 commit comments

Comments
 (0)