chore: rename range to clamp (#8903)

* chore: rename range to clamp

* chore: merge upstream
This commit is contained in:
neverland 2021-06-21 16:20:03 +08:00 committed by GitHub
parent 8fb8055243
commit e0bfb44d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 35 deletions

View File

@ -11,7 +11,7 @@ import {
// Utils // Utils
import { import {
pick, pick,
range, clamp,
extend, extend,
isDate, isDate,
padZero, padZero,
@ -63,7 +63,7 @@ export default defineComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const formatValue = (value?: Date) => { const formatValue = (value?: Date) => {
if (isDate(value)) { if (isDate(value)) {
const timestamp = range( const timestamp = clamp(
value.getTime(), value.getTime(),
props.minDate.getTime(), props.minDate.getTime(),
props.maxDate.getTime() props.maxDate.getTime()

View File

@ -10,7 +10,7 @@ import {
// Utils // Utils
import { import {
pick, pick,
range, clamp,
extend, extend,
padZero, padZero,
createNamespace, createNamespace,
@ -60,8 +60,8 @@ export default defineComponent({
} }
let [hour, minute] = value.split(':'); let [hour, minute] = value.split(':');
hour = padZero(range(+hour, +minHour, +maxHour)); hour = padZero(clamp(+hour, +minHour, +maxHour));
minute = padZero(range(+minute, +minMinute, +maxMinute)); minute = padZero(clamp(+minute, +minMinute, +maxMinute));
return `${hour}:${minute}`; return `${hour}:${minute}`;
}; };

View File

@ -1,7 +1,7 @@
import { watch, computed, reactive, CSSProperties, defineComponent } from 'vue'; import { watch, computed, reactive, CSSProperties, defineComponent } from 'vue';
// Utils // Utils
import { range, preventDefault, createNamespace } from '../utils'; import { clamp, preventDefault, createNamespace } from '../utils';
// Composables // Composables
import { useTouch } from '../composables/use-touch'; import { useTouch } from '../composables/use-touch';
@ -107,7 +107,7 @@ export default defineComponent({
}); });
const setScale = (scale: number) => { const setScale = (scale: number) => {
scale = range(scale, +props.minZoom, +props.maxZoom); scale = clamp(scale, +props.minZoom, +props.maxZoom);
if (scale !== state.scale) { if (scale !== state.scale) {
state.scale = scale; state.scale = scale;
@ -171,8 +171,8 @@ export default defineComponent({
const { deltaX, deltaY } = touch; const { deltaX, deltaY } = touch;
const moveX = deltaX.value + startMoveX; const moveX = deltaX.value + startMoveX;
const moveY = deltaY.value + startMoveY; const moveY = deltaY.value + startMoveY;
state.moveX = range(moveX, -maxMoveX.value, maxMoveX.value); state.moveX = clamp(moveX, -maxMoveX.value, maxMoveX.value);
state.moveY = range(moveY, -maxMoveY.value, maxMoveY.value); state.moveY = clamp(moveY, -maxMoveY.value, maxMoveY.value);
} }
if (state.zooming && touches.length === 2) { if (state.zooming && touches.length === 2) {
@ -224,8 +224,8 @@ export default defineComponent({
if (!event.touches.length) { if (!event.touches.length) {
if (state.zooming) { if (state.zooming) {
state.moveX = range(state.moveX, -maxMoveX.value, maxMoveX.value); state.moveX = clamp(state.moveX, -maxMoveX.value, maxMoveX.value);
state.moveY = range(state.moveY, -maxMoveY.value, maxMoveY.value); state.moveY = clamp(state.moveY, -maxMoveY.value, maxMoveY.value);
state.zooming = false; state.zooming = false;
} }

View File

@ -4,7 +4,7 @@ import { ref, watch, reactive, PropType, defineComponent } from 'vue';
// Utils // Utils
import { deepClone } from '../utils/deep-clone'; import { deepClone } from '../utils/deep-clone';
import { import {
range, clamp,
isObject, isObject,
unknownProp, unknownProp,
preventDefault, preventDefault,
@ -119,7 +119,7 @@ export default defineComponent({
(props.itemHeight * (+props.visibleItemCount - 1)) / 2; (props.itemHeight * (+props.visibleItemCount - 1)) / 2;
const adjustIndex = (index: number) => { const adjustIndex = (index: number) => {
index = range(index, 0, count()); index = clamp(index, 0, count());
for (let i = index; i < count(); i++) { for (let i = index; i < count(); i++) {
if (!isOptionDisabled(state.options[i])) return i; if (!isOptionDisabled(state.options[i])) return i;
@ -178,7 +178,7 @@ export default defineComponent({
}; };
const getIndexByOffset = (offset: number) => const getIndexByOffset = (offset: number) =>
range(Math.round(-offset / props.itemHeight), 0, count() - 1); clamp(Math.round(-offset / props.itemHeight), 0, count() - 1);
const momentum = (distance: number, duration: number) => { const momentum = (distance: number, duration: number) => {
const speed = Math.abs(distance / duration); const speed = Math.abs(distance / duration);
@ -234,7 +234,7 @@ export default defineComponent({
preventDefault(event, true); preventDefault(event, true);
} }
state.offset = range( state.offset = clamp(
startOffset + touch.deltaY.value, startOffset + touch.deltaY.value,
-(count() * props.itemHeight), -(count() * props.itemHeight),
props.itemHeight props.itemHeight

View File

@ -2,7 +2,7 @@ import { ref, computed, PropType, CSSProperties, defineComponent } from 'vue';
// Utils // Utils
import { import {
range, clamp,
addUnit, addUnit,
addNumber, addNumber,
getSizeStyle, getSizeStyle,
@ -108,7 +108,7 @@ export default defineComponent({
const max = +props.max; const max = +props.max;
const step = +props.step; const step = +props.step;
value = range(value, min, max); value = clamp(value, min, max);
const diff = Math.round((value - min) / step) * step; const diff = Math.round((value - min) / step) * step;
return addNumber(min, diff); return addNumber(min, diff);
}; };

View File

@ -1,7 +1,7 @@
import { ref, Ref, reactive, computed, PropType, defineComponent } from 'vue'; import { ref, Ref, reactive, computed, PropType, defineComponent } from 'vue';
// Utils // Utils
import { range, isDef, createNamespace, preventDefault } from '../utils'; import { clamp, isDef, createNamespace, preventDefault } from '../utils';
import { callInterceptor, Interceptor } from '../utils/interceptor'; import { callInterceptor, Interceptor } from '../utils/interceptor';
// Composables // Composables
@ -117,7 +117,7 @@ export default defineComponent({
preventDefault(event, props.stopPropagation); preventDefault(event, props.stopPropagation);
} }
state.offset = range( state.offset = clamp(
deltaX.value + startOffset, deltaX.value + startOffset,
-rightWidth.value, -rightWidth.value,
leftWidth.value leftWidth.value
@ -154,14 +154,13 @@ export default defineComponent({
} }
}; };
const getClickHandler = (position: SwipeCellPosition, stop?: boolean) => ( const getClickHandler =
event: MouseEvent (position: SwipeCellPosition, stop?: boolean) => (event: MouseEvent) => {
) => { if (stop) {
if (stop) { event.stopPropagation();
event.stopPropagation(); }
} onClick(position);
onClick(position); };
};
const renderSideContent = ( const renderSideContent = (
side: SwipeCellSide, side: SwipeCellSide,

View File

@ -15,7 +15,7 @@ import {
// Utils // Utils
import { import {
range, clamp,
isHidden, isHidden,
truthProp, truthProp,
preventDefault, preventDefault,
@ -93,9 +93,8 @@ export default defineComponent({
const touch = useTouch(); const touch = useTouch();
const windowSize = useWindowSize(); const windowSize = useWindowSize();
const { children, linkChildren } = useChildren<ComponentInstance>( const { children, linkChildren } =
SWIPE_KEY useChildren<ComponentInstance>(SWIPE_KEY);
);
const count = computed(() => children.length); const count = computed(() => children.length);
@ -149,9 +148,9 @@ export default defineComponent({
if (pace) { if (pace) {
if (props.loop) { if (props.loop) {
return range(active + pace, -1, count.value); return clamp(active + pace, -1, count.value);
} }
return range(active + pace, 0, maxCount.value); return clamp(active + pace, 0, maxCount.value);
} }
return active; return active;
}; };
@ -164,7 +163,7 @@ export default defineComponent({
let targetOffset = offset - currentPosition; let targetOffset = offset - currentPosition;
if (!props.loop) { if (!props.loop) {
targetOffset = range(targetOffset, minOffset.value, 0); targetOffset = clamp(targetOffset, minOffset.value, 0);
} }
return targetOffset; return targetOffset;

View File

@ -1,4 +1,5 @@
export function range(num: number, min: number, max: number): number { /** clamps number within the inclusive lower and upper bounds */
export function clamp(num: number, min: number, max: number): number {
return Math.min(Math.max(num, min), max); return Math.min(Math.max(num, min), max);
} }