From 7247674054b4795f533b9d5e71c421e261fcf15f Mon Sep 17 00:00:00 2001 From: CKY- Date: Thu, 12 Dec 2024 14:58:54 -0700 Subject: [PATCH] feat: commafyPad --- src/backend/variables/builtin/number/index.ts | 2 ++ .../builtin/number/number-commafy-pad.ts | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/backend/variables/builtin/number/number-commafy-pad.ts diff --git a/src/backend/variables/builtin/number/index.ts b/src/backend/variables/builtin/number/index.ts index eaa7ff7d1..41cbb91e6 100644 --- a/src/backend/variables/builtin/number/index.ts +++ b/src/backend/variables/builtin/number/index.ts @@ -1,6 +1,7 @@ import ensureNumber from './ensure-number'; import numberCeil from './number-ceil'; import numberCommafy from './number-commafy'; +import numberCommafyPad from "./number-commafy-pad"; import numberFloor from './number-floor'; import numberMax from './number-max'; import numberMin from './number-min'; @@ -14,6 +15,7 @@ export default [ ensureNumber, numberCeil, numberCommafy, + numberCommafyPad, numberFloor, numberMax, numberMin, diff --git a/src/backend/variables/builtin/number/number-commafy-pad.ts b/src/backend/variables/builtin/number/number-commafy-pad.ts new file mode 100644 index 000000000..d9a16328b --- /dev/null +++ b/src/backend/variables/builtin/number/number-commafy-pad.ts @@ -0,0 +1,32 @@ +import { ReplaceVariable, Trigger } from "../../../../types/variables"; +import { OutputDataType, VariableCategory } from "../../../../shared/variable-constants"; + +const model : ReplaceVariable = { + definition: { + handle: "commafyPad", + description: "Adds the appropriate commas to a number with decimal padding.", + usage: "commafyPad[value, precision(between 0 and 100)]", + examples: [ + { + usage: "commafyPad[1000000.00, 100]", + description: `Returns "1,000,000.0000"` + } + ], + categories: [VariableCategory.NUMBERS], + possibleDataOutput: [OutputDataType.TEXT] + }, + evaluator: ( + trigger: Trigger, + value: string | number, + //places: string | number, + precision?: string | number + ) : string => { + const number = Number(value).toFixed(precision != null ? Number(precision) : 2); + //const numPlaces = Number(places); + const [integer, fraction] = `${number}.0`.split(/\./g); + + return `${integer.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}.${fraction}`; + } +}; + +export default model; \ No newline at end of file