From 5cdb8c60ab8111fef028bd51d9012712e9d10b10 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Wed, 13 Nov 2024 13:57:17 -0800 Subject: [PATCH] refactor: Split `utils.ts` into multiple files in a `utils/` directory. --- src/utils/byte.ts | 13 ++++++ src/utils/pretty.ts | 11 +++++ src/utils/promise-with-resolvers.ts | 19 ++++++++ .../__tests__/camera-interactions/matchers.ts | 2 +- .../__tests__/camera-interactions/run-test.ts | 2 +- src/visca/command.ts | 3 +- src/visca/port.ts | 4 +- src/visca/utils.ts | 45 ------------------- 8 files changed, 49 insertions(+), 50 deletions(-) create mode 100644 src/utils/byte.ts create mode 100644 src/utils/pretty.ts create mode 100644 src/utils/promise-with-resolvers.ts delete mode 100644 src/visca/utils.ts diff --git a/src/utils/byte.ts b/src/utils/byte.ts new file mode 100644 index 0000000..8e72168 --- /dev/null +++ b/src/utils/byte.ts @@ -0,0 +1,13 @@ +/** Determine if a number is a byte. */ +function isByte(b: number): boolean { + return (b & 0xff) === b +} + +/** Ensure every number in bytes is an actual byte value. */ +export function checkBytes(bytes: readonly number[]): void { + for (const b of bytes) { + if (!isByte(b)) { + throw new RangeError('non-byte found') + } + } +} diff --git a/src/utils/pretty.ts b/src/utils/pretty.ts new file mode 100644 index 0000000..37e7815 --- /dev/null +++ b/src/utils/pretty.ts @@ -0,0 +1,11 @@ +/** Debug representation of a byte value. */ +function prettyByte(byte: number): string { + return ('0' + byte.toString(16).toUpperCase()).slice(-2) +} + +/** Debug representation of a byte list. */ +export function prettyBytes(bytes: readonly number[] | Uint8Array): string { + // Explicitly apply the Array map function so this works on both arrays and + // typed arrays. + return `[${Array.prototype.map.call(bytes, prettyByte).join(' ')}]` +} diff --git a/src/utils/promise-with-resolvers.ts b/src/utils/promise-with-resolvers.ts new file mode 100644 index 0000000..30c0572 --- /dev/null +++ b/src/utils/promise-with-resolvers.ts @@ -0,0 +1,19 @@ +/** Return an unresolved promise and resolve/reject functions for it. */ +export function promiseWithResolvers(): { + promise: Promise + resolve: (value: T) => void + reject: (reason?: any) => void +} { + let promiseResolve: (value: T) => void + let promiseReject: (reason?: any) => void + const promise = new Promise((resolve: (value: T) => void, reject: (reason?: any) => void) => { + promiseResolve = resolve + promiseReject = reject + }) + + return { + promise, + resolve: promiseResolve!, + reject: promiseReject!, + } +} diff --git a/src/visca/__tests__/camera-interactions/matchers.ts b/src/visca/__tests__/camera-interactions/matchers.ts index 1b89131..f62e281 100644 --- a/src/visca/__tests__/camera-interactions/matchers.ts +++ b/src/visca/__tests__/camera-interactions/matchers.ts @@ -1,4 +1,4 @@ -import { prettyBytes } from '../../utils.js' +import { prettyBytes } from '../../../utils/pretty.js' export function MatchVISCABytes(bytes: readonly number[]): RegExp { return new RegExp(`\\[${prettyBytes(bytes).slice(1, -1)}\\]`) diff --git a/src/visca/__tests__/camera-interactions/run-test.ts b/src/visca/__tests__/camera-interactions/run-test.ts index 66e2cab..f3036d6 100644 --- a/src/visca/__tests__/camera-interactions/run-test.ts +++ b/src/visca/__tests__/camera-interactions/run-test.ts @@ -2,7 +2,7 @@ import { assertNever, type CompanionOptionValues, InstanceStatus, type LogLevel import { type Interaction, type Match } from './interactions.js' import net from 'net' import { type MessageType, type PartialInstance, VISCAPort } from '../../port.js' -import { prettyBytes } from '../../utils.js' +import { prettyBytes } from '../../../utils/pretty.js' /** Turn on extra logging in performing test interactions to debug tests. */ const DEBUG_LOGGING = true diff --git a/src/visca/command.ts b/src/visca/command.ts index a65f7ec..5e35615 100644 --- a/src/visca/command.ts +++ b/src/visca/command.ts @@ -1,5 +1,6 @@ import type { CompanionOptionValues } from '@companion-module/base' -import { checkBytes, prettyBytes } from './utils.js' +import { checkBytes } from '../utils/byte.js' +import { prettyBytes } from '../utils/pretty.js' // TERMINOLOGY NOTE: // Eight bits is a byte. The upper or lower four-bit half of a byte is a diff --git a/src/visca/port.ts b/src/visca/port.ts index 722528c..1e8e016 100644 --- a/src/visca/port.ts +++ b/src/visca/port.ts @@ -7,8 +7,8 @@ import { } from '@companion-module/base' import { checkCommandBytes, type Command, type Inquiry, type Response, responseMatches } from './command.js' import type { PtzOpticsInstance } from '../instance.js' -import { prettyBytes } from './utils.js' -import { promiseWithResolvers } from './utils.js' +import { prettyBytes } from '../utils/pretty.js' +import { promiseWithResolvers } from '../utils/promise-with-resolvers.js' const BLAME_MODULE = 'This is likely a bug in the ptzoptics-visca Companion module. Please ' + diff --git a/src/visca/utils.ts b/src/visca/utils.ts deleted file mode 100644 index 4b332f4..0000000 --- a/src/visca/utils.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** Determine if a number is a byte. */ -function isByte(b: number): boolean { - return (b & 0xff) === b -} - -/** Debug representation of a byte value. */ -function prettyByte(byte: number): string { - return ('0' + byte.toString(16).toUpperCase()).slice(-2) -} - -/** Debug representation of a byte list. */ -export function prettyBytes(bytes: readonly number[] | Uint8Array): string { - // Explicitly apply the Array map function so this works on both arrays and - // typed arrays. - return `[${Array.prototype.map.call(bytes, prettyByte).join(' ')}]` -} - -/** Ensure every number in bytes is an actual byte value. */ -export function checkBytes(bytes: readonly number[]): void { - for (const b of bytes) { - if (!isByte(b)) { - throw new RangeError('non-byte found') - } - } -} - -/** Return an unresolved promise and resolve/reject functions for it. */ -export function promiseWithResolvers(): { - promise: Promise - resolve: (value: T) => void - reject: (reason?: any) => void -} { - let promiseResolve: (value: T) => void - let promiseReject: (reason?: any) => void - const promise = new Promise((resolve: (value: T) => void, reject: (reason?: any) => void) => { - promiseResolve = resolve - promiseReject = reject - }) - - return { - promise, - resolve: promiseResolve!, - reject: promiseReject!, - } -}