1 |
3a515b92
|
cagy
|
# map-age-cleaner
|
2 |
|
|
|
3 |
|
|
[![Build Status](https://travis-ci.org/SamVerschueren/map-age-cleaner.svg?branch=master)](https://travis-ci.org/SamVerschueren/map-age-cleaner) [![codecov](https://codecov.io/gh/SamVerschueren/map-age-cleaner/badge.svg?branch=master)](https://codecov.io/gh/SamVerschueren/map-age-cleaner?branch=master)
|
4 |
|
|
|
5 |
|
|
> Automatically cleanup expired items in a Map
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
## Install
|
9 |
|
|
|
10 |
|
|
```
|
11 |
|
|
$ npm install map-age-cleaner
|
12 |
|
|
```
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
## Usage
|
16 |
|
|
|
17 |
|
|
```js
|
18 |
|
|
import mapAgeCleaner from 'map-age-cleaner';
|
19 |
|
|
|
20 |
|
|
const map = new Map([
|
21 |
|
|
['unicorn', {data: '🦄', maxAge: Date.now() + 1000}]
|
22 |
|
|
]);
|
23 |
|
|
|
24 |
|
|
mapAgeCleaner(map);
|
25 |
|
|
|
26 |
|
|
map.has('unicorn');
|
27 |
|
|
//=> true
|
28 |
|
|
|
29 |
|
|
// Wait for 1 second...
|
30 |
|
|
|
31 |
|
|
map.has('unicorn');
|
32 |
|
|
//=> false
|
33 |
|
|
```
|
34 |
|
|
|
35 |
|
|
> **Note**: Items have to be ordered ascending based on the expiry property. This means that the item which will be expired first, should be in the first position of the `Map`.
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
## API
|
39 |
|
|
|
40 |
|
|
### mapAgeCleaner(map, [property])
|
41 |
|
|
|
42 |
|
|
Returns the `Map` instance.
|
43 |
|
|
|
44 |
|
|
#### map
|
45 |
|
|
|
46 |
|
|
Type: `Map`
|
47 |
|
|
|
48 |
|
|
Map instance which should be cleaned up.
|
49 |
|
|
|
50 |
|
|
#### property
|
51 |
|
|
|
52 |
|
|
Type: `string`<br>
|
53 |
|
|
Default: `maxAge`
|
54 |
|
|
|
55 |
|
|
Name of the property which olds the expiry timestamp.
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
## Related
|
59 |
|
|
|
60 |
|
|
- [expiry-map](https://github.com/SamVerschueren/expiry-map) - A `Map` implementation with expirable items
|
61 |
|
|
- [expiry-set](https://github.com/SamVerschueren/expiry-set) - A `Set` implementation with expirable keys
|
62 |
|
|
- [mem](https://github.com/sindresorhus/mem) - Memoize functions
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
## License
|
66 |
|
|
|
67 |
|
|
MIT © [Sam Verschueren](https://github.com/SamVerschueren)
|