Ejemplo n.º 1
0
        public IEnumerable <object> skipWhile(ScriptScopeContext scope, object target, object expression, object scopeOptions)
        {
            var items = target.AssertEnumerable(nameof(skipWhile));
            var expr  = scope.AssertExpression(nameof(skipWhile), expression, scopeOptions, out var itemBinding);

            var to           = new List <object>();
            var i            = 0;
            var keepSkipping = true;

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

                if (!keepSkipping)
                {
                    to.Add(item);
                }
            }

            return(to);
        }
Ejemplo n.º 2
0
        public static JsToken AssertExpression(this ScriptScopeContext scope, string filterName, object expression, object scopeOptions, out string itemBinding)
        {
            if (expression is JsArrowFunctionExpression arrowExpr)
            {
                itemBinding = arrowExpr.Params[0].Name;
                return(arrowExpr.Body);
            }

            var literal      = scope.AssertExpression(filterName, expression);
            var scopedParams = scope.GetParamsWithItemBinding(filterName, scopeOptions, out itemBinding);

            var token = literal.GetCachedJsExpression(scope);

            return(token);
        }
Ejemplo n.º 3
0
        public bool all(ScriptScopeContext scope, object target, object expression, object scopeOptions)
        {
            var items = target.AssertEnumerable(nameof(all));
            var expr  = scope.AssertExpression(nameof(all), expression, scopeOptions, out var itemBinding);

            var i = 0;

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

            return(true);
        }
Ejemplo n.º 4
0
        public int count(ScriptScopeContext scope, object target, object expression, object scopeOptions)
        {
            var items = target.AssertEnumerable(nameof(count));
            var expr  = scope.AssertExpression(nameof(count), expression, scopeOptions, out var itemBinding);

            var total = 0;
            var i     = 0;

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

            return(total);
        }
Ejemplo n.º 5
0
        private object applyInternal(string filterName, ScriptScopeContext scope, object target, object expression, object scopeOptions,
                                     Func <double, double, double> fn)
        {
            if (target is double d)
            {
                return(fn(d, expression.ConvertTo <double>()));
            }
            if (target is int i)
            {
                return((int)fn(i, expression.ConvertTo <double>()));
            }
            if (target is long l)
            {
                return((long)fn(l, expression.ConvertTo <double>()));
            }

            var items = target.AssertEnumerable(filterName);
            var total = filterName == nameof(min)
                ? double.MaxValue
                : 0;
            Type itemType = null;

            if (expression != null)
            {
                var expr = scope.AssertExpression(filterName, expression, scopeOptions, out var itemBinding);

                foreach (var item in items)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    scope.AddItemToScope(itemBinding, item);
                    var result = expr.Evaluate(scope);
                    if (result == null)
                    {
                        continue;
                    }
                    if (itemType == null)
                    {
                        itemType = result.GetType();
                    }

                    total = fn(total, result.ConvertTo <double>());
                }
            }
            else
            {
                foreach (var item in items)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    if (itemType == null)
                    {
                        itemType = item.GetType();
                    }
                    total = fn(total, item.ConvertTo <double>());
                }
            }

            if (filterName == nameof(min) && itemType == null)
            {
                return(0);
            }

            if (expression == null && itemType == null)
            {
                itemType = target.GetType().FirstGenericType()?.GetGenericArguments().FirstOrDefault();
            }

            return(itemType == null || itemType == typeof(double)
                ? total
                : total.ConvertTo(itemType));
        }