Socket
Socket
Sign inDemoInstall

@react-stately/utils

Package Overview
Dependencies
5
Maintainers
2
Versions
684
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.9.1 to 3.10.0

dist/number.main.js

115

dist/main.js

@@ -1,2 +0,3 @@

var $kC0mY$react = require("react");
var $8d8fdfab47455712$exports = require("./useControlledState.main.js");
var $ac8e4d4816275668$exports = require("./number.main.js");

@@ -8,6 +9,6 @@

$parcel$export(module.exports, "useControlledState", () => $8d8fdfab47455712$export$40bfa8c7b0832715);
$parcel$export(module.exports, "clamp", () => $ac8e4d4816275668$export$7d15b64cf5a3a4c4);
$parcel$export(module.exports, "snapValueToStep", () => $ac8e4d4816275668$export$cb6e0bb50bc19463);
$parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$export$b6268554fba451f);
$parcel$export(module.exports, "useControlledState", () => $8d8fdfab47455712$exports.useControlledState);
$parcel$export(module.exports, "clamp", () => $ac8e4d4816275668$exports.clamp);
$parcel$export(module.exports, "snapValueToStep", () => $ac8e4d4816275668$exports.snapValueToStep);
$parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$exports.toFixedNumber);
/*

@@ -23,110 +24,6 @@ * Copyright 2020 Adobe. All rights reserved.

* governing permissions and limitations under the License.
*/ /*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange) {
let [stateValue, setStateValue] = (0, $kC0mY$react.useState)(value || defaultValue);
let isControlledRef = (0, $kC0mY$react.useRef)(value !== undefined);
let isControlled = value !== undefined;
(0, $kC0mY$react.useEffect)(()=>{
let wasControlled = isControlledRef.current;
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
isControlledRef.current = isControlled;
}, [
isControlled
]);
let currentValue = isControlled ? value : stateValue;
let setValue = (0, $kC0mY$react.useCallback)((value, ...args)=>{
let onChangeCaller = (value, ...onChangeArgs)=>{
if (onChange) {
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
}
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
// calling setState multiple times with the same value only emits onChange once.
// We do not use a ref for this because we specifically _do_ want the value to
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
// eslint-disable-next-line react-hooks/exhaustive-deps
currentValue = value;
};
if (typeof value === "function") {
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
// when someone using useControlledState calls setControlledState(myFunc)
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
let updateFunction = (oldValue, ...functionArgs)=>{
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
onChangeCaller(interceptedValue, ...args);
if (!isControlled) return interceptedValue;
return oldValue;
};
setStateValue(updateFunction);
} else {
if (!isControlled) setStateValue(value);
onChangeCaller(value, ...args);
}
}, [
isControlled,
currentValue,
onChange
]);
return [
currentValue,
setValue
];
}
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/ /**
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
*/ function $ac8e4d4816275668$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
let newValue = Math.min(Math.max(value, min), max);
return newValue;
}
function $ac8e4d4816275668$export$cb6e0bb50bc19463(value, min, max, step) {
min = Number(min);
max = Number(max);
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
if (!isNaN(min)) {
if (snappedValue < min) snappedValue = min;
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
// correct floating point behavior by rounding to step precision
let string = step.toString();
let index = string.indexOf(".");
let precision = index >= 0 ? string.length - index : 0;
if (precision > 0) {
let pow = Math.pow(10, precision);
snappedValue = Math.round(snappedValue * pow) / pow;
}
return snappedValue;
}
function $ac8e4d4816275668$export$b6268554fba451f(value, digits, base = 10) {
const pow = Math.pow(base, digits);
return Math.round(value * pow) / pow;
}
//# sourceMappingURL=main.js.map

@@ -1,2 +0,3 @@

import {useState as $6imuh$useState, useRef as $6imuh$useRef, useEffect as $6imuh$useEffect, useCallback as $6imuh$useCallback} from "react";
import {useControlledState as $458b0a5536c1a7cf$export$40bfa8c7b0832715} from "./useControlledState.module.js";
import {clamp as $9446cca9a3875146$export$7d15b64cf5a3a4c4, snapValueToStep as $9446cca9a3875146$export$cb6e0bb50bc19463, toFixedNumber as $9446cca9a3875146$export$b6268554fba451f} from "./number.module.js";

@@ -13,111 +14,7 @@ /*

* governing permissions and limitations under the License.
*/ /*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
let [stateValue, setStateValue] = (0, $6imuh$useState)(value || defaultValue);
let isControlledRef = (0, $6imuh$useRef)(value !== undefined);
let isControlled = value !== undefined;
(0, $6imuh$useEffect)(()=>{
let wasControlled = isControlledRef.current;
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
isControlledRef.current = isControlled;
}, [
isControlled
]);
let currentValue = isControlled ? value : stateValue;
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
let onChangeCaller = (value, ...onChangeArgs)=>{
if (onChange) {
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
}
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
// calling setState multiple times with the same value only emits onChange once.
// We do not use a ref for this because we specifically _do_ want the value to
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
// eslint-disable-next-line react-hooks/exhaustive-deps
currentValue = value;
};
if (typeof value === "function") {
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
// when someone using useControlledState calls setControlledState(myFunc)
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
let updateFunction = (oldValue, ...functionArgs)=>{
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
onChangeCaller(interceptedValue, ...args);
if (!isControlled) return interceptedValue;
return oldValue;
};
setStateValue(updateFunction);
} else {
if (!isControlled) setStateValue(value);
onChangeCaller(value, ...args);
}
}, [
isControlled,
currentValue,
onChange
]);
return [
currentValue,
setValue
];
}
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/ /**
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
*/ function $9446cca9a3875146$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
let newValue = Math.min(Math.max(value, min), max);
return newValue;
}
function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
min = Number(min);
max = Number(max);
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
if (!isNaN(min)) {
if (snappedValue < min) snappedValue = min;
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
// correct floating point behavior by rounding to step precision
let string = step.toString();
let index = string.indexOf(".");
let precision = index >= 0 ? string.length - index : 0;
if (precision > 0) {
let pow = Math.pow(10, precision);
snappedValue = Math.round(snappedValue * pow) / pow;
}
return snappedValue;
}
function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
const pow = Math.pow(base, digits);
return Math.round(value * pow) / pow;
}
export {$458b0a5536c1a7cf$export$40bfa8c7b0832715 as useControlledState, $9446cca9a3875146$export$7d15b64cf5a3a4c4 as clamp, $9446cca9a3875146$export$cb6e0bb50bc19463 as snapValueToStep, $9446cca9a3875146$export$b6268554fba451f as toFixedNumber};
//# sourceMappingURL=module.js.map
{
"name": "@react-stately/utils",
"version": "3.9.1",
"version": "3.10.0",
"description": "Spectrum UI components in React",

@@ -33,3 +33,3 @@ "license": "Apache-2.0",

},
"gitHead": "de9f84a22583fc741c29b341d14cd35ef4cca161"
"gitHead": "f645f29edc1322153fd60af4640cbcab1d992dbd"
}

