UNPKG

tweak-tools

Version:

Tweak your React projects until awesomeness

33 lines (32 loc) 1.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalize = exports.sanitize = exports.format = exports.schema = void 0; const v8n_1 = __importDefault(require("v8n")); const utils_1 = require("../../utils"); const vector_utils_1 = require("../Vector/vector-utils"); const number = (0, v8n_1.default)().number(); const schema = (o, s) => (0, v8n_1.default)().array().length(2).every.number().test(o) && (0, v8n_1.default)().schema({ min: number, max: number }).test(s); exports.schema = schema; const format = (v) => ({ min: v[0], max: v[1] }); exports.format = format; const sanitize = (value, { bounds: [MIN, MAX] }, prevValue) => { // value can be passed as an array externally const _value = Array.isArray(value) ? (0, exports.format)(value) : value; const _newValue = { min: prevValue[0], max: prevValue[1] }; const { min, max } = Object.assign(Object.assign({}, _newValue), _value); return [(0, utils_1.clamp)(Number(min), MIN, Math.max(MIN, max)), (0, utils_1.clamp)(Number(max), Math.min(MAX, min), MAX)]; }; exports.sanitize = sanitize; const normalize = ({ value, min, max }) => { const boundsSettings = { min, max }; const _settings = (0, vector_utils_1.normalizeKeyedNumberSettings)((0, exports.format)(value), { min: boundsSettings, max: boundsSettings }); const bounds = [min, max]; const settings = Object.assign(Object.assign({}, _settings), { bounds }); // sanitizing value to make sure it's withing interval bounds const _value = (0, exports.sanitize)((0, exports.format)(value), settings, value); return { value: _value, settings }; }; exports.normalize = normalize;