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

Commit

Permalink
[ios, macos] Added rounding expression function
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Apr 11, 2018
1 parent d30fbb4 commit 8aa1c1e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion platform/darwin/docs/guides/For Style Authors.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ In style specification | Method, function, or predicate type | Format string syn
`max` | `max:` | `max({1, 2, 2, 3, 4, 7, 9})`
`min` | `min:` | `min({1, 2, 2, 3, 4, 7, 9})`
`pi` | | `%@` representing `NSNumber` containing `M_PI`
`round` | |
`round` | `mgl_round:` | `mgl_round(1.5)`
`sin` | |
`sqrt` | `sqrt:` | `sqrt(2)`
`tan` | |
Expand Down
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 @@ -196,6 +196,7 @@ Initializer parameter | Format string syntax | Description
`mgl_interpolate:withCurveType:parameters:stops:` | `mgl_interpolate:withCurveType:parameters:stops:(x, 'linear', nil, %@)` | Produces continuous, smooth results by interpolating between pairs of input and output values (“stops”). Compared to the `mgl_interpolateWithCurveType:parameters:stops:` custom function, the input expression (that function’s target) is instead passed in as the first argument to this function.
`mgl_step:from:stops:` | `mgl_step:from:stops:(x, 11, %@)` | Produces discrete, stepped results by evaluating a piecewise-constant function defined by pairs of input and output values ("stops"). Compared to the `mgl_stepWithMinimum:stops:` custom function, the input expression (that function’s target) is instead passed in as the first argument to this function.
`mgl_join:` | `mgl_join({'Old', 'MacDonald'})` | Returns the result of concatenating together all the elements of an array in order. Compared to the `stringByAppendingString:` custom function, this function takes only one argument, which is an aggregate expression containing the strings to concatenate.
`mgl_round:` | `mgl_round(1.5)` | Returns the number rounded to the nearest integer. If the number is halfway between two integers, this function rounds it away from zero.
`mgl_coalesce:` | `mgl_coalesce({x, y, z})` | Returns the first non-`nil` value from an array of expressions.
`MGL_LET` | `MGL_LET('age', uppercase('old'), 'name', uppercase('MacDonald'), mgl_join({$age, $name}))` | Any number of variable names interspersed with their assigned `NSExpression` values, followed by an `NSExpression` that may contain references to those variables. Compared to the `mgl_expressionWithContext:` custom function, this function takes the variable names and values inline before the expression that contains references to those variables.
`MGL_MATCH` | `MGL_MATCH(x, 0, 'zero match', 1, 'one match', 'two match', 'default')` | Evaluates the first expression and returns the value that matches the initial condition. After the first expression condition a pair of matching/return value should be added and a default value.
Expand Down
11 changes: 11 additions & 0 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ + (void)installFunctions {
// Install method-like functions, taking the number of arguments implied by
// the selector name.
INSTALL_METHOD(mgl_join:);
INSTALL_METHOD(mgl_round:);
INSTALL_METHOD(mgl_interpolate:withCurveType:parameters:stops:);
INSTALL_METHOD(mgl_step:from:stops:);
INSTALL_METHOD(mgl_coalesce:);
Expand All @@ -83,6 +84,14 @@ - (NSString *)mgl_join:(NSArray<NSString *> *)components {
return [components componentsJoinedByString:@""];
}

/**
Rounds the given number to the nearest integer. If the number is halfway
between two integers, this method rounds it away from zero.
*/
- (NSNumber *)mgl_round:(NSNumber *)number {
return @(round(number.doubleValue));
}

/**
A placeholder for a method that evaluates an interpolation expression.
*/
Expand Down Expand Up @@ -488,6 +497,7 @@ + (instancetype)mgl_expressionWithJSONObject:(id)object {
@"log10": @"log:",
@"ln": @"ln:",
@"abs": @"abs:",
@"round": @"mgl_round:",
@"floor": @"floor:",
@"ceil": @"ceiling:",
@"^": @"raise:toPower:",
Expand Down Expand Up @@ -729,6 +739,7 @@ - (id)mgl_jsonExpressionObject {
@"uppercase:": @"upcase",
@"lowercase:": @"downcase",
@"length:": @"length",
@"mgl_round:": @"round",
// Vararg aftermarket expressions need to be declared with an explicit and implicit first argument.
@"MGL_LET": @"let",
@"MGL_LET:": @"let",
Expand Down
24 changes: 24 additions & 0 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,30 @@ - (void)testArithmeticExpressionObject {
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @-2);
}
{
NSExpression *expression = [NSExpression expressionForFunction:@"mgl_round:" arguments:@[MGLConstantExpression(@1.5)]];
NSArray *jsonExpression = @[@"round", @1.5];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @2);
}
{
NSExpression *expression = [NSExpression expressionForFunction:@"mgl_round:" arguments:@[MGLConstantExpression(@-1.5)]];
NSArray *jsonExpression = @[@"round", @-1.5];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @-2);
}
{
NSExpression *expression = [NSExpression expressionForFunction:@"mgl_round:" arguments:@[MGLConstantExpression(@2.5)]];
NSArray *jsonExpression = @[@"round", @2.5];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @3);
}
{
NSExpression *expression = [NSExpression expressionForFunction:@"mgl_round:" arguments:@[MGLConstantExpression(@-2.5)]];
NSArray *jsonExpression = @[@"round", @-2.5];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], @-3);
}
}

- (void)testTrigonometricExpressionObject {
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/docs/guides/For Style Authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ In style specification | Method, function, or predicate type | Format string syn
`max` | `max:` | `max({1, 2, 2, 3, 4, 7, 9})`
`min` | `min:` | `min({1, 2, 2, 3, 4, 7, 9})`
`pi` | | `%@` representing `NSNumber` containing `M_PI`
`round` | |
`round` | `mgl_round:` | `mgl_round(1.5)`
`sin` | |
`sqrt` | `sqrt:` | `sqrt(2)`
`tan` | |
Expand Down
2 changes: 1 addition & 1 deletion platform/macos/docs/guides/For Style Authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ In style specification | Method, function, or predicate type | Format string syn
`max` | `max:` | `max({1, 2, 2, 3, 4, 7, 9})`
`min` | `min:` | `min({1, 2, 2, 3, 4, 7, 9})`
`pi` | | `%@` representing `NSNumber` containing `M_PI`
`round` | |
`round` | `mgl_round:` | `mgl_round(1.5)`
`sin` | |
`sqrt` | `sqrt:` | `sqrt(2)`
`tan` | |
Expand Down

0 comments on commit 8aa1c1e

Please sign in to comment.