UNPKG

validierung

Version:

Vue composition function for form validation

235 lines (224 loc) 5.14 kB
'use strict'; var vueDemi = require('vue-demi'); const isDefined = (value) => value !== null && value !== void 0; const isRecord = (value) => isObject(value) && !Array.isArray(value); const isArray = (value) => Array.isArray(value); const isObject = (value) => typeof value === "object" && value !== null; const isPromise = (value) => ( // @ts-expect-error typeof (value == null ? void 0 : value.then) === "function" ); function deepCopyImpl(toCopy, copy) { for (const [key, value] of Object.entries(toCopy)) { if (isObject(value) && !(value instanceof File) && !(value instanceof FileList) && !(value instanceof Date) && !(value instanceof Map) && !(value instanceof Set)) { copy[key] = isArray(value) ? [] : {}; deepCopyImpl(value, copy[key]); } else { copy[key] = value; } } } function deepCopy(toCopy) { if (!isObject(toCopy)) { return toCopy; } const copy = isArray(toCopy) ? [] : {}; deepCopyImpl(toCopy, copy); return copy; } function path(path2, obj) { let value = obj[path2[0]]; for (let i = 0; i < path2.length; i++) { const key = path2[i]; if (value === null || value === void 0) { return void 0; } if (i > 0) { value = value[key]; } } return value; } function set(obj, keys, value) { if (keys.length === 0) { return; } for (let i = 0; i < keys.length - 1; i++) { const key = keys[i]; const nextKey = keys[i + 1]; const value2 = obj[key]; if (value2 === void 0) { if (typeof nextKey === "symbol" || Number.isNaN(+nextKey)) { obj[key] = {}; } else { obj[key] = []; } } obj = obj[key]; } if (vueDemi.isVue3) { obj[keys[keys.length - 1]] = value; } else { vueDemi.set(obj, keys[keys.length - 1], value); } } let id = 1; function uid() { return id++; } function debounce(target, { wait }) { let timerId = null; function cancel() { clearTimeout(timerId); timerId = null; } function debounced(...args) { const effect = () => { timerId = null; target.apply(this, args); }; clearTimeout(timerId); timerId = setTimeout(effect, wait); } debounced.cancel = cancel; return debounced; } class LinkedListNode { constructor(value) { this.next = null; this.prev = null; this.value = value; } } class LinkedList { constructor() { this.first = null; this.last = null; this.count = 0; } addFirst(value) { const node = new LinkedListNode(value); if (this.count === 0) { this.first = node; this.last = node; } else { node.next = this.first; this.first.prev = node; this.first = node; } this.count++; return node; } addLast(value) { const node = new LinkedListNode(value); if (this.count === 0) { this.first = node; this.last = node; } else { node.prev = this.last; this.last.next = node; this.last = node; } this.count++; return node; } remove(node) { if (this.count === 0) { return; } if (node === this.first) { this.removeFirst(); } else if (node === this.last) { this.removeLast(); } else { node.prev.next = node.next; node.next.prev = node.prev; node.next = null; node.prev = null; this.count--; } } removeFirst() { if (this.count === 0) { return; } if (this.count === 1) { this.first = null; this.last = null; this.count--; } else { this.first = this.first.next; this.first.prev.next = null; this.first.prev = null; this.count--; } } removeLast() { if (this.count === 0) { return; } if (this.count === 1) { this.first = null; this.last = null; this.count--; } else { this.last = this.last.prev; this.last.next.prev = null; this.last.next = null; this.count--; } } *[Symbol.iterator]() { for (let node = this.first; node !== null; node = node.next) { yield node; } } } class PromiseCancel { constructor() { this.isRacing = false; this.assign(); } cancelResolve(value) { if (this.isRacing) { this.isRacing = false; this.resolve(value); this.assign(); } } cancelReject(reason) { if (this.isRacing) { this.isRacing = false; this.reject(reason); this.assign(); } } async race(...promises) { this.isRacing = true; let result; try { result = await Promise.race([this.promise, ...promises]); } finally { this.isRacing = false; } return result; } assign() { this.promise = new Promise((resolve, reject) => { this.resolve = resolve; this.reject = reject; }); } } exports.LinkedList = LinkedList; exports.PromiseCancel = PromiseCancel; exports.debounce = debounce; exports.deepCopy = deepCopy; exports.isArray = isArray; exports.isDefined = isDefined; exports.isObject = isObject; exports.isPromise = isPromise; exports.isRecord = isRecord; exports.path = path; exports.set = set; exports.uid = uid;