private static Result Sort(Evaluator.Args args) { var objs = args.Flatten(); var ordered = args.HasSwitch("n") // numeric sorting (otherwise string sorting) ? objs.OrderBy(args.ToInt) : objs.OrderBy(args.Preview); var finalOrder = args.HasSwitch("r") // reverse ? ordered.Reverse() : ordered; return(finalOrder.ToArray()); }
private static Result Grep(Evaluator.Args args) { var pattern = args[0]; var flip = args.HasSwitch("v"); return(args .Skip(1) .SelectMany(result => result) .Where(obj => flip ^ args.Matches(args.Preview(obj), pattern)) .ToArray()); }
private static Result Sort(Evaluator.Args args) { var objs = args.Flatten(); var fieldToSortBy = args.GetStrSwitch("k", null); Func <object, object> keySelector = o => o; if (!string.IsNullOrEmpty(fieldToSortBy)) { keySelector = o => args.Eval.Resolve(o).Properties.FirstOrDefault(p => p.Name == fieldToSortBy)?.Value; } IComparer <object> comparer = args.HasSwitch("n") ? Comparer <object> .Create((lhs, rhs) => Comparer <long?> .Default.Compare(args.Eval.TryToNumber(lhs), args.Eval.TryToNumber(rhs))) : Comparer <object> .Create((lhs, rhs) => Comparer <string> .Default.Compare(args.Preview(lhs), args.Preview(rhs))); var ordered = objs.OrderBy(keySelector, comparer); var finalOrder = args.HasSwitch("r") // reverse ? ordered.Reverse() : ordered; return(finalOrder.ToArray()); }
private static Result Uniq(Evaluator.Args args) { var groups = args.Flatten().GroupBy(obj => args.Preview(obj)); if (args.HasSwitch("c")) // count objects in each group { return(groups .Select(grp => $"{grp.Count()}\t{args.Preview(grp.First())}") .ToArray()); } else { return(groups .Select(grp => grp.First()) .ToArray()); } }
private static Result Uniq(Evaluator.Args args) { var fieldToGroupBy = args.GetStrSwitch("k", null); Func <object, object> keySelector = o => o; if (!string.IsNullOrEmpty(fieldToGroupBy)) { keySelector = o => args.Eval.Resolve(o).Properties.FirstOrDefault(p => p.Name == fieldToGroupBy)?.Value; } var aa = args.Flatten(); var groups = aa.GroupBy(obj => args.Preview(keySelector(obj))); if (args.HasSwitch("c")) // count objects in each group { return(groups .Select(grp => { return new ObjectInfo( preview: $"{grp.Count()}: {grp.Key}", properties: new[] { new Property(name: "Key", value: grp.Key), new Property(name: "Count", value: grp.Count()), new Property(name: "Elems", value: grp.ToArray()) }); }) .ToArray()); } else { return(groups .Select(grp => grp.First()) .ToArray()); } }