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,13 @@
/**
* Internal dependencies
*/
import { allSettings } from './settings-init';
export const ADMIN_URL = allSettings.adminUrl;
export const COUNTRIES = allSettings.countries;
export const CURRENCY = allSettings.currency;
export const LOCALE = allSettings.locale;
export const ORDER_STATUSES = allSettings.orderStatuses;
export const SITE_TITLE = allSettings.siteTitle;
export const WC_ASSET_URL = allSettings.wcAssetUrl;
export const DEFAULT_DATE_RANGE = allSettings.defaultDateRange;

View File

@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import { allSettings } from './settings-init';
/**
* Retrieves a setting value from the setting state.
*
* @export
* @param {string} name The identifier for the setting.
* @param {mixed} [fallback=false] The value to use as a fallback
* if the setting is not in the
* state.
* @param {Function} [filter=( val ) => val] A callback for filtering the
* value before it's returned.
* Receives both the found value
* (if it exists for the key) and
* the provided fallback arg.
* @returns {mixed}
*/
export function getSetting( name, fallback = false, filter = ( val ) => val ) {
const value = allSettings.hasOwnProperty( name )
? allSettings[ name ]
: fallback;
return filter( value, fallback );
}

View File

@@ -0,0 +1,44 @@
/**
* Internal dependencies
*/
import { getSetting } from './get-setting';
/**
* External dependencies
*/
import compareVersions from 'compare-versions';
export * from './default-constants';
export { setSetting } from './set-setting';
/**
* Note: this attempts to coerce the wpVersion to a semver for comparison
* This will result in dropping any beta/rc values.
*
* `5.3-beta1-4252` would get converted to `5.3.0-rc.4252`
* `5.3-beta1` would get converted to `5.3.0-rc`.
* `5.3` would not be touched.
*
* For the purpose of these comparisons all pre-release versions are normalized
* to `rc`.
*/
export const compareWithWpVersion = ( version, operator ) => {
let replacement = getSetting( 'wpVersion', '' ).replace(
/-[a-zA-Z0-9]*[\-]*/,
'.0-rc.'
);
replacement = replacement.endsWith( '.' )
? replacement.substring( 0, replacement.length - 1 )
: replacement;
return compareVersions.compare( version, replacement, operator );
};
export { compareVersions, getSetting };
/**
* Returns a string with the site's wp-admin URL appended. JS version of `admin_url`.
*
* @param {String} path Relative path.
* @return {String} Full admin URL.
*/
export const getAdminLink = ( path ) => getSetting( 'adminUrl' ) + path;

View File

@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import { allSettings } from './settings-init';
/**
* Sets a value to a property on the settings state.
*
* @export
* @param {string} name The setting property key for the
* setting being mutated.
* @param {mixed} value The value to set.
* @param {Function} [filter=( val ) => val] Allows for providing a callback
* to sanitize the setting (eg.
* ensure it's a number)
*/
export function setSetting( name, value, filter = ( val ) => val ) {
allSettings[ name ] = filter( value );
}

View File

@@ -0,0 +1,42 @@
const defaults = {
adminUrl: '',
countries: [],
currency: {
code: 'USD',
precision: 2,
symbol: '$',
symbolPosition: 'left',
decimalSeparator: '.',
priceFormat: '%1$s%2$s',
thousandSeparator: ',',
},
defaultDateRange: 'period=month&compare=previous_year',
locale: {
siteLocale: 'en_US',
userLocale: 'en_US',
weekdaysShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
},
orderStatuses: [],
siteTitle: '',
wcAssetUrl: '',
};
const globalSharedSettings = typeof wcSettings === 'object' ? wcSettings : {};
// Use defaults or global settings, depending on what is set.
const allSettings = {
...defaults,
...globalSharedSettings,
};
allSettings.currency = {
...defaults.currency,
...allSettings.currency,
};
allSettings.locale = {
...defaults.locale,
...allSettings.locale,
};
export { allSettings };

View File

@@ -0,0 +1,24 @@
/**
* Internal dependencies
*/
import { compareWithWpVersion, setSetting } from '..';
describe( 'compareWithWpVersion', () => {
it.each`
version | operator | result
${'5.3-beta1'} | ${'>'} | ${true}
${'5.3'} | ${'='} | ${true}
${'5.3-beta12-235'} | ${'>'} | ${true}
${'5.3-rc1'} | ${'<'} | ${false}
${'5.3-rc12-235'} | ${'>'} | ${true}
${'5.3.1'} | ${'<'} | ${true}
${'5.4-beta1'} | ${'<'} | ${true}
`(
'should return $result when $version is the current wpVersion ' +
'and `5.3` is the version compared using `$operator`',
( { version, operator, result } ) => {
setSetting( 'wpVersion', version );
expect( compareWithWpVersion( '5.3', operator ) ).toBe( result );
}
);
} );

View File

@@ -0,0 +1,17 @@
/**
* Internal dependencies
*/
import { getSetting } from '../get-setting';
import { ADMIN_URL } from '../default-constants';
describe( 'getSetting', () => {
it( 'returns provided default for non available setting', () => {
expect( getSetting( 'nada', 'really nada' ) ).toBe( 'really nada' );
} );
it( 'returns expected value for existing setting', () => {
expect( getSetting( 'adminUrl', 'not this' ) ).toEqual( ADMIN_URL );
} );
it( 'filters value via provided filter callback', () => {
expect( getSetting( 'some value', 'default', () => 42 ) ).toBe( 42 );
} );
} );

View File

@@ -0,0 +1,20 @@
/**
* Internal dependencies
*/
import { setSetting } from '../set-setting';
import { getSetting } from '../get-setting';
describe( 'setSetting', () => {
it( 'should add a new value to the settings state for value not present', () => {
setSetting( 'aSetting', 42 );
expect( getSetting( 'aSetting' ) ).toBe( 42 );
} );
it( 'should replace existing value', () => {
setSetting( 'adminUrl', 'not original' );
expect( getSetting( 'adminUrl' ) ).toBe( 'not original' );
} );
it( 'should save the value run through the provided filter', () => {
setSetting( 'aSetting', 'who', () => 42 );
expect( getSetting( 'aSetting' ) ).toBe( 42 );
} );
} );