MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / dictionaryFromExpression

Function dictionaryFromExpression

src/math-json/utils.ts:219–250  ·  view source on GitHub ↗
(
  expr: MathJsonExpression | null
)

Source from the content-addressed store, hash-verified

217// - a `Dictionary` expression
218// - a `KeyValuePair` or `Tuple` expression
219export 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
252export function dictionaryFromEntries(
253 dict: Record<string, MathJsonExpression>

Callers 4

dictionary.test.tsFile · 0.90
serializeExpressionFunction · 0.90
countLeavesFunction · 0.85

Calls 5

isDictionaryObjectFunction · 0.85
keyValuePairFunction · 0.85
operandsFunction · 0.85
operatorFunction · 0.70

Tested by

no test coverage detected