a structure for set that refresh element if same id of element is exists
npm install refresh-set
- using refresh set to hold elements, and update it last time
import { RefreshSet } from 'refresh-set';
type Element = {
id: string;
value: number;
};
const set = new RefreshSet<Element>();
set.add({ id: '1', value: 1 });
set.add({ id: '2', value: 2 });
set.add({ id: '1', value: 3 });
console.log(set.toArray()); // [{ id: '1', value: 3 }, { id: '2', value: 2 }]
- change identifier of element
import { RefreshSet } from 'refresh-set';
type Element = {
key: string;
value: number;
};
// default id supplier is element.id
const set = new RefreshSet<Element>({
idSupplier: (element) => element.key,
});
set.add({ key: '1', value: 1 });
set.add({ key: '2', value: 2 });
set.add({ key: '1', value: 3 });
console.log(set.toArray()); // [{ key: '1', value: 3 }, { key: '2', value: 2 }]
- custom refresh method
import { RefreshSet, RefreshPolicy } from 'refresh-set';
type Element = {
id: string;
value: number;
};
const set = new RefreshSet<Element>({
policy: RefreshPolicy.MERGER,
merger: (oldElement, newElement) => {
newElement.value = oldElement.value + newElement.value;
return newElement;
},
});
set.add({ id: '1', value: 1 });
set.add({ id: '2', value: 2 });
set.add({ id: '1', value: 3 });
console.log(set.toArray()); // [{ id: '1', value: 4 }, { id: '2', value: 2 }]
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request