MCPcopy Index your code
hub / github.com/simstudioai/sim / parseOptionalNumberInput

Function parseOptionalNumberInput

apps/sim/blocks/utils.ts:469–510  ·  view source on GitHub ↗
(
  value: unknown,
  label: string,
  options: ParseOptionalNumberInputOptions = {}
)

Source from the content-addressed store, hash-verified

467 * Returns `undefined` for empty values and throws when the provided value is not a valid number.
468 */
469export function parseOptionalNumberInput(
470 value: unknown,
471 label: string,
472 options: ParseOptionalNumberInputOptions = {}
473): number | undefined {
474 if (value === undefined || value === null || value === '') {
475 return undefined
476 }
477
478 let parsed: number
479
480 if (typeof value === 'number') {
481 parsed = value
482 } else if (typeof value === 'string') {
483 const trimmed = value.trim()
484 if (trimmed.length === 0) {
485 return undefined
486 }
487
488 parsed = Number(trimmed)
489 } else {
490 throw new Error(`Invalid number for ${label}: expected a valid number.`)
491 }
492
493 if (!Number.isFinite(parsed)) {
494 throw new Error(`Invalid number for ${label}: expected a valid number.`)
495 }
496
497 if (options.integer && !Number.isInteger(parsed)) {
498 throw new Error(`Invalid number for ${label}: expected an integer.`)
499 }
500
501 if (options.min != null && parsed < options.min) {
502 throw new Error(`${label} must be at least ${options.min}.`)
503 }
504
505 if (options.max != null && parsed > options.max) {
506 throw new Error(`${label} must be at most ${options.max}.`)
507 }
508
509 return parsed
510}
511
512/**
513 * Parses an optional boolean block input value.

Callers 7

utils.test.tsFile · 0.90
crowdstrike.tsFile · 0.90
codepipeline.tsFile · 0.90
shopify.tsFile · 0.90
a2a.tsFile · 0.90
trello.tsFile · 0.90
executeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected