-
Notifications
You must be signed in to change notification settings - Fork 145
Update @woo…/number code docs, define & export type declarations.
#7840
Conversation
Fix the returned value's type.
`locutus/php/strings/number_format` seems to always return a `String`.
| * - `type = 'average'` returns a rounded `Number` | ||
| * - `type = 'number'` returns a formatted `String` |
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.
See Additional note 2.
| * @param {string} type of number to format, `'average'` or `'number'` | ||
| * @param {number} value to format. | ||
| * @return {?string} A formatted string. | ||
| * @return {string | number | null} A formatted string. |
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.
Technically, it may also be undefined if an incorrect type is given. See Additional note 3.
|
@psealock Would you mind taking a look? |
Yeah, that is absolutely confusing. I see what happened there though. The type
and ultimately fed into a woocommerce-admin/packages/components/src/summary/number.js Lines 114 to 118 in 0cec48d
|
That would require a default |
|
@tomalec Just checking if this is ready for review of if you are still working on it? The breaking change might be better to implement in a follow-up PR. We're planning to merge Woo Admin to Core soon and ideally it would be best have this PR completed before then. |
|
Hi sorry, for the late response, I was away from the keyboard for an extended time :| On my side, it is ready for review. I'm happy to merge it as is and make separate PRs for potential improvements discussed above. |
|
Closing in favor of woocommerce/woocommerce#32325 |
As per the Dependency Extraction Webpack Plugin installing this package in another plugin serves only type checking purposes (see WordPress/gutenberg#35630 (comment)), it would be beneficial to actually define and export those types.
I also updated the docs for the function I find the most confusing and a few return types which I find wrong.
This PR
NumberConfigdata type, export type declarations. (8cdc828)formatValuedocs, (5d7d53f)Now it clearly explains what and in what type is returned for specific
typeparameter values.It's not only
nullorstringbut may also be anumberlocutus/php/strings/number_formatseems to always return aString.Accessibility
n/a - its only code documentation change
Screenshots
Detailed test instructions:
npm install @woocommerce/number{import('@woocommerce/number').NumberConfig}Additional notes:
@woocommerce/currencyto make the code docs shorter.formatValueAPI confusing, as it returns typestringfortype='number'parameter, and typenumberfortype='average'. I'm not sure if that was desired, given the lack of tests. Alternatively, we could change the implementation and make sure it always returns a?stringas the docs stated before. But that may be an effectively breaking change.formatValue'stypeparameter, to make sure it would never returnundefined, which currently may be a result of an invalid param given.No changelog necessary - added entry in package's one.