예제 #1
0
        public IgnoreResult forEach(ScriptScopeContext scope, object target, JsArrowFunctionExpression arrowExpr)
        {
            var token = arrowExpr.Body;

            scope = scope.Clone();
            if (target is IList list)
            {
                var itemBinding  = arrowExpr.Params[0].Name;
                var indexBinding = arrowExpr.Params.Length > 1 ? arrowExpr.Params[1].Name : ScriptConstants.Index;
                var arrayBinding = arrowExpr.Params.Length > 2 ? arrowExpr.Params[2].Name : null;

                for (var i = 0; i < list.Count; i++)
                {
                    scope.ScopedParams[indexBinding] = i;
                    if (arrayBinding != null)
                    {
                        scope.ScopedParams[arrayBinding] = list;
                    }

                    scope = scope.AddItemToScope(itemBinding, list[i]);
                    token.Evaluate(scope);
                }
            }
            else if (target is IDictionary d)
            {
                if (arrowExpr.Params.Length != 2)
                {
                    throw new NotSupportedException("Dictionary.forEach requires 2 lambda params");
                }

                var keyBinding   = arrowExpr.Params[0].Name;
                var valueBinding = arrowExpr.Params[1].Name;

                foreach (var key in d.Keys)
                {
                    scope.ScopedParams[keyBinding]   = key;
                    scope.ScopedParams[valueBinding] = d[key];
                    token.Evaluate(scope);
                }
            }
            else
            {
                throw new NotSupportedException("Can only use forEach on Lists or Dictionaries");
            }

            return(IgnoreResult.Value);
        }
예제 #2
0
        public int findIndex(ScriptScopeContext scope, IList list, JsArrowFunctionExpression expression)
        {
            var items       = list.AssertEnumerable(nameof(findIndex));
            var itemBinding = expression.Params[0].Name;
            var expr        = expression.Body;

            var i = 0;

            foreach (var item in items)
            {
                scope.AddItemToScope(itemBinding, item, i);
                var result = expr.EvaluateToBool(scope);
                if (result)
                {
                    return(i);
                }
                i++;
            }

            return(-1);
        }
예제 #3
0
 public List <object> flatMap(ScriptScopeContext scope, IList list, JsArrowFunctionExpression expression, int depth) =>
 flat((IList)map(scope, list, expression, null), depth);
예제 #4
0
 public List <object> filter(ScriptScopeContext scope, IList list, JsArrowFunctionExpression expression) =>
     where (scope, list, expression, null).ToList();
예제 #5
0
 public object find(ScriptScopeContext scope, IList list, JsArrowFunctionExpression expression) =>
 first(scope, list, expression, null);
예제 #6
0
 public bool some(ScriptScopeContext scope, IList list, JsArrowFunctionExpression expression) =>
 any(scope, list, expression, null);
예제 #7
0
 public bool every(ScriptScopeContext scope, IList list, JsArrowFunctionExpression expression) =>
 all(scope, list, expression, null);
예제 #8
0
 /// <summary>
 /// Equalses the specified other.
 /// </summary>
 /// <param name="other">The other.</param>
 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
 protected bool Equals(JsArrowFunctionExpression other)
 {
     return(Params.EquivalentTo(other.Params) && Equals(Body, other.Body));
 }