Skip to content

Support for lexicographical comparison for numeric operators #14

@sarensw

Description

@sarensw

I want to use the < and > operators to compare strings lexicographically (Dates in form of yyyy-MM-dd to be more precisely). This works in JS but unfortunately not in this .Net port. It throws a FormatException because the strings cannot be converted to double.

I forked and tried to fix this but I'm having a hard time doing so due to the Func based nature in the code.

As a first try, I have changed DoubleArgsSatisfy so that it works in a generic way. But I'm still only able to set one type when calling this method in AddOperator

AddOperator(">", ArgsSatisfy<string>((prev, next) => string.CompareOrdinal(prev, next) > 0));

private Func<IProcessJsonLogic, JToken[], object, object> ArgsSatisfy<T>(Func<T, T, bool> criteria)
{
    return (p, args, data) => {
        var valuesString = args.Select(a => a == null ? "" : p.Apply(a, data).ToString()).ToArray();
        var values = valuesString.Select(v => (T) Convert.ChangeType(v, typeof(T))).ToArray();
        for (int i = 1; i < values.Length; i++) {
            if (!criteria(values[i-1], values[i])) return false;
        }
        return true;
    };
}

Can you give me a hint on how to improve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions