public static PipeValue Map(this PipeValue value, PipeLambda map) { CommonHelper.ConfirmNotNull(map, "map"); PipeValuePropertyEnumerator enumerator = value.GetEnumerator(); PipeValueObjectBuilder collection = new PipeValueObjectBuilder(value.IsArray); while (enumerator.MoveNext()) { collection.Add(map.Invoke(enumerator), enumerator.CurrentKey); } return(collection); }
public static PipeValue First(this PipeValue value, PipeLambda fn, bool returnBoolean = false, bool negate = false) { CommonHelper.ConfirmNotNull(fn, "fn"); PipeValuePropertyEnumerator enumerator = value.GetEnumerator(); while (enumerator.MoveNext()) { if (negate ^ (bool)fn.Invoke(enumerator)) { return(returnBoolean ? true : enumerator.CurrentValue); } } return(returnBoolean ? false : PipeValue.Undefined); }
public static PipeValue Where(this PipeValue value, PipeLambda filter) { CommonHelper.ConfirmNotNull(filter, "filter"); PipeValuePropertyEnumerator enumerator = value.GetEnumerator(); PipeValueObjectBuilder collection = new PipeValueObjectBuilder(value.IsArray); while (enumerator.MoveNext()) { if ((bool)filter.Invoke(enumerator)) { collection.Add(enumerator.CurrentValue, enumerator.CurrentKey); } } return(collection); }
internal PipeValue Evaluate() { bool isValid; PipeValue input = ObjectPath.Empty.Evaluate(context); PipeValue value = (pipe.Count > start ? pipe[start].ObjectPath : ObjectPath.Empty).Evaluate(context, true, out isValid); List <PipeValue> returnArray = new List <PipeValue>(); i = start + (isValid ? 1 : 0); while (i <= end) { int startpos = i; string name = pipe[i++].TextValue; if (name == "|") { returnArray.Add(value); value = HasArgument(true) ? TakeArgument() : input; } else if (name == "&&" || name == "||") { if ((bool)value ^ (name == "&&")) { break; } PipeLambda fn = TakeFunction(); if (fn != null) { value = fn.Invoke(input); } else if (HasArgument(true)) { value = TakeArgument(); } else { value = input; } } else { this.Value = value; try { PipeFunction fn; try { fn = context.ResolveFunction(name); } catch (InvalidPipeFunctionException) { if (startpos == start) { value = PipeValue.Undefined; continue; } throw; } value = fn.Invoke(this); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } PipeExecutionException wrappedException = new PipeExecutionException(ex.Message, ex, new WaterpipeException.CallSite { InputString = context.InputString, ConstructStart = pipe.StartIndex, ConstructEnd = pipe.EndIndex, HighlightStart = pipe[startpos].StartIndex, HighlightEnd = pipe[i - 1].EndIndex }); context.AddException(wrappedException); } } } if (returnArray.Count > 0) { returnArray.Add(value); return(new PipeValueObjectBuilder(returnArray, true)); } return(value); }
/// <summary> /// Replaces occurences of substrings that matches the pattern by the value returned from the invocation of pipe function argument. /// </summary> /// <param name="input">Input string.</param> /// <param name="replacement">A pipe function argument.</param> /// <returns></returns> public string Replace(string input, PipeLambda replacement) { return(Replace(input, new PipeLambdaMatchEvaluator(input, replacement).MatchEvaluator)); }
public PipeLambdaMatchEvaluator(string input, PipeLambda replacement) { this.input = input; this.replacement = replacement; }