private static void BuildQuery(NameValueCollection nv, PipeValue obj, string prefix) { if (prefix != null && obj.IsArray) { PipeValuePropertyEnumerator enumerator = obj.GetEnumerator(); while (enumerator.MoveNext()) { if (Regex.IsMatch(prefix, @"\[\]$")) { nv.Add(prefix, enumerator.CurrentValue.ToString()); } else { BuildQuery(nv, enumerator.CurrentValue, prefix + "[" + (enumerator.CurrentValue.Type == PipeValueType.Object && (bool)enumerator.CurrentValue ? enumerator.CurrentKey : "") + "]"); } } } else if (obj.Type == PipeValueType.Object) { PipeValuePropertyEnumerator enumerator = obj.GetEnumerator(); while (enumerator.MoveNext()) { BuildQuery(nv, enumerator.CurrentValue, prefix != null ? prefix + "[" + enumerator.CurrentKey + "]" : enumerator.CurrentKey); } } else { nv.Add(prefix, obj.ToString()); } }
public static PipeValue RemoveEnd(PipeValue str, PipeValue needle) { string strStr = (string)str; string strNeedle = (string)needle; return(strStr.Substring(-strNeedle.Length) == needle ? ((string)str).Substring(0, -strNeedle.Length) : strStr); }
public static PipeValue PadEnd(PipeValue str, PipeValue needle) { string strStr = (string)str; string strNeedle = (string)needle; return(strStr.Substring(-strNeedle.Length) != needle ? str + needle : str); }
public static PipeValue RemoveStart(PipeValue str, PipeValue needle) { string strStr = (string)str; string strNeedle = (string)needle; return(strStr.Substring(0, strNeedle.Length) == needle?strStr.Substring(strNeedle.Length) : strStr); }
private static PipeValue ParseValue(string str) { switch (str) { case "true": return(true); case "false": return(false); case "undefined": return(PipeValue.Undefined); case "null": return(PipeValue.Null); case "0": return(0); } PipeValue number = +new PipeValue(str); if (!Double.NaN.Equals(number.Value)) { return(number); } return(str); }
public static PipeValue PadStart(PipeValue str, PipeValue needle) { string strStr = (string)str; string strNeedle = (string)needle; return(strStr.Substring(0, strNeedle.Length) != needle ? needle + str : str); }
public static PipeValue FormatQuery(PipeValue value) { NameValueCollection nv = HttpUtility.ParseQueryString(""); BuildQuery(nv, value, null); return(nv.ToString()); }
public static PipeValue Substr(PipeValue str, PipeValue start, PipeValue len) { if (!len.IsEvallable) { return(((string)str).Substring((int)start)); } return(((string)str).Substring((int)start, (int)len)); }
public static object Evaluate(TokenList tokens, PipeValue value, EvaluateOptions options, out PipeExecutionException[] exceptions) { CommonHelper.ConfirmNotNull(tokens, "tokens"); CommonHelper.ConfirmNotNull(options, "options"); EvaluationContext context = new EvaluationContext(tokens, value, options); object returnValue = context.Evaluate(); exceptions = context.Exceptions; return(returnValue); }
public static PipeValue Length(PipeValue obj) { if (obj.IsEvallable) { if (obj.HasProperty("length")) { return(obj["length"]); } } return(0); }
public static PipeValue CutBefore(PipeValue str, PipeValue needle) { string strStr = (string)str; string strNeedle = (string)needle; int pos = strStr.IndexOf((string)needle); if (pos >= 0) { return(strStr.Substring(0, pos)); } return(strStr); }
public static PipeValue CutAfterLast(PipeValue str, PipeValue needle) { string strStr = (string)str; string strNeedle = (string)needle; int pos = strStr.LastIndexOf((string)needle); if (pos >= 0) { return(strStr.Substring(strNeedle.Length + pos)); } return(strStr); }
public static PipeValue FormatDate(PipeValue timestamp, PipeValue format) { DateTime d; if (timestamp.Type == PipeValueType.String) { DateTime.TryParse(timestamp.ToString(), out d); } else { d = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(timestamp.ToDouble()); } return(d.ToLocalTime().ToString((string)format)); }
public static PipeValue Keys(PipeValue obj) { if (obj.IsArray) { return(new PipeValue(Enumerable.Range(0, (int)obj["length"]))); } List <string> keys = new List <string>(); IEnumerator enumerator = obj.GetEnumerator(); while (enumerator.MoveNext()) { keys.Add((string)enumerator.Current); } return(new PipeValue(keys)); }
public PipeValue Evaluate(EvaluationContext context, bool checkValid, out bool isValid) { CommonHelper.ConfirmNotNull(context, "context"); isValid = true; PipeValue value = context.CurrentValue; if (this.Count > 0) { ConstantObjectPath m = this[0] as ConstantObjectPath; if (m != null) { switch (m.Value) { case "#": return(value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).CurrentKey : PipeValue.Undefined); case "##": return(value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).CurrentIndex : PipeValue.Undefined); case "#count": return(value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).Count : 0); } } } value = value.Value is PipeValuePropertyEnumerator ? ((PipeValuePropertyEnumerator)value.Value).CurrentValue : value; if (this.Count > 0) { ConstantObjectPath m = this[0] as ConstantObjectPath; if (m != null) { int intValue; bool hasPropertyName = value.IsEvallable && (value.Type != PipeValueType.Object ? value[m.Value] != PipeValue.Undefined : (value.HasProperty(m.Value) || (value.IsArray && Int32.TryParse(m.Value, out intValue)))); if (checkValid && !hasPropertyName && !context.Globals.ContainsKey(m.Value)) { isValid = false; return(value); } value = hasPropertyName ? value[m.Value] : context.Globals[m.Value]; } } for (int i = 1, len = this.Count; i < len && value.IsEvallable; i++) { string name = this[i] is ConstantObjectPath ? ((ConstantObjectPath)this[i]).Value : (string)this[i].Evaluate(context); value = value[name]; } return(value); }
private EvaluationContext(TokenList tokens, PipeValue data, EvaluateOptions options) { CommonHelper.ConfirmNotNull(tokens, "tokens"); CommonHelper.ConfirmNotNull(options, "options"); this.Options = options; this.tokens = tokens; this.data = data; objStack.Push(data); if (options.OutputXml) { XmlDocument doc = new XmlDocument(); xmlStack.Push(doc.DocumentElement); } this.Globals = new PipeGlobal(options.Globals); this.Globals["_"] = data; }
public static PipeValue FormatPrintf(PipeValue value, PipeValue format) { StringBuilder sb = new StringBuilder(); switch (value.Type) { case PipeValueType.String: sb.EnsureCapacity(((string)value).Length); _snwprintf_s(sb, (IntPtr)sb.MaxCapacity, (IntPtr)32, format.ToString(), (string)value); break; case PipeValueType.Number: sb.EnsureCapacity(100); _snwprintf_s(sb, (IntPtr)100, (IntPtr)32, format.ToString(), (double)value); break; default: sb.EnsureCapacity(100); _snwprintf_s(sb, (IntPtr)100, (IntPtr)32, format.ToString(), value.ToString()); break; } return(sb.ToString()); }
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); }
public static PipeValue Round(PipeValue value) { return(Math.Round((double)value)); }
public static PipeValue FormatJson(PipeValue value) { return(JsonConvert.SerializeObject(value)); }
public static PipeValue Min(PipeValue a, PipeValue b) { return(Math.Max((double)a, (double)b)); }
public static PipeValue Sort(PipeValue value) { PipeValue[] arr = value.CopyAsArray(); Array.Sort(arr, PipeValueObjectComparer.Default); return(new PipeValueObjectBuilder(arr)); }
public static PipeValue Less(PipeValue a, PipeValue b) { return(PipeValueObjectComparer.Default.Compare(a, b) < 0); }
public static PipeValue Ceil(PipeValue value) { return(Math.Ceiling((double)+value)); }
public static PipeValue Floor(PipeValue value) { return(Math.Floor((double)value)); }
public static PipeValue Mod(PipeValue a, PipeValue b) { return((+a) % (+b)); }
public static PipeValue More(PipeValue a, PipeValue b) { return(PipeValueObjectComparer.Default.Compare(a, b) > 0); }
public static PipeValue Join(PipeValue value, PipeValue str) { PipeValue[] arr = value.CopyAsArray(); return(String.Join((string)str, arr)); }
private void WriteJson(JsonWriter writer, PipeValue value, JsonSerializer serializer) { switch (value.Type) { case PipeValueType.Undefined: break; case PipeValueType.String: writer.WriteValue((string)value.Value); break; case PipeValueType.Number: switch (Type.GetTypeCode(value.Value.GetType())) { case TypeCode.Byte: writer.WriteValue((byte)value.Value); break; case TypeCode.Char: writer.WriteValue((char)value.Value); break; case TypeCode.Decimal: writer.WriteValue((decimal)value.Value); break; case TypeCode.Double: writer.WriteValue((double)value.Value); break; case TypeCode.Int16: writer.WriteValue((short)value.Value); break; case TypeCode.Int32: writer.WriteValue((int)value.Value); break; case TypeCode.Int64: writer.WriteValue((long)value.Value); break; case TypeCode.SByte: writer.WriteValue((sbyte)value.Value); break; case TypeCode.Single: writer.WriteValue((float)value.Value); break; case TypeCode.UInt16: writer.WriteValue((ushort)value.Value); break; case TypeCode.UInt32: writer.WriteValue((uint)value.Value); break; case TypeCode.UInt64: writer.WriteValue((ulong)value.Value); break; } break; case PipeValueType.Boolean: writer.WriteValue((bool)value.Value); break; case PipeValueType.Object: if (!value.IsEvallable) { writer.WriteNull(); } else if (value.IsArray) { writer.WriteStartArray(); foreach (object item in (IEnumerable)value.Value) { WriteJson(writer, new PipeValue(item), serializer); } writer.WriteEndArray(); } else { writer.WriteStartObject(); PipeValuePropertyEnumerator enumerator = value.GetEnumerator(); while (enumerator.MoveNext()) { if (enumerator.CurrentValue != PipeValue.Undefined) { writer.WritePropertyName(enumerator.CurrentKey); WriteJson(writer, enumerator.CurrentValue, serializer); } } writer.WriteEndObject(); } break; } }
public static PipeValue Reverse(PipeValue value) { PipeValue[] arr = value.CopyAsArray(); Array.Reverse(arr); return(new PipeValueObjectBuilder(arr)); }