Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stagas/level-path
sensible key / range generator for levelup
https://github.com/stagas/level-path
Last synced: 13 days ago
JSON representation
sensible key / range generator for levelup
- Host: GitHub
- URL: https://github.com/stagas/level-path
- Owner: stagas
- Created: 2013-11-30T17:32:08.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-21T22:41:09.000Z (almost 11 years ago)
- Last Synced: 2024-04-08T15:45:13.973Z (7 months ago)
- Language: JavaScript
- Size: 125 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# level-path
sensible key / range generator for levelup
## Installing
`npm install level-path`
## Example
```js
var path = require('level-path');var users = path('users/:id');
var index = path('index/:model/:attr/:value/:id', { model: users() });var user = { id: 1, username: 'john' };
console.log(users(user)); // => users/id/\xff1
console.log(users('foo')); // => users/id/\xfffooconsole.log(index({
id: user.id,
attr: 'username',
value: user.username
})); // => index/model/users/id/\xff/attr/username/value/john/id/\xff1console.log(index({ attr: 'username' }, user.username, user.id));
// => index/model/users/id/\xff/attr/username/value/john/\xff1console.log(users.range({ start: 5 }));
// => { start: 'users/id/\xff5', end: 'users/id/\xff\xff' }var last = { id: 15, username: 'mary' };
console.log(users.range({
start: user,
end: last
})); // => { start: 'users/id/\xff1', end: 'users/id/\xff15' }console.log(index.range({
attr: 'username',
value: 'john'
}));
// =>
// { start: 'index/model/users/id/\xff/attr/username/value/john/id/\xff',
// end: 'index/model/users/id/\xff/attr/username/value/john/id/\xff\xff' }
```## API
### key = path(str[, defaults])
Creates a key / range generator function for `str`.
Keys are defined using sinatra style `some/:random/:key` paths.
The last key is considered the range key and will be prefixed
accordingly so ranges can be generated for it later with `.range()`.Optionally pass `defaults` to bind an object
to always be evaluated first.### key([any[, ...]])
Generates a key using attributes found in passed values.
### key.range([{ start: [any], end: [any] }])
Generates a key range using optional `start` / `end` attributes.
They can be ommitted for the full key range.
## License
MIT