Skip to content

Commit

Permalink
Lodash: Refactor away from _.clamp() (#41735)
Browse files Browse the repository at this point in the history
* Lodash: Refactor away from _.clamp()

* Remove default arg values
  • Loading branch information
tyxla authored Jun 16, 2022
1 parent d35f8b0 commit ec1cfe9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = {
{
name: 'lodash',
importNames: [
'clamp',
'concat',
'defaultTo',
'differenceWith',
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/focal-point-picker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { Animated, PanResponder, View } from 'react-native';
import Video from 'react-native-video';
import { clamp } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -24,6 +23,7 @@ import FocalPoint from './focal-point';
import Tooltip from './tooltip';
import styles from './style.scss';
import { isVideoType } from './utils';
import { clamp } from '../utils/math';

const MIN_POSITION_VALUE = 0;
const MAX_POSITION_VALUE = 100;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/range-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { clamp } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -20,6 +19,7 @@ import Button from '../button';
import Icon from '../icon';
import { COLORS } from '../utils';
import { floatClamp, useControlledRangeValue } from './utils';
import { clamp } from '../utils/math';
import InputRange from './input-range';
import RangeRail from './rail';
import SimpleTooltip from './tooltip';
Expand Down
5 changes: 1 addition & 4 deletions packages/components/src/range-control/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
import { clamp } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -13,6 +9,7 @@ import { useCallback, useRef, useEffect, useState } from '@wordpress/element';
* Internal dependencies
*/
import { useControlledState } from '../utils/hooks';
import { clamp } from '../utils/math';

const noop = () => {};

Expand Down
19 changes: 14 additions & 5 deletions packages/components/src/utils/math.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { clamp } from 'lodash';

/**
* Parses and retrieves a number value.
*
Expand Down Expand Up @@ -61,6 +56,20 @@ function getPrecision( value ) {
return split[ 1 ] !== undefined ? split[ 1 ].length : 0;
}

/**
* Clamps a value based on a min/max range.
*
* @param {number} value The value.
* @param {number} min The minimum range.
* @param {number} max The maximum range.
*
* @return {number} The clamped value.
*/
export function clamp( value, min, max ) {
const baseValue = getNumber( value );
return Math.max( min, Math.min( baseValue, max ) );
}

/**
* Clamps a value based on a min/max range with rounding
*
Expand Down
23 changes: 22 additions & 1 deletion packages/components/src/utils/test/math.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { add, subtract, roundClamp } from '../math';
import { add, clamp, subtract, roundClamp } from '../math';

describe( 'add', () => {
it( 'should add string and number values', () => {
Expand Down Expand Up @@ -37,6 +37,27 @@ describe( 'subtract', () => {
} );
} );

describe( 'clamp', () => {
it( 'should clamp a value between min and max', () => {
expect( clamp( 10, 1, 10 ) ).toBe( 10 );
expect( clamp( 1, 1, 10 ) ).toBe( 1 );
expect( clamp( 0, 1, 10 ) ).toBe( 1 );

expect( clamp( 50, 1, 10 ) ).toBe( 10 );
expect( clamp( 50, -10, 10 ) ).toBe( 10 );
expect( clamp( -50, -10, 10 ) ).toBe( -10 );

expect( clamp( Infinity, -10, 10 ) ).toBe( 10 );
expect( clamp( -Infinity, -10, 10 ) ).toBe( -10 );
} );

it( 'should clamp number or string values', () => {
expect( clamp( '50', 1, 10 ) ).toBe( 10 );
expect( clamp( '50', -10, 10 ) ).toBe( 10 );
expect( clamp( -50, -10, '10' ) ).toBe( -10 );
} );
} );

describe( 'roundClamp', () => {
it( 'should clamp a value between min and max', () => {
expect( roundClamp( 10, 1, 10 ) ).toBe( 10 );
Expand Down

0 comments on commit ec1cfe9

Please sign in to comment.