1.3.0 • Published 11 years ago
halster v1.3.0
halster
Extract and store hal previous and next links.
Installation
npm install halsterUsage
var halster = require('halster');
var links = halster({
default: function() {
return 'http://mysite.com/api';
},
previous: function(val) {
return 'http://mysite.com/api?until=' + val;
},
next: function(val) {
return 'http://mysite.com/api?from=' + val;
},
normalize: function(val) {
return val.splice(13)[1];
}
});
links.set({
_links: {
previous: {
url: 'until=12345677'
},
next: {
url: 'from=12345677'
}
}
});
links.get({previous: true});
// => 'http://mysite.com/api?until=12345677'API
var links = halster()
Create a new halster instance. Takes an object of arguments. default is the
default link to use if no previous or next were given. If previous or
default are given the links are passed down to burl and handled
there. The normalize link functions the same as in burl, converting urls
into integers that can be compared to determine the newest / oldest link.
Optionally you can also set invert: true to flip next and previous.
var halster = require('halster');
var links = halster({
default: function() {
return 'http://mysite.com/api';
},
previous: function(val) {
return 'http://mysite.com/api?until=' + val;
},
next: function(val) {
return 'http://mysite.com/api?from=' + val;
},
normalize: function(val) {
return val.splice(13)[1];
}
});.set()
Pass in an object with a links / _links property and save the correct link
within halster.
links.set({
_links: {
previous: {
url: 'until=12345677'
},
next: {
url: 'from=12345677'
}
}
});.get()
Get an url. Can be passed an object with either next or previous set to true
for the corresponding link. If no argument is provided the default link will be
returned.
links.get({previous: true});
// => 'http://mysite.com/api?until=12345677'
links.get();
// => 'http://mysite.com/api'