{"name":"@solid-primitives/immutable","version":"0.0.110","description":"Functional programming helpers for making non-mutating changes to data. Keeping it immutable.","author":{"name":"Damian Tarnawski @thetarnav","email":"gthetarnav@gmail.com"},"license":"MIT","homepage":"https://github.com/davedbase/solid-primitives/tree/main/packages/immutable#readme","repository":{"type":"git","url":"git+https://github.com/davedbase/solid-primitives.git"},"primitive":{"name":"immutable","stage":1,"list":["List of functions"],"category":"Utilities"},"private":false,"sideEffects":false,"type":"module","main":"dist/index.cjs","module":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsup","test":"uvu -r solid-register"},"keywords":["fp","immutable","solid","primitives"],"dependencies":{"@solid-primitives/utils":"^0.2.1"},"devDependencies":{"jsdom":"18.1.1","prettier":"^2.5.1","solid-register":"0.1.5","tslib":"^2.3.1","typescript":"^4.5.4","tsup":"^5.11.11","uvu":"^0.5.2","lodash.clonedeep":"^4.5.0","@types/lodash.clonedeep":"^4.5.6"},"licenseText":"MIT License\n\nCopyright (c) 2021 Solid Primitives Working Group\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","_id":"@solid-primitives/immutable@0.0.110","dist":{"shasum":"1c374be78f8cf0590e20f9a7e91320aff502612a","integrity":"sha512-nuUgdzyVJXL9DCn55sJAyYtGSgr+F4eFCNJEWgXQ8Y+WDm+L3jLP8b7rgeqTLc19VRf9k6czi8C13rpZ9CQv2w==","tarball":"http://registry-npm.ywwl.com/@solid-primitives/immutable/download/@solid-primitives/immutable-0.0.110.tgz","fileCount":8,"unpackedSize":31160,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJiCRvNCRA9TVsSAnZWagAA7z8P/25fZZOroObUsmdqmNju\nv4qQhb+V2mAM13rqlT/V2DYz0NHs+sAyr82UrgHJoAwm9GYFget47xuM9VZe\nQrD6R0dzMaIZLxyqpzTnArTIf3v1CwfNljc/d9fSiv5rTQ/rpdJUF8x1DXDX\ng1aLchRAJJq5bMsUWcGKihLYN+fkpm8MmhbuTvDKsnByF82Ji+BIVicFprmP\nnTLOiCvsIxiUHiOc1ZseH0/YNjc6fowojr0yrNZ4/1KsWUTe5R0UycLy86LD\ne9AyK8onWOMkneysgFt9mHJHjy+XWJNzV1ivV4iZEV0SsGnkb+lqiwwdnvNN\nON2JEJ7+hRr8LoRLaG7ReAU4hHFIsYo4/enioKF+TOuHz+QFs8otitsXsHSR\nMTjUpIbNdsK2JRsu8b66U++B0GDvViRAqg8IH+H/2TshnL0s/rjQ69l4MPVR\nYcNdqNng96qjL0OEuoVKy6SQNsAU+PwD9nwmCrJKUXMcg+70rL6h2MM++QMe\nhfNP6VP1XD2mfQbUBynWQCaCl12lLAWlFAxxY3AlHjodpH/zvwOjceVReVBG\nC/UDjFSrvIx0vaS96zfKcLzVTf6+QYhFHmgzIn62ui9IfvMD7GTqAgWcTBq3\nlcMBB3A73ZnJc1OIPAuWBFNdC2tt9TNopz/tJZcC4ZhLmRWGOw3KfG5VewCT\n7Nkb\r\n=U3fv\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAdAv1B1SD4bLWYT9LxVpg74LBpzxCeiXG1vqWTziX2zAiEA/FRxKvEcqEK694jlZ/msHPXZrvcMbYeMcmRN/tLGYzw="}],"size":7367,"noattachment":false,"key":"/@solid-primitives/immutable/-/@solid-primitives/immutable-0.0.110.tgz"},"_npmUser":{"name":"davedbase","email":"dave.dibiase@gmail.com"},"directories":{},"maintainers":[{"name":"lexlohr","email":"alex.th.kloss.77@googlemail.com"},{"name":"davedbase","email":"dave.dibiase@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/immutable_0.0.110_1644764109604_0.73011765006471"},"_hasShrinkwrap":false,"readme":"<p>\n  <img width=\"100%\" src=\"https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=immutable\" alt=\"Solid Primitives Immutable\">\n</p>\n\n# @solid-primitives/immutable\n\n[![turborepo](https://img.shields.io/badge/built%20with-turborepo-cc00ff.svg?style=for-the-badge&logo=turborepo)](https://turborepo.org/)\n[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/immutable?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/immutable)\n[![version](https://img.shields.io/npm/v/@solid-primitives/immutable?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/immutable)\n[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-1.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)\n\nPrimitive for rectifying immutable values and dealing with immutability in Solid.\n\n- [`createImmutable`](#createImmutable) - Creates a store derived from the given immutable source.\n\n## Installation\n\n```bash\nnpm install @solid-primitives/immutable\n# or\nyarn add @solid-primitives/immutable\n# or\npnpm add @solid-primitives/immutable\n```\n\n## `createImmutable`\n\nCreates a store _(deeply nested reactive object)_ derived from the given immutable source. The source can be any signal that is updated in an immutable fashion.\n\nIt's an **experimental** primitive, a proof of concept of derived nested reactivity. It's not meant to be used in production, but rather as a playground for experimenting with new ideas.\n\n### How to use it\n\n`createImmutable` is a function that takes a reactive function as the first param, and an optional configuration object as the second param:\n\n- `source` reactive function returning an immutable object\n- `options` optional configuration\n  - `key` property name to use as unique identifier for objects when their reference changes\n  - `merge` controls how objects witohut a unique identifier are identified when reconciling an array. If `true` the index is used, otherwise the object reference itself is used.\n\n```ts\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\n// source - can be any reactive function returning an immutable object\nconst [data, setData] = createSignal({ a: 1, b: 2 });\n\n// reactive state derived from the source\nconst state = createImmutable(data);\n\n// just like in Solid stores, the updates are fine-grained - only the changed values are updated\ncreateEffect(() => console.log(state.a, state.b));\n// logs 1 2\n\nsetData({ a: 2, b: 3 });\n// logs 2 3\n```\n\n### Usage with Redux Toolkit\n\nThere are many state management libraries that provide immutable data structures, such as [Immer](https://immerjs.github.io/immer/), [Redux Toolkit](https://redux-toolkit.js.org/), [XState](https://xstate.js.org/docs/), etc.\n\n`createImmutable` can help you turn them into reactive objects, only updating the changed values.\n\n> **Warning** `createStore` with `reconcile` will give you the similar result, while being more efficient.\n\n```tsx\nimport { createSlice, configureStore } from \"@reduxjs/toolkit\";\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\nconst slice = createSlice({\n  initialState: [\n    { id: 1, title: \"Learn Solid\", completed: false },\n    { id: 2, title: \"Learn Redux\", completed: false },\n  ],\n  reducers: {\n    /* ... (immutable actions) */\n  },\n});\n\nconst store = configureStore({\n  reducer: slice.reducer,\n});\n\nconst [source, setSource] = createSignal(store.getState());\nstore.subscribe(() => setSource(store.getState()));\n\nconst todos = createImmutable(source);\n\n// the references of todos will be preserved, even though they were destructured in the store\n<For each={todos}>\n  {todo => (\n    <div>\n      <input\n        type=\"checkbox\"\n        checked={todo.completed}\n        onClick={() => store.dispatch(slice.actions.toggleTodo(todo.id))}\n      />\n      {todo.title}\n    </div>\n  )}\n</For>;\n```\n\n### Usage with XState\n\n`createImmutable` doesn't mutate the source objects, as opposed to `createStore` with `reconcile`. This makes it a good fit for XState, which uses relies on diffing the previous and next state to determine the changes.\n\n```tsx\nimport { onCleanup, createSignal } from \"solid-js\";\nimport { createMachine, createActor } from \"xstate\";\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\nconst toggleMachine = createMachine({\n  id: \"toggle\",\n  initial: \"inactive\",\n  states: {\n    inactive: {\n      on: { TOGGLE: \"active\" },\n    },\n    active: {\n      on: { TOGGLE: \"inactive\" },\n    },\n  },\n});\n\nexport const Toggler = () => {\n  const actor = x.createActor(toggleMachine).start();\n  onCleanup(() => actor.stop());\n\n  const [snapshot, setSnapshot] = createSignal(actor.getSnapshot());\n  actor.subscribe(setSnapshot);\n\n  const state = createImmutable(snapshot);\n\n  return (\n    <button onclick={() => actor.send({ type: \"TOGGLE\" })}>\n      {state.value === \"inactive\" ? \"Click to activate\" : \"Active! Click to deactivate\"}\n    </button>\n  );\n};\n```\n\n### Usage with `createResource`\n\nData fetched from the server is immutable, so `createImmutable` can help you turn it into a reactive object, only updating the changed values.\n\n> **Warning** `createResource` provides an experimental `storage` option that can be used together with `createStore` and `reconcile` to achieve the similar result, while being more efficient\n> https://www.solidjs.com/docs/latest/api#createresource\n\n```ts\nimport { createResource } from \"solid-js\";\nimport { createImmutable } from \"@solid-primitives/immutable\";\n\nconst [data, { refetch }] = createResource(() =>\n  fetch(\"https://jsonplaceholder.typicode.com/todos/1\").then(res => res.json()),\n);\n\nconst state = createImmutable(data);\n\ncreateEffect(() => console.log(state.title, state.completed));\n\n// newely fetched data will be merged with the previous state\nrefetch();\n```\n\n## Demo\n\nYou can see the live demo [here](https://primitives.solidjs.community/playground/immutable).\n\n[Source code](./dev/index.tsx)\n\n## Changelog\n\nSee [CHANGELOG.md](./CHANGELOG.md)\n","publish_time":1644764109742,"_cnpm_publish_time":1644764109742,"dist-tags":{"beta":"0.1.8-beta.0","latest":"1.1.5","next":"2.0.0-next.0"}}