< Summary

Information
Class: Elsa.Expressions.JavaScript.Helpers.ObjectConverterHelper
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Helpers/ObjectConverterHelper.cs
Line coverage
88%
Covered lines: 22
Uncovered lines: 3
Coverable lines: 25
Total lines: 68
Line coverage: 88%
Branch coverage
88%
Covered branches: 23
Total branches: 26
Branch coverage: 88.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ProcessVariableValue(...)100%44100%
ConvertToJsObject(...)100%11100%
ConvertToJsValue(...)86.36%242284.21%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Helpers/ObjectConverterHelper.cs

#LineLine coverage
 1using System.Collections;
 2using System.Dynamic;
 3using Elsa.Extensions;
 4using Jint;
 5using Jint.Native;
 6using Jint.Native.Object;
 7
 8namespace Elsa.Expressions.JavaScript.Helpers;
 9
 10internal static class ObjectConverterHelper
 11{
 12    public static object? ProcessVariableValue(Engine engine, object? variableValue)
 13    {
 1614        if (variableValue == null)
 115            return null;
 16
 1517        if (variableValue is not ExpandoObject expandoObject)
 718            return variableValue;
 19
 820        return ConvertToJsObject(engine, expandoObject);
 21    }
 22
 23    public static ObjectInstance ConvertToJsObject(Engine engine, IDictionary<string, object?> expando)
 24    {
 25        // CreateFromEntries defines the same writable, enumerable and configurable properties the explicit
 26        // descriptor used to spell out, but builds the object directly in the engine's hidden-class
 27        // representation rather than filling a per-object property dictionary. Two wins, both within a single
 28        // evaluation: it drops the second descriptor allocation the explicit one caused inside Jint's
 29        // ValidateAndApplyPropertyDescriptor, and objects presenting the same keys — sibling variables, and the
 30        // nested objects of a repeated payload shape — share one layout instead of each carrying its own
 31        // descriptors. Nothing carries across evaluations: hidden classes are interned per engine and Elsa
 32        // builds a fresh engine every time.
 2933        return JsObject.CreateFromEntries(engine, expando.Select(kvp => new KeyValuePair<string, JsValue>(kvp.Key, Conve
 34    }
 35
 36    private static JsValue ConvertToJsValue(Engine engine, object? value)
 37    {
 2438        if (value == null)
 039            return JsValue.Null;
 40
 2441        if (value is IDictionary<string, object?> dict)
 042            return ConvertToJsObject(engine, dict);
 43
 2444        var valueType = value.GetType();
 2445        if (valueType.IsCollectionType())
 46        {
 147            var list = (ICollection)value;
 148            var jsArray = engine.Intrinsics.Array.Construct(list.Count);
 149            var index = 0;
 50
 851            foreach (var item in list)
 352                jsArray.Set(index++, ConvertToJsValue(engine, item), true);
 53
 154            return jsArray;
 55        }
 56
 2357        if (value is string str)
 958            return JsValue.FromObject(engine, str);
 59
 1460        if (value is int or double or float or decimal)
 961            return JsValue.FromObject(engine, Convert.ToDouble(value));
 62
 563        if (value is bool b)
 064            return JsValue.FromObject(engine, b);
 65
 566        return JsValue.FromObject(engine, value);
 67    }
 68}