Skip to content

Commit 6304b8e

Browse files
committed
Implemente round with precision and removed Expr#log(base) due to potential for confusion
1 parent e261a1a commit 6304b8e

File tree

3 files changed

+113
-36
lines changed

3 files changed

+113
-36
lines changed

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

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,8 +1917,43 @@ export abstract class Expression implements ProtoValueSerializable, UserData {
19171917
*
19181918
* @return A new `Expr` representing the rounded value.
19191919
*/
1920-
round(): FunctionExpression {
1921-
return new FunctionExpression('round', [this]);
1920+
round(): FunctionExpression;
1921+
/**
1922+
* Creates an expression that rounds a numeric value to the specified number of decimal places.
1923+
*
1924+
* ```typescript
1925+
* // Round the value of the 'price' field to two decimal places.
1926+
* field("price").round(2);
1927+
* ```
1928+
*
1929+
* @param decimalPlaces A constant specifying the rounding precision in decimal places.
1930+
*
1931+
* @return A new `Expr` representing the rounded value.
1932+
*/
1933+
round(decimalPlaces: number): FunctionExpression;
1934+
/**
1935+
* Creates an expression that rounds a numeric value to the specified number of decimal places.
1936+
*
1937+
* ```typescript
1938+
* // Round the value of the 'price' field to two decimal places.
1939+
* field("price").round(constant(2));
1940+
* ```
1941+
*
1942+
* @param decimalPlaces An expression specifying the rounding precision in decimal places.
1943+
*
1944+
* @return A new `Expr` representing the rounded value.
1945+
*/
1946+
round(decimalPlaces: Expression): FunctionExpression;
1947+
round(decimalPlaces?: number | Expression): FunctionExpression {
1948+
if (decimalPlaces === undefined) {
1949+
return new FunctionExpression('round', [this]);
1950+
} else {
1951+
return new FunctionExpression(
1952+
'round',
1953+
[this, valueToDefaultExpr(decimalPlaces)],
1954+
'round'
1955+
);
1956+
}
19221957
}
19231958

19241959
/**
@@ -1966,35 +2001,6 @@ export abstract class Expression implements ProtoValueSerializable, UserData {
19662001
return new FunctionExpression('ln', [this]);
19672002
}
19682003

1969-
/**
1970-
* Creates an expression that computes the logarithm of this expression to a given base.
1971-
*
1972-
* ```typescript
1973-
* // Compute the logarithm of the 'value' field with base 10.
1974-
* field("value").log(10);
1975-
* ```
1976-
*
1977-
* @param base The base of the logarithm.
1978-
* @return A new {@code Expr} representing the logarithm of the numeric value.
1979-
*/
1980-
log(base: number): FunctionExpression;
1981-
1982-
/**
1983-
* Creates an expression that computes the logarithm of this expression to a given base.
1984-
*
1985-
* ```typescript
1986-
* // Compute the logarithm of the 'value' field with the base in the 'base' field.
1987-
* field("value").log(field("base"));
1988-
* ```
1989-
*
1990-
* @param base The base of the logarithm.
1991-
* @return A new {@code Expr} representing the logarithm of the numeric value.
1992-
*/
1993-
log(base: Expression): FunctionExpression;
1994-
log(base: number | Expression): FunctionExpression {
1995-
return new FunctionExpression('log', [this, valueToDefaultExpr(base)]);
1996-
}
1997-
19982004
/**
19992005
* Creates an expression that computes the square root of a numeric value.
20002006
*
@@ -6876,8 +6882,49 @@ export function round(fieldName: string): FunctionExpression;
68766882
* @return A new `Expr` representing the rounded value.
68776883
*/
68786884
export function round(expression: Expression): FunctionExpression;
6879-
export function round(expr: Expression | string): FunctionExpression {
6880-
return fieldOrExpression(expr).round();
6885+
6886+
/**
6887+
* Creates an expression that rounds a numeric value to the specified number of decimal places.
6888+
*
6889+
* ```typescript
6890+
* // Round the value of the 'price' field to two decimal places.
6891+
* round("price", 2);
6892+
* ```
6893+
*
6894+
* @param fieldName The name of the field to round.
6895+
* @param decimalPlaces A constant or expression specifying the rounding precision in decimal places.
6896+
* @return A new `Expr` representing the rounded value.
6897+
*/
6898+
export function round(
6899+
fieldName: string,
6900+
decimalPlaces: number | Expression
6901+
): FunctionExpression;
6902+
6903+
/**
6904+
* Creates an expression that rounds a numeric value to the specified number of decimal places.
6905+
*
6906+
* ```typescript
6907+
* // Round the value of the 'price' field to two decimal places.
6908+
* round(field("price"), constant(2));
6909+
* ```
6910+
*
6911+
* @param expression An expression evaluating to a numeric value, which will be rounded.
6912+
* @param decimalPlaces A constant or expression specifying the rounding precision in decimal places.
6913+
* @return A new `Expr` representing the rounded value.
6914+
*/
6915+
export function round(
6916+
expression: Expression,
6917+
decimalPlaces: number | Expression
6918+
): FunctionExpression;
6919+
export function round(
6920+
expr: Expression | string,
6921+
decimalPlaces?: number | Expression
6922+
): FunctionExpression {
6923+
if (decimalPlaces === undefined) {
6924+
return fieldOrExpression(expr).round();
6925+
} else {
6926+
return fieldOrExpression(expr).round(valueToDefaultExpr(decimalPlaces));
6927+
}
68816928
}
68826929

68836930
/**
@@ -7032,7 +7079,10 @@ export function log(
70327079
expr: Expression | string,
70337080
base: number | Expression
70347081
): FunctionExpression {
7035-
return fieldOrExpression(expr).log(valueToDefaultExpr(base));
7082+
return new FunctionExpression('log', [
7083+
fieldOrExpression(expr),
7084+
valueToDefaultExpr(base)
7085+
]);
70367086
}
70377087

70387088
/**

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3363,6 +3363,32 @@ apiDescribe.only('Pipelines', persistence => {
33633363
});
33643364
});
33653365

3366+
it('can round a numeric value to specified precision', async () => {
3367+
const snapshot = await execute(
3368+
firestore
3369+
.pipeline()
3370+
.collection(randomCol.path)
3371+
.limit(1)
3372+
.replaceWith(
3373+
map({
3374+
foo: 4.123456
3375+
})
3376+
)
3377+
.select(
3378+
field('foo').round(0).as('0'),
3379+
round('foo', 1).as('1'),
3380+
round('foo', constant(2)).as('2'),
3381+
round(field('foo'), 4).as('4')
3382+
)
3383+
);
3384+
expectResults(snapshot, {
3385+
'0': 4,
3386+
'1': 4.1,
3387+
'2': 4.12,
3388+
'4': 4.1235
3389+
});
3390+
});
3391+
33663392
it('can get the collectionId from a path', async () => {
33673393
const snapshot = await execute(
33683394
firestore
@@ -3532,7 +3558,7 @@ apiDescribe.only('Pipelines', persistence => {
35323558
.collection(randomCol.path)
35333559
.where(field('title').equal("The Hitchhiker's Guide to the Galaxy"))
35343560
.limit(1)
3535-
.select(field('rating').log(10).as('logRating'))
3561+
.select(log(field('rating'), 10).as('logRating'))
35363562
);
35373563
expectResults(snapshot, {
35383564
logRating: 0.6232492903979004

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ import {
104104
toLower,
105105
toUpper,
106106
trim,
107-
arrayGet
107+
arrayGet,
108+
byteLength
108109
} from '../../src/lite-api/expressions';
109110
import { documentId as documentIdFieldPath } from '../../src/lite-api/field_path';
110111
import { vector } from '../../src/lite-api/field_value_impl';

0 commit comments

Comments
 (0)