Skip to content

Conversation

@egedarici
Copy link
Contributor

@egedarici egedarici commented Apr 11, 2025

  • If a dash joined unit is registered, return it
  • If not, parse it as expected

Cases:

  • If unit is registered as a single unit, return it (watt-hour)
  • If unit is not registered as a single unit, process tokens individually (gram-liter into [gram, liter]
  • If unit has an operator (/ or *)
    • Allow dash in units since we can have 2 cases: watt-hour/kg or for example gram-liter/hour
      • watt-hour/kg is tokenized into [watt-hour,/,kilogram] since watt-hour is a registered single unit
      • gram-liter/hour is tokenized into [gram, liter, /, hour] since gram-liter is not a registered single unit and needs to be processed by gram and liter

@egedarici egedarici marked this pull request as ready for review April 11, 2025 13:55
@egedarici egedarici requested a review from tomhooijenga as a code owner April 11, 2025 13:56
public function test_parses_dash_joined_unit_directly_if_registered()
{
$this->registry->register('watt-hour', new Unit(
// Watt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Watt
// Watt

Copy link
Collaborator

@tomhooijenga tomhooijenga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when my unit-with-a-dash is part of some bigger unit? Like watt-hour/kg?

@egedarici
Copy link
Contributor Author

What happens when my unit-with-a-dash is part of some bigger unit? Like watt-hour/kg?

watt-hour is an individual token, as well as kilogram now. However due to the operator being present, both tokens get processed through the ratio calculation since power can change for numerator/denominator's unit if watt_hour/kg or kg/watt-hour.

Cases:

  • If unit is registered as a single unit, return it (watt-hour)
  • If unit is not registered as a single unit, process tokens individually (gram-liter into [gram, liter]
  • If unit has an operator (/ or *)
    • Allow dash in units since we can have 2 cases: watt-hour/kg or for example gram-liter/hour
      • watt-hour/kg is tokenized into [watt-hour,/,kilogram] since watt-hour is a registered single unit
      • gram-liter/hour is tokenized into [gram, liter, /, hour] since gram-liter is not a registered single unit and needs to be processed by gram and liter

src/Parser.php Outdated
'value' => $match['unit'],
'power' => (int)($match['power'] ?? 1),
];
// If the unit contains a dash, check if it's a registered unit as a whole

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, are there any units that actually have a dash but aren't compounds? Because that would completely ruin us right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be possible, but I guess we should be able to handle it. We do test for gram-liter and handle them individually for example

)
);

static::registerSiUnit(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pull master.


return new Unit(...array_merge([], ...$parts));
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep it?

@egedarici egedarici force-pushed the WAK-1758-Lakh-ton-is-not-working-correctly branch from 92e9bda to d9a31cb Compare April 17, 2025 08:02
@egedarici egedarici force-pushed the WAK-1758-Lakh-ton-is-not-working-correctly branch from 11317e5 to 7b0b58d Compare April 17, 2025 08:07
@egedarici egedarici closed this Apr 17, 2025
@egedarici egedarici deleted the WAK-1758-Lakh-ton-is-not-working-correctly branch April 17, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants