Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@felte/common

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@felte/common - npm Package Compare versions

Comparing version 0.3.4 to 0.3.5

dist/esm/index.js

6

CHANGELOG.md
# @felte/common
## 0.3.5
### Patch Changes
- d2d79cb: Make package tree shakeable
## 0.3.4

@@ -4,0 +10,0 @@

213

dist/index.d.ts

@@ -1,196 +0,19 @@

import { Readable, Writable } from "svelte/store";
type CreateSubmitHandlerConfig<Data extends Obj> = {
onSubmit?: FormConfig<Data>["onSubmit"];
validate?: FormConfig<Data>["validate"];
onError?: FormConfig<Data>["onError"];
};
type CurrentForm<Data extends Obj> = {
form?: HTMLFormElement;
controls?: FormControl[];
errors: Writable<Errors<Data>>;
data: Writable<Data>;
touched: Writable<Touched<Data>>;
config: FormConfig<Data>;
};
type OnSubmitErrorState<Data extends Obj> = {
data: Data;
errors: Errors<Data>;
};
type ReporterHandler<Data extends Obj> = {
destroy?: () => void;
onSubmitError?: (state: OnSubmitErrorState<Data>) => void;
};
type Reporter<Data extends Obj = Obj> = (currentForm: CurrentForm<Data>) => ReporterHandler<Data>;
type ExtenderHandler<Data extends Obj> = {
destroy?: () => void;
onSubmitError?: (state: OnSubmitErrorState<Data>) => void;
};
type Extender<Data extends Obj = Obj> = (currentForm: CurrentForm<Data>) => ExtenderHandler<Data>;
/** `Record<string, unknown>` */
type Obj = Record<string, unknown>;
/** Possible field values. */
type FieldValue = string | string[] | boolean | number | File | File[] | undefined;
type FormControl = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
type ValidationFunction<Data extends Obj> = (values: Data) => Errors<Data> | undefined | Promise<Errors<Data> | undefined>;
/**
* Configuration object when `initialValues` is not set. Used when using the `form` action.
*/
interface FormConfigWithoutInitialValues<Data extends Obj> {
/** Optional function to validate the data. */
validate?: ValidationFunction<Data> | ValidationFunction<Data>[];
/** Required function to handle the form data on submit. */
onSubmit: (values: Data) => Promise<void> | void;
/** Optional function that accepts any thrown exceptions from the onSubmit function. You can return an object with the same shape [[`Errors`]] for a reporter to use it. */
onError?: (errors: unknown) => void | Errors<Data>;
/** Optional function/s to handle reporting errors. */
reporter?: Reporter<Data> | Reporter<Data>[];
/** Optional function/s to extend Felte's functionality. */
extend?: Extender<Data> | Extender<Data>[];
[key: string]: unknown;
}
/**
* Configuration object when `initialValues` is set. Used when using the `data` store to bind to form inputs.
*/
interface FormConfigWithInitialValues<Data extends Obj> extends FormConfigWithoutInitialValues<Data> {
/** Initial values for the form. To be used when not using the `form` action. */
initialValues: Data;
[key: string]: unknown;
}
/**
* Configuration object type. `initialValues` is optional.
*/
interface FormConfig<Data extends Obj> extends FormConfigWithoutInitialValues<Data> {
initialValues?: Data;
[key: string]: unknown;
}
/** The errors object may contain either a string or array or string per key. */
type Errors<Data extends Obj> = {
[key in keyof Data]?: Data[key] extends Obj ? Errors<Data[key]> : string | string[] | null;
};
/** The touched object may only contain booleans per key. */
type Touched<Data extends Obj> = {
[key in keyof Data]: Data[key] extends Obj ? Touched<Data[key]> : boolean;
};
type FormAction = (node: HTMLFormElement) => {
destroy: () => void;
};
/** The stores that `createForm` creates. */
type Stores<Data extends Obj> = {
/** Writable store that contains the form's data. */
data: Writable<Data>;
/** Writable store that contains the form's validation errors. */
errors: Writable<Errors<Data>>;
/** Writable store that denotes if any field has been touched. */
touched: Writable<Touched<Data>>;
/** Writable store containing only a boolean that represents if the form is submitting. */
isSubmitting: Writable<boolean>;
/** Readable store containing only a boolean that represents if the form is valid. */
isValid: Readable<boolean>;
};
/** The return type for the `createForm` function. */
type Form<Data extends Obj> = {
/** Action function to be used with the `use` directive on your `form` elements. */
form: FormAction;
/** Function to handle submit to be passed to the on:submit event. Not necessary if using the `form` action. */
handleSubmit: (e?: Event) => void;
/** Function that creates a submit handler. If a function is passed as first argument it overrides the default `onSubmit` function set in the `createForm` config object. */
createSubmitHandler: (altConfig?: CreateSubmitHandlerConfig<Data>) => (e?: Event) => void;
/** Function that resets the form to its initial values */
reset: () => void;
/** Helper function to touch a specific field. */
setTouched: (path: string) => void;
/** Helper function to set an error to a specific field. */
setError: (path: string, error: string | string[]) => void;
/** Helper function to set the value of a specific field. Set `touch` to `false` if you want to set the value without setting the field to touched. */
setField: (path: string, value?: FieldValue, touch?: boolean) => void;
/** Helper function to set all values of the form. Useful for "initializing" values after the form has loaded. */
setFields: (values: Data) => void;
/** Helper function that validates every fields and touches all of them. It updates the `errors` store. */
validate: () => Promise<Errors<Data> | void>;
} & Stores<Data>;
type DeepSetResult<Data extends Obj, Value> = {
[key in keyof Data]: Data[key] extends Obj ? DeepSetResult<Data[key], Value> : Value;
};
/** @category Helper */
declare function isFieldValue(value: unknown): value is FieldValue;
/** @ignore */
declare function _some(obj: Obj, pred: (value: unknown) => boolean): boolean;
/** @ignore */
declare function _mapValues(obj: Obj, updater: (value: unknown) => unknown): Obj;
/** @ignore */
declare function _cloneDeep<T extends Record<string, unknown>>(obj: T): T;
/** @ignore */
declare function _mergeWith<T extends Obj>(...args: any[]): T;
/** @ignore */
declare function _merge<T extends Obj>(...args: any[]): T;
/** @ignore */
declare function _defaultsDeep<T extends Obj>(...args: any[]): T;
/** @ignore */
declare function _get<Data extends Obj, Default = undefined>(obj: Data, path: string, defaultValue?: Default): FieldValue | Default | undefined;
/** @ignore */
declare function _set<Data extends Obj>(obj: Data | undefined, path: string, value: FieldValue): Data;
/** @ignore */
declare function _unset<Data extends Obj>(obj: Data, path: string): Data;
/** @ignore */
declare function _update<Data extends Obj, Value = FieldValue>(obj: Data, path: string, updater: (value: Value) => Value): Data;
/** @ignore */
declare function _isPlainObject(value: unknown): boolean;
/**
* @category Helper
*/
declare function deepSet<Data extends Obj, Value>(obj: Data, value: Value): DeepSetResult<Data, Value>;
/**
* @category Helper
*/
declare function deepSome(obj: Obj, pred: (value: unknown) => boolean): boolean;
/**
* @category Helper
*/
declare function isInputElement(el: EventTarget): el is HTMLInputElement;
/**
* @category Helper
*/
declare function isTextAreaElement(el: EventTarget): el is HTMLTextAreaElement;
/**
* @category Helper
*/
declare function isSelectElement(el: EventTarget): el is HTMLSelectElement;
/**
* @category Helper
*/
declare function isFieldSetElement(el: EventTarget): el is HTMLFieldSetElement;
/**
* @category Helper
*/
declare function isFormControl(el: EventTarget): el is FormControl;
/**
* @category Helper
*/
declare function isElement(el: Node): el is Element;
/**
* @category Helper
*/
declare function getPath(el: FormControl): string;
/**
* @ignore
*/
declare function getFormControls(el: Element): FormControl[];
/**
* @ignore
*/
declare function addAttrsFromFieldset(fieldSet: HTMLFieldSetElement): void;
/** @ignore */
declare function getInputTextOrNumber(el: FormControl): string | number | undefined;
/**
* @ignore
*/
declare function getFormDefaultValues<Data extends Obj>(node: HTMLFormElement): {
defaultData: Data;
defaultTouched: Touched<Data>;
};
declare function setControlValue(el: FormControl, value: FieldValue): void;
/** Sets the form inputs value to match the data object provided. */
declare function setForm<Data extends Obj>(node: HTMLFormElement, data: Data): void;
declare function executeValidation<Data extends Obj>(values: Data, validations?: ValidationFunction<Data>[] | ValidationFunction<Data>): Promise<ReturnType<ValidationFunction<Data>>>;
export { isFieldValue, _some, _mapValues, _cloneDeep, _mergeWith, _merge, _defaultsDeep, _get, _set, _unset, _update, _isPlainObject, deepSet, deepSome, isInputElement, isTextAreaElement, isSelectElement, isFieldSetElement, isFormControl, isElement, getPath, getFormControls, addAttrsFromFieldset, getInputTextOrNumber, getFormDefaultValues, setControlValue, setForm, executeValidation, CreateSubmitHandlerConfig, CurrentForm, OnSubmitErrorState, ReporterHandler, Reporter, ExtenderHandler, Extender, Obj, FieldValue, FormControl, ValidationFunction, FormConfigWithoutInitialValues, FormConfigWithInitialValues, FormConfig, Errors, Touched, FormAction, Stores, Form };
export { _some } from "./utils/some";
export { _mapValues } from "./utils/mapValues";
export { isFieldValue } from "./utils/isFieldValue";
export { _cloneDeep } from "./utils/cloneDeep";
export { _isPlainObject } from "./utils/isPlainObject";
export { _mergeWith } from "./utils/mergeWith";
export { _defaultsDeep } from "./utils/defaultsDeep";
export { _merge } from "./utils/merge";
export { _get } from "./utils/get";
export { _set } from "./utils/set";
export { _unset } from "./utils/unset";
export { _update } from "./utils/update";
export { deepSet } from "./utils/deepSet";
export { deepSome } from "./utils/deepSome";
export * from "./utils/typeGuards";
export { getPath } from "./utils/getPath";
export * from "./utils/domUtils";
export * from "./types-9bd61299";
//# sourceMappingURL=index.d.ts.map

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

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Common={})}(this,(function(e){"use strict";function t(e,t){return Object.keys(e).some((n=>t(e[n])))}function n(e,t){return Object.keys(e).reduce(((n,o)=>Object.assign(Object.assign({},n),{[o]:t(e[o])})),{})}function o(e){return Object.keys(e||{}).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:l(e[n])?o(e[n]):e[n]})),{})}function i(...e){const t=e.pop(),n=o(e.shift());if(0===e.length)return n;for(const r of e){if(!r)continue;const e=Object.keys(r);for(const u of e){const e=t(n[u],r[u]);if(void 0!==e)n[u]=e;else if(l(r[u])&&l(n[u]))n[u]=i(n[u],r[u],t);else if(l(r[u])){const e=s(o(r[u]),void 0);n[u]=i(e,r[u],t)}else void 0!==r[u]&&(n[u]=r[u])}}return n}function r(e,t){if(!l(e)||!l(t))return void 0!==e?e:void 0}function u(e,t,n){const o=t.split(".");let i=e;try{for(const e of o)i=i[e]}catch(e){return n}return void 0===i?n:i}function c(e,t,n){null!=e||(e={});const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n,e}function f(e,t,n){const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n(i[o[0]]),e}function l(e){return"[object Object]"===Object.prototype.toString.call(e)}function s(e,t){return n(e,(e=>l(e)?s(e,t):t))}function a(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 v(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function y(e){return a(e)||d(e)||m(e)}function p(e){const t=e.dataset.felteFieldset;return t?`${t}.${e.name}`:e.name}function h(e){for(const t of e.elements)(y(t)||v(t))&&(e.name&&t.name&&(t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${e.name}`:e.name),"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true"))}function b(e){return e.type.match(/^(number|range)$/)?e.value?+e.value:void 0:e.value}function g(e,t){if(a(e)&&"checkbox"===e.type){const n=t;return void 0===n||"boolean"==typeof n?void(e.checked=!!n):void(Array.isArray(n)&&(n.includes(e.value)?e.checked=!0:e.checked=!1))}if(a(e)&&"radio"===e.type){const n=t;e.value===n?e.checked=!0:e.checked=!1}else{if(a(e)&&"file"===e.type)return e.files=null,void(e.value="");e.value=String(t||"")}}function A(e,t){if(!l(e)&&!l(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]}}e._cloneDeep=o,e._defaultsDeep=function(...e){return i(...e,r)},e._get=u,e._isPlainObject=l,e._mapValues=n,e._merge=function(...e){return i(...e,(()=>{}))},e._mergeWith=i,e._set=c,e._some=t,e._unset=function(e,t){const n=t.split(".");let o=e;for(;n.length-1;){const e=n.shift();e&&(e in o||(o[e]={}),o=o[e])}return delete o[n[0]],e},e._update=f,e.addAttrsFromFieldset=h,e.deepSet=s,e.deepSome=function e(n,o){return t(n,(t=>l(t)?e(t,o):o(t)))},e.executeValidation=async function(e,t){if(!t)return;return Array.isArray(t)?i(...await Promise.all(t.map((t=>t(e)))),A):t(e)},e.getFormControls=function e(t){if(y(t))return[t];if(0===t.childElementCount)return[];const n=new Set;for(const o of t.children){if(y(o)&&n.add(o),v(o))for(const e of o.elements)y(e)&&n.add(e);o.childElementCount>0&&e(o).forEach((e=>n.add(e)))}return Array.from(n)},e.getFormDefaultValues=function(e){var t;const n={},o={};for(const i of e.elements){if(v(i)&&h(i),!y(i)||!i.name)continue;const r=p(i);if(c(o,r,!1),a(i)&&"checkbox"===i.type){if(void 0===u(n,r)){if(1===e.querySelectorAll(`[name="${i.name}"]`).length){c(n,r,i.checked);continue}c(n,r,i.checked?[i.value]:[]);continue}Array.isArray(u(n,r))&&i.checked&&f(n,r,(e=>[...e,i.value]));continue}if(a(i)&&"radio"===i.type){if(u(n,r))continue;c(n,r,i.checked?i.value:void 0);continue}if(a(i)&&"file"===i.type){c(n,r,i.multiple?Array.from(i.files||[]):null===(t=i.files)||void 0===t?void 0:t[0]);continue}c(n,r,b(i))}return{defaultData:n,defaultTouched:o}},e.getInputTextOrNumber=b,e.getPath=p,e.isElement=function(e){return e.nodeType===Node.ELEMENT_NODE},e.isFieldSetElement=v,e.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},e.isFormControl=y,e.isInputElement=a,e.isSelectElement=m,e.isTextAreaElement=d,e.setControlValue=g,e.setForm=function(e,t){for(const n of e.elements){if(v(n)&&h(n),!y(n)||!n.name)continue;g(n,u(t,p(n)))}},Object.defineProperty(e,"__esModule",{value:!0})}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Common={})}(this,(function(e){"use strict";function t(e,t){return Object.keys(e).some((n=>t(e[n])))}function n(e,t){return Object.keys(e).reduce(((n,o)=>Object.assign(Object.assign({},n),{[o]:t(e[o])})),{})}function o(e){return"[object Object]"===Object.prototype.toString.call(e)}function i(e){return Object.keys(e||{}).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:o(e[n])?i(e[n]):e[n]})),{})}function r(e,t){return n(e,(e=>o(e)?r(e,t):t))}function u(...e){const t=e.pop(),n=i(e.shift());if(0===e.length)return n;for(const c of e){if(!c)continue;const e=Object.keys(c);for(const f of e){const e=t(n[f],c[f]);if(void 0!==e)n[f]=e;else if(o(c[f])&&o(n[f]))n[f]=u(n[f],c[f],t);else if(o(c[f])){const e=r(i(c[f]),void 0);n[f]=u(e,c[f],t)}else void 0!==c[f]&&(n[f]=c[f])}}return n}function c(e,t){if(!o(e)||!o(t))return void 0!==e?e:void 0}function f(e,t,n){const o=t.split(".");let i=e;try{for(const e of o)i=i[e]}catch(e){return n}return void 0===i?n:i}function l(e,t,n){null!=e||(e={});const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n,e}function s(e,t,n){const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n(i[o[0]]),e}function a(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 v(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function y(e){return a(e)||d(e)||m(e)}function p(e){const t=e.dataset.felteFieldset;return t?`${t}.${e.name}`:e.name}function h(e){for(const t of e.elements)(y(t)||v(t))&&(e.name&&t.name&&(t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${e.name}`:e.name),"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true"))}function b(e){return e.type.match(/^(number|range)$/)?e.value?+e.value:void 0:e.value}function g(e,t){if(a(e)&&"checkbox"===e.type){const n=t;return void 0===n||"boolean"==typeof n?void(e.checked=!!n):void(Array.isArray(n)&&(n.includes(e.value)?e.checked=!0:e.checked=!1))}if(a(e)&&"radio"===e.type){const n=t;e.value===n?e.checked=!0:e.checked=!1}else{if(a(e)&&"file"===e.type)return e.files=null,void(e.value="");e.value=String(t||"")}}function A(e,t){if(!o(e)&&!o(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]}}e._cloneDeep=i,e._defaultsDeep=function(...e){return u(...e,c)},e._get=f,e._isPlainObject=o,e._mapValues=n,e._merge=function(...e){return u(...e,(()=>{}))},e._mergeWith=u,e._set=l,e._some=t,e._unset=function(e,t){const n=t.split(".");let o=e;for(;n.length-1;){const e=n.shift();e&&(e in o||(o[e]={}),o=o[e])}return delete o[n[0]],e},e._update=s,e.addAttrsFromFieldset=h,e.deepSet=r,e.deepSome=function e(n,i){return t(n,(t=>o(t)?e(t,i):i(t)))},e.executeValidation=async function(e,t){if(!t)return;return Array.isArray(t)?u(...await Promise.all(t.map((t=>t(e)))),A):t(e)},e.getFormControls=function e(t){if(y(t))return[t];if(0===t.childElementCount)return[];const n=new Set;for(const o of t.children){if(y(o)&&n.add(o),v(o))for(const e of o.elements)y(e)&&n.add(e);o.childElementCount>0&&e(o).forEach((e=>n.add(e)))}return Array.from(n)},e.getFormDefaultValues=function(e){var t;const n={},o={};for(const i of e.elements){if(v(i)&&h(i),!y(i)||!i.name)continue;const r=p(i);if(l(o,r,!1),a(i)&&"checkbox"===i.type){if(void 0===f(n,r)){if(1===e.querySelectorAll(`[name="${i.name}"]`).length){l(n,r,i.checked);continue}l(n,r,i.checked?[i.value]:[]);continue}Array.isArray(f(n,r))&&i.checked&&s(n,r,(e=>[...e,i.value]));continue}if(a(i)&&"radio"===i.type){if(f(n,r))continue;l(n,r,i.checked?i.value:void 0);continue}if(a(i)&&"file"===i.type){l(n,r,i.multiple?Array.from(i.files||[]):null===(t=i.files)||void 0===t?void 0:t[0]);continue}l(n,r,b(i))}return{defaultData:n,defaultTouched:o}},e.getInputTextOrNumber=b,e.getPath=p,e.isElement=function(e){return e.nodeType===Node.ELEMENT_NODE},e.isFieldSetElement=v,e.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},e.isFormControl=y,e.isInputElement=a,e.isSelectElement=m,e.isTextAreaElement=d,e.setControlValue=g,e.setForm=function(e,t){for(const n of e.elements){if(v(n)&&h(n),!y(n)||!n.name)continue;g(n,f(t,p(n)))}},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.js.map
{
"name": "@felte/common",
"version": "0.3.4",
"version": "0.3.5",
"description": "Common utilities for Felte packages",

@@ -10,4 +10,5 @@ "author": "Pablo Berganza <pablo@berganza.dev>",

"browser": "dist/index.js",
"module": "dist/index.mjs",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"files": [

@@ -35,3 +36,8 @@ "dist"

"dependencies": {},
"devDependencies": {}
"devDependencies": {},
"exports": {
".": "./dist/esm/index.js",
"./dist/utils/*": "./dist/esm/utils/*.js",
"./package.json": "./package.json"
}
}

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc