-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: round number to decimal places
- Loading branch information
1 parent
7f1d354
commit 5526af9
Showing
4 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ckend/src/apps/formatter/actions/numbers/transforms/round-number-decimal-places/fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import z from 'zod' | ||
|
||
import { numericStringSchema } from '@/apps/formatter/common/numeric-string-schema' | ||
import { type TransformSpec } from '@/apps/formatter/common/transform-spec' | ||
import { ensureZodEnumValue, ensureZodObjectKey } from '@/helpers/zod-utils' | ||
|
||
const opTypeEnum = z.enum(['roundDown', 'roundUp', 'roundOff']) | ||
|
||
export const fieldSchema = z | ||
.object({ | ||
roundNumberValue: numericStringSchema, | ||
roundNumberOp: opTypeEnum, | ||
roundNumberToDecimalPlaces: numericStringSchema.pipe( | ||
z | ||
.number() | ||
.int('Enter whole numbers without decimals') | ||
.nonnegative('Enter a number greater than 0'), | ||
), | ||
}) | ||
.transform((params) => ({ | ||
value: params.roundNumberValue, | ||
op: params.roundNumberOp, | ||
toDecimalPlaces: params.roundNumberToDecimalPlaces, | ||
})) | ||
|
||
export const fields = [ | ||
{ | ||
label: 'What number do you want to round?', | ||
key: ensureZodObjectKey(fieldSchema.sourceType(), 'roundNumberValue'), | ||
type: 'string' as const, | ||
required: true, | ||
variables: true, | ||
}, | ||
{ | ||
label: 'What kind of rounding do you want?', | ||
key: ensureZodObjectKey(fieldSchema.sourceType(), 'roundNumberOp'), | ||
type: 'dropdown' as const, | ||
required: true, | ||
variables: false, | ||
showOptionValue: false, | ||
options: [ | ||
{ | ||
label: 'Round Down', | ||
value: ensureZodEnumValue(opTypeEnum, 'roundDown'), | ||
}, | ||
{ | ||
label: 'Round Up', | ||
value: ensureZodEnumValue(opTypeEnum, 'roundUp'), | ||
}, | ||
{ | ||
label: 'Round Off', | ||
value: ensureZodEnumValue(opTypeEnum, 'roundOff'), | ||
}, | ||
], | ||
}, | ||
{ | ||
label: 'How many decimal places do you want to round to?', | ||
key: ensureZodObjectKey( | ||
fieldSchema.sourceType(), | ||
'roundNumberToDecimalPlaces', | ||
), | ||
type: 'string' as const, | ||
required: true, | ||
variables: true, | ||
}, | ||
] satisfies TransformSpec['fields'] |
19 changes: 19 additions & 0 deletions
19
...ackend/src/apps/formatter/actions/numbers/transforms/round-number-decimal-places/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { TransformSpec } from '@/apps/formatter/common/transform-spec' | ||
|
||
import { fields } from './fields' | ||
import { transformData } from './transform-data' | ||
|
||
const id = 'roundNumberDecimalPlaces' satisfies TransformSpec['id'] | ||
|
||
const dropdownConfig = { | ||
label: 'Round number to decimal places', | ||
description: | ||
'Round down, round up, or round off a number to the decimal places you want', | ||
} satisfies TransformSpec['dropdownConfig'] | ||
|
||
export const spec = { | ||
id, | ||
dropdownConfig, | ||
fields, | ||
transformData, | ||
} satisfies TransformSpec |
49 changes: 49 additions & 0 deletions
49
...c/apps/formatter/actions/numbers/transforms/round-number-decimal-places/transform-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Big from 'big.js' | ||
import { ZodError } from 'zod' | ||
|
||
import type { TransformSpec } from '@/apps/formatter/common/transform-spec' | ||
import StepError, { GenericSolution } from '@/errors/step' | ||
import { firstZodParseError } from '@/helpers/zod-utils' | ||
|
||
import { fieldSchema } from './fields' | ||
|
||
export const transformData: TransformSpec['transformData'] = async ($) => { | ||
try { | ||
const { value, op, toDecimalPlaces } = fieldSchema.parse($.step.parameters) | ||
|
||
let result = '' | ||
switch (op) { | ||
case 'roundOff': | ||
result = new Big(value).toFixed(toDecimalPlaces) | ||
break | ||
case 'roundUp': | ||
result = new Big(value).toFixed(toDecimalPlaces, Big.roundUp) | ||
break | ||
case 'roundDown': | ||
result = new Big(value).toFixed(toDecimalPlaces, Big.roundDown) | ||
break | ||
} | ||
|
||
$.setActionItem({ | ||
raw: { | ||
result, | ||
}, | ||
}) | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
throw new StepError( | ||
`Configuration problem: '${firstZodParseError(error)}'`, | ||
GenericSolution.ReconfigureInvalidField, | ||
$.step.position, | ||
$.app.name, | ||
) | ||
} else { | ||
throw new StepError( | ||
`Error performing rounding: '${error.message}'`, | ||
'Ensure that you have entered valid numbers.', | ||
$.step.position, | ||
$.app.name, | ||
) | ||
} | ||
} | ||
} |