Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Update @woo…/number code docs, define & export type declarations. #7840

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
26 changes: 17 additions & 9 deletions packages/number/src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const numberFormatter = require( 'locutus/php/strings/number_format' );

/**
* Number formatting configuration object
*
* @typedef {Object} NumberConfig
* @property {number} [precision] Decimal precision.
* @property {string} [decimalSeparator] Decimal separator.
* @property {string} [thousandSeparator] Character used to separate thousands groups.
*/

/**
* Formats a number using site's current locale
*
* @see http://locutus.io/php/strings/number_format/
* @param {Object} numberConfig number formatting configuration object.
* @param {number} numberConfig.precision
* @param {string} numberConfig.decimalSeparator
* @param {string} numberConfig.thousandSeparator
* @param {NumberConfig} numberConfig Number formatting configuration object.
* @param {number|string} number number to format
* @return {?string} A formatted string.
* @return {string} A formatted string.
*/
export function numberFormat(
{ precision = null, decimalSeparator = '.', thousandSeparator = ',' },
Expand Down Expand Up @@ -39,12 +45,14 @@ export function numberFormat(
}

/**
* Formats a number string based on type of `average` or `number`.
* Formats a number as average or number string according to the given `type`.
* - `type = 'average'` returns a rounded `Number`
* - `type = 'number'` returns a formatted `String`
Comment on lines +49 to +50
Copy link
Member Author

Choose a reason for hiding this comment

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

*
* @param {Object} numberConfig number formatting configuration object.
* @param {string} type of number to format, average or number
* @param {NumberConfig} numberConfig number formatting configuration object.
* @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.
Copy link
Member Author

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.

*/
export function formatValue( numberConfig, type, value ) {
if ( ! Number.isFinite( value ) ) {
Expand Down
1 change: 1 addition & 0 deletions packages/number/tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../tsconfig-cjs",
"compilerOptions": {
"declaration": true,
"outDir": "build"
}
}
1 change: 1 addition & 0 deletions packages/number/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../tsconfig",
"compilerOptions": {
"declaration": true,
"rootDir": "src",
"outDir": "build-module"
}
Expand Down