Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "length" operation. #4

Open
jwadhams opened this issue Jul 28, 2017 · 9 comments
Open

Add a "length" operation. #4

jwadhams opened this issue Jul 28, 2017 · 9 comments

Comments

@jwadhams
Copy link
Owner

jwadhams commented Jul 28, 2017

For strings, returns their length in characters. (Note there can be oddities in a language's string length calculation, like JavaScript's "🤔 ".length === 2)

For arrays, returns the number of elements.

For non-countables (null, boolean, objects, numbers) returns 0 (zero)

Should internally use the widest idiomatic counting solution in the target language (e.g. the length magic parameter in JavaScript, the count() method and Countable interface in PHP, the len function and __len__ special method in Python, etc.)

Note, this is going to have a really hard relationship with the unary sugar. This could be ambiguous {"length":["apple"]} -- am I counting a one-element-array or a five-letter-word? I think we have to side with "assume the rule author is not using unary syntax." which means arrays have to be written {"length":[ [1,2,3] ]}

@jwadhams
Copy link
Owner Author

jwadhams commented Jul 28, 2017

Proposed tests, in the common test format from http://jsonlogic.com/tests.json like

[ test, data, expected result ]

"Unary syntax sugar, interpreted as one string arg",
[ {"length":"apple"}, null, 5], 
"Normal syntax, interpreted as one string arg",
[ {"length":["apple"]}, null, 5],
"Interpreted as one argument, an array with one element",
[ {"length":[ ["apple"] ]}, null, 1],

"Interpreted as two string arguments, second is ignored.",
[ {"length":["apple", "banana"]}, null, 5],
"Interpreted as one array argument",
[ {"length":[ ["apple", "banana"] ]}, null, 2],

"Unlengthable",
[ {"length":null}, null, 0],
[ {"length":[null]}, null, 0],
[ {"length":false}, null, 0],
[ {"length":[false]}, null, 0],
[ {"length":true}, null, 0],
[ {"length":[true]}, null, 0],
[ {"length":42}, null, 0],
[ {"length":[42]}, null, 0],
[ {"length":{"var":"a"}}, {"a":{"b":"banana"}}, 0],
[ {"length":[{"var":"a"}]}, {"a":{"b":"banana"}}, 0],

@jwadhams jwadhams reopened this Jul 28, 2017
@MatissJanis
Copy link

Yes, please 👍

@ilons
Copy link

ilons commented Jul 3, 2018

Yes, this would be quite useful.
In the meantime, one could do this to count elements in an array:

{
    "reduce": [
        {"map": [[1, 2, 3, 4], {"if": [{"var": ""}, 1, 0]}]},
        {"+": [{"var": "current"}, {"var": "accumulator"}]},
        0
    ]
}

@atulagrawal
Copy link

May i know what is pending for this Pull Request?

@edene
Copy link

edene commented Dec 9, 2019

FYI, the simpler way to get the length of an array would be:

{
    "reduce": [
        [1, 2, 3, 4],
        {"+": [1, {"var": "accumulator"}]},
        0
    ]
}

@danielwolf1
Copy link

Are there plans to add this operation?

@xStoryTeller
Copy link

Has there been any updates to this? I see work-arounds for arrays but we have a use case where we need this for strings. So for example, a string needs to have at least 5 characters ( length >= 5 ).

@evenflow58
Copy link

I may have this wrong but it seems to work in my tests bed. You can check string length by just using the length property of a string. Something like

{ ">=": [ "var": "some.string.object.here.length" ], 100}

@4c0n
Copy link

4c0n commented Sep 18, 2024

I may have this wrong but it seems to work in my tests bed. You can check string length by just using the length property of a string. Something like

{ ">=": [ "var": "some.string.object.here.length" ], 100}

That might work well in the javascript implementation because string objects have the length attribute, however in other implementations strings do not have that attribute. So I suppose the fact that it works is by coincidence rather than by design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants