< 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
90%
Covered lines: 28
Uncovered lines: 3
Coverable lines: 31
Total lines: 75
Line coverage: 90.3%
Branch coverage
89%
Covered branches: 25
Total branches: 28
Branch coverage: 89.2%
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%22100%
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 Elsa.Expressions.JavaScript.Options;
 5using Jint;
 6using Jint.Native;
 7using Jint.Native.Object;
 8using Jint.Runtime.Descriptors;
 9using Microsoft.Extensions.Options;
 10
 11namespace Elsa.Expressions.JavaScript.Helpers;
 12
 13internal static class ObjectConverterHelper
 14{
 15
 16
 17    public static object? ProcessVariableValue(Engine engine, object? variableValue)
 18    {
 1319        if (variableValue == null)
 120            return null;
 21
 1222        if (variableValue is not ExpandoObject expandoObject)
 623            return variableValue;
 24
 625        return ConvertToJsObject(engine, expandoObject);
 26    }
 27
 28    public static ObjectInstance ConvertToJsObject(Engine engine, IDictionary<string, object?> expando)
 29    {
 630        var jsObject = engine.Intrinsics.Object.Construct([]);
 31
 4632        foreach (var kvp in expando)
 33        {
 1734            var value = kvp.Value;
 1735            var jsValue = ConvertToJsValue(engine, value);
 1736            var propertyDescriptor = new PropertyDescriptor(jsValue, true, true, true);
 1737            jsObject.DefineOwnProperty(kvp.Key, propertyDescriptor);
 38        }
 39
 640        return jsObject;
 41    }
 42
 43    private static JsValue ConvertToJsValue(Engine engine, object? value)
 44    {
 2045        if (value == null)
 046            return JsValue.Null;
 47
 2048        if (value is IDictionary<string, object?> dict)
 049            return ConvertToJsObject(engine, dict);
 50
 2051        var valueType = value.GetType();
 2052        if (valueType.IsCollectionType())
 53        {
 154            var list = (ICollection)value;
 155            var jsArray = engine.Intrinsics.Array.Construct(list.Count);
 156            var index = 0;
 57
 858            foreach (var item in list)
 359                jsArray.Set(index++, ConvertToJsValue(engine, item), true);
 60
 161            return jsArray;
 62        }
 63
 1964        if (value is string str)
 765            return JsValue.FromObject(engine, str);
 66
 1267        if (value is int or double or float or decimal)
 768            return JsValue.FromObject(engine, Convert.ToDouble(value));
 69
 570        if (value is bool b)
 071            return JsValue.FromObject(engine, b);
 72
 573        return JsValue.FromObject(engine, value);
 74    }
 75}