UNPKG

12.4 kBMarkdownView Raw
1# cache-base [![NPM version](https://img.shields.io/npm/v/cache-base.svg?style=flat)](https://www.npmjs.com/package/cache-base) [![NPM monthly downloads](https://img.shields.io/npm/dm/cache-base.svg?style=flat)](https://npmjs.org/package/cache-base) [![NPM total downloads](https://img.shields.io/npm/dt/cache-base.svg?style=flat)](https://npmjs.org/package/cache-base) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/cache-base.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/cache-base)
2
3> Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
4
5Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6
7- [Install](#install)
8- [Quickstart](#quickstart)
9- [API](#api)
10- [Usage examples](#usage-examples)
11- [About](#about)
12
13_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
14
15## Install
16
17Install with [npm](https://www.npmjs.com/):
18
19```sh
20$ npm install --save cache-base
21```
22
23## Quickstart
24
25```js
26const CacheBase = require('cache-base');
27const app = new CacheBase();
28
29app.set('a.b', 'c');
30
31console.log(app.cache.a); //=> { b: 'c' }
32console.log(app.cache.a.b); //=> 'c'
33
34console.log(app.get('a')); //=> { b: 'c' }
35console.log(app.get('a.b')); //=> 'c'
36```
37
38More [usage examples](#usage-examples) below.
39
40## API
41
42**Params**
43
44* `prop` **{String|Object}**: (optional) Property name to use for the cache, or the object to initialize with.
45* `cache` **{Object}**: (optional) An object to initialize with.
46
47**Example**
48
49```js
50const app = new CacheBase();
51```
52
53### [.set](index.js#L65)
54
55Assign `value` to `key`. Also emits `set` with the key and value.
56
57**Params**
58
59* `key` **{String|Array}**: The name of the property to set. Dot-notation may be used to set nested properties.
60* `value` **{any}**
61* `returns` **{Object}**: Returns the instance for chaining.
62
63**Events**
64
65* `emits`: `set` with `key` and `value` as arguments.
66
67**Example**
68
69```js
70app.on('set', function(key, val) {
71 // do something when `set` is emitted
72});
73
74app.set('admin', true);
75
76// also takes an object or an array of objects
77app.set({ name: 'Brian' });
78app.set([{ foo: 'bar' }, { baz: 'quux' }]);
79console.log(app);
80//=> { name: 'Brian', foo: 'bar', baz: 'quux' }
81```
82
83### [.get](index.js#L90)
84
85Return the value of `key`.
86
87**Params**
88
89* `key` **{String|Array}**: The name of the property to get. Dot-notation may be used to set nested properties.
90* `returns` **{any}**: Returns the value of `key`
91
92**Events**
93
94* `emits`: `get` with `key` and `value` as arguments.
95
96**Example**
97
98```js
99app.set('a.b.c', 'd');
100app.get('a.b');
101//=> { c: 'd' }
102```
103
104### [.prime](index.js#L120)
105
106Create a property on the cache with the given `value` only if it doesn't already exist.
107
108**Params**
109
110* `key` **{String}**: Property name or object path notation.
111* `val` **{any}**
112* `returns` **{Object}**: Returns the instance for chaining.
113
114**Example**
115
116```js
117console.log(app.cache); //=> {}
118app.set('one', { foo: 'bar' });
119app.prime('one', { a: 'b' });
120app.prime('two', { c: 'd' });
121console.log(app.cache.one); //=> { foo: 'bar' }
122console.log(app.cache.two); //=> { c: 'd' }
123```
124
125### [.default](index.js#L162)
126
127Set a default value to be used when `.get()` is called and the value is not defined on the cache. Returns a value from the defaults when only a key is passed.
128
129**Params**
130
131* `key` **{String|Array}**: The name of the property to set. Dot-notation may be used to set nested properties.
132* `value` **{any}**: (optional) The value to set on the defaults object.
133* `returns` **{Object}**: Returns the instance for chaining.
134
135**Example**
136
137```js
138app.set('foo', 'xxx');
139app.default('foo', 'one');
140app.default('bar', 'two');
141app.default('baz', 'three');
142app.set('baz', 'zzz');
143
144console.log(app.get('foo'));
145//=> 'xxx'
146
147console.log(app.get('bar'));
148//=> 'two'
149
150console.log(app.get('baz'));
151//=> 'zzz'
152
153console.log(app);
154// CacheBase {
155// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
156// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
157```
158
159### [.union](index.js#L199)
160
161Set an array of unique values on cache `key`.
162
163**Params**
164
165* `key` **{String|Array}**: The name of the property to union. Dot-notation may be used to set nested properties.
166* `value` **{any}**
167* `returns` **{Object}**: Returns the instance for chaining.
168
169**Example**
170
171```js
172app.union('a.b.c', 'foo');
173app.union('a.b.c', 'bar');
174app.union('a.b.c', ['bar', 'baz']);
175console.log(app.get('a'));
176//=> { b: { c: ['foo', 'bar', 'baz'] } }
177```
178
179### [.has](index.js#L223)
180
181Return true if the value of property `key` is not `undefined`.
182
183**Params**
184
185* `key` **{String|Array}**: The name of the property to check. Dot-notation may be used to set nested properties.
186* `returns` **{Boolean}**
187
188**Example**
189
190```js
191app.set('foo', true);
192app.set('baz', null);
193app.set('bar', undefined);
194
195app.has('foo'); //=> true
196app.has('bar'); //=> true
197app.has('baz'); //=> false
198```
199
200### [.hasOwn](index.js#L253)
201
202Returns true if the specified property is an own (not inherited) property. Similar to [.has()](#has), but returns true if the key exists, even if the value is `undefined`.
203
204**Params**
205
206* `key` **{String}**
207* `returns` **{Boolean}**: Returns true if object `key` exists. Dot-notation may be used to set nested properties.
208
209**Example**
210
211```js
212app.set('a.b.c', 'd');
213app.set('x', false);
214app.set('y', null);
215app.set('z', undefined);
216
217app.hasOwn('a'); //=> true
218app.hasOwn('b'); //=> true
219app.hasOwn('c'); //=> true
220app.hasOwn('a.b.c'); //=> true
221app.hasOwn('x'); //=> true
222app.hasOwn('y'); //=> true
223app.hasOwn('z'); //=> true
224app.hasOwn('lslsls'); //=> false
225```
226
227### [.del](index.js#L278)
228
229Delete one or more properties from the instance.
230
231**Params**
232
233* `key` **{String|Array}**: The name of the property to delete. Dot-notation may be used to set nested properties.
234* `returns` **{Object}**: Returns the instance for chaining.
235
236**Events**
237
238* `emits`: `del` with the `key` as the only argument.
239
240**Example**
241
242```js
243// setup a listener to update a property with a default
244// value when it's deleted by the user
245app.on('del', key => app.set(key, app.default(key)));
246
247app.del(); // delete all properties on the cache
248// or
249app.del('foo');
250// or an array of keys
251app.del(['foo', 'bar']);
252```
253
254### [.clear](index.js#L301)
255
256Reset the entire cache to an empty object. Note that this does not also clear the `defaults` object, since you can manually do `cache.defaults = {}` if you want to reset that object as well.
257
258**Example**
259
260```js
261// clear "defaults" whenever the cache is cleared
262app.on('clear', key => (app.defaults = {}));
263app.clear();
264```
265
266### [.visit](index.js#L318)
267
268Visit (or map visit) the specified method (`key`) over the properties in the
269given object or array.
270
271**Params**
272
273* `key` **{String|Array}**: The name of the method to visit.
274* `val` **{Object|Array}**: The object or array to iterate over.
275* `returns` **{Object}**: Returns the instance for chaining.
276
277### [.keys](index.js#L338)
278
279Gets an array of names of all enumerable properties on the cache.
280
281**Example**
282
283```js
284const app = new CacheBase();
285app.set('user', true);
286app.set('admin', false);
287
288console.log(app.keys);
289//=> ['user', 'admin']
290```
291
292### [.size](index.js#L357)
293
294Gets the length of [keys](#keys).
295
296**Example**
297
298```js
299const app = new CacheBase();
300app.set('user', true);
301app.set('admin', false);
302
303console.log(app.size);
304//=> 2
305```
306
307## Usage examples
308
309**Create an instance of cache-base**
310
311```js
312const app = new CacheBase();
313
314app.set('a', 'b');
315app.set('c.d', 'e');
316
317console.log(app.get('a'));
318//=> 'b'
319console.log(app.get('c'));
320//=> { d: 'e' }
321console.log(app);
322//=> CacheBase { a: 'b' }
323```
324
325**Initialize with an object**
326
327```js
328const app = new CacheBase({ a: 'b', c: { d: 'e' } });
329
330console.log(app.get('a'));
331//=> 'b'
332console.log(app.get('c'));
333//=> { d: 'e' }
334console.log(app.get('c.d'));
335//=> 'e'
336console.log(app);
337//=> CacheBase { cache: { a: 'b' } }
338```
339
340**Inherit**
341
342```js
343class MyApp extends CacheBase {}
344
345const app = new MyApp();
346app.set('a', 'b');
347app.set('c', 'd');
348
349console.log(app.get('a'));
350//=> 'b'
351
352console.log(app);
353//=> MyApp { cache: { a: 'b', c: 'd' } }
354```
355
356**Custom namespace**
357
358Pass 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.
359
360```js
361const CacheBase = require('cache-base');
362const app = new CacheBase('data', { a: 'b' });
363app.set('c.d', 'e');
364
365// get values
366console.log(app.get('a'));
367//=> 'b'
368console.log(app.get('c'));
369//=> { d: 'e' }
370console.log(app.data);
371//=> { a: 'b', c: { d: 'e' } }
372console.log(app);
373//=> CacheBase { data: { a: 'b', c: { d: 'e' } } }
374```
375
376## About
377
378<details>
379<summary><strong>Contributing</strong></summary>
380
381Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
382
383</details>
384
385<details>
386<summary><strong>Running Tests</strong></summary>
387
388Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
389
390```sh
391$ npm install && npm test
392```
393
394</details>
395
396<details>
397<summary><strong>Building docs</strong></summary>
398
399_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
400
401To generate the readme, run the following command:
402
403```sh
404$ npm install -g verbose/verb#dev verb-generate-readme && verb
405```
406
407</details>
408
409### Related projects
410
411You might also be interested in these projects:
412
413* [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/jonschlinkert/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods "base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.")
414* [get-value](https://www.npmjs.com/package/get-value): Use property paths like 'a.b.c' to get a nested value from an object. Even works… [more](https://github.com/jonschlinkert/get-value) | [homepage](https://github.com/jonschlinkert/get-value "Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).")
415* [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.")
416* [option-cache](https://www.npmjs.com/package/option-cache): Simple API for managing options in JavaScript applications. | [homepage](https://github.com/jonschlinkert/option-cache "Simple API for managing options in JavaScript applications.")
417* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
418* [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value "Delete nested properties from an object using dot notation.")
419
420### Contributors
421
422| **Commits** | **Contributor** |
423| --- | --- |
424| 67 | [jonschlinkert](https://github.com/jonschlinkert) |
425| 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
426
427### Author
428
429**Jon Schlinkert**
430
431* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
432* [GitHub Profile](https://github.com/jonschlinkert)
433* [Twitter Profile](https://twitter.com/jonschlinkert)
434
435### License
436
437Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
438Released under the [MIT License](LICENSE).
439
440***
441
442_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 23, 2018._
\No newline at end of file