( expr: MathJsonExpression | null )
| 217 | // - a `Dictionary` expression |
| 218 | // - a `KeyValuePair` or `Tuple` expression |
| 219 | export function dictionaryFromExpression( |
| 220 | expr: MathJsonExpression | null |
| 221 | ): null | MathJsonDictionaryObject { |
| 222 | if (expr === null) return null; |
| 223 | |
| 224 | if (isDictionaryObject(expr)) return expr as MathJsonDictionaryObject; |
| 225 | |
| 226 | // Is it a KeyValuePair or Tuple expression? |
| 227 | const kv = keyValuePair(expr); |
| 228 | if (kv) |
| 229 | return { |
| 230 | dict: { [kv[0]]: expressionToDictionaryValue(kv[1]) ?? 'Nothing' }, |
| 231 | } as unknown as MathJsonDictionaryObject; |
| 232 | |
| 233 | // Is it a Dictionary expression? |
| 234 | if (operator(expr) === 'Dictionary') { |
| 235 | const dict: Record<string, DictionaryValue> = {}; |
| 236 | // `operands()` is already 0-based and head-stripped — iterate from the |
| 237 | // first operand (the loop used to start at 1, silently dropping the first |
| 238 | // dictionary entry). |
| 239 | for (const op of operands(expr)) { |
| 240 | const kv = keyValuePair(op); |
| 241 | if (kv) { |
| 242 | dict[kv[0]] = expressionToDictionaryValue(kv[1]) ?? 'Nothing'; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return { dict } as unknown as MathJsonDictionaryObject; |
| 247 | } |
| 248 | |
| 249 | return null; |
| 250 | } |
| 251 | |
| 252 | export function dictionaryFromEntries( |
| 253 | dict: Record<string, MathJsonExpression> |
no test coverage detected