surshar / claude-code
Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
Built by Anthropic · github.com/anthropics
| 1 | import { CHOICES_LIMIT } from '../choices-limit' |
| 2 | import { isRecord } from '../is-record' |
| 3 | import { refusal } from '../refusal' |
| 4 | import { TOKEN } from '../token' |
| 5 | |
| 6 | /** |
| 7 | * One property's value as it goes into the metadata: a finite number, a |
| 8 | * boolean, or a Choice's value chosen from its token list. |
| 9 | * |
| 10 | * A bare string is refused: free text never reaches the row. |
| 11 | * |
| 12 | * @param key the property's key, named in a refusal |
| 13 | * @param value what the caller passed under it |
| 14 | * @returns the value as stored: the boolean or finite number unchanged, or the |
| 15 | * chosen member when value is a Choice |
| 16 | */ |
| 17 | export function checkedValue( |
| 18 | key: string, |
| 19 | value: unknown, |
| 20 | ): string | number | boolean { |
| 21 | if (typeof value === 'boolean') { |
| 22 | return value |
| 23 | } |
| 24 | |
| 25 | if (typeof value === 'number') { |
| 26 | if (Number.isFinite(value)) { |
| 27 | return value |
| 28 | } |
| 29 | |
| 30 | throw refusal(`props.${key}: a number is finite`) |
| 31 | } |
| 32 | |
| 33 | if (typeof value === 'string') { |
| 34 | throw refusal( |
| 35 | `props.${key}: free text is refused; a string is a Choice, ` + |
| 36 | `{ value, of: [...] }`, |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | if (!isRecord(value) || !Array.isArray(value.of)) { |
| 41 | throw refusal( |
| 42 | `props.${key}: a value is a finite number, a boolean, or a Choice, ` + |
| 43 | `{ value, of: [...] }`, |
| 44 | ) |
| 45 | } |
| 46 | |
| 47 | const members = value.of |
| 48 | const chosen = value.value |
| 49 | const isTokenList = |
| 50 | members.length > 0 && |
| 51 | members.length <= CHOICES_LIMIT && |
| 52 | members.every(member => typeof member === 'string' && TOKEN.test(member)) |
| 53 | |
| 54 | if (!isTokenList) { |
| 55 | throw refusal( |
| 56 | `props.${key}.of: a list of 1 to ${CHOICES_LIMIT} ` + `snake_case tokens`, |
| 57 | ) |
| 58 | } |
| 59 | |
| 60 | if (typeof chosen !== 'string' || !members.includes(chosen)) { |
| 61 | throw refusal(`props.${key}.value: one of the members of \`of\``) |
| 62 | } |
| 63 | |
| 64 | return chosen |
| 65 | } |