...
 
Commits (6)
{
"extends": [
"eslint:recommended"
],
"env": {
"browser": false,
"es6": true,
......@@ -6,6 +10,15 @@
"mocha": true
},
"parserOptions":{
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
}
},
"globals": {
"document": false,
"navigator": false,
......
package-lock=false
......@@ -9,7 +9,3 @@ node_js:
- '8'
- '7'
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
## Usage
## Quickstart
```js
const Cache = require('cache-base');
const CacheBase = require('cache-base');
const app = new CacheBase();
app.set('a.b', 'c');
console.log(app.cache.a); //=> { b: 'c' }
console.log(app.cache.a.b); //=> 'c'
console.log(app.get('a')); //=> { b: 'c' }
console.log(app.get('a.b')); //=> 'c'
```
**Instantiate**
More [usage examples](#usage-examples) below.
## API
{%= apidocs("index.js") %}
## Usage examples
**Create an instance of cache-base**
```js
// instantiate
const cache = new Cache();
const app = new CacheBase();
// set values
cache.set('a', 'b');
cache.set('c.d', 'e');
app.set('a', 'b');
app.set('c.d', 'e');
// get values
console.log(cache.get('a'));
console.log(app.get('a'));
//=> 'b'
console.log(cache.get('c'));
console.log(app.get('c'));
//=> { d: 'e' }
console.log(cache);
//=> Cache { a: 'b' }
console.log(app);
//=> CacheBase { a: 'b' }
```
**Initialize with an object**
```js
// instantiate
const cache = new Cache({ a: 'b', c: { d: 'e' } });
const app = new CacheBase({ a: 'b', c: { d: 'e' } });
// get values
console.log(cache.get('a'));
console.log(app.get('a'));
//=> 'b'
console.log(cache.get('c'));
console.log(app.get('c'));
//=> { d: 'e' }
console.log(cache.get('c.d'));
console.log(app.get('c.d'));
//=> 'e'
console.log(cache);
//=> Cache { a: 'b' }
console.log(app);
//=> CacheBase { cache: { a: 'b' } }
```
**Inherit**
```js
class MyApp extends Cache {}
class MyApp extends CacheBase {}
var cache = new MyApp();
cache.set('a', 'b');
console.log(cache.get('a'));
const app = new MyApp();
app.set('a', 'b');
app.set('c', 'd');
console.log(app.get('a'));
//=> 'b'
console.log(app);
//=> MyApp { cache: { a: 'b', c: 'd' } }
```
**Custom namespace**
Define a custom property name for storing values. By default, values are stored directly on the instance (for example, when `cache.set('foo', 'bar')` is used, `cache.foo` would be `bar`).
Pass a string as the first value to the contructor to define a custom property name to use for the cache. By default values are stored on the `cache` property.
```js
const Cache = require('cache-base');
const cache = new Cache('data', { a: 'b' });
cache.set('c.d', 'e');
const CacheBase = require('cache-base');
const app = new CacheBase('data', { a: 'b' });
app.set('c.d', 'e');
// get values
console.log(cache.get('a'));
console.log(app.get('a'));
//=> 'b'
console.log(cache.get('c'));
console.log(app.get('c'));
//=> { d: 'e' }
console.log(cache.data);
console.log(app.data);
//=> { a: 'b', c: { d: 'e' } }
console.log(cache);
//=> Cache { data: { a: 'b', c: { d: 'e' } } }
console.log(app);
//=> CacheBase { data: { a: 'b', c: { d: 'e' } } }
```
## API
{%= apidocs("index.js") %}
......@@ -33,6 +33,13 @@ Changelog entries are classified using the following labels _(from [keep-a-chang
</details>
## [3.0.0] - 2018-01-27
**Changed**
- adds `.prime()` and `.default()` methods
## [2.0.0] - 2017-12-17
**Changed**
......@@ -40,13 +47,11 @@ Changelog entries are classified using the following labels _(from [keep-a-chang
- convert to class
- refactor to allow namespace to be set on constructor, thus `.namespace` was removed from the main export. Please see the readme for details.
## [1.0.1] - 2017-07-22
- run update, lint, update deps
## [1.0.0] - 2017-02-25
-
## [0.8.5] - 2017-02-25
......@@ -115,13 +120,16 @@ Changelog entries are classified using the following labels _(from [keep-a-chang
## [0.2.0] - 2014-11-15
- first commit
- remove namespace stuff
- update fixtures
- update verbfile
- clean up, refactor a number of methods:
- update docs, run verb
## 0.1.0
- first commit
[2.0.0]: https://github.com/jonschlinkert/cache-base/compare/1.0.1...2.0.0
[1.0.1]: https://github.com/jonschlinkert/cache-base/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/jonschlinkert/cache-base/compare/0.8.5...1.0.0
......
The MIT License (MIT)
Copyright (c) 2014-2017, Jon Schlinkert.
Copyright (c) 2014-2018, Jon Schlinkert.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
This diff is collapsed.
node-cache-base (2.0.2-1) unstable; urgency=medium
node-cache-base (4.0.0-1) UNRELEASED; urgency=medium
* Team upload
* New upstream release
* TODO: wait till node-base 1.0.1-1 can be uploaded
* Team upload.
-- Pirate Praveen <praveen@debian.org> Wed, 10 Jan 2018 19:35:02 +0530
* Update Vcs fields for migration to https://salsa.debian.org/
* Bump standard version
* Bump debhelper compat
* New upstream version
* Update copyright and dependencies
* TODO: wait till node-base 3.0.0-1 can be uploaded
-- Paolo Greppi <paolo.greppi@libpf.com> Tue, 01 May 2018 22:39:48 +0200
node-cache-base (0.8.4-1) unstable; urgency=low
......
......@@ -4,30 +4,33 @@ Priority: optional
Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.debian.org>
Uploaders: Sruthi Chandran <srud@disroot.org>
Build-Depends:
debhelper (>= 9)
, dh-buildinfo
, nodejs
debhelper (>= 11)
, nodejs (>= 6)
, mocha
, node-sellside-emitter (>= 1.2.1)
, node-collection-visit (>= 1.0.0)
, node-get-value (>= 2.0.6)
, node-isobject (>= 3.0.1)
, node-get-value (>= 3.0.1)
, node-has-own-deep (>= 1.1.0)
, node-kind-of (>= 6.0.2)
, node-set-value (>= 3.0.0)
, node-union-value (>= 1.0.0)
, node-unset-value (>= 1.0.0)
Standards-Version: 4.1.3
Standards-Version: 4.1.4
Homepage: https://github.com/jonschlinkert/cache-base
Vcs-Git: https://anonscm.debian.org/git/pkg-javascript/node-cache-base.git
Vcs-Browser: https://anonscm.debian.org/cgit/pkg-javascript/node-cache-base.git
Vcs-Git: https://salsa.debian.org/js-team/node-cache-base.git
Vcs-Browser: https://salsa.debian.org/js-team/node-cache-base
Package: node-cache-base
Architecture: all
Depends:
${misc:Depends}
, nodejs
, nodejs (>= 6)
, node-sellside-emitter (>= 1.2.1)
, node-collection-visit (>= 1.0.0)
, node-get-value (>= 2.0.6)
, node-isobject (>= 3.0.1)
, node-get-value (>= 3.0.1)
, node-has-own-deep (>= 1.1.0)
, node-kind-of (>= 6.0.2)
, node-set-value (>= 3.0.0)
, node-union-value (>= 1.0.0)
, node-unset-value (>= 1.0.0)
Description: Basic object cache for node.js/javascript projects
......
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: cache-base
Upstream-Contact: https://github.com/jonschlinkert/cache-base/issues
Source: https://github.com/jonschlinkert/cache-base
Files: *
Copyright: 2016 Jon Schlinkert (https://github.com/jonschlinkert)
Copyright: 2016-2018, Jon Schlinkert (https://github.com/jonschlinkert)
License: Expat
Files: debian/*
Copyright: 2016 Sruthi Chandran <srud@disroot.org>
Copyright:
2016, Sruthi Chandran <srud@disroot.org>
2018, Paolo Greppi <paolo.greppi@libpf.com>
License: Expat
License: Expat
......@@ -31,4 +33,3 @@ License: Expat
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/sh
set -e
nodejs -e "require('cache-base');"
node -e "require('cache-base');"
const CacheBase = require('..');
const app = new CacheBase();
app.default('foo', 42);
app.default('fez', 42);
app.set('foo', 1);
app.set('bar', 2);
app.set('baz', 3);
app.set('qux', 4);
app.set('fez', 5);
console.log(app.cache);
console.log(app.defaults);
console.log('---');
app.clear();
console.log(app.cache);
console.log(app.defaults);
console.log('---');
app.on('clear', key => (app.defaults = {}));
app.clear();
console.log(app.cache);
console.log(app.defaults);
console.log('---');
const Cache = require('..');
const app = new Cache('data');
app.set('a', 'b');
app.set({ c: 'd' });
app.set('e.f', 'g')
console.log(app.get('e.f'));
//=> 'g'
console.log(app.get());
//=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app.data);
//=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app);
//=> Cache { data: { a: 'b', c: 'd', e: { f: 'g' } } }
const CacheBase = require('..');
const app = new CacheBase();
app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.set('baz', 'zzz');
console.log(app.get('foo'));
//=> 'xxx'
console.log(app.get('bar'));
//=> 'two'
console.log(app.get('baz'));
//=> 'zzz'
console.log(app);
// Cache {
// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
const CacheBase = require('..');
const app = new CacheBase();
// app.on('del', key => app.set(key, app.default(key)));
app.set('foo', 'xxx');
app.default('foo', 'one');
console.log(app.get('foo')); //=> 'xxx'
console.log(app.cache.foo); //=> 'xxx'
app.del('foo');
console.log(app.get('foo')); //=> 'xxx'
console.log(app.cache.foo); //=> undefined
const CacheBase = require('..');
const app = new CacheBase();
app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.default('qux', 'faz');
app.set('baz', 'zzz');
console.log(app.get('foo'));
//=> 'xxx'
console.log(app.get('bar'));
//=> 'two'
console.log(app.get('baz'));
//=> 'zzz'
console.log(app.merge());
//=> { foo: 'xxx', bar: 'two', baz: 'zzz', qux: 'faz' }
console.log(app.merge({qux: 'aaa'}));
//=> { foo: 'xxx', bar: 'two', baz: 'zzz', qux: 'aaa' }
console.log(app);
// Cache {
// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
const Cache = require('..');
const app = new Cache();
app.set('a', 'b');
app.set({ c: 'd' });
app.set('e.f', 'g')
console.log(app.get('e.f')); //=> 'g'
console.log(app.get()); //=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app.data); //=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app);
This diff is collapsed.
{
"name": "cache-base",
"description": "Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.",
"version": "2.0.2",
"version": "4.0.0",
"homepage": "https://github.com/jonschlinkert/cache-base",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
......@@ -18,23 +18,25 @@
],
"main": "index.js",
"engines": {
"node": ">=0.10.0"
"node": ">=6"
},
"scripts": {
"test": "mocha"
"test": "nyc --reporter=text --reporter=html mocha"
},
"dependencies": {
"@sellside/emitter": "^1.2.1",
"collection-visit": "^1.0.0",
"get-value": "^2.0.6",
"isobject": "^3.0.1",
"set-value": "^2.0.0",
"get-value": "^3.0.1",
"has-own-deep": "^1.1.0",
"kind-of": "^6.0.2",
"set-value": "^3.0.0",
"union-value": "^1.0.0",
"unset-value": "^1.0.0"
},
"devDependencies": {
"gulp-format-md": "^1.0.0",
"mocha": "^3.5.3"
"mocha": "^3.5.3",
"nyc": "^11.6.0"
},
"keywords": [
"base",
......@@ -51,7 +53,7 @@
],
"verb": {
"run": true,
"toc": false,
"toc": true,
"layout": "default",
"tasks": [
"readme"
......
/*!
* cache-base <https://github.com/jonschlinkert/cache-base>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
const assert = require('assert');
const Cache = require('../');
let app, cache;
describe('custom property', function() {
beforeEach(function() {
app = new Cache('data');
});
describe('constructor:', function() {
it('should create an instance of Cache', function() {
assert(app instanceof Cache);
});
it('should set ', function() {
app = new Cache('data', { one: 1, two: 2 });
assert.equal(app.data.one, 1);
assert.equal(app.data.two, 2);
});
});
describe('get/set:', function() {
describe('set() - add:', function() {
it('should set a new property with the given value', function() {
app.set('one', 1);
assert.equal(app.get('one'), 1);
assert.equal(app.data.one, 1);
});
});
describe('set() - update:', function() {
it('should update an existing property with the given value', function() {
app.set('one', 2);
assert.equal(app.get('one'), 2);
assert.equal(app.data.one, 2);
});
it('should get the given property', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
assert.equal(app.data.a, 'b');
});
});
});
describe('.set()', function() {
it('should set a value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
assert.equal(app.data.a, 'b');
});
it('should set properties on the `data` object', function() {
app.set('a', 'b');
assert.equal(app.data.a, 'b');
});
it('should allow an object to be set directly', function() {
app.set({x: 'y'});
assert.equal(app.data.x, 'y');
assert.equal(app.get('x'), 'y');
});
it('should set nested properties on the `data` object', function() {
app.set('c', {d: 'e'});
assert.equal(app.get('c').d, 'e');
});
it('should return the instance', function() {
assert.equal(app.set('a', 'b'), app);
});
it('should be chainable', function() {
app
.set('aa', 'bb')
.set('bb', 'cc')
.set('cc', 'dd');
assert.equal(app.get('aa'), 'bb');
assert.equal(app.get('bb'), 'cc');
assert.equal(app.get('cc'), 'dd');
assert.equal(app.data.aa, 'bb');
assert.equal(app.data.bb, 'cc');
assert.equal(app.data.cc, 'dd');
});
it('should return undefined when not set', function() {
assert.equal(app.set('sfsfsdfs', undefined), app);
});
});
describe('.get()', function() {
it('should otherwise return the value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
assert.equal(app.data.a, 'b');
assert.equal(app.get('zllzzl'), undefined);
});
});
});
/*!
* cache-base <https://github.com/jonschlinkert/cache-base>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
const assert = require('assert');
const Cache = require('../');
let app, cache;
describe('events', function() {
beforeEach(function() {
app = new Cache();
});
describe('set', function() {
it('should emit a "set" event', function(cb) {
app.on('set', () => cb());
app.set('a', 'b');
});
it('should emit the key with "set" events', function(cb) {
app.on('set', function(key, val) {
assert.equal(key, 'a');
cb();
});
app.set('a', 'b');
});
it('should emit the value with "set" events', function(cb) {
app.on('set', function(key, val) {
assert.equal(val, 'b');
cb();
});
app.set('a', 'b');
});
});
describe('get', function() {
it('should emit a get event', function(cb) {
app.on('get', () => cb());
app.get('a');
});
it('should emit the key with "get" events', function(cb) {
app.on('get', function(key, val) {
assert.equal(key, 'a');
cb();
});
app.set('a', 'b');
app.get('a');
});
it('should emit the value with "get" events', function(cb) {
app.on('get', function(key, val) {
assert.equal(val, 'b');
cb();
});
app.set('a', 'b');
app.get('a');
});
});
describe('del', function() {
it('should emit a del event', function(cb) {
app.on('del', () => cb());
app.del('a');
});
it('should emit the key with "del" events', function(cb) {
app.on('del', function(key) {
assert.equal(key, 'a');
cb();
});
app.set('a', 'b');
app.del('a');
});
it('should emit each deleted key when multiple properties are deleted', function(cb) {
var keys = [];
app.on('del', key => keys.push(key));
app.set('a', 'b');
app.set('c', 'd');
app.del('a');
app.del('c');
assert.deepEqual(keys, ['a', 'c']);
assert(!app.a);
assert(!app.c);
cb();
});
});
});
This diff is collapsed.