@@ -21,2 +21,14 @@ /*

export function roundToStepPrecision(value: number, step: number) {
let roundedValue = value;
let stepString = step.toString();
let pointIndex = stepString.indexOf('.');
let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;
if (precision > 0) {
let pow = Math.pow(10, precision);
roundedValue = Math.round(roundedValue * pow) / pow;
}
return roundedValue;
}
export function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {

@@ -26,5 +38,5 @@ min = Number(min);

let remainder = ((value - (isNaN(min) ? 0 : min)) % step);
let snappedValue = Math.abs(remainder) * 2 >= step
let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step
? value + Math.sign(remainder) * (step - Math.abs(remainder))
: value - remainder;
: value - remainder, step);

@@ -35,18 +47,11 @@ if (!isNaN(min)) {

} else if (!isNaN(max) && snappedValue > max) {
snappedValue = min + Math.floor((max - min) / step) * step;
snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;
}
} else if (!isNaN(max) && snappedValue > max) {
snappedValue = Math.floor(max / step) * step;
snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step;
}
// correct floating point behavior by rounding to step precision
let string = step.toString();
let index = string.indexOf('.');
let precision = index >= 0 ? string.length - index : 0;
snappedValue = roundToStepPrecision(snappedValue, step);
if (precision > 0) {
let pow = Math.pow(10, precision);
snappedValue = Math.round(snappedValue * pow) / pow;
}
return snappedValue;

@@ -53,0 +58,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc