diff --git a/lib/action/uuid.js b/lib/action/uuid.js index a51a1385..61fd8aec 100644 --- a/lib/action/uuid.js +++ b/lib/action/uuid.js @@ -40,7 +40,7 @@ class ActionUuid { this._bytes = bytes; } else { // Generate random UUID. - let uuid = randomUUID().replace(/-/g, ''); + let uuid = randomUUID().replaceAll('-', ''); this._bytes = Uint8Array.from(Buffer.from(uuid, 'hex')); } } diff --git a/lib/client.js b/lib/client.js index 8d849470..f56486d5 100644 --- a/lib/client.js +++ b/lib/client.js @@ -25,51 +25,6 @@ const { const { assertValidMessage } = require('./message_validation.js'); const debug = require('debug')('rclnodejs:client'); -// Polyfill for AbortSignal.any() for Node.js <= 20.3.0 -// AbortSignal.any() was added in Node.js 20.3.0 -// See https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static -if (!AbortSignal.any) { - AbortSignal.any = function (signals) { - // Filter out null/undefined values and validate inputs - const validSignals = Array.isArray(signals) - ? signals.filter((signal) => signal != null) - : []; - - // If no valid signals, return a never-aborting signal - if (validSignals.length === 0) { - return new AbortController().signal; - } - - const controller = new AbortController(); - const listeners = []; - - // Cleanup function to remove all event listeners - const cleanup = () => { - listeners.forEach(({ signal, listener }) => { - signal.removeEventListener('abort', listener); - }); - }; - - for (const signal of validSignals) { - if (signal.aborted) { - cleanup(); - controller.abort(signal.reason); - return controller.signal; - } - - const listener = () => { - cleanup(); - controller.abort(signal.reason); - }; - - signal.addEventListener('abort', listener); - listeners.push({ signal, listener }); - } - - return controller.signal; - }; -} - /** * @class - Class representing a Client in ROS * @hideconstructor diff --git a/lib/interface_loader.js b/lib/interface_loader.js index 94accd9f..40b500e4 100644 --- a/lib/interface_loader.js +++ b/lib/interface_loader.js @@ -57,7 +57,7 @@ let interfaceLoader = { } // TODO(Kenny): more checks of the string argument - if (name.indexOf('/') !== -1) { + if (name.includes('/')) { let [packageName, type, messageName] = name.split('/'); return this.loadInterface(packageName, type, messageName); } diff --git a/lib/message_introspector.js b/lib/message_introspector.js index 2b0c82ad..e14829b2 100644 --- a/lib/message_introspector.js +++ b/lib/message_introspector.js @@ -88,35 +88,7 @@ class MessageIntrospector { const instance = new this.#typeClass(); this.#defaultsCache = toPlainObject(instance); } - return this.#deepClone(this.#defaultsCache); - } - - /** - * Deep clone an object. - * @param {any} obj - Object to clone - * @returns {any} Cloned object - * @private - */ - #deepClone(obj) { - if (obj === null || typeof obj !== 'object') { - return obj; - } - - if (Array.isArray(obj)) { - return obj.map((item) => this.#deepClone(item)); - } - - if (ArrayBuffer.isView(obj) && !(obj instanceof DataView)) { - return obj.slice(); - } - - const cloned = {}; - for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - cloned[key] = this.#deepClone(obj[key]); - } - } - return cloned; + return structuredClone(this.#defaultsCache); } } diff --git a/lib/message_serialization.js b/lib/message_serialization.js index 295e1eb0..df0127e2 100644 --- a/lib/message_serialization.js +++ b/lib/message_serialization.js @@ -62,7 +62,7 @@ function toPlainArrays(obj) { if (typeof obj === 'object' && obj !== null) { const result = {}; for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { + if (Object.hasOwn(obj, key)) { result[key] = toPlainArrays(obj[key]); } } @@ -105,7 +105,7 @@ function toJSONSafe(obj) { if (typeof obj === 'object' && obj !== null) { const result = {}; for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { + if (Object.hasOwn(obj, key)) { result[key] = toJSONSafe(obj[key]); } } diff --git a/rosidl_gen/packages.js b/rosidl_gen/packages.js index 66761263..3663a3b7 100644 --- a/rosidl_gen/packages.js +++ b/rosidl_gen/packages.js @@ -29,7 +29,7 @@ const serviceMsgPath = path.join(generatedRoot, 'srv_msg'); function getPackageName(filePath, amentExecuted) { if (os.type() === 'Windows_NT') { - filePath = filePath.replace(/\\/g, '/'); + filePath = filePath.replaceAll('\\', '/'); } if (amentExecuted) { @@ -41,14 +41,14 @@ function getPackageName(filePath, amentExecuted) { // If |packageName| equals to the file's extension, e.g. msg/srv, one level // up directory will be used as the package name. - return packageName === path.parse(filePath).ext.substr(1) + return packageName === path.parse(filePath).ext.substring(1) ? folders.pop() : packageName; } function getSubFolder(filePath, amentExecuted) { if (os.type() === 'Windows_NT') { - filePath = filePath.replace(/\\/g, '/'); + filePath = filePath.replaceAll('\\', '/'); } if (amentExecuted) { @@ -61,7 +61,7 @@ function getSubFolder(filePath, amentExecuted) { } // If the |amentExecuted| equals to false, the file's extension will be assigned as // the name of sub folder. - return path.parse(filePath).ext.substr(1); + return path.parse(filePath).ext.substring(1); } function grabInterfaceInfo(filePath, amentExecuted) { diff --git a/rosidl_gen/templates/message-template.js b/rosidl_gen/templates/message-template.js index f8d72a95..242ff728 100644 --- a/rosidl_gen/templates/message-template.js +++ b/rosidl_gen/templates/message-template.js @@ -156,15 +156,15 @@ const typedArrayType = [ ]; function isPrimitivePackage(baseType) { - return primitiveBaseType.indexOf(baseType.type) !== -1; + return primitiveBaseType.includes(baseType.type); } function isTypedArrayType(type) { - return typedArrayType.indexOf(type.type.toLowerCase()) !== -1; + return typedArrayType.includes(type.type.toLowerCase()); } function isBigInt(type) { - return ['int64', 'uint64'].indexOf(type.type.toLowerCase()) !== -1; + return ['int64', 'uint64'].includes(type.type.toLowerCase()); } function getWrapperNameByType(type) { @@ -283,7 +283,7 @@ function generateMessage(data) { !fieldType.isPrimitiveType || fieldType.type === 'string'); - if (shouldReq && existedModules.indexOf(requiredModule) === -1) { + if (shouldReq && !existedModules.includes(requiredModule)) { existedModules.push(requiredModule); return true; } else { @@ -692,7 +692,7 @@ ${spec.fields hasMember(name) { let memberNames = ${extractMemberNames(spec.fields)}; - return memberNames.indexOf(name) !== -1; + return memberNames.includes(name); } }`; } diff --git a/scripts/run_test.js b/scripts/run_test.js index 181f682d..9e22fd55 100644 --- a/scripts/run_test.js +++ b/scripts/run_test.js @@ -27,7 +27,7 @@ utils const testDir = path.join(__dirname, '../test/'); // eslint-disable-next-line const tests = fs.readdirSync(testDir).filter((file) => { - return file.substr(0, 5) === 'test-'; + return file.startsWith('test-'); }); // eslint-disable-next-line @@ -37,7 +37,7 @@ utils let ignoredCases = blocklist[os.type()]; tests.forEach((test) => { - if (ignoredCases.indexOf(test) === -1) { + if (!ignoredCases.includes(test)) { mocha.addFile(path.join(testDir, test)); } });