@felte/common
Advanced tools
Comparing version 0.6.0 to 1.0.0-next.0
@@ -1,2 +0,22 @@ | ||
export{_some}from"./utils/some.js";export{_mapValues}from"./utils/mapValues.js";export{isFieldValue}from"./utils/isFieldValue.js";export{_cloneDeep}from"./utils/cloneDeep.js";export{_isPlainObject}from"./utils/isPlainObject.js";export{_mergeWith}from"./utils/mergeWith.js";export{_defaultsDeep}from"./utils/defaultsDeep.js";export{_merge}from"./utils/merge.js";export{_get}from"./utils/get.js";export{_set}from"./utils/set.js";export{_unset}from"./utils/unset.js";export{_update}from"./utils/update.js";export{deepSet}from"./utils/deepSet.js";export{deepSome}from"./utils/deepSome.js";export{getIndex}from"./utils/getIndex.js";export{isElement,isFieldSetElement,isFormControl,isInputElement,isSelectElement,isTextAreaElement}from"./utils/typeGuards.js";export{getPath}from"./utils/getPath.js";export{getPathFromDataset}from"./utils/getPathFromDataset.js";export{shouldIgnore}from"./utils/shouldIgnore.js";export{addAttrsFromFieldset,executeTransforms,executeValidation,getFormControls,getFormDefaultValues,getInputTextOrNumber,setControlValue,setForm}from"./utils/domUtils.js"; | ||
export { _some } from './utils/some.js'; | ||
export { _mapValues } from './utils/mapValues.js'; | ||
export { isFieldValue } from './utils/isFieldValue.js'; | ||
export { _cloneDeep } from './utils/cloneDeep.js'; | ||
export { _isPlainObject } from './utils/isPlainObject.js'; | ||
export { _mergeWith } from './utils/mergeWith.js'; | ||
export { _defaultsDeep } from './utils/defaultsDeep.js'; | ||
export { _merge } from './utils/merge.js'; | ||
export { _get } from './utils/get.js'; | ||
export { _set } from './utils/set.js'; | ||
export { _unset } from './utils/unset.js'; | ||
export { _update } from './utils/update.js'; | ||
export { deepSet } from './utils/deepSet.js'; | ||
export { deepSome } from './utils/deepSome.js'; | ||
export { getIndex } from './utils/getIndex.js'; | ||
export { isElement, isFieldSetElement, isFormControl, isInputElement, isSelectElement, isTextAreaElement } from './utils/typeGuards.js'; | ||
export { getPath } from './utils/getPath.js'; | ||
export { getPathFromDataset } from './utils/getPathFromDataset.js'; | ||
export { shouldIgnore } from './utils/shouldIgnore.js'; | ||
export { getValue } from './utils/getValue.js'; | ||
export { addAttrsFromFieldset, executeTransforms, executeValidation, getFormControls, getFormDefaultValues, getInputTextOrNumber, setControlValue, setForm } from './utils/domUtils.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,13 @@ | ||
import{_isPlainObject as r}from"./isPlainObject.js";function e(s){return Object.keys(s||{}).reduce(((t,i)=>Object.assign(Object.assign({},t),{[i]:r(s[i])?e(s[i]):Array.isArray(s[i])?[...s[i]]:s[i]})),{})}export{e as _cloneDeep}; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
/** @ignore */ | ||
function _cloneDeep(obj) { | ||
return Object.keys(obj || {}).reduce((res, key) => (Object.assign(Object.assign({}, res), { [key]: _isPlainObject(obj[key]) | ||
? _cloneDeep(obj[key]) | ||
: Array.isArray(obj[key]) | ||
? [...obj[key]] | ||
: obj[key] })), {}); | ||
} | ||
export { _cloneDeep }; | ||
//# sourceMappingURL=cloneDeep.js.map |
@@ -1,2 +0,23 @@ | ||
import{_mapValues as r}from"./mapValues.js";import{_isPlainObject as n}from"./isPlainObject.js";function t(i,o){return r(i,(r=>n(r)?t(r,o):Array.isArray(r)?r.map(function(r){return function(i){return n(i)?t(i,r):r}}(o)):o))}export{t as deepSet}; | ||
import { _mapValues } from './mapValues.js'; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
function handleArray(value) { | ||
return function (propVal) { | ||
if (_isPlainObject(propVal)) | ||
return deepSet(propVal, value); | ||
return value; | ||
}; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function deepSet(obj, value) { | ||
return _mapValues(obj, (prop) => _isPlainObject(prop) | ||
? deepSet(prop, value) | ||
: Array.isArray(prop) | ||
? prop.map(handleArray(value)) | ||
: value); | ||
} | ||
export { deepSet }; | ||
//# sourceMappingURL=deepSet.js.map |
@@ -1,2 +0,12 @@ | ||
import{_some as o}from"./some.js";import{_isPlainObject as r}from"./isPlainObject.js";function t(i,m){return o(i,(o=>r(o)?t(o,m):m(o)))}export{t as deepSome}; | ||
import { _some } from './some.js'; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
/** | ||
* @category Helper | ||
*/ | ||
function deepSome(obj, pred) { | ||
return _some(obj, (value) => _isPlainObject(value) ? deepSome(value, pred) : pred(value)); | ||
} | ||
export { deepSome }; | ||
//# sourceMappingURL=deepSome.js.map |
@@ -1,2 +0,22 @@ | ||
import{_isPlainObject as r}from"./isPlainObject.js";import{_mergeWith as i}from"./mergeWith.js";function t(i,t){if(!r(i)||!r(t)){if(Array.isArray(t)){if(t.some(r))return;const n=Array.isArray(i)?i:[];return t.map(((r,i)=>{var t;return null!==(t=n[i])&&void 0!==t?t:r}))}return void 0!==i?i:void 0}}function n(...r){return i(...r,t)}export{n as _defaultsDeep}; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
import { _mergeWith } from './mergeWith.js'; | ||
function defaultsCustomizer(objValue, srcValue) { | ||
if (_isPlainObject(objValue) && _isPlainObject(srcValue)) | ||
return; | ||
if (Array.isArray(srcValue)) { | ||
if (srcValue.some(_isPlainObject)) | ||
return; | ||
const objArray = Array.isArray(objValue) ? objValue : []; | ||
return srcValue.map((value, index) => { var _a; return (_a = objArray[index]) !== null && _a !== void 0 ? _a : value; }); | ||
} | ||
if (typeof objValue !== 'undefined') | ||
return objValue; | ||
} | ||
/** @ignore */ | ||
function _defaultsDeep(...args) { | ||
return _mergeWith(...args, defaultsCustomizer); | ||
} | ||
export { _defaultsDeep }; | ||
//# sourceMappingURL=defaultsDeep.js.map |
@@ -1,2 +0,202 @@ | ||
import{isFormControl as e,isFieldSetElement as t,isInputElement as r}from"./typeGuards.js";import{_mergeWith as n}from"./mergeWith.js";import{_isPlainObject as i}from"./isPlainObject.js";import{_get as o}from"./get.js";import{_set as f}from"./set.js";import{_update as a}from"./update.js";import{getPath as u}from"./getPath.js";import{getIndex as c}from"./getIndex.js";function l(r){if(e(r))return[r];if(0===r.childElementCount)return[];const n=new Set;for(const i of r.children){if(e(i)&&n.add(i),t(i))for(const t of i.elements)e(t)&&n.add(t);i.childElementCount>0&&l(i).forEach((e=>n.add(e)))}return Array.from(n)}function s(r){for(const n of r.elements)if(e(n)||t(n)){if(r.name&&n.name){const e=c(r),t=void 0===e?r.name:`${r.name}[${e}]`;n.dataset.felteFieldset=r.dataset.felteFieldset?`${r.dataset.felteFieldset}.${t}`:t}"true"!==r.dataset.felteUnsetOnRemove||n.hasAttribute("data-felte-unset-on-remove")||(n.dataset.felteUnsetOnRemove="true")}}function d(e){return e.type.match(/^(number|range)$/)?e.value?+e.value:void 0:e.value}function m(n){var i;let l={};for(const m of n.elements){if(t(m)&&s(m),!e(m)||!m.name)continue;const y=u(m),v=c(m);if(r(m)){if("checkbox"===m.type){if(void 0===o(l,y)){if(1===Array.from(n.querySelectorAll(`[name="${m.name}"]`)).filter((t=>{if(!e(t))return!1;if(void 0!==v){return Number(t.dataset.felteIndex)===v}return y===u(t)})).length){l=f(l,y,m.checked);continue}l=f(l,y,m.checked?[m.value]:[]);continue}Array.isArray(o(l,y))&&m.checked&&a(l,y,(e=>(void 0===v||Array.isArray(e)||(e=[]),[...e,m.value])));continue}if("radio"===m.type){if(o(l,y))continue;l=f(l,y,m.checked?m.value:void 0);continue}if("file"===m.type){l=f(l,y,m.multiple?Array.from(m.files||[]):null===(i=m.files)||void 0===i?void 0:i[0]);continue}}const p=d(m);l=f(l,y,p)}return{defaultData:l}}function y(t,n){if(!e(t))return;const i=n;if(r(t)){if("checkbox"===t.type){const e=i;return void 0===e||"boolean"==typeof e?void(t.checked=!!e):void(Array.isArray(e)&&(e.includes(t.value)?t.checked=!0:t.checked=!1))}if("radio"===t.type){const e=i;return void(t.value===e?t.checked=!0:t.checked=!1)}if("file"===t.type)return t.files=null,void(t.value="")}t.value=String(null!=i?i:"")}function v(r,n){for(const i of r.elements){if(t(i)&&s(i),!e(i)||!i.name)continue;const r=u(i);y(i,o(n,r))}}function p(e,t){if(!i(e)&&!i(t)){if(null===e)return t;if(null===t)return e;if(e&&t)return Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),[...e,...t]}}async function h(e,t){if(!t)return;if(!Array.isArray(t))return t(e);const r=await Promise.all(t.map((t=>t(e))));return n(...r,p)}function A(e,t){return t?Array.isArray(t)?t.reduce(((e,t)=>t(e)),e):t(e):e}export{s as addAttrsFromFieldset,A as executeTransforms,h as executeValidation,l as getFormControls,m as getFormDefaultValues,d as getInputTextOrNumber,y as setControlValue,v as setForm}; | ||
import { isFormControl, isFieldSetElement, isInputElement } from './typeGuards.js'; | ||
import { _mergeWith } from './mergeWith.js'; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
import { _get } from './get.js'; | ||
import { _set } from './set.js'; | ||
import { _update } from './update.js'; | ||
import { getPath } from './getPath.js'; | ||
import { getIndex } from './getIndex.js'; | ||
/** | ||
* @ignore | ||
*/ | ||
function getFormControls(el) { | ||
if (isFormControl(el)) | ||
return [el]; | ||
if (el.childElementCount === 0) | ||
return []; | ||
const foundControls = new Set(); | ||
for (const child of el.children) { | ||
if (isFormControl(child)) | ||
foundControls.add(child); | ||
if (isFieldSetElement(child)) { | ||
for (const fieldsetChild of child.elements) { | ||
if (isFormControl(fieldsetChild)) | ||
foundControls.add(fieldsetChild); | ||
} | ||
} | ||
if (child.childElementCount > 0) | ||
getFormControls(child).forEach((value) => foundControls.add(value)); | ||
} | ||
return Array.from(foundControls); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function addAttrsFromFieldset(fieldSet) { | ||
for (const element of fieldSet.elements) { | ||
if (!isFormControl(element) && !isFieldSetElement(element)) | ||
continue; | ||
if (fieldSet.name && element.name) { | ||
const index = getIndex(fieldSet); | ||
const fieldsetName = typeof index === 'undefined' | ||
? fieldSet.name | ||
: `${fieldSet.name}[${index}]`; | ||
element.dataset.felteFieldset = fieldSet.dataset.felteFieldset | ||
? `${fieldSet.dataset.felteFieldset}.${fieldsetName}` | ||
: fieldsetName; | ||
} | ||
if (fieldSet.hasAttribute('data-felte-keep-on-remove') && | ||
!element.hasAttribute('data-felte-keep-on-remove')) { | ||
element.dataset.felteKeepOnRemove = fieldSet.dataset.felteKeepOnRemove; | ||
} | ||
} | ||
} | ||
/** @ignore */ | ||
function getInputTextOrNumber(el) { | ||
if (el.type.match(/^(number|range)$/)) { | ||
return !el.value ? undefined : +el.value; | ||
} | ||
else { | ||
return el.value; | ||
} | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function getFormDefaultValues(node) { | ||
var _a; | ||
let defaultData = {}; | ||
for (const el of node.elements) { | ||
if (isFieldSetElement(el)) | ||
addAttrsFromFieldset(el); | ||
if (!isFormControl(el) || !el.name) | ||
continue; | ||
const elName = getPath(el); | ||
const index = getIndex(el); | ||
if (isInputElement(el)) { | ||
if (el.type === 'checkbox') { | ||
if (typeof _get(defaultData, elName) === 'undefined') { | ||
const checkboxes = Array.from(node.querySelectorAll(`[name="${el.name}"]`)).filter((checkbox) => { | ||
if (!isFormControl(checkbox)) | ||
return false; | ||
if (typeof index !== 'undefined') { | ||
const felteIndex = Number(checkbox.dataset.felteIndex); | ||
return felteIndex === index; | ||
} | ||
return elName === getPath(checkbox); | ||
}); | ||
if (checkboxes.length === 1) { | ||
defaultData = _set(defaultData, elName, el.checked); | ||
continue; | ||
} | ||
defaultData = _set(defaultData, elName, el.checked ? [el.value] : []); | ||
continue; | ||
} | ||
if (Array.isArray(_get(defaultData, elName)) && el.checked) { | ||
_update(defaultData, elName, (value) => { | ||
if (typeof index !== 'undefined' && !Array.isArray(value)) | ||
value = []; | ||
return [...value, el.value]; | ||
}); | ||
} | ||
continue; | ||
} | ||
if (el.type === 'radio') { | ||
if (_get(defaultData, elName)) | ||
continue; | ||
defaultData = _set(defaultData, elName, el.checked ? el.value : undefined); | ||
continue; | ||
} | ||
if (el.type === 'file') { | ||
defaultData = _set(defaultData, elName, el.multiple ? Array.from(el.files || []) : (_a = el.files) === null || _a === void 0 ? void 0 : _a[0]); | ||
continue; | ||
} | ||
} | ||
const inputValue = getInputTextOrNumber(el); | ||
defaultData = _set(defaultData, elName, inputValue); | ||
} | ||
return { defaultData }; | ||
} | ||
function setControlValue(el, value) { | ||
if (!isFormControl(el)) | ||
return; | ||
const fieldValue = value; | ||
if (isInputElement(el)) { | ||
if (el.type === 'checkbox') { | ||
const checkboxesDefaultData = fieldValue; | ||
if (typeof checkboxesDefaultData === 'undefined' || | ||
typeof checkboxesDefaultData === 'boolean') { | ||
el.checked = !!checkboxesDefaultData; | ||
return; | ||
} | ||
if (Array.isArray(checkboxesDefaultData)) { | ||
if (checkboxesDefaultData.includes(el.value)) { | ||
el.checked = true; | ||
} | ||
else { | ||
el.checked = false; | ||
} | ||
} | ||
return; | ||
} | ||
if (el.type === 'radio') { | ||
const radioValue = fieldValue; | ||
if (el.value === radioValue) | ||
el.checked = true; | ||
else | ||
el.checked = false; | ||
return; | ||
} | ||
if (el.type === 'file') { | ||
el.files = null; | ||
el.value = ''; | ||
return; | ||
} | ||
} | ||
el.value = String(fieldValue !== null && fieldValue !== void 0 ? fieldValue : ''); | ||
} | ||
/** Sets the form inputs value to match the data object provided. */ | ||
function setForm(node, data) { | ||
for (const el of node.elements) { | ||
if (isFieldSetElement(el)) | ||
addAttrsFromFieldset(el); | ||
if (!isFormControl(el) || !el.name) | ||
continue; | ||
const elName = getPath(el); | ||
setControlValue(el, _get(data, elName)); | ||
} | ||
} | ||
function executeCustomizer(objValue, srcValue) { | ||
if (_isPlainObject(objValue) || _isPlainObject(srcValue)) | ||
return; | ||
if (objValue === null) | ||
return srcValue; | ||
if (srcValue === null) | ||
return objValue; | ||
if (!objValue || !srcValue) | ||
return; | ||
if (!Array.isArray(objValue)) | ||
objValue = [objValue]; | ||
if (!Array.isArray(srcValue)) | ||
srcValue = [srcValue]; | ||
return [...objValue, ...srcValue]; | ||
} | ||
async function executeValidation(values, validations) { | ||
if (!validations) | ||
return; | ||
if (!Array.isArray(validations)) | ||
return validations(values); | ||
const errorArray = await Promise.all(validations.map((v) => v(values))); | ||
return _mergeWith(...errorArray, executeCustomizer); | ||
} | ||
function executeTransforms(values, transforms) { | ||
if (!transforms) | ||
return values; | ||
if (!Array.isArray(transforms)) | ||
return transforms(values); | ||
return transforms.reduce((res, t) => t(res), values); | ||
} | ||
export { addAttrsFromFieldset, executeTransforms, executeValidation, getFormControls, getFormDefaultValues, getInputTextOrNumber, setControlValue, setForm }; | ||
//# sourceMappingURL=domUtils.js.map |
@@ -1,2 +0,13 @@ | ||
function t(t,o,e){const l=e=>String.prototype.split.call(o,e).filter(Boolean).reduce(((t,o)=>null!=t?t[o]:t),t),n=l(/[,[\]]+?/)||l(/[,[\].]+?/);return void 0===n||n===t?e:n}export{t as _get}; | ||
/* From: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get */ | ||
/** @ignore */ | ||
function _get(obj, path, defaultValue) { | ||
const travel = (regexp) => String.prototype.split | ||
.call(path, regexp) | ||
.filter(Boolean) | ||
.reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj); | ||
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); | ||
return result === undefined || result === obj ? defaultValue : result; | ||
} | ||
export { _get }; | ||
//# sourceMappingURL=get.js.map |
@@ -1,2 +0,11 @@ | ||
function e(e){return e.hasAttribute("data-felte-index")?Number(e.dataset.felteIndex):void 0}export{e as getIndex}; | ||
/** | ||
* @ignore | ||
*/ | ||
function getIndex(el) { | ||
return el.hasAttribute('data-felte-index') | ||
? Number(el.dataset.felteIndex) | ||
: undefined; | ||
} | ||
export { getIndex }; | ||
//# sourceMappingURL=getIndex.js.map |
@@ -1,2 +0,32 @@ | ||
import{isFormControl as e,isFieldSetElement as n}from"./typeGuards.js";import{getIndex as o}from"./getIndex.js";function t(t,r){const m=o(t);let a="";r?a=r:e(t)&&(a=t.name),a=void 0===m?a:`${a}[${m}]`;let d=t.parentNode;if(!d)return a;for(;d&&"FORM"!==d.nodeName;){if(n(d)&&d.name){const e=o(d);a=`${void 0===e?d.name:`${d.name}[${e}]`}.${a}`}d=d.parentNode}return a}export{t as getPath}; | ||
import { isFormControl, isFieldSetElement } from './typeGuards.js'; | ||
import { getIndex } from './getIndex.js'; | ||
/** | ||
* @category Helper | ||
*/ | ||
function getPath(el, name) { | ||
const index = getIndex(el); | ||
let path = ''; | ||
if (name) { | ||
path = name; | ||
} | ||
else if (isFormControl(el)) { | ||
path = el.name; | ||
} | ||
path = typeof index === 'undefined' ? path : `${path}[${index}]`; | ||
let parent = el.parentNode; | ||
if (!parent) | ||
return path; | ||
while (parent && parent.nodeName !== 'FORM') { | ||
if (isFieldSetElement(parent) && parent.name) { | ||
const index = getIndex(parent); | ||
const fieldsetName = typeof index === 'undefined' ? parent.name : `${parent.name}[${index}]`; | ||
path = `${fieldsetName}.${path}`; | ||
} | ||
parent = parent.parentNode; | ||
} | ||
return path; | ||
} | ||
export { getPath }; | ||
//# sourceMappingURL=getPath.js.map |
@@ -1,2 +0,11 @@ | ||
import{getIndex as e}from"./getIndex.js";function t(t){const n=t.dataset.felteFieldset,o=e(t),r=void 0===o?t.name:`${t.name}[${o}]`;return n?`${n}.${r}`:r}export{t as getPathFromDataset}; | ||
import { getIndex } from './getIndex.js'; | ||
function getPathFromDataset(el) { | ||
const fieldSetName = el.dataset.felteFieldset; | ||
const index = getIndex(el); | ||
const fieldName = typeof index === 'undefined' ? el.name : `${el.name}[${index}]`; | ||
return fieldSetName ? `${fieldSetName}.${fieldName}` : fieldName; | ||
} | ||
export { getPathFromDataset }; | ||
//# sourceMappingURL=getPathFromDataset.js.map |
@@ -1,2 +0,15 @@ | ||
function e(e){return Array.isArray(e)?0===e.length||e.some((e=>e instanceof File||"string"==typeof e)):"string"==typeof e||"number"==typeof e||"boolean"==typeof e||e instanceof File}export{e as isFieldValue}; | ||
/** @category Helper */ | ||
function isFieldValue(value) { | ||
if (Array.isArray(value)) { | ||
if (value.length === 0) | ||
return true; | ||
return value.some((v) => v instanceof File || typeof v === 'string'); | ||
} | ||
return (typeof value === 'string' || | ||
typeof value === 'number' || | ||
typeof value === 'boolean' || | ||
value instanceof File); | ||
} | ||
export { isFieldValue }; | ||
//# sourceMappingURL=isFieldValue.js.map |
@@ -1,2 +0,7 @@ | ||
function t(t){return"[object Object]"===Object.prototype.toString.call(t)}export{t as _isPlainObject}; | ||
/** @ignore */ | ||
function _isPlainObject(value) { | ||
return Object.prototype.toString.call(value) === '[object Object]'; | ||
} | ||
export { _isPlainObject }; | ||
//# sourceMappingURL=isPlainObject.js.map |
@@ -1,2 +0,8 @@ | ||
function e(e,t){return Object.keys(e).reduce(((c,n)=>Object.assign(Object.assign({},c),{[n]:t(e[n])})),{})}export{e as _mapValues}; | ||
/** @ignore */ | ||
function _mapValues(obj, updater) { | ||
const keys = Object.keys(obj); | ||
return keys.reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: updater(obj[key]) })), {}); | ||
} | ||
export { _mapValues }; | ||
//# sourceMappingURL=mapValues.js.map |
@@ -1,2 +0,9 @@ | ||
import{_mergeWith as r}from"./mergeWith.js";function t(...t){return r(...t,(()=>{}))}export{t as _merge}; | ||
import { _mergeWith } from './mergeWith.js'; | ||
/** @ignore */ | ||
function _merge(...args) { | ||
return _mergeWith(...args, () => undefined); | ||
} | ||
export { _merge }; | ||
//# sourceMappingURL=merge.js.map |
@@ -1,2 +0,44 @@ | ||
import{_cloneDeep as r}from"./cloneDeep.js";import{_isPlainObject as e}from"./isPlainObject.js";import{deepSet as o}from"./deepSet.js";function t(...i){const s=i.pop(),n=r(i.shift());if(0===i.length)return n;for(const f of i){if(!f)continue;const i=Object.keys(f);for(const c of i){const i=s(n[c],f[c]);if(void 0!==i)n[c]=i;else if(e(f[c])&&e(n[c]))n[c]=t(n[c],f[c],s);else if(Array.isArray(f[c]))n[c]=f[c].map(((r,o)=>{if(!e(r))return r;return t(Array.isArray(n[c])?n[c][o]:n[c],r,s)}));else if(e(f[c])){const e=o(r(f[c]),void 0);n[c]=t(e,f[c],s)}else void 0!==f[c]&&(n[c]=f[c])}}return n}export{t as _mergeWith}; | ||
import { _cloneDeep } from './cloneDeep.js'; | ||
import { _isPlainObject } from './isPlainObject.js'; | ||
import { deepSet } from './deepSet.js'; | ||
/** @ignore */ | ||
function _mergeWith(...args) { | ||
const customizer = args.pop(); | ||
const obj = _cloneDeep(args.shift()); | ||
if (args.length === 0) | ||
return obj; | ||
for (const source of args) { | ||
if (!source) | ||
continue; | ||
const keys = Object.keys(source); | ||
for (const key of keys) { | ||
const rsValue = customizer(obj[key], source[key]); | ||
if (typeof rsValue !== 'undefined') { | ||
obj[key] = rsValue; | ||
} | ||
else if (_isPlainObject(source[key]) && _isPlainObject(obj[key])) { | ||
obj[key] = _mergeWith(obj[key], source[key], customizer); | ||
} | ||
else if (Array.isArray(source[key])) { | ||
obj[key] = source[key].map((val, i) => { | ||
if (!_isPlainObject(val)) | ||
return val; | ||
const newObj = Array.isArray(obj[key]) ? obj[key][i] : obj[key]; | ||
return _mergeWith(newObj, val, customizer); | ||
}); | ||
} | ||
else if (_isPlainObject(source[key])) { | ||
const defaultObj = deepSet(_cloneDeep(source[key]), undefined); | ||
obj[key] = _mergeWith(defaultObj, source[key], customizer); | ||
} | ||
else if (typeof source[key] !== 'undefined') { | ||
obj[key] = source[key]; | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
export { _mergeWith }; | ||
//# sourceMappingURL=mergeWith.js.map |
@@ -1,2 +0,29 @@ | ||
import{_cloneDeep as e}from"./cloneDeep.js";function r(r,t,c){Object(r)!==r?r={}:void 0!==r&&(r=e(r));let o=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return o.slice(0,-1).reduce(((e,r,t)=>Object(e[r])===e[r]?e[r]:e[r]=Math.abs(Number(o[t+1]))>>0==+o[t+1]?[]:{}),r)[o[o.length-1]]=c,r}export{r as _set}; | ||
import { _cloneDeep } from './cloneDeep.js'; | ||
/* From: https://stackoverflow.com/a/54733755 */ | ||
/** @ignore */ | ||
function _set(obj, path, value) { | ||
if (Object(obj) !== obj) | ||
obj = {}; | ||
// When obj is not an object | ||
else if (typeof obj !== 'undefined') | ||
obj = _cloneDeep(obj); | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = !Array.isArray(path) | ||
? path.toString().match(/[^.[\]]+/g) || [] | ||
: path; | ||
newPath.slice(0, -1).reduce((a, c, i // Iterate all of them except the last one | ||
) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: // No: create the key. Is the next key a potential array-index? | ||
(a[c] = | ||
Math.abs(Number(newPath[i + 1])) >> 0 === +newPath[i + 1] | ||
? [] // Yes: assign a new array object | ||
: {}), // No: assign a new plain object | ||
obj)[newPath[newPath.length - 1]] = value; // Finally assign the value to the last key | ||
return obj; // Return the top-level object to allow chaining | ||
} | ||
export { _set }; | ||
//# sourceMappingURL=set.js.map |
@@ -1,2 +0,15 @@ | ||
function e(e){let t=e;for(;t&&"FORM"!==t.nodeName;){if(t.hasAttribute("data-felte-ignore"))return!0;t=t.parentElement}return!1}export{e as shouldIgnore}; | ||
/** | ||
* @category Helper | ||
*/ | ||
function shouldIgnore(el) { | ||
let parent = el; | ||
while (parent && parent.nodeName !== 'FORM') { | ||
if (parent.hasAttribute('data-felte-ignore')) | ||
return true; | ||
parent = parent.parentElement; | ||
} | ||
return false; | ||
} | ||
export { shouldIgnore }; | ||
//# sourceMappingURL=shouldIgnore.js.map |
@@ -1,2 +0,8 @@ | ||
function e(e,t){return Object.keys(e).some((n=>t(e[n])))}export{e as _some}; | ||
/** @ignore */ | ||
function _some(obj, pred) { | ||
const keys = Object.keys(obj); | ||
return keys.some((key) => pred(obj[key])); | ||
} | ||
export { _some }; | ||
//# sourceMappingURL=some.js.map |
@@ -1,2 +0,43 @@ | ||
function n(n){var o;return"INPUT"===(null===(o=n)||void 0===o?void 0:o.nodeName)}function o(n){var o;return"TEXTAREA"===(null===(o=n)||void 0===o?void 0:o.nodeName)}function e(n){var o;return"SELECT"===(null===(o=n)||void 0===o?void 0:o.nodeName)}function r(n){var o;return"FIELDSET"===(null===(o=n)||void 0===o?void 0:o.nodeName)}function u(r){return n(r)||o(r)||e(r)}function d(n){return n.nodeType===Node.ELEMENT_NODE}export{d as isElement,r as isFieldSetElement,u as isFormControl,n as isInputElement,e as isSelectElement,o as isTextAreaElement}; | ||
/** | ||
* @category Helper | ||
*/ | ||
function isInputElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'INPUT'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isTextAreaElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'TEXTAREA'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isSelectElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'SELECT'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isFieldSetElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'FIELDSET'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isFormControl(el) { | ||
return isInputElement(el) || isTextAreaElement(el) || isSelectElement(el); | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isElement(el) { | ||
return el.nodeType === Node.ELEMENT_NODE; | ||
} | ||
export { isElement, isFieldSetElement, isFormControl, isInputElement, isSelectElement, isTextAreaElement }; | ||
//# sourceMappingURL=typeGuards.js.map |
@@ -1,2 +0,22 @@ | ||
import{_cloneDeep as e}from"./cloneDeep.js";function r(r,t){var i;if(Object(r)!==r)return;void 0!==r&&(r=e(r));let o=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return null===(i=o.slice(0,-1).reduce(((e,r)=>Object(e[r])===e[r]?e[r]:void 0),r))||void 0===i||delete i[o[o.length-1]],r}export{r as _unset}; | ||
import { _cloneDeep } from './cloneDeep.js'; | ||
function _unset(obj, path) { | ||
var _a; | ||
if (Object(obj) !== obj) | ||
return; | ||
// When obj is not an object | ||
else if (typeof obj !== 'undefined') | ||
obj = _cloneDeep(obj); | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = !Array.isArray(path) | ||
? path.toString().match(/[^.[\]]+/g) || [] | ||
: path; | ||
(_a = newPath.slice(0, -1).reduce((a, c) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: undefined, obj)) === null || _a === void 0 ? true : delete _a[newPath[newPath.length - 1]]; | ||
return obj; // Return the top-level object to allow chaining | ||
} | ||
export { _unset }; | ||
//# sourceMappingURL=unset.js.map |
@@ -1,2 +0,23 @@ | ||
import{_get as t}from"./get.js";function e(e,r,c){Object(e)!==e&&(e={});let n=r.toString().match(/[^.[\]]+/g)||[];return n.slice(0,-1).reduce(((t,e,r)=>Object(t[e])===t[e]?t[e]:t[e]=Math.abs(Number(n[r+1]))>>0==+n[r+1]?[]:{}),e)[n[n.length-1]]=c(t(e,r)),e}export{e as _update}; | ||
import { _get } from './get.js'; | ||
/** @ignore */ | ||
function _update(obj, path, updater) { | ||
if (Object(obj) !== obj) | ||
obj = {}; // When obj is not an object | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = path.toString().match(/[^.[\]]+/g) || []; | ||
newPath.slice(0, -1).reduce((a, c, i // Iterate all of them except the last one | ||
) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: // No: create the key. Is the next key a potential array-index? | ||
(a[c] = | ||
Math.abs(Number(newPath[i + 1])) >> 0 === +newPath[i + 1] | ||
? [] // Yes: assign a new array object | ||
: {}), // No: assign a new plain object | ||
obj)[newPath[newPath.length - 1]] = updater(_get(obj, path)); // Finally assign the value to the last key | ||
return obj; // Return the top-level object to allow chaining | ||
} | ||
export { _update }; | ||
//# sourceMappingURL=update.js.map |
@@ -20,4 +20,5 @@ export { _some } from "./utils/some"; | ||
export { shouldIgnore } from "./utils/shouldIgnore"; | ||
export { getValue } from "./utils/getValue"; | ||
export * from "./utils/domUtils"; | ||
export * from "./types-9c37b16f"; | ||
export * from "./types-aca88280"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,532 @@ | ||
"use strict";function e(e,t){return Object.keys(e).some((r=>t(e[r])))}function t(e,t){return Object.keys(e).reduce(((r,n)=>Object.assign(Object.assign({},r),{[n]:t(e[n])})),{})}function r(e){return"[object Object]"===Object.prototype.toString.call(e)}function n(e){return Object.keys(e||{}).reduce(((t,o)=>Object.assign(Object.assign({},t),{[o]:r(e[o])?n(e[o]):Array.isArray(e[o])?[...e[o]]:e[o]})),{})}function o(e,n){return t(e,(e=>r(e)?o(e,n):Array.isArray(e)?e.map(function(e){return function(t){return r(t)?o(t,e):e}}(n)):n))}function i(...e){const t=e.pop(),u=n(e.shift());if(0===e.length)return u;for(const a of e){if(!a)continue;const e=Object.keys(a);for(const s of e){const e=t(u[s],a[s]);if(void 0!==e)u[s]=e;else if(r(a[s])&&r(u[s]))u[s]=i(u[s],a[s],t);else if(Array.isArray(a[s]))u[s]=a[s].map(((e,n)=>{if(!r(e))return e;return i(Array.isArray(u[s])?u[s][n]:u[s],e,t)}));else if(r(a[s])){const e=o(n(a[s]),void 0);u[s]=i(e,a[s],t)}else void 0!==a[s]&&(u[s]=a[s])}}return u}function u(e,t){if(!r(e)||!r(t)){if(Array.isArray(t)){if(t.some(r))return;const n=Array.isArray(e)?e:[];return t.map(((e,t)=>{var r;return null!==(r=n[t])&&void 0!==r?r:e}))}return void 0!==e?e:void 0}}function a(e,t,r){const n=r=>String.prototype.split.call(t,r).filter(Boolean).reduce(((e,t)=>null!=e?e[t]:e),e),o=n(/[,[\]]+?/)||n(/[,[\].]+?/);return void 0===o||o===e?r:o}function s(e,t,r){Object(e)!==e?e={}:void 0!==e&&(e=n(e));let o=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return o.slice(0,-1).reduce(((e,t,r)=>Object(e[t])===e[t]?e[t]:e[t]=Math.abs(Number(o[r+1]))>>0==+o[r+1]?[]:{}),e)[o[o.length-1]]=r,e}function c(e,t,r){Object(e)!==e&&(e={});let n=t.toString().match(/[^.[\]]+/g)||[];return n.slice(0,-1).reduce(((e,t,r)=>Object(e[t])===e[t]?e[t]:e[t]=Math.abs(Number(n[r+1]))>>0==+n[r+1]?[]:{}),e)[n[n.length-1]]=r(a(e,t)),e}function l(e){return e.hasAttribute("data-felte-index")?Number(e.dataset.felteIndex):void 0}function f(e){var t;return"INPUT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function d(e){var t;return"TEXTAREA"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function m(e){var t;return"SELECT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function p(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function v(e){return f(e)||d(e)||m(e)}function y(e,t){const r=l(e);let n="";t?n=t:v(e)&&(n=e.name),n=void 0===r?n:`${n}[${r}]`;let o=e.parentNode;if(!o)return n;for(;o&&"FORM"!==o.nodeName;){if(p(o)&&o.name){const e=l(o);n=`${void 0===e?o.name:`${o.name}[${e}]`}.${n}`}o=o.parentNode}return n}function x(e){for(const t of e.elements)if(v(t)||p(t)){if(e.name&&t.name){const r=l(e),n=void 0===r?e.name:`${e.name}[${r}]`;t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${n}`:n}"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true")}}function A(e){return e.type.match(/^(number|range)$/)?e.value?+e.value:void 0:e.value}function b(e,t){if(!v(e))return;const r=t;if(f(e)){if("checkbox"===e.type){const t=r;return void 0===t||"boolean"==typeof t?void(e.checked=!!t):void(Array.isArray(t)&&(t.includes(e.value)?e.checked=!0:e.checked=!1))}if("radio"===e.type){const t=r;return void(e.value===t?e.checked=!0:e.checked=!1)}if("file"===e.type)return e.files=null,void(e.value="")}e.value=String(null!=r?r:"")}function h(e,t){if(!r(e)&&!r(t)){if(null===e)return t;if(null===t)return e;if(e&&t)return Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),[...e,...t]}}Object.defineProperty(exports,"__esModule",{value:!0}),exports._cloneDeep=n,exports._defaultsDeep=function(...e){return i(...e,u)},exports._get=a,exports._isPlainObject=r,exports._mapValues=t,exports._merge=function(...e){return i(...e,(()=>{}))},exports._mergeWith=i,exports._set=s,exports._some=e,exports._unset=function(e,t){var r;if(Object(e)!==e)return;void 0!==e&&(e=n(e));let o=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return null===(r=o.slice(0,-1).reduce(((e,t)=>Object(e[t])===e[t]?e[t]:void 0),e))||void 0===r||delete r[o[o.length-1]],e},exports._update=c,exports.addAttrsFromFieldset=x,exports.deepSet=o,exports.deepSome=function t(n,o){return e(n,(e=>r(e)?t(e,o):o(e)))},exports.executeTransforms=function(e,t){return t?Array.isArray(t)?t.reduce(((e,t)=>t(e)),e):t(e):e},exports.executeValidation=async function(e,t){if(!t)return;return Array.isArray(t)?i(...await Promise.all(t.map((t=>t(e)))),h):t(e)},exports.getFormControls=function e(t){if(v(t))return[t];if(0===t.childElementCount)return[];const r=new Set;for(const n of t.children){if(v(n)&&r.add(n),p(n))for(const e of n.elements)v(e)&&r.add(e);n.childElementCount>0&&e(n).forEach((e=>r.add(e)))}return Array.from(r)},exports.getFormDefaultValues=function(e){var t;let r={};for(const n of e.elements){if(p(n)&&x(n),!v(n)||!n.name)continue;const o=y(n),i=l(n);if(f(n)){if("checkbox"===n.type){if(void 0===a(r,o)){if(1===Array.from(e.querySelectorAll(`[name="${n.name}"]`)).filter((e=>{if(!v(e))return!1;if(void 0!==i){return Number(e.dataset.felteIndex)===i}return o===y(e)})).length){r=s(r,o,n.checked);continue}r=s(r,o,n.checked?[n.value]:[]);continue}Array.isArray(a(r,o))&&n.checked&&c(r,o,(e=>(void 0===i||Array.isArray(e)||(e=[]),[...e,n.value])));continue}if("radio"===n.type){if(a(r,o))continue;r=s(r,o,n.checked?n.value:void 0);continue}if("file"===n.type){r=s(r,o,n.multiple?Array.from(n.files||[]):null===(t=n.files)||void 0===t?void 0:t[0]);continue}}const u=A(n);r=s(r,o,u)}return{defaultData:r}},exports.getIndex=l,exports.getInputTextOrNumber=A,exports.getPath=y,exports.getPathFromDataset=function(e){const t=e.dataset.felteFieldset,r=l(e),n=void 0===r?e.name:`${e.name}[${r}]`;return t?`${t}.${n}`:n},exports.isElement=function(e){return e.nodeType===Node.ELEMENT_NODE},exports.isFieldSetElement=p,exports.isFieldValue=function(e){return Array.isArray(e)?0===e.length||e.some((e=>e instanceof File||"string"==typeof e)):"string"==typeof e||"number"==typeof e||"boolean"==typeof e||e instanceof File},exports.isFormControl=v,exports.isInputElement=f,exports.isSelectElement=m,exports.isTextAreaElement=d,exports.setControlValue=b,exports.setForm=function(e,t){for(const r of e.elements){if(p(r)&&x(r),!v(r)||!r.name)continue;b(r,a(t,y(r)))}},exports.shouldIgnore=function(e){let t=e;for(;t&&"FORM"!==t.nodeName;){if(t.hasAttribute("data-felte-ignore"))return!0;t=t.parentElement}return!1}; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/** @ignore */ | ||
function _some(obj, pred) { | ||
const keys = Object.keys(obj); | ||
return keys.some((key) => pred(obj[key])); | ||
} | ||
/** @ignore */ | ||
function _mapValues(obj, updater) { | ||
const keys = Object.keys(obj); | ||
return keys.reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: updater(obj[key]) })), {}); | ||
} | ||
/** @category Helper */ | ||
function isFieldValue(value) { | ||
if (Array.isArray(value)) { | ||
if (value.length === 0) | ||
return true; | ||
return value.some((v) => v instanceof File || typeof v === 'string'); | ||
} | ||
return (typeof value === 'string' || | ||
typeof value === 'number' || | ||
typeof value === 'boolean' || | ||
value instanceof File); | ||
} | ||
/** @ignore */ | ||
function _isPlainObject(value) { | ||
return Object.prototype.toString.call(value) === '[object Object]'; | ||
} | ||
/** @ignore */ | ||
function _cloneDeep(obj) { | ||
return Object.keys(obj || {}).reduce((res, key) => (Object.assign(Object.assign({}, res), { [key]: _isPlainObject(obj[key]) | ||
? _cloneDeep(obj[key]) | ||
: Array.isArray(obj[key]) | ||
? [...obj[key]] | ||
: obj[key] })), {}); | ||
} | ||
function handleArray(value) { | ||
return function (propVal) { | ||
if (_isPlainObject(propVal)) | ||
return deepSet(propVal, value); | ||
return value; | ||
}; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function deepSet(obj, value) { | ||
return _mapValues(obj, (prop) => _isPlainObject(prop) | ||
? deepSet(prop, value) | ||
: Array.isArray(prop) | ||
? prop.map(handleArray(value)) | ||
: value); | ||
} | ||
/** @ignore */ | ||
function _mergeWith(...args) { | ||
const customizer = args.pop(); | ||
const obj = _cloneDeep(args.shift()); | ||
if (args.length === 0) | ||
return obj; | ||
for (const source of args) { | ||
if (!source) | ||
continue; | ||
const keys = Object.keys(source); | ||
for (const key of keys) { | ||
const rsValue = customizer(obj[key], source[key]); | ||
if (typeof rsValue !== 'undefined') { | ||
obj[key] = rsValue; | ||
} | ||
else if (_isPlainObject(source[key]) && _isPlainObject(obj[key])) { | ||
obj[key] = _mergeWith(obj[key], source[key], customizer); | ||
} | ||
else if (Array.isArray(source[key])) { | ||
obj[key] = source[key].map((val, i) => { | ||
if (!_isPlainObject(val)) | ||
return val; | ||
const newObj = Array.isArray(obj[key]) ? obj[key][i] : obj[key]; | ||
return _mergeWith(newObj, val, customizer); | ||
}); | ||
} | ||
else if (_isPlainObject(source[key])) { | ||
const defaultObj = deepSet(_cloneDeep(source[key]), undefined); | ||
obj[key] = _mergeWith(defaultObj, source[key], customizer); | ||
} | ||
else if (typeof source[key] !== 'undefined') { | ||
obj[key] = source[key]; | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
function defaultsCustomizer(objValue, srcValue) { | ||
if (_isPlainObject(objValue) && _isPlainObject(srcValue)) | ||
return; | ||
if (Array.isArray(srcValue)) { | ||
if (srcValue.some(_isPlainObject)) | ||
return; | ||
const objArray = Array.isArray(objValue) ? objValue : []; | ||
return srcValue.map((value, index) => { var _a; return (_a = objArray[index]) !== null && _a !== void 0 ? _a : value; }); | ||
} | ||
if (typeof objValue !== 'undefined') | ||
return objValue; | ||
} | ||
/** @ignore */ | ||
function _defaultsDeep(...args) { | ||
return _mergeWith(...args, defaultsCustomizer); | ||
} | ||
/** @ignore */ | ||
function _merge(...args) { | ||
return _mergeWith(...args, () => undefined); | ||
} | ||
/* From: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get */ | ||
/** @ignore */ | ||
function _get(obj, path, defaultValue) { | ||
const travel = (regexp) => String.prototype.split | ||
.call(path, regexp) | ||
.filter(Boolean) | ||
.reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj); | ||
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); | ||
return result === undefined || result === obj ? defaultValue : result; | ||
} | ||
/* From: https://stackoverflow.com/a/54733755 */ | ||
/** @ignore */ | ||
function _set(obj, path, value) { | ||
if (Object(obj) !== obj) | ||
obj = {}; | ||
// When obj is not an object | ||
else if (typeof obj !== 'undefined') | ||
obj = _cloneDeep(obj); | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = !Array.isArray(path) | ||
? path.toString().match(/[^.[\]]+/g) || [] | ||
: path; | ||
newPath.slice(0, -1).reduce((a, c, i // Iterate all of them except the last one | ||
) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: // No: create the key. Is the next key a potential array-index? | ||
(a[c] = | ||
Math.abs(Number(newPath[i + 1])) >> 0 === +newPath[i + 1] | ||
? [] // Yes: assign a new array object | ||
: {}), // No: assign a new plain object | ||
obj)[newPath[newPath.length - 1]] = value; // Finally assign the value to the last key | ||
return obj; // Return the top-level object to allow chaining | ||
} | ||
function _unset(obj, path) { | ||
var _a; | ||
if (Object(obj) !== obj) | ||
return; | ||
// When obj is not an object | ||
else if (typeof obj !== 'undefined') | ||
obj = _cloneDeep(obj); | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = !Array.isArray(path) | ||
? path.toString().match(/[^.[\]]+/g) || [] | ||
: path; | ||
(_a = newPath.slice(0, -1).reduce((a, c) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: undefined, obj)) === null || _a === void 0 ? true : delete _a[newPath[newPath.length - 1]]; | ||
return obj; // Return the top-level object to allow chaining | ||
} | ||
/** @ignore */ | ||
function _update(obj, path, updater) { | ||
if (Object(obj) !== obj) | ||
obj = {}; // When obj is not an object | ||
// If not yet an array, get the keys from the string-path | ||
let newPath = path.toString().match(/[^.[\]]+/g) || []; | ||
newPath.slice(0, -1).reduce((a, c, i // Iterate all of them except the last one | ||
) => Object(a[c]) === a[c] // Does the key exist and is its value an object? | ||
? // Yes: then follow that path | ||
a[c] | ||
: // No: create the key. Is the next key a potential array-index? | ||
(a[c] = | ||
Math.abs(Number(newPath[i + 1])) >> 0 === +newPath[i + 1] | ||
? [] // Yes: assign a new array object | ||
: {}), // No: assign a new plain object | ||
obj)[newPath[newPath.length - 1]] = updater(_get(obj, path)); // Finally assign the value to the last key | ||
return obj; // Return the top-level object to allow chaining | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function deepSome(obj, pred) { | ||
return _some(obj, (value) => _isPlainObject(value) ? deepSome(value, pred) : pred(value)); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function getIndex(el) { | ||
return el.hasAttribute('data-felte-index') | ||
? Number(el.dataset.felteIndex) | ||
: undefined; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isInputElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'INPUT'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isTextAreaElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'TEXTAREA'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isSelectElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'SELECT'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isFieldSetElement(el) { | ||
var _a; | ||
return ((_a = el) === null || _a === void 0 ? void 0 : _a.nodeName) === 'FIELDSET'; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isFormControl(el) { | ||
return isInputElement(el) || isTextAreaElement(el) || isSelectElement(el); | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function isElement(el) { | ||
return el.nodeType === Node.ELEMENT_NODE; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function getPath(el, name) { | ||
const index = getIndex(el); | ||
let path = ''; | ||
if (name) { | ||
path = name; | ||
} | ||
else if (isFormControl(el)) { | ||
path = el.name; | ||
} | ||
path = typeof index === 'undefined' ? path : `${path}[${index}]`; | ||
let parent = el.parentNode; | ||
if (!parent) | ||
return path; | ||
while (parent && parent.nodeName !== 'FORM') { | ||
if (isFieldSetElement(parent) && parent.name) { | ||
const index = getIndex(parent); | ||
const fieldsetName = typeof index === 'undefined' ? parent.name : `${parent.name}[${index}]`; | ||
path = `${fieldsetName}.${path}`; | ||
} | ||
parent = parent.parentNode; | ||
} | ||
return path; | ||
} | ||
function getPathFromDataset(el) { | ||
const fieldSetName = el.dataset.felteFieldset; | ||
const index = getIndex(el); | ||
const fieldName = typeof index === 'undefined' ? el.name : `${el.name}[${index}]`; | ||
return fieldSetName ? `${fieldSetName}.${fieldName}` : fieldName; | ||
} | ||
/** | ||
* @category Helper | ||
*/ | ||
function shouldIgnore(el) { | ||
let parent = el; | ||
while (parent && parent.nodeName !== 'FORM') { | ||
if (parent.hasAttribute('data-felte-ignore')) | ||
return true; | ||
parent = parent.parentElement; | ||
} | ||
return false; | ||
} | ||
function getValue(storeValue, selectorOrPath) { | ||
if (!_isPlainObject(storeValue) || !selectorOrPath) | ||
return storeValue; | ||
if (typeof selectorOrPath === 'string') { | ||
return _get(storeValue, selectorOrPath); | ||
} | ||
return selectorOrPath(storeValue); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function getFormControls(el) { | ||
if (isFormControl(el)) | ||
return [el]; | ||
if (el.childElementCount === 0) | ||
return []; | ||
const foundControls = new Set(); | ||
for (const child of el.children) { | ||
if (isFormControl(child)) | ||
foundControls.add(child); | ||
if (isFieldSetElement(child)) { | ||
for (const fieldsetChild of child.elements) { | ||
if (isFormControl(fieldsetChild)) | ||
foundControls.add(fieldsetChild); | ||
} | ||
} | ||
if (child.childElementCount > 0) | ||
getFormControls(child).forEach((value) => foundControls.add(value)); | ||
} | ||
return Array.from(foundControls); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function addAttrsFromFieldset(fieldSet) { | ||
for (const element of fieldSet.elements) { | ||
if (!isFormControl(element) && !isFieldSetElement(element)) | ||
continue; | ||
if (fieldSet.name && element.name) { | ||
const index = getIndex(fieldSet); | ||
const fieldsetName = typeof index === 'undefined' | ||
? fieldSet.name | ||
: `${fieldSet.name}[${index}]`; | ||
element.dataset.felteFieldset = fieldSet.dataset.felteFieldset | ||
? `${fieldSet.dataset.felteFieldset}.${fieldsetName}` | ||
: fieldsetName; | ||
} | ||
if (fieldSet.hasAttribute('data-felte-keep-on-remove') && | ||
!element.hasAttribute('data-felte-keep-on-remove')) { | ||
element.dataset.felteKeepOnRemove = fieldSet.dataset.felteKeepOnRemove; | ||
} | ||
} | ||
} | ||
/** @ignore */ | ||
function getInputTextOrNumber(el) { | ||
if (el.type.match(/^(number|range)$/)) { | ||
return !el.value ? undefined : +el.value; | ||
} | ||
else { | ||
return el.value; | ||
} | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function getFormDefaultValues(node) { | ||
var _a; | ||
let defaultData = {}; | ||
for (const el of node.elements) { | ||
if (isFieldSetElement(el)) | ||
addAttrsFromFieldset(el); | ||
if (!isFormControl(el) || !el.name) | ||
continue; | ||
const elName = getPath(el); | ||
const index = getIndex(el); | ||
if (isInputElement(el)) { | ||
if (el.type === 'checkbox') { | ||
if (typeof _get(defaultData, elName) === 'undefined') { | ||
const checkboxes = Array.from(node.querySelectorAll(`[name="${el.name}"]`)).filter((checkbox) => { | ||
if (!isFormControl(checkbox)) | ||
return false; | ||
if (typeof index !== 'undefined') { | ||
const felteIndex = Number(checkbox.dataset.felteIndex); | ||
return felteIndex === index; | ||
} | ||
return elName === getPath(checkbox); | ||
}); | ||
if (checkboxes.length === 1) { | ||
defaultData = _set(defaultData, elName, el.checked); | ||
continue; | ||
} | ||
defaultData = _set(defaultData, elName, el.checked ? [el.value] : []); | ||
continue; | ||
} | ||
if (Array.isArray(_get(defaultData, elName)) && el.checked) { | ||
_update(defaultData, elName, (value) => { | ||
if (typeof index !== 'undefined' && !Array.isArray(value)) | ||
value = []; | ||
return [...value, el.value]; | ||
}); | ||
} | ||
continue; | ||
} | ||
if (el.type === 'radio') { | ||
if (_get(defaultData, elName)) | ||
continue; | ||
defaultData = _set(defaultData, elName, el.checked ? el.value : undefined); | ||
continue; | ||
} | ||
if (el.type === 'file') { | ||
defaultData = _set(defaultData, elName, el.multiple ? Array.from(el.files || []) : (_a = el.files) === null || _a === void 0 ? void 0 : _a[0]); | ||
continue; | ||
} | ||
} | ||
const inputValue = getInputTextOrNumber(el); | ||
defaultData = _set(defaultData, elName, inputValue); | ||
} | ||
return { defaultData }; | ||
} | ||
function setControlValue(el, value) { | ||
if (!isFormControl(el)) | ||
return; | ||
const fieldValue = value; | ||
if (isInputElement(el)) { | ||
if (el.type === 'checkbox') { | ||
const checkboxesDefaultData = fieldValue; | ||
if (typeof checkboxesDefaultData === 'undefined' || | ||
typeof checkboxesDefaultData === 'boolean') { | ||
el.checked = !!checkboxesDefaultData; | ||
return; | ||
} | ||
if (Array.isArray(checkboxesDefaultData)) { | ||
if (checkboxesDefaultData.includes(el.value)) { | ||
el.checked = true; | ||
} | ||
else { | ||
el.checked = false; | ||
} | ||
} | ||
return; | ||
} | ||
if (el.type === 'radio') { | ||
const radioValue = fieldValue; | ||
if (el.value === radioValue) | ||
el.checked = true; | ||
else | ||
el.checked = false; | ||
return; | ||
} | ||
if (el.type === 'file') { | ||
el.files = null; | ||
el.value = ''; | ||
return; | ||
} | ||
} | ||
el.value = String(fieldValue !== null && fieldValue !== void 0 ? fieldValue : ''); | ||
} | ||
/** Sets the form inputs value to match the data object provided. */ | ||
function setForm(node, data) { | ||
for (const el of node.elements) { | ||
if (isFieldSetElement(el)) | ||
addAttrsFromFieldset(el); | ||
if (!isFormControl(el) || !el.name) | ||
continue; | ||
const elName = getPath(el); | ||
setControlValue(el, _get(data, elName)); | ||
} | ||
} | ||
function executeCustomizer(objValue, srcValue) { | ||
if (_isPlainObject(objValue) || _isPlainObject(srcValue)) | ||
return; | ||
if (objValue === null) | ||
return srcValue; | ||
if (srcValue === null) | ||
return objValue; | ||
if (!objValue || !srcValue) | ||
return; | ||
if (!Array.isArray(objValue)) | ||
objValue = [objValue]; | ||
if (!Array.isArray(srcValue)) | ||
srcValue = [srcValue]; | ||
return [...objValue, ...srcValue]; | ||
} | ||
async function executeValidation(values, validations) { | ||
if (!validations) | ||
return; | ||
if (!Array.isArray(validations)) | ||
return validations(values); | ||
const errorArray = await Promise.all(validations.map((v) => v(values))); | ||
return _mergeWith(...errorArray, executeCustomizer); | ||
} | ||
function executeTransforms(values, transforms) { | ||
if (!transforms) | ||
return values; | ||
if (!Array.isArray(transforms)) | ||
return transforms(values); | ||
return transforms.reduce((res, t) => t(res), values); | ||
} | ||
exports._cloneDeep = _cloneDeep; | ||
exports._defaultsDeep = _defaultsDeep; | ||
exports._get = _get; | ||
exports._isPlainObject = _isPlainObject; | ||
exports._mapValues = _mapValues; | ||
exports._merge = _merge; | ||
exports._mergeWith = _mergeWith; | ||
exports._set = _set; | ||
exports._some = _some; | ||
exports._unset = _unset; | ||
exports._update = _update; | ||
exports.addAttrsFromFieldset = addAttrsFromFieldset; | ||
exports.deepSet = deepSet; | ||
exports.deepSome = deepSome; | ||
exports.executeTransforms = executeTransforms; | ||
exports.executeValidation = executeValidation; | ||
exports.getFormControls = getFormControls; | ||
exports.getFormDefaultValues = getFormDefaultValues; | ||
exports.getIndex = getIndex; | ||
exports.getInputTextOrNumber = getInputTextOrNumber; | ||
exports.getPath = getPath; | ||
exports.getPathFromDataset = getPathFromDataset; | ||
exports.getValue = getValue; | ||
exports.isElement = isElement; | ||
exports.isFieldSetElement = isFieldSetElement; | ||
exports.isFieldValue = isFieldValue; | ||
exports.isFormControl = isFormControl; | ||
exports.isInputElement = isInputElement; | ||
exports.isSelectElement = isSelectElement; | ||
exports.isTextAreaElement = isTextAreaElement; | ||
exports.setControlValue = setControlValue; | ||
exports.setForm = setForm; | ||
exports.shouldIgnore = shouldIgnore; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,4 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _cloneDeep<T extends Obj>(obj: T): T; | ||
export { _cloneDeep }; | ||
//# sourceMappingURL=utils/cloneDeep.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Obj, DeepSetResult } from "../types-9c37b16f"; | ||
import { Obj, DeepSetResult } from "../types-aca88280"; | ||
/** | ||
@@ -7,2 +7,1 @@ * @category Helper | ||
export { deepSet }; | ||
//# sourceMappingURL=utils/deepSet.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** | ||
@@ -7,2 +7,1 @@ * @category Helper | ||
export { deepSome }; | ||
//# sourceMappingURL=utils/deepSome.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _defaultsDeep<T extends Obj>(...args: any[]): T; | ||
export { _defaultsDeep }; | ||
//# sourceMappingURL=utils/defaultsDeep.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { FormControl, Obj, FieldValue, ValidationFunction, TransformFunction } from "../types-9c37b16f"; | ||
import { FormControl, Obj, FieldValue, ValidationFunction, TransformFunction } from "../types-aca88280"; | ||
/** | ||
@@ -24,2 +24,1 @@ * @ignore | ||
export { getFormControls, addAttrsFromFieldset, getInputTextOrNumber, getFormDefaultValues, setControlValue, setForm, executeValidation, executeTransforms }; | ||
//# sourceMappingURL=utils/domUtils.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj, FieldValue } from "../types-9c37b16f"; | ||
import { Obj, FieldValue } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _get<Data extends Obj, Default = undefined>(obj: Data, path: string, defaultValue?: Default): Default | FieldValue | FieldValue[] | undefined; | ||
export { _get }; | ||
//# sourceMappingURL=utils/get.d.ts.map |
@@ -6,2 +6,1 @@ /** | ||
export { getIndex }; | ||
//# sourceMappingURL=utils/getIndex.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { FormControl } from "../types-9c37b16f"; | ||
import { FormControl } from "../types-aca88280"; | ||
/** | ||
@@ -7,2 +7,1 @@ * @category Helper | ||
export { getPath }; | ||
//# sourceMappingURL=utils/getPath.d.ts.map |
@@ -1,4 +0,3 @@ | ||
import { FormControl } from "../types-9c37b16f"; | ||
import { FormControl } from "../types-aca88280"; | ||
declare function getPathFromDataset(el: FormControl): string; | ||
export { getPathFromDataset }; | ||
//# sourceMappingURL=utils/getPathFromDataset.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { FieldValue } from "../types-9c37b16f"; | ||
import { FieldValue } from "../types-aca88280"; | ||
/** @category Helper */ | ||
declare function isFieldValue(value: unknown): value is FieldValue; | ||
export { isFieldValue }; | ||
//# sourceMappingURL=utils/isFieldValue.d.ts.map |
/** @ignore */ | ||
declare function _isPlainObject(value: unknown): boolean; | ||
declare function _isPlainObject(value: unknown): value is Record<string, unknown>; | ||
export { _isPlainObject }; | ||
//# sourceMappingURL=utils/isPlainObject.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _mapValues(obj: Obj, updater: (value: unknown) => unknown): Obj; | ||
export { _mapValues }; | ||
//# sourceMappingURL=utils/mapValues.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _merge<T extends Obj>(...args: any[]): T; | ||
export { _merge }; | ||
//# sourceMappingURL=utils/merge.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _mergeWith<T extends Obj>(...args: any[]): T; | ||
export { _mergeWith }; | ||
//# sourceMappingURL=utils/mergeWith.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj, FieldValue } from "../types-9c37b16f"; | ||
import { Obj, FieldValue } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _set<Data extends Obj>(obj: Data | undefined, path: string | string[], value: FieldValue | FieldValue[]): Data; | ||
export { _set }; | ||
//# sourceMappingURL=utils/set.d.ts.map |
@@ -6,2 +6,1 @@ /** | ||
export { shouldIgnore }; | ||
//# sourceMappingURL=utils/shouldIgnore.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _some(obj: Obj, pred: (value: unknown) => boolean): boolean; | ||
export { _some }; | ||
//# sourceMappingURL=utils/some.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { FormControl } from "../types-9c37b16f"; | ||
import { FormControl } from "../types-aca88280"; | ||
/** | ||
@@ -27,2 +27,1 @@ * @category Helper | ||
export { isInputElement, isTextAreaElement, isSelectElement, isFieldSetElement, isFormControl, isElement }; | ||
//# sourceMappingURL=utils/typeGuards.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Obj } from "../types-9c37b16f"; | ||
import { Obj } from "../types-aca88280"; | ||
/** @ignore */ | ||
@@ -6,2 +6,1 @@ declare function _unset(obj: undefined, path: string | string[]): undefined; | ||
export { _unset }; | ||
//# sourceMappingURL=utils/unset.d.ts.map |
@@ -1,5 +0,4 @@ | ||
import { Obj, FieldValue } from "../types-9c37b16f"; | ||
import { Obj, FieldValue } from "../types-aca88280"; | ||
/** @ignore */ | ||
declare function _update<Data extends Obj, Value = FieldValue>(obj: Data | undefined, path: string, updater: (value: Value) => Value): Data; | ||
export { _update }; | ||
//# sourceMappingURL=utils/update.d.ts.map |
{ | ||
"name": "@felte/common", | ||
"version": "0.6.0", | ||
"version": "1.0.0-next.0", | ||
"description": "Common utilities for Felte packages", | ||
@@ -12,2 +12,6 @@ "author": "Pablo Berganza <pablo@berganza.dev>", | ||
"types": "dist/index.d.ts", | ||
"type": "module", | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
"sideEffects": false, | ||
@@ -47,3 +51,4 @@ "keywords": [ | ||
"test:ci": "jest --ci --coverage" | ||
} | ||
}, | ||
"readme": "# @felte/common\n\n[![Bundle size](https://img.shields.io/bundlephobia/min/@felte/common)](https://bundlephobia.com/result?p=@felte/common)\n[![NPM Version](https://img.shields.io/npm/v/@felte/common)](https://www.npmjs.com/package/@felte/common)\n\nCommon utilities and types for Felte and its related packages.\n" | ||
} |
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
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
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
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
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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
363352
5209
Yes
1