Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.
Closed
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
1 change: 1 addition & 0 deletions packages/number/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Dev - Export data types. #7840
- Update all js packages with minor/patch version changes. #8392

# 2.2.0
Expand Down
30 changes: 19 additions & 11 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 {number|string} number number to format
* @return {?string} A formatted string.
* @param {NumberConfig} numberConfig Number formatting configuration object.
* @param {number|string} number number to format
* @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
Contributor 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 {number} value to format.
* @return {?string} A formatted string.
* @param {NumberConfig} numberConfig number formatting configuration object.
* @param {string} type of number to format, `'average'` or `'number'`
* @param {number} value to format.
* @return {string | number | null} A formatted string.
Copy link
Contributor 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