Add safer isArray function (#3606)

* Add safer isArray function

* Fix formatting
This commit is contained in:
Jonathan Tran
2024-08-22 16:08:49 -04:00
committed by GitHub
parent 5df996d877
commit 64500d055a
3 changed files with 24 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import {
import * as TOML from '@iarna/toml' import * as TOML from '@iarna/toml'
import { SaveSettingsPayload } from 'lib/settings/settingsTypes' import { SaveSettingsPayload } from 'lib/settings/settingsTypes'
import { SETTINGS_FILE_NAME } from 'lib/constants' import { SETTINGS_FILE_NAME } from 'lib/constants'
import { isArray } from 'lib/utils'
type TestColor = [number, number, number] type TestColor = [number, number, number]
export const TEST_COLORS = { export const TEST_COLORS = {
@ -582,7 +583,7 @@ const _makeTemplate = (
templateParts: TemplateStringsArray, templateParts: TemplateStringsArray,
...options: TemplateOptions ...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 = '' let reExpTemplate = ''
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
const currentStr = templateParts.map((str, index) => { const currentStr = templateParts.map((str, index) => {
@ -590,7 +591,7 @@ const _makeTemplate = (
return ( return (
escapeRegExp(str) + escapeRegExp(str) +
String( String(
Array.isArray(currentOptions) isArray(currentOptions)
? currentOptions[i] ? currentOptions[i]
: typeof currentOptions === 'number' : typeof currentOptions === 'number'
? currentOptions ? currentOptions

View File

@ -75,7 +75,13 @@ import {
changeSketchArguments, changeSketchArguments,
updateStartProfileAtArgs, updateStartProfileAtArgs,
} from 'lang/std/sketch' } from 'lang/std/sketch'
import { isOverlap, normaliseAngle, roundOff, throttle } from 'lib/utils' import {
isArray,
isOverlap,
normaliseAngle,
roundOff,
throttle,
} from 'lib/utils'
import { import {
addStartProfileAt, addStartProfileAt,
createArrayExpression, createArrayExpression,
@ -99,6 +105,7 @@ import {
import { getThemeColorForThreeJs } from 'lib/theme' import { getThemeColorForThreeJs } from 'lib/theme'
import { err, trap } from 'lib/trap' import { err, trap } from 'lib/trap'
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer' import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer'
import { Point3d } from 'wasm-lib/kcl/bindings/Point3d'
type DraftSegment = 'line' | 'tangentialArcTo' type DraftSegment = 'line' | 'tangentialArcTo'
@ -116,6 +123,8 @@ export const SEGMENT_WIDTH_PX = 1.6
export const HIDE_SEGMENT_LENGTH = 75 // in pixels export const HIDE_SEGMENT_LENGTH = 75 // in pixels
export const HIDE_HOVER_SEGMENT_LENGTH = 60 // 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. // This singleton Class is responsible for all of the things the user sees and interacts with.
// That mostly mean sketch elements. // That mostly mean sketch elements.
// Cameras, controls, raycasters, etc are handled by sceneInfra // Cameras, controls, raycasters, etc are handled by sceneInfra
@ -384,7 +393,7 @@ export class SceneEntities {
if (err(sketchGroup)) return Promise.reject(sketchGroup) if (err(sketchGroup)) return Promise.reject(sketchGroup)
if (!sketchGroup) return Promise.reject('sketchGroup not found') if (!sketchGroup) return Promise.reject('sketchGroup not found')
if (!Array.isArray(sketchGroup?.value)) if (!isArray(sketchGroup?.value))
return { return {
truncatedAst, truncatedAst,
programMemoryOverride, programMemoryOverride,
@ -1838,6 +1847,7 @@ export function getSketchQuaternion(
}) })
if (err(sketchGroup)) return sketchGroup if (err(sketchGroup)) return sketchGroup
const zAxis = sketchGroup?.on.zAxis || sketchNormalBackUp const zAxis = sketchGroup?.on.zAxis || sketchNormalBackUp
if (!zAxis) return Error('SketchGroup zAxis not found')
return getQuaternionFromZAxis(massageFormats(zAxis)) return getQuaternionFromZAxis(massageFormats(zAxis))
} }
@ -1962,8 +1972,6 @@ export function getQuaternionFromZAxis(zAxis: Vector3): Quaternion {
return quaternion return quaternion
} }
function massageFormats(a: any): Vector3 { function massageFormats(a: Vec3Array | Point3d): Vector3 {
return Array.isArray(a) return isArray(a) ? new Vector3(a[0], a[1], a[2]) : new Vector3(a.x, a.y, a.z)
? new Vector3(a[0], a[1], a[2])
: new Vector3(a.x, a.y, a.z)
} }

View File

@ -4,6 +4,13 @@ import { v4 } from 'uuid'
export const uuidv4 = v4 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) { export function isOverlap(a: SourceRange, b: SourceRange) {
const [startingRange, secondRange] = a[0] < b[0] ? [a, b] : [b, a] const [startingRange, secondRange] = a[0] < b[0] ? [a, b] : [b, a]
const [lastOfFirst, firstOfSecond] = [startingRange[1], secondRange[0]] const [lastOfFirst, firstOfSecond] = [startingRange[1], secondRange[0]]