Skip to content

Commit

Permalink
adds examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Mar 3, 2018
1 parent 4e426f6 commit 8192061
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/clear.js
@@ -0,0 +1,27 @@
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('---');
18 changes: 18 additions & 0 deletions examples/custom-prop.js
@@ -0,0 +1,18 @@
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' } } }
22 changes: 22 additions & 0 deletions examples/defaults.js
@@ -0,0 +1,22 @@
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' } }
14 changes: 14 additions & 0 deletions examples/del.js
@@ -0,0 +1,14 @@
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
29 changes: 29 additions & 0 deletions examples/merge.js
@@ -0,0 +1,29 @@
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' } }
11 changes: 11 additions & 0 deletions examples/set.js
@@ -0,0 +1,11 @@
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);

0 comments on commit 8192061

Please sign in to comment.