Skip to content

Commit

Permalink
New: Add Carbon.Number.pxToRem
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Feb 4, 2024
1 parent eab52a6 commit 7361f5f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Classes/EelHelper/NumberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ public function decimalDigits(float $number): int
}
}

/**
* Convert a pixel value to rem
*
* @param mixed $value The value to convert
* @param mixed $fallback Fallback value if $value is false or null
* @return string
*/
public function pxToRem($value, $fallback = null): string
{
if ($fallback && ($value === false || $value === null)) {
$value = (int) $fallback;
}

$value = (int) $value;
if (!$value) {
return '0';
}

return ($value / 16) . 'rem';
}

/**
* All methods are considered safe
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,26 @@ Get number of decimal digits.

- `number` (float, required) The number being formatted

### `Carbon.Number.pxToRem(value, fallback)`

Convert a pixel value to rem

- `value` (numeric | string, required) The value to converted
- `fallback` (numeric | string, optional) Fallback value if `value` is `false` or `null`

Examples:

| Expression | Result |
| ------------------------------------- | -------- |
| `Carbon.Number.pxToRem('')` | `0` |
| `Carbon.Number.pxToRem('8px')` | `0.5rem` |
| `Carbon.Number.pxToRem(16)` | `1rem` |
| `Carbon.Number.pxToRem(0)` | `0` |
| `Carbon.Number.pxToRem(false, 32)` | `2rem` |
| `Carbon.Number.pxToRem(null, '16px')` | `2rem` |

Returns a string with the converted value

## Backend Helper

### `Carbon.Backend.language()`
Expand Down

0 comments on commit 7361f5f

Please sign in to comment.