-
Notifications
You must be signed in to change notification settings - Fork 0
Add REST API server #6
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
Conversation
- GET /convert/:number - Convert to words (supports ?lang=es, etc.) - GET /ordinal/:number - Convert to ordinal - GET /decimal/:number - Convert decimals - GET /currency/:amount - Currency to words - GET /roman/:number - Roman numerals - GET /parse/:words - Parse words to number - GET /comma/:number - Format with commas - GET /languages - List supported languages Server runs on port 3456 (configurable via PORT env var) Excluded from npm package via .npmignore Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 523559041e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const { number } = req.params; | ||
| const { lang, cap: capStyle, punc } = req.query; | ||
|
|
||
| const num = number.includes('n') ? BigInt(number.replace('n', '')) : parseInt(number, 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard BigInt parsing to avoid 500s on bad inputs
Inputs that contain the character n but are not valid BigInt literals (e.g., /convert/none or /convert/ten) will hit BigInt(number.replace('n','')) and throw a synchronous exception. Express will turn that into a 500 instead of the intended 400, so malformed inputs containing n behave differently from other invalid numbers. This same pattern appears in /comma/:number as well. Consider validating with a strict regex and returning 400 on failure or wrapping the BigInt conversion in a try/catch.
Useful? React with 👍 / 👎.
| const { lang, cap: capStyle, punc } = req.query; | ||
|
|
||
| const num = number.includes('n') ? BigInt(number.replace('n', '')) : parseInt(number, 10); | ||
|
|
||
| const opt = {}; | ||
| if (lang) opt.lang = lang; | ||
| if (capStyle) opt.cap = capStyle; | ||
| if (punc) opt.punc = punc; | ||
|
|
||
| const result = lang ? toWords(num, opt) : numberstring(num, opt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve punctuation option when lang is supplied
When a caller includes lang in the query string, the handler switches from numberstring() to toWords(). toWords() only applies cap and ignores punc, so requests like /convert/42?lang=en&punc=! silently drop the punctuation. This is surprising for clients that always send lang and expect the documented punc option to work. Either keep using numberstring() for English or apply punctuation after toWords() as well.
Useful? React with 👍 / 👎.
Summary
Adds a REST API server for numberstring, similar to how cAPIta wraps capstring.
Endpoints
GET /convert/:number- Convert to words (supports?lang=es, etc.)GET /ordinal/:number- Convert to ordinal wordsGET /decimal/:number- Convert decimals to wordsGET /currency/:amount- Currency to wordsGET /roman/:number- Roman numeralsGET /parse/:words- Parse words back to numberGET /comma/:number- Format with commasGET /languages- List supported languagesUsage
Note
Server is excluded from npm package via
.npmignore- it's repo-only.Test plan
🤖 Generated with Claude Code