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

fix(core): updates to SpelledNumbers #325

Merged
merged 2 commits into from
Dec 24, 2024

Conversation

grantlemons
Copy link
Collaborator

  • Fix comments
  • Change from suggesting if <= 10 to < 10
  • Expand spelling function (110 would give stack overflow before)
  • Update tests accordingly

I wasn't able to find an "Automattic style guide" as referenced in recent
commits on this, but it is rather standard to spell out numbers under but not
including 10. This is what APA does, and I believe MLA as well.

Recent commits also didn't update doc comments to reflect new behavior, so I
fixed that.

One thing to keep in mind, but I don't think is implemented yet, is that
numbers beginning sentences should always be spelled out, so it would be best
to expand behavior to include this in further enhancements.

- Fix comments
- Change from suggesting if <= 10 to < 10
- Expand spelling function (actually works for 110 now)
- Update tests accordingly
Comment on lines +82 to +84
"{}{}{}",
spell_out_number(parent).unwrap(),
if num <= 99 { '-' } else { ' ' },
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is because numbers between twenty-one and ninety-nine should be hyphenated.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Toss this info in a comment in the code.

Comment on lines +77 to +79
let n = 10u64.pow((num as f32).log10() as u32);
let parent = (num / n) * n; // truncate
let child = num % n;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This modification allows the truncation to extend to n digits, instead of just working for two digits as in the past.

This used to cause a stack overflow for n = 110. (and probably 101 to 109 as well)

Comment on lines +73 to +75
hundred if hundred % 100 == 0 => {
format!("{} hundred", spell_out_number(hundred / 100).unwrap())
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This logic should probably be expanded to include more suffixes for powers of 10 up to 10^19 so it will work for large numbers at the beginning of sentences.

(10^19 is the largest power of 10 a u64 could represent)

lints.push(Lint {
span: number_tok.span,
lint_kind: LintKind::Readability,
suggestions: vec![Suggestion::ReplaceWith(
spell_out_number(number as u64).unwrap().chars().collect(),
)],
message: "Try to spell out numbers less than a hundred.".to_string(),
message: "Try to spell out numbers less than ten.".to_string(),
Copy link
Collaborator Author

@grantlemons grantlemons Dec 22, 2024

Choose a reason for hiding this comment

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

It is kinda funny that this is spelled out when under my change it wouldn't be suggested.

I've always worked on the principle of:

  • Spelled out for < 10
  • Numerals for > 10
  • Whatever feels best for 10 in the moment

@elijah-potter
Copy link
Collaborator

One thing to keep in mind, but I don't think is implemented yet, is that
numbers beginning sentences should always be spelled out, so it would be best
to expand behavior to include this in further enhancements.

I am aware. This was planned.

I wasn't able to find an "Automattic style guide" as referenced in recent
commits on this

It's quite closely based on the Chicago Style, with a few changes.

Overall, this PR looks great! Merging...

@elijah-potter elijah-potter merged commit e933eb6 into Automattic:master Dec 24, 2024
17 checks passed
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.

2 participants