Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔢 Support for scientific notation and decimal numbers in si role #577

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions packages/myst-parser/tests/roles/si.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,108 @@ cases:
unit: Hz
units: [hertz]
value: 100 Hz

- title: decimal number parses
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a couple more test cases (hopefully easy copy/paste)? At least one with a negative, maybe one with only a decimal, no exponent?

Hmm, I think .345 <\hertz> might fail still... That's totally fine with me, just another case to consider (maybe add a failing case for this one with error: true if you agree that failing is the right behaviour). Could also add a failing case for point-delimited numbers, eg. 1.000.000,05 🤷‍♀️

markdown: '{si}`1.345e10 <\hertz>`'
mdast:
type: root
children:
- type: paragraph
children:
- type: mystRole
name: si
value: 1.345e10 <\hertz>
children:
- type: si
alt: hertz
number: '1.345e10'
unit: Hz
units: [hertz]
value: 1.345e10 Hz

- title: negative decimal number parses
markdown: '{si}`-1.345e10 <\hertz>`'
mdast:
type: root
children:
- type: paragraph
children:
- type: mystRole
name: si
value: -1.345e10 <\hertz>
children:
- type: si
alt: hertz
number: '-1.345e10'
unit: Hz
units: [hertz]
value: -1.345e10 Hz

- title: decimal number parses
markdown: '{si}`1.345 <\hertz>`'
mdast:
type: root
children:
- type: paragraph
children:
- type: mystRole
name: si
value: 1.345 <\hertz>
children:
- type: si
alt: hertz
number: '1.345'
unit: Hz
units: [hertz]
value: 1.345 Hz

- title: exponent integer number parses
markdown: '{si}`1e45 <\hertz>`'
mdast:
type: root
children:
- type: paragraph
children:
- type: mystRole
name: si
value: 1e45 <\hertz>
children:
- type: si
alt: hertz
number: '1e45'
unit: Hz
units: [hertz]
value: 1e45 Hz

- title: negative exponent grouped decimal number parses
markdown: '{si}`-1,008,000.342e45 <\hertz>`'
mdast:
type: root
children:
- type: paragraph
children:
- type: mystRole
name: si
value: -1,008,000.342e45 <\hertz>
children:
- type: si
alt: hertz
number: '-1,008,000.342e45'
unit: Hz
units: [hertz]
value: -1,008,000.342e45 Hz

- title: wrongly grouped integer number errors
markdown: '{si}`-1.008.000,05 <\hertz>`'
mdast:
type: root
children:
- type: paragraph
children:
- type: mystRole
name: si
value: -1.008.000,05 <\hertz>
children:
- type: si
error: true
value: -1.008.000,05 <\hertz>
21 changes: 19 additions & 2 deletions packages/myst-roles/src/si.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@ export const siRole: RoleSpec = {
},
run(data: RoleData): GenericNode[] {
const value = data.body as string;
const match = value.match(/([0-9]+)\s?<([\\a-zA-Z\s]+)>/);
const match = value.match(
/* In verbose regex, the following pattern looks like e.g. -1000,000,000.192e24
^
(-? # Optional unary negative e.g. -
( # <<< Decimal group
\d+ # Leading integers (1+) e.g. 1000
(,\d+)* # Grouped runs of integers (0+) e.g. [,000,000]
(\.\d+)? # Optional decimal with trailing integer e.g. [.192]
)? # Decimal is optional >>>
(e\d+)? # Optional scientific exponent e.g. [e24]
)
\s?
<([\\a-zA-Z\s]+)>
$
*/
/^(-?(\d+(,\d+)*(\.\d+)?)?(e\d+)?)\s?<([\\a-zA-Z\s]+)>$/,
);
if (!match) {
return [{ type: 'si', error: true, value }];
}
const [, number, commands] = match;
const number = match[1];
const commands = match[match.length - 1];
const parsed = [...commands.matchAll(/\\([a-zA-Z]+)/g)];
const units = parsed.filter((c) => !!c).map(([, c]) => c);
const translated = units.map((c) => UNITS[c] ?? c);
Expand Down
Loading