https://github.com/axtgr/match-store
A JavaScript storage that uses a function to match keys when retrieving values
https://github.com/axtgr/match-store
Last synced: about 1 year ago
JSON representation
A JavaScript storage that uses a function to match keys when retrieving values
- Host: GitHub
- URL: https://github.com/axtgr/match-store
- Owner: axtgr
- License: mit
- Created: 2016-11-26T08:18:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-03T07:31:48.000Z (over 9 years ago)
- Last Synced: 2025-06-13T06:05:07.012Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# match-store
A simple JavaScript storage that puts values into arrays at a specified key and uses a matching function to retrieve them. Most suitable for event emitters when used with [wildcard-match](https://github.com/alex-shnayder/wildcard-match).
## Installation
`npm install --save match-store`
## Usage
```javascript
let Store = require('match-store');
// Settings are optional
let store = new Store({
prefix: '~', // key prefix for the internal storage to avoid clashes
match: function areOfSameLength(a, b) { // this function is used to match the keys when retrieving values
return a.length === b.length;
}
});
store.put('one.two.three', 'foo');
store.put('four.five.six', 'bar', 1);
store.put('seven', 'baz');
store.get('thirteenchars'); // ['foo', 'bar'] because the matching function only checks the length
store.get('same---length'); // ['foo'] because 'bar' had a limit of 1 retrieval
```
## API
### `put(key, value, [uses])`
Saves the value to the internal storage at the specified key. The third parameter limits the number of times the value can be retrieved.
### `get(key)`
Returns an array of values with matching keys. Uses the `match` function provided in the settings to match the keys. If no function is set, checks the keys for equality.
### `del(key, [value])`
Removes values whose keys *exactly* match the provided one. If a value is provided, deletes only records that are equal to it.
## License
[MIT](LICENSE)