-
Notifications
You must be signed in to change notification settings - Fork 14
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
Comments
Proposed tests, in the common test format from http://jsonlogic.com/tests.json like
"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], |
Yes, please 👍 |
Yes, this would be quite useful.
|
May i know what is pending for this Pull Request? |
FYI, the simpler way to get the length of an array would be:
|
Are there plans to add this operation? |
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 ). |
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 |
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, thecount()
method andCountable
interface in PHP, thelen
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] ]}
The text was updated successfully, but these errors were encountered: