Skip to content

Commit

Permalink
Don't default to fallback in getSetting if value is falsy (woocommerc…
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu authored and grogou committed Aug 20, 2021
1 parent f95f12d commit 0464883
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions assets/js/settings/shared/test/get-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe( 'getSetting', () => {
it( 'returns expected value for existing setting', () => {
expect( getSetting( 'adminUrl', 'not this' ) ).toEqual( ADMIN_URL );
} );
it( "returns expected value for existing setting even if it's a falsy value", () => {
expect( getSetting( 'currentUserIsAdmin', true ) ).toBe( false );
} );
it( 'filters value via provided filter callback', () => {
expect( getSetting( 'some value', 'default', () => 42 ) ).toBe( 42 );
} );
Expand Down
8 changes: 5 additions & 3 deletions assets/js/settings/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { allSettings } from './settings-init';
/**
* Retrieves a setting value from the setting state.
*
* If a setting with key `name` does not exist, the `fallback` will be returned instead. An optional `filter` callback
* can be passed to format the returned value.
* If a setting with key `name` does not exist or is undefined,
* the `fallback` will be returned instead. An optional `filter`
* callback can be passed to format the returned value.
*/
export const getSetting = (
name: string,
fallback: unknown = false,
filter = ( val: unknown, fb: unknown ) => val || fb
filter = ( val: unknown, fb: unknown ) =>
typeof val !== 'undefined' ? val : fb
): unknown => {
const value = name in allSettings ? allSettings[ name ] : fallback;
return filter( value, fallback );
Expand Down

0 comments on commit 0464883

Please sign in to comment.