Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Convert length operator to length: for strings
Browse files Browse the repository at this point in the history
Only works for literal strings.
  • Loading branch information
1ec5 committed Mar 19, 2018
1 parent c86892e commit f66b1ee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions platform/darwin/docs/guides/Predicates and Expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Initializer parameter | Format string syntax
`uppercase:` | `uppercase('Elysian Fields')`
`lowercase:` | `lowercase('DOWNTOWN')`
`noindex:` | `noindex(0 + 2 + c)`
`length:` | `length('Wapakoneta')`

The following predefined functions are not supported:

Expand Down
8 changes: 7 additions & 1 deletion platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,12 @@ + (instancetype)mgl_expressionWithJSONObject:(id)object {
return [NSExpression expressionForKeyPath:argumentObjects.firstObject];
} else if ([op isEqualToString:@"length"]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(argumentObjects);
return [NSExpression expressionForFunction:@"count:" arguments:@[subexpressions.firstObject]];
NSString *function = @"count:";
if ([subexpressions.firstObject expressionType] == NSConstantValueExpressionType
&& [[subexpressions.firstObject constantValue] isKindOfClass:[NSString class]]) {
function = @"length:";
}
return [NSExpression expressionForFunction:function arguments:@[subexpressions.firstObject]];
} else if ([op isEqualToString:@"min"]) {
NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(argumentObjects);
NSExpression *subexpression = [NSExpression expressionForAggregate:subexpressions];
Expand Down Expand Up @@ -540,6 +545,7 @@ - (id)mgl_jsonExpressionObject {
@"raise:toPower:": @"^",
@"uppercase:": @"upcase",
@"lowercase:": @"downcase",
@"length:": @"length",
};
});

Expand Down
6 changes: 6 additions & 0 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ - (void)testStringFormattingExpressionObject {
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
{
NSExpression *expression = [NSExpression expressionForFunction:@"length:" arguments:arguments];
NSArray *jsonExpression = @[@"length", @"MacDonald"];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression mgl_expressionWithJSONObject:jsonExpression], expression);
}
}

- (void)testTypeConversionExpressionObject {
Expand Down

0 comments on commit f66b1ee

Please sign in to comment.