public static PipeValue Replace(PipeContext context) { string needle = (string)context.TakeArgument(); string replacement = null; PipeLambda fn = context.TakeFunction(); if (fn == null) { replacement = (string)context.TakeArgument(); } EcmaScriptRegex re; if (EcmaScriptRegex.TryParse(needle, out re)) { if (fn != null) { return(re.Replace((string)context.Value, fn)); } return(re.Replace((string)context.Value, replacement)); } string str = (string)context.Value; int pos = str.IndexOf(needle); if (pos >= 0) { return(String.Concat(str.Substring(0, pos), replacement, str.Substring(pos + needle.Length))); } return(context.Value); }
public static PipeValue Test(PipeContext context) { PipeLambda testFn = context.TakeFunction() ?? context.TakeArgumentAsConstantFunction(); PipeLambda trueFn = context.TakeFunction() ?? context.TakeArgumentAsConstantFunction(); PipeLambda falseFn = context.TakeFunction() ?? context.TakeArgumentAsConstantFunction(); return(testFn.Invoke(context.Value) ? trueFn.Invoke(context.Value) : falseFn.Invoke(context.Value)); }
public static PipeValue Let(PipeContext context) { while (context.HasArgument()) { string name = (string)context.TakeArgument(); PipeLambda fn = context.TakeFunction(); if (fn != null) { context.Globals[name] = fn.Invoke(context.Value); } else { context.Globals[name] = context.TakeArgument(); } } return(PipeValue.Undefined); }
public static PipeValue SortBy(PipeContext context) { List <PipeValue> array = new List <PipeValue>(); PipeLambda fn = context.TakeFunction() ?? context.TakeArgumentAsKeyFunction(); PipeValuePropertyEnumerator enumerator = context.Value.GetEnumerator(); while (enumerator.MoveNext()) { array.Add(new PipeValue(new object[] { fn.Invoke(enumerator), enumerator.CurrentKey })); } array.Sort(PipeValueObjectComparer.Default); PipeValueObjectBuilder collection = new PipeValueObjectBuilder(context.Value.IsArray); foreach (PipeValue value in array) { string key = (string)value["1"]; collection.Add(context.Value[key], key); } return(collection); }
public static PipeValue Sum(PipeContext context) { PipeValue result = PipeValue.Undefined; PipeLambda fn = context.TakeFunction(); if (fn == null) { result = context.TakeArgument(); fn = context.TakeFunction(); } if (fn == null) { if (context.HasArgument()) { fn = context.TakeArgumentAsKeyFunction(); } else { fn = new PipeLambda((obj, i) => obj); } } PipeValuePropertyEnumerator enumerator = context.Value.GetEnumerator(); while (enumerator.MoveNext()) { if (result != PipeValue.Undefined) { result = result + fn.Invoke(enumerator); } else { result = fn.Invoke(enumerator); } } return(result); }