{"_id":"@aenlo/move-element-virtual","_rev":"2-99432f81eb1a7a40ba2cf91e5c381a55","name":"@aenlo/move-element-virtual","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@aenlo/move-element-virtual","version":"1.0.0","description":"A simple package (with minimal dependencies) for moving elements within an array","main":"index.js","scripts":{"test":"jest","clean":"rimraf dist","prebuild":"npm run clean","build":"tsc -p tsconfig-build.json"},"author":"","license":"ISC","devDependencies":{"@types/jest":"^27.5.0","@types/lodash.clonedeep":"^4.5.7","jest":"^28.0.3","rimraf":"^3.0.2","ts-jest":"^28.0.1","typescript":"^4.6.4"},"dependencies":{"lodash.clonedeep":"^4.5.0"},"_id":"@aenlo/move-element-virtual@1.0.0","_nodeVersion":"16.13.0","_npmVersion":"8.1.0","dist":{"integrity":"sha512-aInC4NBvQnmWiUBwdtpx6g2WUdzyWRB6PdBg8hB1pGRFesgZgCoIrw9Fnl3FR0VS4VoqDyTColnXI/LTIfEXeQ==","shasum":"db68362901a98cfbe74d4067b2eab5b57c9594e9","tarball":"https://registry.npmjs.org/@aenlo/move-element-virtual/-/move-element-virtual-1.0.0.tgz","fileCount":10,"unpackedSize":49419,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD1RDIFhh7Pg1BOz4U6xxQMCyNLnzsFIPGy7QWHKft15gIgRwbFMKrtHAHk/APFyQ4vHOZSBysm4SdnMglguHQHlf8="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjQgKEACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqFMhAAhqDrHfHwcCFEHOUDSZBCb+v3MOv40njH4QhdPU5V9S6ftEnB\r\nC+rnCLcZWue3OtftMB0xQjX5RcoVzrDODivQ0fYh3OKWSukGpcBuc1wXWIv/\r\nQrIRM3yAjFqgAVhIAkg8T4OzDgtAsq9bLvT6qSLggH2w0WBaI/lqCGID9pDL\r\nxSt4yrDonZHEWMbjytLGYzuhn7taOW8ai9vzxA7FP3gnH3yHjcPY8Rir51BY\r\ng0+os3I6XTzjHHoeAOnscYvSPM3PsNqwjHVxhxmcuYRbDX9Zoz82b6oAKu+P\r\nMLtliuL/Gx9uBvd4YMEv3xTRD09rqOZyjZm4KulezFH4psXdyqUdYoOwiIis\r\nuu0XB+5kV5A1FZbKPByPpcO1gJUPNAGDmX63HV6LJNi6SQXCK3YbmW7KstxJ\r\ny8qwy75yyg0D213X298nFdD2Nu1PAsFUxVLt/tysmmGe1Qfr0pCUolpNe5Sa\r\n/C8HMfvEUEPMrZ5oMwcS+S8XZegSfDcxgngbOaizyZddS54rXsYqmDSuEq4Z\r\nosbg6ScEkNmg7H4TM4gwBE01b40ThiEy/xQ+C9SXdeG1enULqoY1NPGfJZn6\r\nB5Szkj1iA5ZGlanDvfOpTWzUdtpgg4UNwyCy2s4yxy95iPjOZzXiXGZdIyrP\r\nYCLbBl12h2ZQul1A5YYi0vT4IrI7emsbTbY=\r\n=8wZ9\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"aenlo","email":"itsjusttoast@protonmail.com"},"directories":{},"maintainers":[{"name":"aenlo","email":"itsjusttoast@protonmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/move-element-virtual_1.0.0_1665270404739_0.017076236322943972"},"_hasShrinkwrap":false,"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."},"1.0.1":{"name":"@aenlo/move-element-virtual","version":"1.0.1","description":"A simple package (with minimal dependencies) for moving elements within an array","keywords":["array","move","element","virtual","splice","pagination","position","mutable","immutable"],"main":"index.js","scripts":{"test":"jest","clean":"rimraf dist","prebuild":"npm run clean","build":"tsc -p tsconfig-build.json"},"author":"","license":"ISC","devDependencies":{"@types/jest":"^27.5.0","@types/lodash.clonedeep":"^4.5.7","jest":"^28.0.3","rimraf":"^3.0.2","ts-jest":"^28.0.1","typescript":"^4.6.4"},"dependencies":{"lodash.clonedeep":"^4.5.0"},"_id":"@aenlo/move-element-virtual@1.0.1","_nodeVersion":"16.13.0","_npmVersion":"8.1.0","dist":{"integrity":"sha512-avPklsC7FiY+7Du+sZnXGv/672y8bIEoByPW2IC13wI+7sUIovt4NLABD6Ax8k3mmhDR1mwvQ+1pQBaVwDgz9g==","shasum":"03af026297b7ef435d495d12e49325abc90f73d7","tarball":"https://registry.npmjs.org/@aenlo/move-element-virtual/-/move-element-virtual-1.0.1.tgz","fileCount":10,"unpackedSize":49534,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICRi5ED2Z4dI/9jzEvbC0NrmV8awFi0eMh1tI0kLgP/iAiARUgYgZu+GVBJbX0haJI7aRoxLv95Sa6LTatV7lnGDYg=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjQuNLACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoxvA/+KwcteRzjyYY+wQ3IxRp93G8TVdc07bZvD9tVD6g2eXYGMmLv\r\n9cCKy8CcdkCADD/VKG/JQZJlvW3bXAF/+5Gx9zanCaLt9ia650v3pnB1E5P5\r\nkLtjlWN+zPQKbYxll66CGRAtjPK4taLNhrOz0Dve/JjMSG0rw8sazd7L58OR\r\n+QkrVOX2ww1VL8GVXPBGGzrSI19DxAcISqp4ARihnk9zNQYxdW/+KrlIvr8G\r\njE3CbMiFxTE7tniDBxfCzwS+WemRVozXW88lfYPodYvXogTSewGUYff1Y2UR\r\nEABXHnPScx7Ig6FXkoL8FpDr8p+dr0sPLccOL2+/Oj8KpENPmOGXszoWFCeg\r\nHbCwL7jYsYwNmp3aJMMvQG7li5HLztlEGlPq5wWaTJF4U/U1afOmbLytoDej\r\nLFgrJDOrMHwP4qAthrzKPio+1HOPuE8KU1ArylL2jyxiDixsN1/hDKXAj4YX\r\nzxshcHJRmHLcnCw6icIXAF9X2J4mNj4Y3wcBsm1t713Xp2GIvaNyTthXrqE2\r\nHX9S/77W4ydqjS6HUszn6UeJO1VAMyKULshrFO9rGNBifpJcrVosnFAUhPf2\r\n66ZlUQ61YqcV44uIZODJrx6oG5Elwg3bPyXcXjKylKcj0iJ8TpxQU3z+6GZe\r\np2rl0ZJoiQFj9uIvj32U0pxuF/TUReU0m84=\r\n=d7sC\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"aenlo","email":"itsjusttoast@protonmail.com"},"directories":{},"maintainers":[{"name":"aenlo","email":"itsjusttoast@protonmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/move-element-virtual_1.0.1_1665327946956_0.23199521507694376"},"_hasShrinkwrap":false,"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info."}},"time":{"created":"2022-10-08T23:06:44.680Z","1.0.0":"2022-10-08T23:06:44.923Z","modified":"2023-05-05T01:55:35.573Z","1.0.1":"2022-10-09T15:05:47.181Z"},"maintainers":[{"name":"aenlo","email":"itsjusttoast@protonmail.com"}],"description":"A simple package (with minimal dependencies) for moving elements within an array","license":"ISC","readme":"# @aenlo/move-element-virtual\n\nA simple package (with minimal dependencies) for moving an element within an array. This can be done literally OR virtually, and is about making sure the rest of the elements in the list are arranged, based on that movement. `moveElement()` is for simple movement and uses `Array.splice()`, where `moveElementVirtual()` and `moveElementVirtualImmutable()` are for 'virtual' movement by adjusting the origin element as well as all other elements in the list to account for that virtual movement. Note: this is not swapping elements. This is moving one element from an origin position to a destination position, and adjusting the other elements above/below that point of destination, accordingly. See below for examples.\n\nI'm using the word 'virtual' in terms of movement to mean that you aren't necessarily relying on actually moving the elements in the array, but are relying on some other property within your objects to do your ordering.\n\nFor example, let's say you are interacting with an array of data whose meaningful order isn't tied to the actual array order (that is to say, maybe it is hinging on a property value of the data that is returned, such as `[{ position: 2, data: { ... } }, { position: 1, data: { ... } }]`). If you need to 'move' an element within that data, you may need to do so virtually and keep the other elements' corresponding property updated, as well. If so, this package may be helpful.\n\n## Installation\n\nUsing npm:\n\n```\nnpm install @aenlo/move-element-virtual\n```\n\n## Usage\n\nCalling `moveElement()` will simply move the element using `Array.splice()` and the array is adjusted accordingly.\n\n```typescript\nimport { moveElement } from \"@aenlo/move-element-virtual\";\n\nconst list = [\"A\", \"B\", \"C\", \"D\"];\nmoveElement(list, 0, 2);\nconsole.log(list); // [\"B\", \"C\", \"A\", \"D\"] <- \"A\" is moved to index 2\n```\n\nCalling `moveElementImmutable()` will still replace the element using `splice()`, but does not mutate the data and immediately creates a deep copy (using \\_.cloneDeep).\n\n```typescript\nimport { moveElement } from \"@aenlo/move-element-virtual\";\n\nconst list = [\"A\", \"B\", \"C\", \"D\"];\nconst newList = moveElementImmutable(list, 0, 2);\nconsole.log(list); // [\"A\", \"B\", \"C\", \"D\"] <- stays the same\nconsole.log(newList); // [\"B\", \"C\", \"A\", \"D\"] <- \"A\" is moved to index 2\n```\n\nCalling `moveElementVirtual()` allows you to adjust the elements based on a property (including nested properties). It assumes a 0-indexed values for the order numbers, but you can pass in an optional flag to use 1-indexed values. Mutates the original array and cannot be sorted (...just yet).\n\n```javascript\nimport { moveElementVirtual } from \"@aenlo/move-element-virtual\";\n\nconst list = [\n  {\n    data: { position: 0 },\n    otherData:\n      \"Originally position 0, but will change the position property to 2\",\n  },\n  {\n    data: { position: 1 },\n    otherData:\n      \"Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2\",\n  },\n  {\n    data: { position: 2 },\n    otherData:\n      \"Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2\",\n  },\n];\n\nconst nestedPropertyPath = [\"data\", \"position\"];\nconst isZeroIndexed = true;\nmoveElementVirtual(list, 0, 2, nestedPropertyPath, isZeroIndexed);\nconsole.log(list)\n/**\n  {\n    data: { position: 2 },\n    otherData:\n      \"Originally position 0, but will change the position property to 2\",\n  },\n  {\n    data: { position: 0 },\n    otherData:\n      \"Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2\",\n  },\n  {\n    data: { position: 1 },\n    otherData:\n      \"Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2\",\n  },\n * /\n```\n\nCalling `moveElementVirtualImmutable()` is similar, but it does not mutate the data and immediately creates a deep copy (using \\_.cloneDeep). You can optionally pass in a boolean flag to sort the array, if desired.\n\n```javascript\nimport { moveElementVirtual } from \"@aenlo/move-element-virtual\";\n\nconst list = [\n  {\n    data: { position: 0 },\n    otherData:\n      \"Originally position 0, but will change the position property to 2\",\n  },\n  {\n    data: { position: 1 },\n    otherData:\n      \"Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2\",\n  },\n  {\n    data: { position: 2 },\n    otherData:\n      \"Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2\",\n  },\n];\n\nconst nestedPropertyPath = [\"data\", \"position\"];\nconst isZeroIndexed = true;\nconst shouldSort = true;\nconst result = moveElementVirtualImmutable(list, 0, 2, nestedPropertyPath, isZeroIndexed, shouldSort);\n/**\n  {\n    data: { position: 0 },\n    otherData:\n      \"Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2\",\n  },\n  {\n    data: { position: 1 },\n    otherData:\n      \"Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2\",\n  },\n  {\n    data: { position: 2 },\n    otherData:\n      \"Originally position 0, but will change the position property to 2\",\n  },\n * /\n```\n\n## Additional Details\n\n### Method: `moveElement()`\n\n- Input:\n  ```typescript\n  /** The list of elements holding the element to be moved */\n  elemList: any[],\n  /** The index of the element to be moved */\n  originIndex: number,\n  /** The destination index of the element to be moved */\n  destinationIndex: number\n  ```\n- Output: N/A - It mutates the original array and nothing is returned.\n\n### Method: `moveElementImmutable()`\n\n- Inputs: Same as `moveElement()`\n- Output: It returns a modified deep clone of the original array and the original array remains intact.\n\n### Method: `moveElementVirtual()`\n\n- Input:\n  ```typescript\n  /** The list of elements holding the element to be moved */\n  elemList: any[],\n  /** The index of the element to be moved */\n  originIndex: number,\n  /** The destination index of the element to be moved */\n  destinationIndex: number,\n  /** A reference or references to the properties/keys needed to access the position value */\n  indexKey: string | number | (string | number)[],\n  /** Optional, defaults to true. An indication as to whether the index is 0-indexed or 1-indexed */\n  zeroIndexed: boolean = true\n  ```\n- Output: N/A - It mutates the original array and nothing is returned.\n\n### Method: `moveElementVirtualImmutable()`\n\n- Inputs:\n  ```typescript\n  /** The list of elements holding the element to be moved */\n  elemList: any[],\n  /** The index of the element to be moved */\n  originIndex: number,\n  /** The destination index of the element to be moved */\n  destinationIndex: number,\n  /** A reference or references to the properties/keys needed to access the position value */\n  indexKey: string | number | (string | number)[],\n  /** Optional, defaults to true. An indication as to whether the index is 0-indexed or 1-indexed */\n  zeroIndexed: boolean = true,\n  /** Optional, defaults to false. A flag that will sort the results based on the ultimate position values */\n  sort: boolean = false\n  ```\n- Output: It returns a modified deep clone of the original array and the original array remains intact.\n","readmeFilename":"README.md","keywords":["array","move","element","virtual","splice","pagination","position","mutable","immutable"]}