diff --git a/src/lib/components/parameters/ParameterView.svelte b/src/lib/components/parameters/ParameterView.svelte index 7dfaa500da91e5f93a560b629446d5b32fb9e5b4..4fc158ba09fc595731cedfb1ada3a258b562dfe2 100644 --- a/src/lib/components/parameters/ParameterView.svelte +++ b/src/lib/components/parameters/ParameterView.svelte @@ -18,7 +18,7 @@ labelFromScaleType, labelFromValueType, } from "$lib/parameters" - import { getUnitShortLabel } from "$lib/units" + import { getUnitByName, getUnitShortLabel } from "$lib/units" import type { SelfTargetAProps } from "$lib/urls" export let date: string @@ -195,8 +195,8 @@ {:else} <!-- TODO: Handle when valueAtInstant.value is a string array or a string by string dict. --> <td class="border p-1 text-center font-serif" - >{(valueAtInstant.unit ?? parameter.unit) == "/1" && - typeof valueAtInstant.value === "number" + >{getUnitByName(valueAtInstant.unit ?? parameter.unit) + ?.ratio && typeof valueAtInstant.value === "number" ? parseFloat((valueAtInstant.value * 100).toFixed(8)) // trick to round value * 100 : valueAtInstant.value ?? ""}</td > diff --git a/src/lib/components/parameters/ScaleAtInstantEdit.svelte b/src/lib/components/parameters/ScaleAtInstantEdit.svelte index 97b71b6911e7a4cca996e67d77f9b22dffa2658d..fbfa581aaeebaa009066e0cd8207b6553f4ceac7 100644 --- a/src/lib/components/parameters/ScaleAtInstantEdit.svelte +++ b/src/lib/components/parameters/ScaleAtInstantEdit.svelte @@ -21,7 +21,7 @@ asRateBracketAtInstant, asRateScaleParameter, } from "$lib/parameters" - import { getUnitShortLabel } from "$lib/units" + import { getUnitAtDate, getUnitShortLabel } from "$lib/units" export let date: string let globalErrors: { [key: string]: unknown } @@ -57,14 +57,14 @@ function changeValue( index: number, key: keyof AmountBracketAtInstant | keyof RateBracketAtInstant, - unitName: string | undefined | null, + unit: string | undefined | null, { target }: Event, ) { const { value } = target as HTMLInputElement let [validValue, error] = auditStringToNumber(laxAudit, value) let errorsAtIndex = errors[index] as { [key: string]: unknown } | undefined if (error === null) { - if (unitName === "/1") { + if (getUnitAtDate(unit, date)?.ratio) { validValue = validValue / 100 } if (errorsAtIndex?.[key] !== undefined) { @@ -117,7 +117,7 @@ if (number == null) { return null } - return unitName === "/1" + return getUnitAtDate(unitName, date)?.ratio ? parseFloat((number * 100).toFixed(8)) // trick to round value * 100 : number } diff --git a/src/lib/components/parameters/ScaleView.svelte b/src/lib/components/parameters/ScaleView.svelte index 2e8a171db15655eff6e6f12870f32aa92743632f..b31d7f657a60246456da18bc75b6e22e1dd1bf4c 100644 --- a/src/lib/components/parameters/ScaleView.svelte +++ b/src/lib/components/parameters/ScaleView.svelte @@ -13,6 +13,7 @@ asRateScaleParameter, buildInstantReferencesAndScaleArray, } from "$lib/parameters" + import { getUnitByName } from "$lib/units" export let parameter: ScaleParameter @@ -34,7 +35,7 @@ if (number == null) { return null } - return unitName === "/1" + return getUnitByName(unitName)?.ratio ? parseFloat((number * 100).toFixed(8)) // trick to round value * 100 : number } diff --git a/src/lib/components/parameters/ValueAtInstantEdit.svelte b/src/lib/components/parameters/ValueAtInstantEdit.svelte index c39d3cc54766340d547a60c4f54f8f2c1a5be3c5..699dd0b14b1d11a8d7a4731cc2b468725e3f4d37 100644 --- a/src/lib/components/parameters/ValueAtInstantEdit.svelte +++ b/src/lib/components/parameters/ValueAtInstantEdit.svelte @@ -19,7 +19,7 @@ import { createEventDispatcher } from "svelte" import { auditEditedAttribute } from "$lib/errors" - import { getUnitLabel, units } from "$lib/units" + import { getUnitAtDate, getUnitLabel, units } from "$lib/units" export let date: string let globalErrors: { [key: string]: unknown } @@ -83,7 +83,7 @@ ) if ( validErrors.unit === undefined && - validValueAtInstant?.unit === "/1" && + getUnitAtDate(validValueAtInstant?.unit, date)?.ratio && typeof validValueAtInstant?.value === "number" ) { validValueAtInstant.value = validValueAtInstant.value / 100 @@ -141,7 +141,7 @@ value={valueAtInstant === "expected" ? null : (errors.unit === undefined && - valueAtInstant?.unit === "/1" && + getUnitAtDate(valueAtInstant?.unit, date)?.ratio && typeof valueAtInstant.value === "number" ? parseFloat((asNumberValue(valueAtInstant).value * 100).toFixed(8)) // trick to round value * 100 : asMaybeNumberValue(valueAtInstant).value) ?? null} diff --git a/src/lib/components/variables/VariableReferredScaleAtInstant.svelte b/src/lib/components/variables/VariableReferredScaleAtInstant.svelte index fe3bd6f49c0b6d0ab2ae6a9b6f133e9c9fcc1ef5..38729bb2fb72aa2675a400c9162ac7d07599d055 100644 --- a/src/lib/components/variables/VariableReferredScaleAtInstant.svelte +++ b/src/lib/components/variables/VariableReferredScaleAtInstant.svelte @@ -25,7 +25,7 @@ asRateBracketAtInstantOrNullable, asRateScaleParameter, } from "$lib/parameters" - import { getUnitShortLabel } from "$lib/units" + import { getUnitAtDate, getUnitShortLabel } from "$lib/units" export let billParameter: ScaleParameter export let billScaleAtInstant: ScaleAtInstant | null @@ -174,7 +174,7 @@ changeValue(index, "threshold", event)} revaluationValue={revaluationScaleAtInstant?.[index] ?.threshold} - unitName={billParameter.threshold_unit} + unit={getUnitAtDate(billParameter.threshold_unit, date)} value={bracketAtInstant.threshold === "expected" ? null : bracketAtInstant.threshold?.value ?? null} @@ -185,7 +185,7 @@ lawValue={lawScaleAtInstant?.[index]?.threshold} revaluationValue={revaluationScaleAtInstant?.[index] ?.threshold} - unitName={billParameter.threshold_unit} + unit={getUnitAtDate(billParameter.threshold_unit, date)} /> {/if} <span class="mb-1.5 text-base"> @@ -216,8 +216,10 @@ revaluationValue={asAmountBracketAtInstantOrNullable( revaluationScaleAtInstant?.[index], )?.amount} - unitName={asAmountScaleParameter(billParameter) - .amount_unit} + unit={getUnitAtDate( + asAmountScaleParameter(billParameter).amount_unit, + date, + )} value={asAmountBracketAtInstant(bracketAtInstant) .amount === "expected" ? null @@ -236,8 +238,10 @@ revaluationValue={asAmountBracketAtInstantOrNullable( revaluationScaleAtInstant?.[index], )?.amount} - unitName={asAmountScaleParameter(billParameter) - .amount_unit} + unit={getUnitAtDate( + asAmountScaleParameter(billParameter).amount_unit, + date, + )} /> {/if} <span class="text-base"> @@ -272,7 +276,7 @@ revaluationValue={asRateBracketAtInstantOrNullable( revaluationScaleAtInstant?.[index], )?.base} - unitName={billParameter.threshold_unit} + unit={getUnitAtDate(billParameter.threshold_unit, date)} value={asRateBracketAtInstant(bracketAtInstant).base === "expected" ? null @@ -292,7 +296,7 @@ revaluationValue={asRateBracketAtInstantOrNullable( revaluationScaleAtInstant?.[index], )?.base} - unitName={billParameter.threshold_unit} + unit={getUnitAtDate(billParameter.threshold_unit, date)} /> {/if} <span class="mb-1.5"> @@ -324,7 +328,10 @@ revaluationValue={asRateBracketAtInstantOrNullable( revaluationScaleAtInstant?.[index], )?.rate} - unitName={asRateScaleParameter(billParameter).rate_unit} + unit={getUnitAtDate( + asRateScaleParameter(billParameter).rate_unit, + date, + )} value={asRateBracketAtInstant(bracketAtInstant).rate === "expected" ? null @@ -343,7 +350,10 @@ revaluationValue={asRateBracketAtInstantOrNullable( revaluationScaleAtInstant?.[index], )?.rate} - unitName={asRateScaleParameter(billParameter).rate_unit} + unit={getUnitAtDate( + asRateScaleParameter(billParameter).rate_unit, + date, + )} /> {/if} <span class="mb-1.5"> diff --git a/src/lib/components/variables/VariableReferredValueEdit.svelte b/src/lib/components/variables/VariableReferredValueEdit.svelte index e0cdb3ef11a9774e55d07a0aa7382a6a74abae24..e2c0cb8399868dfe963278f82e1dcfae0d04a8fd 100644 --- a/src/lib/components/variables/VariableReferredValueEdit.svelte +++ b/src/lib/components/variables/VariableReferredValueEdit.svelte @@ -1,5 +1,5 @@ <script lang="ts"> - import type { ValueAtInstant } from "@openfisca/json-model" + import type { Unit, ValueAtInstant } from "@openfisca/json-model" import { createEventDispatcher } from "svelte" import { removeNegativeZero } from "$lib/values" @@ -7,7 +7,7 @@ export let billValue: ValueAtInstant | undefined | null export let lawValue: ValueAtInstant | undefined | null export let revaluationValue: ValueAtInstant | undefined | null - export let unitName: string | undefined | null + export let unit: Unit | undefined export let value: | boolean | number @@ -18,6 +18,8 @@ | undefined | null + $: console.log(unit) + const dispatch = createEventDispatcher() const numberFormatter = (value: number): string => new Intl.NumberFormat("fr-FR", { @@ -29,7 +31,7 @@ ? "absent" : typeof billValue.value === "number" ? numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((billValue.value * 100).toFixed(8)) // trick to round value * 100 : billValue.value, ) @@ -39,7 +41,7 @@ ? "absent" : typeof lawValue.value === "number" ? numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((lawValue.value * 100).toFixed(8)) // trick to round value * 100 : lawValue.value, ) @@ -51,7 +53,7 @@ ? "absent" : typeof revaluationValue.value === "number" ? numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((revaluationValue.value * 100).toFixed(8)) // trick to round value * 100 : revaluationValue.value, ) @@ -61,7 +63,7 @@ ? "absent" : typeof value === "number" ? numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((value * 100).toFixed(8)) // trick to round value * 100 : value, ) @@ -73,7 +75,7 @@ function changeValue({ target }: Event) { let { value } = target as HTMLInputElement - if (unitName === "/1") { + if (unit !== undefined && unit.ratio) { const validValue = parseFloat(value) if (validValue != null && !Number.isNaN(validValue)) { value = (validValue / 100).toString() @@ -124,7 +126,7 @@ placeholder={value === "expected" ? "attendu" : null} step="any" value={typeof value === "number" - ? unitName === "/1" + ? unit !== undefined && unit.ratio ? parseFloat((value * 100).toFixed(8)) // trick to round value * 100 : value : null} diff --git a/src/lib/components/variables/VariableReferredValueParameter.svelte b/src/lib/components/variables/VariableReferredValueParameter.svelte index e0d663c6038b18f8f9957722984f588785c9d3c6..6c3f58ca495eefde601000033f0acb7ec222b600 100644 --- a/src/lib/components/variables/VariableReferredValueParameter.svelte +++ b/src/lib/components/variables/VariableReferredValueParameter.svelte @@ -26,7 +26,7 @@ type ParametricReform, type ValueParameterReform, } from "$lib/reforms" - import { getUnitShortLabel } from "$lib/units" + import { getUnitAtDate, getUnitShortLabel } from "$lib/units" import { budgetEditableParametersNameByVariableName, budgetVariablesName, @@ -214,14 +214,14 @@ {lawValue} on:changeValue={changeValue} {revaluationValue} - unitName={billParameter.unit} + unit={getUnitAtDate(billParameter.unit, date)} {value} /> {:else} <VariableReferredValueView {billValue} {lawValue} - unitName={billParameter.unit} + unit={getUnitAtDate(billParameter.unit, date)} {revaluationValue} /> {/if} diff --git a/src/lib/components/variables/VariableReferredValueView.svelte b/src/lib/components/variables/VariableReferredValueView.svelte index cadda194205c99dcf36ff79b1ef0a116bf00e6ac..2ee596649e4060b40b2c98ec9c95127240c20cb3 100644 --- a/src/lib/components/variables/VariableReferredValueView.svelte +++ b/src/lib/components/variables/VariableReferredValueView.svelte @@ -1,5 +1,5 @@ <script lang="ts"> - import type { ValueAtInstant } from "@openfisca/json-model" + import type { Unit, ValueAtInstant } from "@openfisca/json-model" import { removeNegativeZero } from "$lib/values" import Tooltip from "$lib/components/Tooltip.svelte" @@ -7,7 +7,7 @@ export let billValue: ValueAtInstant | undefined | null export let lawValue: ValueAtInstant | undefined | null export let revaluationValue: ValueAtInstant | undefined | null - export let unitName: string | undefined | null + export let unit: Unit | undefined const numberFormatter = (value: number): string => new Intl.NumberFormat("fr-FR", { @@ -18,10 +18,10 @@ billValue == null || billValue === "expected" || billValue.value === null ? "absent" : typeof billValue.value === "number" - ? unitName === "year" + ? unit !== undefined && unit.name === "year" ? billValue.value.toString() // No space in years : numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((billValue.value * 100).toFixed(8)) // trick to round value * 100 : billValue.value, ) @@ -31,10 +31,10 @@ lawValue == null || lawValue === "expected" || lawValue.value === null ? "absent" : typeof lawValue.value === "number" - ? unitName === "year" + ? unit !== undefined && unit.name === "year" ? lawValue.value.toString() // No space in years : numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((lawValue.value * 100).toFixed(8)) // trick to round value * 100 : lawValue.value, ) @@ -46,10 +46,10 @@ revaluationValue.value === null ? "absent" : typeof revaluationValue.value === "number" - ? unitName === "year" + ? unit !== undefined && unit.name === "year" ? revaluationValue.value.toString() // No space in years : numberFormatter( - unitName === "/1" + unit !== undefined && unit.ratio ? parseFloat((revaluationValue.value * 100).toFixed(8)) // trick to round value * 100 : revaluationValue.value, ) diff --git a/src/lib/units.ts b/src/lib/units.ts index b0f3f29b84590c8ac238320f0e0abc74f8d1ee5b..b344e9eac150c63b294b18e5487391812611e2fa 100644 --- a/src/lib/units.ts +++ b/src/lib/units.ts @@ -21,6 +21,15 @@ export function getUnitAtDate( return getUnitAtDateOriginal(unitByName, name, date) } +export function getUnitByName( + name: string | undefined | null, +): Unit | undefined { + if (name === undefined || name === null) { + return undefined + } + return unitByName[name] +} + export function getUnitLabel( name: string | undefined | null, date: string, diff --git a/src/lib/values.ts b/src/lib/values.ts index 39c420e1794ffff691342229bb25d331191026a0..9e4d58eef3272dad06518b2a8866151402bbcd00 100644 --- a/src/lib/values.ts +++ b/src/lib/values.ts @@ -1,3 +1,5 @@ +import { getUnitByName } from "$lib/units" + export function formatValue(value: number, unitName?: string): string { return valueFormatter(value, unitName)(value) } @@ -13,6 +15,7 @@ export function valueFormatter( unitName?: string | undefined | null, compact = false, ): (value: unknown) => string { + const unit = getUnitByName(unitName) return baseValue === undefined ? ((() => "") as (value: unknown) => string) : typeof baseValue === "boolean" @@ -20,7 +23,7 @@ export function valueFormatter( value: unknown, ) => string) : typeof baseValue === "number" - ? unitName === "/1" // rate + ? unit !== undefined && unit.ratio // rate ? (value: unknown): string => new Intl.NumberFormat("fr-FR", { maximumFractionDigits: 2,