Skip to content

Commit e261a1a

Browse files
committed
Add abs expression and did formatting
1 parent f0cc89b commit e261a1a

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

packages/firestore/src/lite-api/expressions.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,20 @@ export abstract class Expression implements ProtoValueSerializable, UserData {
11021102
return new FunctionExpression('floor', [this]);
11031103
}
11041104

1105+
/**
1106+
* Creates an expression that computes the absolute value of a numeric value.
1107+
*
1108+
* ```typescript
1109+
* // Compute the absolute value of the 'price' field.
1110+
* field("price").abs();
1111+
* ```
1112+
*
1113+
* @return A new {@code Expr} representing the absolute value of the numeric value.
1114+
*/
1115+
abs(): FunctionExpression {
1116+
return new FunctionExpression('abs', [this]);
1117+
}
1118+
11051119
/**
11061120
* Creates an expression that computes e to the power of this expression.
11071121
*
@@ -2674,11 +2688,7 @@ export class BooleanExpression extends FunctionExpression {
26742688
* @return A new {@code Expr} representing the 'ifError' operation.
26752689
*/
26762690
ifError(catchValue: BooleanExpression): BooleanExpression {
2677-
return new BooleanExpression(
2678-
'if_error',
2679-
[this, catchValue],
2680-
'ifError'
2681-
);
2691+
return new BooleanExpression('if_error', [this, catchValue], 'ifError');
26822692
}
26832693
}
26842694

@@ -2909,7 +2919,10 @@ export function ifError(
29092919
tryExpr: Expression,
29102920
catchValue: unknown
29112921
): FunctionExpression {
2912-
if (tryExpr instanceof BooleanExpression && catchValue instanceof BooleanExpression) {
2922+
if (
2923+
tryExpr instanceof BooleanExpression &&
2924+
catchValue instanceof BooleanExpression
2925+
) {
29132926
return tryExpr.ifError(catchValue);
29142927
} else {
29152928
return tryExpr.ifError(valueToDefaultExpr(catchValue));
@@ -7079,6 +7092,25 @@ export function stringReverse(expr: Expression | string): FunctionExpression {
70797092
return fieldOrExpression(expr).stringReverse();
70807093
}
70817094

7095+
/**
7096+
* Creates an expression that computes the absolute value of a numeric value.
7097+
*
7098+
* @param expr The expression to compute the absolute value of.
7099+
* @return A new {@code Expr} representing the absolute value of the numeric value.
7100+
*/
7101+
export function abs(expr: Expression): FunctionExpression;
7102+
7103+
/**
7104+
* Creates an expression that computes the absolute value of a numeric value.
7105+
*
7106+
* @param fieldName The field to compute the absolute value of.
7107+
* @return A new {@code Expr} representing the absolute value of the numeric value.
7108+
*/
7109+
export function abs(fieldName: string): FunctionExpression;
7110+
export function abs(expr: Expression | string): FunctionExpression {
7111+
return fieldOrExpression(expr).abs();
7112+
}
7113+
70827114
// TODO(new-expression): Add new top-level expression function definitions above this line
70837115

70847116
/**

packages/firestore/test/integration/api/pipeline.test.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import {
4545
log,
4646
sqrt,
4747
stringReverse,
48-
length
48+
length,
49+
abs
4950
} from '../../../src/lite-api/expressions';
5051
import { PipelineSnapshot } from '../../../src/lite-api/pipeline-result';
5152
import { addEqualityMatcher } from '../../util/equality_matcher';
@@ -2583,9 +2584,12 @@ apiDescribe.only('Pipelines', persistence => {
25832584
ifError(divide(constant(1), constant(0)), constant('was error')).as(
25842585
'ifError'
25852586
),
2586-
ifError(divide(constant(1), constant(0)).greaterThan(1), constant(true)).not().as(
2587-
'ifErrorBooleanExpression'
2588-
),
2587+
ifError(
2588+
divide(constant(1), constant(0)).greaterThan(1),
2589+
constant(true)
2590+
)
2591+
.not()
2592+
.as('ifErrorBooleanExpression'),
25892593
isAbsent('foo').as('isAbsent'),
25902594
isNotNull('title').as('titleIsNotNull'),
25912595
isNotNan('cost').as('costIsNotNan'),
@@ -2619,9 +2623,11 @@ apiDescribe.only('Pipelines', persistence => {
26192623
divide(constant(1), constant(0))
26202624
.ifError(constant('was error'))
26212625
.as('ifError'),
2622-
divide(constant(1), constant(0)).greaterThan(1).ifError(constant(true)).not().as(
2623-
'ifErrorBooleanExpression'
2624-
),
2626+
divide(constant(1), constant(0))
2627+
.greaterThan(1)
2628+
.ifError(constant(true))
2629+
.not()
2630+
.as('ifErrorBooleanExpression'),
26252631
field('foo').isAbsent().as('isAbsent'),
26262632
field('title').isNotNull().as('titleIsNotNull'),
26272633
field('cost').isNotNan().as('costIsNotNan')
@@ -3765,6 +3771,30 @@ apiDescribe.only('Pipelines', persistence => {
37653771
expectResults(snapshot, { reverseTitle: '4891' });
37663772
});
37673773

3774+
it('testAbs', async () => {
3775+
const snapshot = await execute(
3776+
firestore
3777+
.pipeline()
3778+
.collection(randomCol.path)
3779+
.limit(1)
3780+
.select(
3781+
constant(-10).as('neg10'),
3782+
constant(-22.22).as('neg22'),
3783+
constant(1).as('pos1')
3784+
)
3785+
.select(
3786+
abs('neg10').as('10'),
3787+
abs(field('neg22')).as('22'),
3788+
field('pos1').as('1')
3789+
)
3790+
);
3791+
expectResults(snapshot, {
3792+
'10': 10,
3793+
'22': 22.22,
3794+
'1': 1
3795+
});
3796+
});
3797+
37683798
// TODO(new-expression): Add new expression tests above this line
37693799
});
37703800

packages/firestore/test/lite/pipeline.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ import {
9999
arrayLength,
100100
charLength,
101101
divide,
102-
byteLength,
102+
abs,
103103
not,
104104
toLower,
105105
toUpper,
@@ -1903,6 +1903,20 @@ describe('Firestore Pipelines', () => {
19031903
});
19041904
});
19051905

1906+
it('testAbs', async () => {
1907+
const snapshot = await execute(
1908+
firestore
1909+
.pipeline()
1910+
.collection(randomCol.path)
1911+
.where(equal('title', 'To Kill a Mockingbird'))
1912+
.select(abs(field('rating')).as('absRating'))
1913+
.limit(1)
1914+
);
1915+
expectResults(snapshot, {
1916+
absRating: 4.2
1917+
});
1918+
});
1919+
19061920
it('testComparisonOperators', async () => {
19071921
const snapshot = await execute(
19081922
firestore

0 commit comments

Comments
 (0)