Source: lib/duration.js

  1. // Copyright (c) 2018 Intel Corporation. All rights reserved.
  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. 'use strict';
  15. const rclnodejs = require('bindings')('rclnodejs');
  16. const int64 = require('int64-napi');
  17. /**
  18. * @class - Class representing a Duration in ROS
  19. */
  20. class Duration {
  21. /**
  22. * Create a Duration.
  23. * @param {number|string} [seconds=0] - The second part of the duration.
  24. * @param {number|string} [nanoseconds=0] - The nanosecond part of the duration.
  25. */
  26. constructor(seconds = 0, nanoseconds = 0) {
  27. if (typeof seconds !== 'number' && typeof seconds !== 'string') {
  28. throw new TypeError('Invalid argument of seconds');
  29. }
  30. if (typeof nanoseconds !== 'number' && typeof nanoseconds !== 'string') {
  31. throw new TypeError('Invalid argument of nanoseconds');
  32. }
  33. let secondInt64 = int64.from(seconds);
  34. let nanoInt64 = int64.from(nanoseconds);
  35. if (typeof seconds === 'string' && seconds.startsWith('-')) {
  36. secondInt64 = int64.negative(secondInt64);
  37. }
  38. if (typeof nanoseconds === 'string' && nanoseconds.startsWith('-')) {
  39. nanoInt64 = int64.negative(nanoInt64);
  40. }
  41. this._nanoseconds = secondInt64.multiply(1e9).add(nanoInt64);
  42. this._handle = rclnodejs.createDuration(this._nanoseconds.toString());
  43. }
  44. /**
  45. * Get the nanosecond part of the Duration.
  46. * @name Duration#get:nanoseconds
  47. * @function
  48. * @return {number|string} - value in nanosecond, if the value is greater than Number.MAX_SAFE_INTEGER (2^53-1), will be presented in string of decimal format.
  49. */
  50. get nanoseconds() {
  51. let nanoStr = rclnodejs.getDurationNanoseconds(this._handle);
  52. let nano;
  53. if (nanoStr.startsWith('-')) {
  54. nano = int64.negative(int64.from(nanoStr));
  55. } else {
  56. nano = int64.from(nanoStr);
  57. }
  58. if (Number.isFinite(nano.toNumber())) {
  59. return nano.toNumber();
  60. }
  61. return nano.toString();
  62. }
  63. /**
  64. * Determine whether two Duration objects are equal.
  65. * @param {Duration} other - The Duration object to be compared.
  66. * @return {boolean} Return true if they are equal.
  67. */
  68. eq(other) {
  69. if (other instanceof Duration) {
  70. return this._nanoseconds.eq(other.nanoseconds);
  71. }
  72. throw new TypeError(
  73. `Can't compare duration with object of type: ${other.constructor.name}`
  74. );
  75. }
  76. /**
  77. * Determine whether two Duration objects are not equal.
  78. * @param {Duration} other - The Duration object to be compared.
  79. * @return {boolean} Return true if they are not equal.
  80. */
  81. ne(other) {
  82. if (other instanceof Duration) {
  83. return this._nanoseconds.ne(other.nanoseconds);
  84. }
  85. throw new TypeError('Invalid argument');
  86. }
  87. /**
  88. * Determine whether the Duration object is less than another one.
  89. * @param {Duration} other - The Duration object to be compared.
  90. * @return {boolean} Return true if it's less than other.
  91. */
  92. lt(other) {
  93. if (other instanceof Duration) {
  94. return this._nanoseconds.lt(other.nanoseconds);
  95. }
  96. throw new TypeError('Invalid argument');
  97. }
  98. /**
  99. * Determine whether the Duration object is less than or equal with another one.
  100. * @param {Duration} other - The Duration object to be compared.
  101. * @return {boolean} Return true if it's less than or equal with other.
  102. */
  103. lte(other) {
  104. if (other instanceof Duration) {
  105. return this._nanoseconds.lte(other.nanoseconds);
  106. }
  107. throw new TypeError('Invalid argument');
  108. }
  109. /**
  110. * Determine whether the Duration object is greater than another one.
  111. * @param {Duration} other - The Duration object to be compared.
  112. * @return {boolean} Return true if it's greater than other.
  113. */
  114. gt(other) {
  115. if (other instanceof Duration) {
  116. return this._nanoseconds.gt(other.nanoseconds);
  117. }
  118. throw new TypeError('Invalid argument');
  119. }
  120. /**
  121. * Determine whether the Duration object is greater than or equal with another one.
  122. * @param {Duration} other - The Duration object to be compared.
  123. * @return {boolean} Return true if it's greater than or equal with other.
  124. */
  125. gte(other) {
  126. if (other instanceof Duration) {
  127. return this._nanoseconds.gte(other.nanoseconds);
  128. }
  129. throw new TypeError('Invalid argument');
  130. }
  131. }
  132. module.exports = Duration;