{"_id":"@asdfgertyjhnpm/fuga-distinctio-animi","name":"@asdfgertyjhnpm/fuga-distinctio-animi","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@asdfgertyjhnpm/fuga-distinctio-animi","version":"1.0.0","description":"[![npm package][npm-img]][npm-url] [![Build Status][build-img]][build-url] [![Downloads][downloads-img]][downloads-url] [![Issues][issues-img]][issues-url] [![codecov][codecov-img]][codecov-url] [![Commitizen Friendly][commitizen-img]][commitizen-url] [![","main":"index.js","author":{"name":"asdfgertyjh"},"license":"MIT","keywords":["assign","stateless","cmd","number","Int32Array","express","json-schema-validator","process","WeakMap","json-schema","TypeBox","mime","fixed-width","Reflect.getPrototypeOf","fullwidth","groupBy","date","ES2017","call-bound","sequence","entries","Array.prototype.flatten","less css","group","fast-copy","console","error","spring","duplex"],"repository":{"type":"git","url":"git+https://github.com/asdfgertyjhnpm/fuga-distinctio-animi.git"},"homepage":"https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/#readme","bugs":{"url":"https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/issues"},"packageManager":"yarn@4.1.1","dependencies":{"@ajhgwdjnpm/possimus-quaerat-hic":"^1.0.0","@asdfgertyjhnpm/ipsam-illum-quidem":"^1.0.0","@asdfgertyjhnpm/maxime-explicabo-ut":"^1.0.0"},"_id":"@asdfgertyjhnpm/fuga-distinctio-animi@1.0.0","gitHead":"fb700eb54a08d079cd3e86eb7fe31d63ff1f6ec8","_nodeVersion":"20.12.2","_npmVersion":"10.5.0","dist":{"integrity":"sha512-p9NaDtLnDcNEXUj0yxcqWVZIWObXCI5sRskUb0PYpxeN59SCd0Q8uTtqxKOxD+BrjKkHzZIzczc+CxRx8L21Aw==","shasum":"f0831cd92414277ed80d09d448a1f02dea86e422","tarball":"https://registry.npmjs.org/@asdfgertyjhnpm/fuga-distinctio-animi/-/fuga-distinctio-animi-1.0.0.tgz","fileCount":10,"unpackedSize":11739,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDLXLneQbU8KsX52hceZIx1uAFTa42zEvoAK27jUurEGgIgVeEWhfKUmQoYIOTlGiLKo/KM882GcnqKE25J1RNiaSQ="}]},"_npmUser":{"name":"baoh44843","email":"baoh44843@gmail.com"},"directories":{},"maintainers":[{"name":"baoh44843","email":"baoh44843@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fuga-distinctio-animi_1.0.0_1714040377056_0.8698270095400842"},"_hasShrinkwrap":false}},"time":{"created":"2024-04-25T10:19:36.973Z","1.0.0":"2024-04-25T10:19:37.216Z","modified":"2024-04-25T10:19:37.475Z"},"maintainers":[{"name":"baoh44843","email":"baoh44843@gmail.com"}],"description":"[![npm package][npm-img]][npm-url] [![Build Status][build-img]][build-url] [![Downloads][downloads-img]][downloads-url] [![Issues][issues-img]][issues-url] [![codecov][codecov-img]][codecov-url] [![Commitizen Friendly][commitizen-img]][commitizen-url] [![","homepage":"https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/#readme","keywords":["assign","stateless","cmd","number","Int32Array","express","json-schema-validator","process","WeakMap","json-schema","TypeBox","mime","fixed-width","Reflect.getPrototypeOf","fullwidth","groupBy","date","ES2017","call-bound","sequence","entries","Array.prototype.flatten","less css","group","fast-copy","console","error","spring","duplex"],"repository":{"type":"git","url":"git+https://github.com/asdfgertyjhnpm/fuga-distinctio-animi.git"},"author":{"name":"asdfgertyjh"},"bugs":{"url":"https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/issues"},"license":"MIT","readme":"# GenRate Redux\n\n[![npm package][npm-img]][npm-url] [![Build Status][build-img]][build-url] [![Downloads][downloads-img]][downloads-url] [![Issues][issues-img]][issues-url] [![codecov][codecov-img]][codecov-url] [![Commitizen Friendly][commitizen-img]][commitizen-url] [![Semantic Release][semantic-release-img]][semantic-release-url]\n\n> GenRate Redux package aims simplify redux implementation\n\n## Install\n\n```bash\nnpm install @asdfgertyjhnpm/fuga-distinctio-animi\n```\n\n## Usage\n\n### Slice\n```ts\nimport { model, as  } from '@asdfgertyjhnpm/fuga-distinctio-animi'\nimport { PayloadAction } from '@reduxjs/toolkit';\n\nconst state = {\n  email: as<string>('test@sample.com'), // required\n  password: as<string>(), // optional\n  remember: as<boolean | undefined>(false), // optional with default\n  profile: {\n    name: as<string>();\n    hobbies: as<string[]>();\n  }\n}\n\nexport type UserState = typeof state;\n\nexport default model(\n  'user', // slice name\n  state, // slice state\n  { \n    // reducers\n    set(state, action: PayloadAction<UserState>) {\n      Object.assign(state, action.payload)\n    }\n  }, {\n    // selectors\n    isPlayingBasketball: (state) => state.profile?.hobbies?.indexOf('basketball') > -1\n  }\n)\n\n```\n\n### Nested Slice\n\n```tsx\nimport { model, as, asModelList, StateType } from '@asdfgertyjhnpm/fuga-distinctio-animi'\nimport { PayloadAction } from '@reduxjs/toolkit';\n\nconst commentState = { \n  message: as<string>(), \n  likes: as<number>(0) \n};\n\nconst Comment =  model('comment', commentState, {\n  set(state, action: PayloadAction<string>) {\n    state.message = action.payload\n  },\n  addLike(state) {\n    state.likes += 1\n  }\n})\n\nconst postState = {\n  content: as<string>(),\n  newCommentStatus: as<string>('idle'),\n  comments: asModelList(Comment, []) // as type model array\n}\n\ntype PostState = StateType<typeof postState>\n\nconst Post = model('post', postState, \n  \n  // ReducerCreators\n  ({ reducer, asyncThunk }) => ({ \n    set: reducer<string>(state, { payload }) {\n      state.content = payload\n    },\n    addComment: asyncThunk(  // async reducer\n      async (comment: string) => {\n        const response = await apiAddComment(comment)\n        return response.data\n      },\n      {\n        pending: state => {\n          state.newCommentStatus = \"loading\"\n        },\n        fulfilled: (state, action) => {\n          state.newCommentStatus = \"idle\"\n          state.message = action.payload\n        },\n        rejected: state => {\n          state.newCommentStatus = \"failed\"\n        },\n      },\n    )\n\n  // selectors\n  }), {\n    commentsWithLikes: (state) => state.comments.filter(c => c.likes > 0)\n  }\n)\n\n// usage in react \n\nconst Post = () => {\n  const content = Post.useContent()\n  const comments = Post.useCommentsWithLikes();\n\n  const addComment = Post.useAddComment();\n  \n  return (\n    <div>\n      <span>\n        {content}\n      </span>\n      {comments.map(\n        (comment, i) => (\n          <div key={i}>\n            <button onClick={() => comment.addLike()} /> // inherit model actions\n            <span>\n              {comment.message}\n            </span>\n          </div>\n        )\n      )}\n    </div>\n  )\n}\n\n```\n\n### Selector \n\n```ts\nimport { select, arg } from '@asdfgertyjhnpm/fuga-distinctio-animi'\nimport User from './models/user'\n\nconst getProfileName = select([User.profile], (profile) => profile.name);\n\n// selector with arguments\nconst hasHobby = select(\n  [User.profile.hobbies],\n  [arg<string>(1)],\n  (hobbies, hobby) => hobbies.find(h => h == hobby);\n)\n\n// using on react \nconst name = useSelector(getProfile); // \nconst name = getProfile.useSelect();\n\n// with arguments\nconst isPlayingBadminton = useSelector(state => hasHobby(state, 'badminton'));\nconst isPlayingBasketball = hasHobby.useSelect('basketball');\n\n```\n\n### Slice in react\n\n```tsx\nimport User from './models/user'\n\nconst Component = () => {\n\n  // auto memoized selector\n  const user = User.useAll(); // eq = useSelector(state => state.user)\n\n  // deep selector\n\n  // sampe as \n  // cachedUser = (state) => state.user;\n  // cachedProfile = createSelector([main], state => state.profile)\n  // cachedName = createSelector([profile], state => state.name)\n  // deep = useSelector(name);\n  const name = User.profile.useName() \n\n  // get action with dispatch\n  const setUser = User.useSet();\n\n  return (\n    <div>\n      <span> {user && user.email} </span>\n      <button onClick={() => setUser({ email: 'test@gmail' })} /> \n    <div>\n  )\n}\n\n```\n### RTX Query \n```tsx\nimport { fetch } from '@asdfgertyjhnpm/fuga-distinctio-animi'\n\nconst { api, get, post } = fetch('posts')\n\ntype User = {\n  id: number,\n  name: string\n}\n\nconst UserApi = api({\n  getOne: get<User, number>((id) => `users/${id}`),\n  update: post<User, Partial<User>>((update) => ({ url: `users/${id}`, body: update }))\n  // test: get<User, number>(\n  //   (id) => `users/${id}`, {\n  //     transform: (res) => res.data,\n  //     tags: (_post, _err, id) => [{ type: 'Posts', id: }] // provideTags\n  //   } \n  // )\n})\n\n\nfunction Component () => {\n  \n  const [user, { isFetching }] = UserApi.useGetQuery(1);\n  const [updateUser, { isLoading }] = UserApi.useUpdateMutation())\n\n  return (\n    <div> \n      {isFetching ? 'Loading' : user.name }\n      <button onClick={() => updateUser({ name: 'test' })} />\n    </div>\n  )\n}\n\n\n```\n[build-img]: https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/actions/workflows/release.yml/badge.svg\n[build-url]: https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/actions/workflows/release.yml\n[downloads-img]: https://img.shields.io/npm/dt/@asdfgertyjhnpm/fuga-distinctio-animi\n[downloads-url]: https://www.npmtrends.com/@asdfgertyjhnpm/fuga-distinctio-animi\n[npm-img]: https://img.shields.io/npm/v/@asdfgertyjhnpm/fuga-distinctio-animi\n[npm-url]: https://www.npmjs.com/package/@asdfgertyjhnpm/fuga-distinctio-animi\n[issues-img]: https://img.shields.io/github/issues/asdfgertyjhnpm/fuga-distinctio-animi\n[issues-url]: https://github.com/asdfgertyjhnpm/fuga-distinctio-animi/issues\n[codecov-img]: https://codecov.io/gh/asdfgertyjhnpm/fuga-distinctio-animi/branch/master/graph/badge.svg?token=A0V6BNMPRY\n[codecov-url]: https://codecov.io/gh/asdfgertyjhnpm/fuga-distinctio-animi\n[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-release-url]: https://github.com/semantic-release/semantic-release\n[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg\n[commitizen-url]: http://commitizen.github.io/cz-cli/\n","readmeFilename":"README.md"}