You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
7.7 KiB

4 years ago
  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. > Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save cache-base
  7. ```
  8. ## Usage
  9. ```js
  10. var Cache = require('cache-base');
  11. // instantiate
  12. var app = new Cache();
  13. // set values
  14. app.set('a', 'b');
  15. app.set('c.d', 'e');
  16. // get values
  17. app.get('a');
  18. //=> 'b'
  19. app.get('c');
  20. //=> {d: 'e'}
  21. console.log(app.cache);
  22. //=> {a: 'b'}
  23. ```
  24. **Inherit**
  25. ```js
  26. var util = require('util');
  27. var Cache = require('cache-base');
  28. function MyApp() {
  29. Cache.call(this);
  30. }
  31. util.inherits(MyApp, Cache);
  32. var app = new MyApp();
  33. app.set('a', 'b');
  34. app.get('a');
  35. //=> 'b'
  36. ```
  37. **Namespace**
  38. Define a custom property for storing values.
  39. ```js
  40. var Cache = require('cache-base').namespace('data');
  41. var app = new Cache();
  42. app.set('a', 'b');
  43. console.log(app.data);
  44. //=> {a: 'b'}
  45. ```
  46. ## API
  47. ### [namespace](index.js#L29)
  48. Create a `Cache` constructor that when instantiated will store values on the given `prop`.
  49. **Params**
  50. * `prop` **{String}**: The property name to use for storing values.
  51. * `returns` **{Function}**: Returns a custom `Cache` constructor
  52. **Example**
  53. ```js
  54. var Cache = require('cache-base').namespace('data');
  55. var cache = new Cache();
  56. cache.set('foo', 'bar');
  57. //=> {data: {foo: 'bar'}}
  58. ```
  59. ### [Cache](index.js#L43)
  60. Create a new `Cache`. Internally the `Cache` constructor is created using the `namespace` function, with `cache` defined as the storage object.
  61. **Params**
  62. * `cache` **{Object}**: Optionally pass an object to initialize with.
  63. **Example**
  64. ```js
  65. var app = new Cache();
  66. ```
  67. ### [.set](index.js#L84)
  68. Assign `value` to `key`. Also emits `set` with the key and value.
  69. **Params**
  70. * `key` **{String}**
  71. * `value` **{any}**
  72. * `returns` **{Object}**: Returns the instance for chaining.
  73. **Events**
  74. * `emits`: `set` with `key` and `value` as arguments.
  75. **Example**
  76. ```js
  77. app.on('set', function(key, val) {
  78. // do something when `set` is emitted
  79. });
  80. app.set(key, value);
  81. // also takes an object or array
  82. app.set({name: 'Halle'});
  83. app.set([{foo: 'bar'}, {baz: 'quux'}]);
  84. console.log(app);
  85. //=> {name: 'Halle', foo: 'bar', baz: 'quux'}
  86. ```
  87. ### [.union](index.js#L114)
  88. Union `array` to `key`. Also emits `set` with the key and value.
  89. **Params**
  90. * `key` **{String}**
  91. * `value` **{any}**
  92. * `returns` **{Object}**: Returns the instance for chaining.
  93. **Example**
  94. ```js
  95. app.union('a.b', ['foo']);
  96. app.union('a.b', ['bar']);
  97. console.log(app.get('a'));
  98. //=> {b: ['foo', 'bar']}
  99. ```
  100. ### [.get](index.js#L144)
  101. Return the value of `key`. Dot notation may be used to get [nested property values](https://github.com/jonschlinkert/get-value).
  102. **Params**
  103. * `key` **{String}**: The name of the property to get. Dot-notation may be used.
  104. * `returns` **{any}**: Returns the value of `key`
  105. **Events**
  106. * `emits`: `get` with `key` and `value` as arguments.
  107. **Example**
  108. ```js
  109. app.set('a.b.c', 'd');
  110. app.get('a.b');
  111. //=> {c: 'd'}
  112. app.get(['a', 'b']);
  113. //=> {c: 'd'}
  114. ```
  115. ### [.has](index.js#L171)
  116. Return true if app has a stored value for `key`, false only if value is `undefined`.
  117. **Params**
  118. * `key` **{String}**
  119. * `returns` **{Boolean}**
  120. **Events**
  121. * `emits`: `has` with `key` and true or false as arguments.
  122. **Example**
  123. ```js
  124. app.set('foo', 'bar');
  125. app.has('foo');
  126. //=> true
  127. ```
  128. ### [.del](index.js#L199)
  129. Delete one or more properties from the instance.
  130. **Params**
  131. * `key` **{String|Array}**: Property name or array of property names.
  132. * `returns` **{Object}**: Returns the instance for chaining.
  133. **Events**
  134. * `emits`: `del` with the `key` as the only argument.
  135. **Example**
  136. ```js
  137. app.del(); // delete all
  138. // or
  139. app.del('foo');
  140. // or
  141. app.del(['foo', 'bar']);
  142. ```
  143. ### [.clear](index.js#L218)
  144. Reset the entire cache to an empty object.
  145. **Example**
  146. ```js
  147. app.clear();
  148. ```
  149. ### [.visit](index.js#L235)
  150. Visit `method` over the properties in the given object, or map
  151. visit over the object-elements in an array.
  152. **Params**
  153. * `method` **{String}**: The name of the `base` method to call.
  154. * `val` **{Object|Array}**: The object or array to iterate over.
  155. * `returns` **{Object}**: Returns the instance for chaining.
  156. ## About
  157. ### Related projects
  158. * [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`.")
  159. * [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.")
  160. * [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.")
  161. * [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.")
  162. * [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.")
  163. * [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.")
  164. ### Contributing
  165. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  166. ### Contributors
  167. | **Commits** | **Contributor** |
  168. | --- | --- |
  169. | 54 | [jonschlinkert](https://github.com/jonschlinkert) |
  170. | 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  171. ### Building docs
  172. _(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.)_
  173. To generate the readme, run the following command:
  174. ```sh
  175. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  176. ```
  177. ### Running tests
  178. 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:
  179. ```sh
  180. $ npm install && npm test
  181. ```
  182. ### Author
  183. **Jon Schlinkert**
  184. * [github/jonschlinkert](https://github.com/jonschlinkert)
  185. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  186. ### License
  187. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  188. Released under the [MIT License](LICENSE).
  189. ***
  190. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 22, 2017._