Skip to content

Commit

Permalink
Add safer isArray function
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran committed Aug 21, 2024
1 parent d656a38 commit 4291789
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions e2e/playwright/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import * as TOML from '@iarna/toml'
import { SaveSettingsPayload } from 'lib/settings/settingsTypes'
import { SETTINGS_FILE_NAME } from 'lib/constants'
import { isArray } from 'lib/utils'

type TestColor = [number, number, number]
export const TEST_COLORS = {
Expand Down Expand Up @@ -505,15 +506,15 @@ const _makeTemplate = (
templateParts: TemplateStringsArray,
...options: TemplateOptions
) => {
const length = Math.max(...options.map((a) => (Array.isArray(a) ? a[0] : 0)))
const length = Math.max(...options.map((a) => (isArray(a) ? a[0] : 0)))
let reExpTemplate = ''
for (let i = 0; i < length; i++) {
const currentStr = templateParts.map((str, index) => {
const currentOptions = options[index]
return (
escapeRegExp(str) +
String(
Array.isArray(currentOptions)
isArray(currentOptions)
? currentOptions[i]
: typeof currentOptions === 'number'
? currentOptions
Expand Down
12 changes: 8 additions & 4 deletions src/clientSideScene/sceneEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import {
changeSketchArguments,
updateStartProfileAtArgs,
} from 'lang/std/sketch'
import { isOverlap, normaliseAngle, roundOff, throttle } from 'lib/utils'
import { isArray, isOverlap, normaliseAngle, roundOff, throttle } from 'lib/utils'
import {
addStartProfileAt,
createArrayExpression,
Expand All @@ -99,6 +99,7 @@ import {
import { getThemeColorForThreeJs } from 'lib/theme'
import { err, trap } from 'lib/trap'
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'
import { Point3d } from 'wasm-lib/kcl/bindings/Point3d'

type DraftSegment = 'line' | 'tangentialArcTo'

Expand All @@ -116,6 +117,8 @@ export const SEGMENT_WIDTH_PX = 1.6
export const HIDE_SEGMENT_LENGTH = 75 // in pixels
export const HIDE_HOVER_SEGMENT_LENGTH = 60 // in pixels

type Vec3Array = [number, number, number]

// This singleton Class is responsible for all of the things the user sees and interacts with.
// That mostly mean sketch elements.
// Cameras, controls, raycasters, etc are handled by sceneInfra
Expand Down Expand Up @@ -384,7 +387,7 @@ export class SceneEntities {
if (err(sketchGroup)) return Promise.reject(sketchGroup)
if (!sketchGroup) return Promise.reject('sketchGroup not found')

if (!Array.isArray(sketchGroup?.value))
if (!isArray(sketchGroup?.value))
return {
truncatedAst,
programMemoryOverride,
Expand Down Expand Up @@ -1838,6 +1841,7 @@ export function getSketchQuaternion(
})
if (err(sketchGroup)) return sketchGroup
const zAxis = sketchGroup?.on.zAxis || sketchNormalBackUp
if (!zAxis) return Error('SketchGroup zAxis not found')

return getQuaternionFromZAxis(massageFormats(zAxis))
}
Expand Down Expand Up @@ -1962,8 +1966,8 @@ export function getQuaternionFromZAxis(zAxis: Vector3): Quaternion {
return quaternion
}

function massageFormats(a: any): Vector3 {
return Array.isArray(a)
function massageFormats(a: Vec3Array | Point3d): Vector3 {
return isArray(a)
? new Vector3(a[0], a[1], a[2])
: new Vector3(a.x, a.y, a.z)
}
7 changes: 7 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { v4 } from 'uuid'

export const uuidv4 = v4

/**
* A safer type guard for arrays since the built-in Array.isArray() asserts `any[]`.
*/
export function isArray(val: any): val is unknown[] {
return Array.isArray(val)
}

export function isOverlap(a: SourceRange, b: SourceRange) {
const [startingRange, secondRange] = a[0] < b[0] ? [a, b] : [b, a]
const [lastOfFirst, firstOfSecond] = [startingRange[1], secondRange[0]]
Expand Down

0 comments on commit 4291789

Please sign in to comment.