https://github.com/ericvera/sort-updates
A JavaScript utility library to determine updates needed to re-order an item in a sorted list.
https://github.com/ericvera/sort-updates
Last synced: 11 months ago
JSON representation
A JavaScript utility library to determine updates needed to re-order an item in a sorted list.
- Host: GitHub
- URL: https://github.com/ericvera/sort-updates
- Owner: ericvera
- License: mit
- Created: 2017-10-02T08:08:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-05T16:16:21.000Z (over 8 years ago)
- Last Synced: 2025-02-23T17:48:08.430Z (over 1 year ago)
- Language: JavaScript
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sort-updates
A JavaScript utility library to determine updates needed to re-order an item in a sorted list.
# Usage
Install the module:
```sh
npm install sort-updates --save
```
```js
sortUpdates(insertAtIndex, itemToInsert, list);
```
Returns an array with objects that need to be updated in order to insert an item at the specified location.
Whenever there is space (sortValues have a maximum of 25 decimal places) between the before and after, there will be a single update and the sort value for the item to insert will be approximaltely between the two items.
Whenever there is no space items will be shifted until there is space.
## Sample when there is space
```js
import sortUpdates from 'sort-updates';
const list = [
{ key: 'aba348f', sortValue: 0 },
{ key: '5ba348f', sortValue: 5 }
]
const itemToInsert = { key: '8ba3e8f', sortValue: undefined }
const insertAtIndex = 1
const expectedUpdtes =
let updates = sortUpdates(insertAtIndex, itemToInsert, list)
console.log(updates);
// Output: [{ key: '8ba3e8f', sortValue: 3 }]
```
## Sample when there is no space
```js
import sortUpdates from 'sort-updates';
const list = [
{ key: 'aba348f', sortValue: 0 },
{ key: '5ba348f', sortValue: 0.0000000000000000000000001 },
{ key: '6ba348f', sortValue: 0.0000000000000000000000002 },
{ key: '9ba348f', sortValue: 0.0000000000000000000000003 }
]
const itemToInsert = { key: '8ba3e8f', sortValue: undefined }
const insertAtIndex = 3
let updates = sortUpdates(insertAtIndex, itemToInsert, list)
console.log(updates);
// Output: [
// { key: '8ba3e8f', sortValue: 0.0000000000000000000000002 },
// { key: '6ba348f', sortValue: 0.0000000000000000000000001 },
// { key: '5ba348f', sortValue: 0 },
// { key: 'aba348f', sortValue: -1 }
//]
```
# License
[MIT](https://github.com/ericvera/sort-updates/blob/master/LICENSE)