This commit is contained in:
KhaiNguyen
2020-02-13 10:39:37 +07:00
commit 59401cb805
12867 changed files with 4646216 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
/**
* Given a JS error or a fetch response error, parse and format it so it can be displayed to the user.
*
* @param {Object} error Error object.
* @param {Function} [error.json] If a json method is specified, it will try parsing the error first.
* @param {string} [error.message] If a message is specified, it will be shown to the user.
* @param {string} [error.type] The context in which the error was triggered.
* @return {Object} Error object containing a message and type.
*/
export const formatError = async ( error ) => {
if ( typeof error.json === 'function' ) {
try {
const parsedError = await error.json();
return {
message: parsedError.message,
type: parsedError.type || 'api',
};
} catch ( e ) {
return {
message: e.message,
type: 'general',
};
}
}
return {
message: error.message,
type: error.type || 'general',
};
};

View File

@@ -0,0 +1,2 @@
export * from './errors';
export * from './price';

View File

@@ -0,0 +1,33 @@
/**
* External dependencies
*/
import { sprintf } from '@wordpress/i18n';
import { CURRENCY } from '@woocommerce/settings';
/**
* Format a price with currency data.
*
* @param {number} value Number to format.
* @param {string} priceFormat Price format string.
* @param {string} currencySymbol Currency symbol.
*/
export const formatPrice = (
value,
priceFormat = CURRENCY.priceFormat,
currencySymbol = CURRENCY.symbol
) => {
const formattedNumber = parseInt( value, 10 );
if ( ! isFinite( formattedNumber ) ) {
return '';
}
const formattedValue = sprintf(
priceFormat,
currencySymbol,
formattedNumber
);
// This uses a textarea to magically decode HTML currency symbols.
const txt = document.createElement( 'textarea' );
txt.innerHTML = formattedValue;
return txt.value;
};

View File

@@ -0,0 +1,42 @@
/**
* Internal dependencies
*/
import { formatError } from '../errors';
describe( 'formatError', () => {
test( 'should format general errors', async () => {
const error = await formatError( {
message: 'Lorem Ipsum',
} );
const expectedError = {
message: 'Lorem Ipsum',
type: 'general',
};
expect( error ).toEqual( expectedError );
} );
test( 'should format API errors', async () => {
const error = await formatError( {
json: () => Promise.resolve( { message: 'Lorem Ipsum' } ),
} );
const expectedError = {
message: 'Lorem Ipsum',
type: 'api',
};
expect( error ).toEqual( expectedError );
} );
test( 'should format JSON parse errors', async () => {
const error = await formatError( {
json: () => Promise.reject( { message: 'Lorem Ipsum' } ),
} );
const expectedError = {
message: 'Lorem Ipsum',
type: 'general',
};
expect( error ).toEqual( expectedError );
} );
} );

View File

@@ -0,0 +1,29 @@
/**
* Internal dependencies
*/
import { formatPrice } from '../price';
describe( 'formatPrice', () => {
test.each`
value | priceFormat | currencySymbol | expected
${10} | ${'%1$s%2$s'} | ${'€'} | ${'€10'}
${10} | ${'%2$s%1$s'} | ${'€'} | ${'10€'}
${10} | ${'%2$s%1$s'} | ${'$'} | ${'10$'}
${'10'} | ${'%1$s%2$s'} | ${'€'} | ${'€10'}
${0} | ${'%1$s%2$s'} | ${'€'} | ${'€0'}
${''} | ${'%1$s%2$s'} | ${'€'} | ${''}
${null} | ${'%1$s%2$s'} | ${'€'} | ${''}
${undefined} | ${'%1$s%2$s'} | ${'€'} | ${''}
`(
'correctly formats price given "$value", "$priceFormat", and "$currencySymbol"',
( { value, priceFormat, currencySymbol, expected } ) => {
const formattedPrice = formatPrice(
value,
priceFormat,
currencySymbol
);
expect( formattedPrice ).toEqual( expected );
}
);
} );