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 |
|
5 | Please 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 |
|
17 | Install with [npm](https://www.npmjs.com/):
|
18 |
|
19 | ```sh
|
20 | $ npm install --save cache-base
|
21 | ```
|
22 |
|
23 | ## Quickstart
|
24 |
|
25 | ```js
|
26 | const CacheBase = require('cache-base');
|
27 | const app = new CacheBase();
|
28 |
|
29 | app.set('a.b', 'c');
|
30 |
|
31 | console.log(app.cache.a); //=> { b: 'c' }
|
32 | console.log(app.cache.a.b); //=> 'c'
|
33 |
|
34 | console.log(app.get('a')); //=> { b: 'c' }
|
35 | console.log(app.get('a.b')); //=> 'c'
|
36 | ```
|
37 |
|
38 | More [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
|
50 | const app = new CacheBase();
|
51 | ```
|
52 |
|
53 | ### [.set](index.js#L65)
|
54 |
|
55 | Assign `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
|
70 | app.on('set', function(key, val) {
|
71 | // do something when `set` is emitted
|
72 | });
|
73 |
|
74 | app.set('admin', true);
|
75 |
|
76 | // also takes an object or an array of objects
|
77 | app.set({ name: 'Brian' });
|
78 | app.set([{ foo: 'bar' }, { baz: 'quux' }]);
|
79 | console.log(app);
|
80 | //=> { name: 'Brian', foo: 'bar', baz: 'quux' }
|
81 | ```
|
82 |
|
83 | ### [.get](index.js#L90)
|
84 |
|
85 | Return 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
|
99 | app.set('a.b.c', 'd');
|
100 | app.get('a.b');
|
101 | //=> { c: 'd' }
|
102 | ```
|
103 |
|
104 | ### [.prime](index.js#L120)
|
105 |
|
106 | Create 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
|
117 | console.log(app.cache); //=> {}
|
118 | app.set('one', { foo: 'bar' });
|
119 | app.prime('one', { a: 'b' });
|
120 | app.prime('two', { c: 'd' });
|
121 | console.log(app.cache.one); //=> { foo: 'bar' }
|
122 | console.log(app.cache.two); //=> { c: 'd' }
|
123 | ```
|
124 |
|
125 | ### [.default](index.js#L162)
|
126 |
|
127 | Set 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
|
138 | app.set('foo', 'xxx');
|
139 | app.default('foo', 'one');
|
140 | app.default('bar', 'two');
|
141 | app.default('baz', 'three');
|
142 | app.set('baz', 'zzz');
|
143 |
|
144 | console.log(app.get('foo'));
|
145 | //=> 'xxx'
|
146 |
|
147 | console.log(app.get('bar'));
|
148 | //=> 'two'
|
149 |
|
150 | console.log(app.get('baz'));
|
151 | //=> 'zzz'
|
152 |
|
153 | console.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 |
|
161 | Set 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
|
172 | app.union('a.b.c', 'foo');
|
173 | app.union('a.b.c', 'bar');
|
174 | app.union('a.b.c', ['bar', 'baz']);
|
175 | console.log(app.get('a'));
|
176 | //=> { b: { c: ['foo', 'bar', 'baz'] } }
|
177 | ```
|
178 |
|
179 | ### [.has](index.js#L223)
|
180 |
|
181 | Return 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
|
191 | app.set('foo', true);
|
192 | app.set('baz', null);
|
193 | app.set('bar', undefined);
|
194 |
|
195 | app.has('foo'); //=> true
|
196 | app.has('bar'); //=> true
|
197 | app.has('baz'); //=> false
|
198 | ```
|
199 |
|
200 | ### [.hasOwn](index.js#L253)
|
201 |
|
202 | Returns 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
|
212 | app.set('a.b.c', 'd');
|
213 | app.set('x', false);
|
214 | app.set('y', null);
|
215 | app.set('z', undefined);
|
216 |
|
217 | app.hasOwn('a'); //=> true
|
218 | app.hasOwn('b'); //=> true
|
219 | app.hasOwn('c'); //=> true
|
220 | app.hasOwn('a.b.c'); //=> true
|
221 | app.hasOwn('x'); //=> true
|
222 | app.hasOwn('y'); //=> true
|
223 | app.hasOwn('z'); //=> true
|
224 | app.hasOwn('lslsls'); //=> false
|
225 | ```
|
226 |
|
227 | ### [.del](index.js#L278)
|
228 |
|
229 | Delete 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
|
245 | app.on('del', key => app.set(key, app.default(key)));
|
246 |
|
247 | app.del(); // delete all properties on the cache
|
248 | // or
|
249 | app.del('foo');
|
250 | // or an array of keys
|
251 | app.del(['foo', 'bar']);
|
252 | ```
|
253 |
|
254 | ### [.clear](index.js#L301)
|
255 |
|
256 | Reset 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
|
262 | app.on('clear', key => (app.defaults = {}));
|
263 | app.clear();
|
264 | ```
|
265 |
|
266 | ### [.visit](index.js#L318)
|
267 |
|
268 | Visit (or map visit) the specified method (`key`) over the properties in the
|
269 | given 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 |
|
279 | Gets an array of names of all enumerable properties on the cache.
|
280 |
|
281 | **Example**
|
282 |
|
283 | ```js
|
284 | const app = new CacheBase();
|
285 | app.set('user', true);
|
286 | app.set('admin', false);
|
287 |
|
288 | console.log(app.keys);
|
289 | //=> ['user', 'admin']
|
290 | ```
|
291 |
|
292 | ### [.size](index.js#L357)
|
293 |
|
294 | Gets the length of [keys](#keys).
|
295 |
|
296 | **Example**
|
297 |
|
298 | ```js
|
299 | const app = new CacheBase();
|
300 | app.set('user', true);
|
301 | app.set('admin', false);
|
302 |
|
303 | console.log(app.size);
|
304 | //=> 2
|
305 | ```
|
306 |
|
307 | ## Usage examples
|
308 |
|
309 | **Create an instance of cache-base**
|
310 |
|
311 | ```js
|
312 | const app = new CacheBase();
|
313 |
|
314 | app.set('a', 'b');
|
315 | app.set('c.d', 'e');
|
316 |
|
317 | console.log(app.get('a'));
|
318 | //=> 'b'
|
319 | console.log(app.get('c'));
|
320 | //=> { d: 'e' }
|
321 | console.log(app);
|
322 | //=> CacheBase { a: 'b' }
|
323 | ```
|
324 |
|
325 | **Initialize with an object**
|
326 |
|
327 | ```js
|
328 | const app = new CacheBase({ a: 'b', c: { d: 'e' } });
|
329 |
|
330 | console.log(app.get('a'));
|
331 | //=> 'b'
|
332 | console.log(app.get('c'));
|
333 | //=> { d: 'e' }
|
334 | console.log(app.get('c.d'));
|
335 | //=> 'e'
|
336 | console.log(app);
|
337 | //=> CacheBase { cache: { a: 'b' } }
|
338 | ```
|
339 |
|
340 | **Inherit**
|
341 |
|
342 | ```js
|
343 | class MyApp extends CacheBase {}
|
344 |
|
345 | const app = new MyApp();
|
346 | app.set('a', 'b');
|
347 | app.set('c', 'd');
|
348 |
|
349 | console.log(app.get('a'));
|
350 | //=> 'b'
|
351 |
|
352 | console.log(app);
|
353 | //=> MyApp { cache: { a: 'b', c: 'd' } }
|
354 | ```
|
355 |
|
356 | **Custom namespace**
|
357 |
|
358 | 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.
|
359 |
|
360 | ```js
|
361 | const CacheBase = require('cache-base');
|
362 | const app = new CacheBase('data', { a: 'b' });
|
363 | app.set('c.d', 'e');
|
364 |
|
365 | // get values
|
366 | console.log(app.get('a'));
|
367 | //=> 'b'
|
368 | console.log(app.get('c'));
|
369 | //=> { d: 'e' }
|
370 | console.log(app.data);
|
371 | //=> { a: 'b', c: { d: 'e' } }
|
372 | console.log(app);
|
373 | //=> CacheBase { data: { a: 'b', c: { d: 'e' } } }
|
374 | ```
|
375 |
|
376 | ## About
|
377 |
|
378 | <details>
|
379 | <summary><strong>Contributing</strong></summary>
|
380 |
|
381 | Pull 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 |
|
388 | Running 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 |
|
401 | To 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 |
|
411 | You 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 |
|
437 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
|
438 | Released 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 |