UNPKG

rvx

Version:

A signal based rendering library

67 lines (58 loc) 1.74 kB
/*! This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ import { $, watchUpdates, teardown, map } from './rvx.js'; function debounce(source, delay) { const input = $(source.value, source); watchUpdates(input, value => { if (!Object.is(source.value, value)) { const timeout = setTimeout(() => { source.value = value; }, delay); teardown(() => clearTimeout(timeout)); } }); watchUpdates(source, value => { input.value = value; }); return input; } function optionalString(input) { return map(input, value => { if (value === null || value === undefined) { return value; } return String(value); }); } function separated(input, separator = " ") { return map(input, v => { if (Array.isArray(v)) { return v.join(separator); } return v; }); } function string(input) { return map(input, String); } function trim(source) { const input = $(source.value, source); watchUpdates(input, value => { source.value = value.trim(); }); watchUpdates(source, value => { if (input.value.trim() !== value) { input.value = value; } }); return input; } export { debounce, optionalString, separated, string, trim };