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

jotai-optics

Package Overview
Dependencies
Maintainers
3
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jotai-optics - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

4

CHANGELOG.md

@@ -5,2 +5,6 @@ # Change Log

## [0.3.0] - 2023-01-31
### Added
- Migrate to Jotai v2 API #4
## [0.2.1] - 2023-01-29

@@ -7,0 +11,0 @@ ### Changed

2

dist/index.modern.js

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

import{atom as t}from"jotai";import*as e from"optics-ts";const o=(t,e,o)=>(e.has(o)?e:e.set(o,t())).get(o),r=new WeakMap;function n(n,a){return((c,i,p)=>{const f=o(()=>new WeakMap,r,i);return o(()=>{const o=a(e.optic());return t(t=>s(o,t(n)),(t,r,s)=>{const a="function"==typeof s?e.modify(o)(s):e.set(o)(s);return r(n,a(t(n)))})},f,p)})(0,n,a)}const s=(t,o)=>"Traversal"===t._tag?e.collect(t)(o):"Prism"===t._tag?e.preview(t)(o):e.get(t)(o);export{n as focusAtom};
import{atom as t}from"jotai/vanilla";import*as e from"optics-ts";const n=(t,e,n)=>(e.has(n)?e:e.set(n,t())).get(n),o=new WeakMap;function r(r,a){return((i,c,p)=>{const f=n(()=>new WeakMap,o,c);return n(()=>{const n=a(e.optic());return t(t=>{const e=t(r);return e instanceof Promise?e.then(t=>s(n,t)):s(n,e)},(t,o,s)=>{const a="function"==typeof s?e.modify(n)(s):e.set(n)(s),i=t(r);return o(r,i instanceof Promise?i.then(a):a(i))})},f,p)})(0,r,a)}const s=(t,n)=>"Traversal"===t._tag?e.collect(t)(n):"Prism"===t._tag?e.preview(t)(n):e.get(t)(n);export{r as focusAtom};
//# sourceMappingURL=index.modern.mjs.map

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

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai"),require("optics-ts")):"function"==typeof define&&define.amd?define(["exports","jotai","optics-ts"],t):t((e||self).jotaiOptics={},e.jotai,e.opticsTs)}(this,function(e,t,n){function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,t}var o=/*#__PURE__*/r(n),i=function(e,t,n){return(t.has(n)?t:t.set(n,e())).get(n)},u=new WeakMap;e.focusAtom=function(e,n){return r=function(){var r=n(o.optic());return t.atom(function(t){return function(e,t){return"Traversal"===e._tag?o.collect(e)(t):"Prism"===e._tag?o.preview(e)(t):o.get(e)(t)}(r,t(e))},function(t,n,i){var u="function"==typeof i?o.modify(r)(i):o.set(r)(i);return n(e,u(t(e)))})},f=n,c=i(function(){return new WeakMap},u,e),i(r,c,f);var r,f,c}});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("jotai/vanilla"),require("optics-ts")):"function"==typeof define&&define.amd?define(["exports","jotai/vanilla","optics-ts"],t):t((e||self).jotaiOptics={},e.vanilla,e.opticsTs)}(this,function(e,t,n){function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,t}var o=/*#__PURE__*/r(n),i=function(e,t,n){return(t.has(n)?t:t.set(n,e())).get(n)},u=new WeakMap,a=function(e,t){return"Traversal"===e._tag?o.collect(e)(t):"Prism"===e._tag?o.preview(e)(t):o.get(e)(t)};e.focusAtom=function(e,n){return r=function(){var r=n(o.optic());return t.atom(function(t){var n=t(e);return n instanceof Promise?n.then(function(e){return a(r,e)}):a(r,n)},function(t,n,i){var u="function"==typeof i?o.modify(r)(i):o.set(r)(i),a=t(e);return n(e,a instanceof Promise?a.then(u):u(a))})},f=n,c=i(function(){return new WeakMap},u,e),i(r,c,f);var r,f,c}});
//# sourceMappingURL=index.umd.js.map

@@ -1,16 +0,16 @@

