Skip to content

Commit

Permalink
refactor(publicodes-states): nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 10, 2023
1 parent ad09960 commit 18e7675
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
5 changes: 2 additions & 3 deletions src/publicodes-state/helpers/safeEvaluateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ export const safeEvaluateHelper = (
rule: string,
engineUsed: Engine
): NGCEvaluatedNode | null => {
let evaluation = null
try {
evaluation = engineUsed.evaluate(rule)
return engineUsed.evaluate(rule)
} catch (error) {
// TODO: Sending error to Sentry breaks the app
console.warn(error)
}
return evaluation
return null
}
58 changes: 32 additions & 26 deletions src/publicodes-state/simulationProvider/useCategories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react'
import { safeEvaluateHelper } from '../helpers/safeEvaluateHelper'
import { Engine, NGCRuleNode } from '../types'

type Props = {
engine: Engine
root: string
Expand All @@ -14,35 +15,40 @@ export default function useCategories({
safeGetRule,
order,
}: Props) {
const categories = useMemo<string[]>(
() =>
safeGetRule(root)?.rawNode?.formule?.somme.sort((a: string, b: string) =>
!order ? 0 : order.indexOf(a) > order.indexOf(b) ? 1 : -1
),
[root, order, safeGetRule]
)
const categories = useMemo<string[]>(() => {
const sum = safeGetRule(root)?.rawNode?.formule?.somme
return order
? // NOTE(@EmileRolley): what should be the wanted behavior if the order
// doesn't match the sum?
sum.sort((a: string, b: string) => order.indexOf(a) - order.indexOf(b))
: sum
}, [root, order, safeGetRule])

const subcategories = useMemo<Record<string, string[]>>(
() =>
categories.reduce(
(accumulator: object, currentValue: string) => ({
...accumulator,
[currentValue]:
currentValue === 'services sociétaux'
? []
: (
safeGetRule(currentValue)?.rawNode?.formule?.somme?.map(
(rule: string) => currentValue + ' . ' + rule
) || []
).sort((a: string, b: string) =>
(safeEvaluateHelper(a, engine)?.nodeValue || 0) >
(safeEvaluateHelper(b, engine)?.nodeValue || 0)
? -1
: 1
),
}),
{}
),
categories.reduce((acc: object, category: string) => {
const categorySum = safeGetRule(category)?.rawNode?.formule?.somme

if (!categorySum || category === 'services sociétaux') {
return {
...acc,
[category]: [],
}
}

const sortedSubCategory = categorySum
.map((rule: string) => category + ' . ' + rule)
.sort(
(a: string, b: string) =>
(safeEvaluateHelper(a, engine)?.nodeValue ?? 0) -
(safeEvaluateHelper(b, engine)?.nodeValue ?? 0)
)

return {
...acc,
[category]: sortedSubCategory,
}
}, {}),
[categories, safeGetRule, engine]
)
return { categories, subcategories }
Expand Down
3 changes: 2 additions & 1 deletion src/publicodes-state/simulationProvider/useEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { NGCEvaluatedNode, NGCRuleNode, Rules } from '../types'
*
* Also return safeEvaluate and safeGetRule wich catch errors if dottedName is invalid
*
* And a pristine engine wich can be used to assess rules without any situation (for exemple, we can reliably sort the subcategories this way)
* And a pristine engine wich can be used to evaluate rules without any situation
* (for exemple, we can reliably sort the subcategories this way)
*/
export default function useEngine(rules: Rules) {
const engine = useMemo<Engine>(
Expand Down
2 changes: 1 addition & 1 deletion src/publicodes-state/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type NGCRuleNode = RuleNode & {
export type NGCRulesNodes = Record<string, NGCRuleNode>

//TODO: complete explanation type
export type NGCEvaluatedNode = EvaluatedNode & {
export type NGCEvaluatedNode = EvaluatedNode<number> & {
explanation: {
ruleDisabledByItsParent: boolean
}
Expand Down

0 comments on commit 18e7675

Please sign in to comment.