public static Value Sort(Value[] values) { if (values.Length != 1 && values.Length != 2) { throw new InvalidOperationException(); } Array array = (Array)values[0]; List <Value> results = new List <Value>(array.Count); for (int i = 0, j = array.Count; i < j; i++) { results.Add(array[i]); } if (values.Length == 1) { results.Sort(Compare); return(Array.From(results)); } if (values[1] != null) { switch (values[1].Type) { case ValueType.Number: int index = (int)values[1]; results.Sort((left, right) => Compare(((Array)left)[index], ((Array)right)[index])); return(Array.From(results)); case ValueType.String: string member = (string)values[1]; results.Sort((left, right) => Compare(((Table)left)[member], ((Table)right)[member])); return(Array.From(results)); } } throw new ArgumentOutOfRangeException(); }
public static Value Map(Value[] values) { if (values.Length != 2) { throw new InvalidOperationException(); } Array array = (Array)values[0]; if (values[1] != null) { Value[] results = new Value[array.Count]; switch (values[1].Type) { case ValueType.Number: int index = (int)values[1]; for (int i = 0; i < results.Length; i++) { results[i] = ((Array)array[i])[index]; } return(Array.From(results)); case ValueType.String: string member = (string)values[1]; for (int i = 0; i < results.Length; i++) { results[i] = ((Table)array[i])[member]; } return(Array.From(results)); } } throw new ArgumentOutOfRangeException(); }
public static Value Reverse(Value[] values) { if (values.Length != 1) { throw new InvalidOperationException(); } Array array = (Array)values[0]; Value[] results = new Value[array.Count]; for (int i = 0; i < results.Length; i++) { results[results.Length - i - 1] = array[i]; } return(Array.From(results)); }
public static Value Split(Value[] values) { if (values.Length != 2) { throw new InvalidOperationException(); } string[] split = Regex.Split((string)values[0], (string)values[1], RegexOptions.Singleline | RegexOptions.ExplicitCapture); Value[] result = new Value[split.Length]; for (int i = 0; i < split.Length; i++) { result[i] = split[i]; } return(Array.From(result)); }
public static async Task Prepare() { compile = await files.Compile("start"); List <object> rows = new List <object>(); for (int i = 0; i < 100; i++) { rows.Add(new { ID = i, Message = string.Format("message {0}", i), Print = i % 2 == 0, }); } table = Table.From(new { rows = Array.From(rows), title = "libla" }); }
public static Value Concat(Value[] values) { if (values.Length == 0) { throw new InvalidOperationException(); } List <Value> list = new List <Value>(); for (int i = 0; i < values.Length; i++) { Array array = (Array)values[i]; for (int j = 0, k = array.Count; j < k; j++) { list.Add(array[j]); } } return(Array.From(list)); }
public static Value Split(Value[] values) { if (values.Length < 2) { throw new InvalidOperationException(); } string[] separator = new string[values.Length - 1]; for (int i = 1; i < values.Length; i++) { separator[i - 1] = (string)values[i]; } string[] split = ((string)values[0]).Split(separator, StringSplitOptions.None); Value[] result = new Value[split.Length]; for (int i = 0; i < split.Length; i++) { result[i] = split[i]; } return(Array.From(result)); }
public static Value Limit(Value[] values) { if (values.Length != 2) { throw new InvalidOperationException(); } Array array = (Array)values[0]; int limit = (int)values[1]; if (limit >= array.Count) { return(values[0]); } Value[] results = new Value[limit]; for (int i = 0; i < limit; i++) { results[i] = array[i]; } return(Array.From(results)); }
public static Value Matches(Value[] values) { if (values.Length != 2) { throw new InvalidOperationException(); } Regex regex = new Regex((string)values[1], RegexOptions.Singleline | RegexOptions.ExplicitCapture); MatchCollection matches = regex.Matches((string)values[0]); if (matches.Count == 0) { return(EmptyMatch); } Value[] results = new Value[matches.Count]; string[] groups = regex.GetGroupNames(); for (int i = 0; i < results.Length; i++) { results[i] = From(groups, matches[i]); } return(Array.From(results)); }
public static Value Compact(Value[] values) { if (values.Length != 1) { throw new InvalidOperationException(); } Array array = (Array)values[0]; List <Value> list = new List <Value>(array.Count); for (int i = 0, j = array.Count; i < j; i++) { Value value = array[i]; if (value == null) { continue; } switch (value.Type) { case ValueType.String: if (((string)value).Length == 0) { continue; } break; case ValueType.Array: if (((Array)value).Count == 0) { continue; } break; } list.Add(array[i]); } return(Array.From(list)); }
public static Value Page(Value[] values) { if (values.Length != 2) { throw new InvalidOperationException(); } Array array = (Array)values[0]; int limit = (int)values[1]; int total = array.Count; Value[] results = new Value[(total + limit - 1) / limit]; for (int i = 0; i < results.Length; i++) { int start = i * limit; int count = Math.Min(limit, total - start); Value[] page = new Value[count]; for (int j = 0; j < count; j++) { page[j] = array[start + j]; } results[i] = Array.From(page); } return(Array.From(results)); }
public static Value Unique(Value[] values) { if (values.Length != 1) { throw new InvalidOperationException(); } Array array = (Array)values[0]; int count = array.Count; List <Value> list = new List <Value>(count); HashSet <double> numbers = new HashSet <double>(); HashSet <string> strings = new HashSet <string>(); bool hasTrue = false; bool hasFalse = false; bool hasNull = false; for (int i = 0; i < count; i++) { Value value = array[i]; if (value == null) { if (!hasNull) { hasNull = true; list.Add(null); } } else { switch (value.Type) { case ValueType.Boolean: if ((bool)value) { if (!hasTrue) { hasTrue = true; list.Add(value); } } else { if (!hasFalse) { hasFalse = true; list.Add(value); } } break; case ValueType.Number: if (numbers.Add((double)value)) { list.Add(value); } break; case ValueType.String: if (strings.Add((string)value)) { list.Add(value); } break; case ValueType.Table: list.Add(value); break; case ValueType.Array: list.Add(value); break; } } } return(Array.From(list)); }