import type { SetStateAction, WritableAtom } from 'jotai';
import type { SetStateAction, WritableAtom } from 'jotai/vanilla';
import * as O from 'optics-ts';
type NonFunction<T> = [T] extends [(...args: any[]) => any] ? never : T;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S, NonFunction<S>, R>, callback: (optic: O.OpticFor<S>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>): WritableAtom<A | undefined, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>): WritableAtom<A[], SetStateAction<A>, R>;
export declare function focusAtom<S, A, R extends void | Promise<void>>(baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>, callback: (optic: O.OpticFor<S | undefined>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, SetStateAction<A>, R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<Promise<A | undefined>, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<Promise<A[]>, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>, callback: (optic: O.OpticFor<S>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<Promise<A>, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>, callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>): WritableAtom<Promise<A | undefined>, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>, callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>): WritableAtom<Promise<A[]>, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>, callback: (optic: O.OpticFor<S | undefined>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<Promise<A>, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<S, [NonFunction<S>], R>, callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>): WritableAtom<A | undefined, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<S, [NonFunction<S>], R>, callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>): WritableAtom<A[], [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<S, [NonFunction<S>], R>, callback: (optic: O.OpticFor<S>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>, callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>): WritableAtom<A | undefined, [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>, callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>): WritableAtom<A[], [SetStateAction<A>], R>;
export declare function focusAtom<S, A, R>(baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>, callback: (optic: O.OpticFor<S | undefined>) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>): WritableAtom<A, [SetStateAction<A>], R>;
export {};
{
"name": "jotai-optics",
"description": "👻💎",
"version": "0.2.1",
"version": "0.3.0",
"author": "merisbahti",

@@ -12,3 +12,3 @@ "contributors": [

"type": "git",
"url": "https://github.com/jotai-labs/jotai-optics.git"
"url": "https://github.com/jotaijs/jotai-optics.git"
},

@@ -59,4 +59,4 @@ "source": "./src/index.ts",

"@types/react-dom": "^18.0.10",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"eslint": "^8.33.0",

@@ -68,3 +68,3 @@ "eslint-config-prettier": "^8.6.0",

"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",

@@ -75,3 +75,3 @@ "expect-type": "^0.15.0",

"jest-environment-jsdom": "^29.4.1",
"jotai": "^1.13.1",
"jotai": "^2.0.0",
"microbundle": "^0.15.1",

@@ -86,3 +86,3 @@ "npm-run-all": "^4.1.5",

"ts-loader": "^9.4.2",
"typescript": "^4.9.4",
"typescript": "^4.9.5",
"webpack": "^5.75.0",

@@ -93,5 +93,5 @@ "webpack-cli": "^5.0.1",

"peerDependencies": {
"jotai": "*",
"jotai": ">=1.11.0",
"optics-ts": "*"
}
}

@@ -1,3 +0,3 @@

import { atom } from 'jotai'
import type { SetStateAction, WritableAtom } from 'jotai'
import { atom } from 'jotai/vanilla'
import type { SetStateAction, WritableAtom } from 'jotai/vanilla'
import * as O from 'optics-ts'

@@ -20,80 +20,80 @@

export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>
): WritableAtom<A | undefined, SetStateAction<A>, R>
): WritableAtom<Promise<A | undefined>, [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>
): WritableAtom<A[], SetStateAction<A>, R>
): WritableAtom<Promise<A[]>, [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<Promise<S>, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S>, [Promise<S>], R>,
callback: (
optic: O.OpticFor<S>
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>
): WritableAtom<A, SetStateAction<A>, R>
): WritableAtom<Promise<A>, [SetStateAction<A>], R>
// Pattern 2: Promise with undefined type
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>
): WritableAtom<A | undefined, SetStateAction<A>, R>
): WritableAtom<Promise<A | undefined>, [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>
): WritableAtom<A[], SetStateAction<A>, R>
): WritableAtom<Promise<A[]>, [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<Promise<S | undefined>, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<Promise<S | undefined>, [Promise<S>], R>,
callback: (
optic: O.OpticFor<S | undefined>
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>
): WritableAtom<A, SetStateAction<A>, R>
): WritableAtom<Promise<A>, [SetStateAction<A>], R>
// Pattern 3: Default
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (optic: O.OpticFor<S>) => O.Prism<S, any, A>
): WritableAtom<A | undefined, SetStateAction<A>, R>
): WritableAtom<A | undefined, [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (optic: O.OpticFor<S>) => O.Traversal<S, any, A>
): WritableAtom<A[], SetStateAction<A>, R>
): WritableAtom<A[], [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (
optic: O.OpticFor<S>
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>
): WritableAtom<A, SetStateAction<A>, R>
): WritableAtom<A, [SetStateAction<A>], R>
// Pattern 4: Default with undefined type
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (optic: O.OpticFor<S | undefined>) => O.Prism<S, any, A>
): WritableAtom<A | undefined, SetStateAction<A>, R>
): WritableAtom<A | undefined, [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (optic: O.OpticFor<S | undefined>) => O.Traversal<S, any, A>
): WritableAtom<A[], SetStateAction<A>, R>
): WritableAtom<A[], [SetStateAction<A>], R>
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S | undefined, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S | undefined, [NonFunction<S>], R>,
callback: (
optic: O.OpticFor<S | undefined>
) => O.Lens<S, any, A> | O.Equivalence<S, any, A> | O.Iso<S, any, A>
): WritableAtom<A, SetStateAction<A>, R>
): WritableAtom<A, [SetStateAction<A>], R>
// Implementation
export function focusAtom<S, A, R extends void | Promise<void>>(
baseAtom: WritableAtom<S, NonFunction<S>, R>,
export function focusAtom<S, A, R>(
baseAtom: WritableAtom<S, [NonFunction<S>], R>,
callback: (

@@ -112,3 +112,8 @@ optic: O.OpticFor<S>

const derivedAtom = atom(
(get) => getValueUsingOptic(focus, get(baseAtom)),
(get) => {
const base = get(baseAtom)
return base instanceof Promise
? base.then((v) => getValueUsingOptic(focus, v))
: getValueUsingOptic(focus, base)
},
(get, set, update: SetStateAction<A>) => {

@@ -118,5 +123,8 @@ const newValueProducer = isFunction(update)

: O.set(focus)(update)
const base = get(baseAtom)
return set(
baseAtom,
newValueProducer(get(baseAtom)) as NonFunction<S>
(base instanceof Promise
? base.then(newValueProducer)
: newValueProducer(base)) as NonFunction<S>
)

@@ -123,0 +131,0 @@ }

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

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