Skip to content

Commit

Permalink
refactor: Split utils.ts into multiple files in a utils/ directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
jswalden committed Nov 13, 2024
1 parent ae46e3d commit 5cdb8c6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 50 deletions.
13 changes: 13 additions & 0 deletions src/utils/byte.ts
Original file line number Diff line number Diff line change
@@ -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')
}
}
}
11 changes: 11 additions & 0 deletions src/utils/pretty.ts
Original file line number Diff line number Diff line change
@@ -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(' ')}]`
}
19 changes: 19 additions & 0 deletions src/utils/promise-with-resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** Return an unresolved promise and resolve/reject functions for it. */
export function promiseWithResolvers<T>(): {
promise: Promise<T>
resolve: (value: T) => void
reject: (reason?: any) => void
} {
let promiseResolve: (value: T) => void
let promiseReject: (reason?: any) => void
const promise = new Promise<T>((resolve: (value: T) => void, reject: (reason?: any) => void) => {
promiseResolve = resolve
promiseReject = reject
})

return {
promise,
resolve: promiseResolve!,
reject: promiseReject!,
}
}
2 changes: 1 addition & 1 deletion src/visca/__tests__/camera-interactions/matchers.ts
Original file line number Diff line number Diff line change
@@ -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)}\\]`)
Expand Down
2 changes: 1 addition & 1 deletion src/visca/__tests__/camera-interactions/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/visca/command.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/visca/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand Down
45 changes: 0 additions & 45 deletions src/visca/utils.ts

This file was deleted.

0 comments on commit 5cdb8c6

Please sign in to comment.