https://github.com/l2silver/reorder-ordinals
simple js library for reordering a list of objects with an ordinal property
https://github.com/l2silver/reorder-ordinals
Last synced: about 1 year ago
JSON representation
simple js library for reordering a list of objects with an ordinal property
- Host: GitHub
- URL: https://github.com/l2silver/reorder-ordinals
- Owner: l2silver
- Created: 2020-07-16T21:36:44.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-12T20:31:04.000Z (almost 6 years ago)
- Last Synced: 2025-06-22T23:07:15.441Z (about 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Reorder Ordinals
A simple library to change the ordinal property of objects in an array.
## Getting started
````
npm i reorder-ordinals
````
## Example Usecase
````
const reorderOrdinalsGenerator = require('reorder-ordinals'); //(options?: Object)=>Func
const reorderOrdinals = reorderOrdinalsGenerator(); //(arr: [{ordinal: Int}, ...], startingOrdinal: Int, endingOrdinal: Int);
let result, expected, i1, i2, i3, items;
i1 = {
id: 1,
ordinal: 0
}
i2 = {
id: 2,
ordinal: 2
}
i3 = {
id: 3,
ordinal: 1
};
items = [
i1, i2, i3
];
result = reorderOrdinals(items, 1, 0)
expected = [{...i1, ordinal: 1}, i2, {...i3, ordinal: 0}];
assert.deepEqual(result, expected);
````
## Options
````
reorderOrdinalsGenerator(options=
{
simple: true // takes an array and treats elements index as ordinal
getOrdinal: (ele)=>Int // func to return the ordinal of an element in the array
updateOrdinal: (ele, nextVal) => ele (updated) // func that updates the ordinal of an element
}
);
````