Skip to content

Commit

Permalink
v0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvachon committed Feb 7, 2016
1 parent 91f5e68 commit 002cf68
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_js:
- "0.10"
- "0.12"
- "4"
- "5"
script: npm test
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# urlcache [![NPM Version][npm-image]][npm-url] [![Bower Version][bower-image]][bower-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][david-image]][david-url]

> URL key-value cache and store.
> URL key-value cache.
## Installation

[Node.js](http://nodejs.org/) `>= 0.10` is required. To install, type this at the command line:
[Node.js](http://nodejs.org/) `>= 0.10` is required; `< 4.0` will need an `Object.assign` polyfill. To install, type this at the command line:

```shell
npm install urlcache --save-dev
npm install urlcache
```


Expand All @@ -22,16 +22,19 @@ var cache = new UrlCache(options);
**Note:** all instances of `url` can be either a `String` or a [`url.parse()`](https://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost)-compatible `Object`.

### .clear([url])
Removes `url` from cache. If `url` is not defined, *all* cached key value pairs will be removed.
Removes the `url` key-value pair. If the `url` argument is not defined, *all* pairs will be removed.

### .get(url)
Returns the stored value of `url`. If no such value exists, `undefined` will be returned.

### .length()
Returns the number of stored key-value pairs.

### .set(url, value[, expiryTime])
Stores `value` (any type) into `url` key. Optionally, define `expiryTime` to override `options.expiryTime`.
```js
cache.set("url", {"key":"value"});
cache.get("url"); //-> {"key":"value"}
cache.get("url"); //=> {"key":"value"}

cache.set("url", new Promise(function(resolve, reject) {
// set value after some delayed event
Expand All @@ -41,7 +44,7 @@ cache.set("url", new Promise(function(resolve, reject) {
});

Promise.resolve(cache.get("url")).then(function(value) {
console.log(value); //-> "value"
console.log(value); //=> "value"
});
```
Expand Down Expand Up @@ -70,6 +73,7 @@ When `true`, will remove `#hashes` from URLs. They are most likely not useful to
## Changelog
* 0.6.0 added `.length()` and removed `Object.assign()` polyfill
* 0.5.0 removed use of Promises as they were unnecessary
* 0.4.0 simpler `Promise`-based API
* 0.3.0 added `options.defaultPorts`, more tests
Expand Down
2 changes: 1 addition & 1 deletion index-browser.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
var objectAssign = require("object-assign");
var urllib = require("url");
var urlobj = require("urlobj");

Expand All @@ -15,7 +14,7 @@ var defaultOptions =

function UrlCache(options)
{
this.options = objectAssign({}, defaultOptions, options);
this.options = Object.assign({}, defaultOptions, options);

this.clear();
}
Expand All @@ -33,10 +32,13 @@ UrlCache.prototype.clear = function(url)
{
delete this.expiries[url];
delete this.values[url];

this.count--;
}
}
else
{
this.count = 0;
this.expiries = {};
this.values = {};
}
Expand All @@ -55,6 +57,13 @@ UrlCache.prototype.get = function(url)



UrlCache.prototype.length = function()
{
return this.count;
};



UrlCache.prototype.set = function(url, value, expiryTime)
{
// Avoid filling cache with values that will only cause rejection
Expand All @@ -66,6 +75,8 @@ UrlCache.prototype.set = function(url, value, expiryTime)

this.expiries[url] = Date.now() + expiryTime;
this.values[url] = value;

this.count++;
};


Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "urlcache",
"description": "URL key-value cache and store.",
"version": "0.5.0",
"description": "URL key-value cache.",
"version": "0.6.0",
"license": "MIT",
"homepage": "https://github.com/stevenvachon/urlcache",
"author": {
Expand All @@ -17,21 +17,20 @@
"url": "https://github.com/stevenvachon/urlcache/issues"
},
"dependencies": {
"object-assign": "^4.0.1",
"urlobj": "0.0.8"
},
"devDependencies": {
"bhttp": "^1.2.1",
"browserify": "^11.2.0",
"chai": "^3.3.0",
"chai-as-promised": "^5.1.0",
"browserify": "^13.0.0",
"chai": "^3.5.0",
"chai-as-promised": "^5.2.0",
"es6-promise": "^3.0.2",
"mocha": "^2.3.3",
"node-static": "~0.7.7",
"uglify-js": "^2.5.0"
"mocha": "^2.4.5",
"object.assign": "^4.0.3",
"uglify-js": "^2.6.1"
},
"engines": {
"node": ">= 0.10"
"node": ">= 4"
},
"scripts": {
"browserify": "browserify index.js --standalone UrlCache | uglifyjs --compress --mangle -o index-browser.js",
Expand Down
38 changes: 36 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
var UrlCache = require("./");

var chai = require("chai");
var objectAssign = require("object-assign");
var urllib = require("url");

var expect = chai.expect;
chai.use( require("chai-as-promised") );

require("es6-promise").polyfill();
require("object.assign").shim();



Expand All @@ -22,7 +22,7 @@ function options(overrides)
stripUrlHashes: false
};

return objectAssign({}, testDefaults, overrides);
return Object.assign({}, testDefaults, overrides);
}


Expand Down Expand Up @@ -163,6 +163,40 @@ describe("clear()", function()



describe("length()", function()
{
it("should work", function()
{
var cache = new UrlCache( options() );

expect(cache.length()).to.equal(0);

cache.clear();
expect(cache.length()).to.equal(0);

cache.clear("not-available");
expect(cache.length()).to.equal(0);

cache.set("some-url1", "some value1");
expect(cache.length()).to.equal(1);

cache.set("some-url2", "some value2");
cache.set("some-url3", "some value3");
expect(cache.length()).to.equal(3);

cache.clear("not-available");
expect(cache.length()).to.equal(3);

cache.clear("some-url3");
expect(cache.length()).to.equal(2);

cache.clear();
expect(cache.length()).to.equal(0);
});
});



describe("options", function()
{
it("expiryTime = 50", function(done)
Expand Down

0 comments on commit 002cf68

Please sign in to comment.