UNPKG

plasma-cache

Version:
181 lines (118 loc) 4.21 kB
# plasma-cache [![NPM version](https://badge.fury.io/js/plasma-cache.svg)](http://badge.fury.io/js/plasma-cache) > Object cache for [Plasma](https://github.com/jonschlinkert/plasma). ## Install Install with [npm](https://www.npmjs.com/) ```sh $ npm i plasma-cache --save ``` Install with [bower](http://bower.io/) ```sh $ bower install plasma-cache --save ``` ## Usage ```js var PlasmaCache = require('plasma-cache'); var cache = new PlasmaCache(); ``` ## API ### [PlasmaCache](index.js#L23) Initialize a new `PlasmaCache`. ### [.plasma](index.js#L58) Extend the `data` object with the value returned by [plasma](https://github.com/jonschlinkert/plasma). See the [plasma](https://github.com/jonschlinkert/plasma) documentation for all available options. **Params** * `data` **{Object|String|Array}**: File path(s), glob pattern, or object of data. * `options` **{Object}**: Options to pass to plasma. **Example** ```js cache .plasma({foo: 'bar'}, {baz: 'quux'}); .plasma({fez: 'bang'}); ``` ### [.dataLoader](index.js#L82) Register a `dataLoader` that will read and load data from files with the given `ext`. **Params** * `ext` **{String}**: The extension of files to read. * `fn` **{String}**: The loader function **Example** ```js var fs = require('fs'); var yaml = require('js-yaml'); plasma.dataLoader('yml', function (fp) { var str = fs.readFileSync(fp, 'utf8'); return yaml.safeLoad(str); }); ``` ### [.process](index.js#L96) Use [expander](https://github.com/tkellen/expander) to recursively expand template strings into their resolved values. **Params** * `lookup` **{*}**: Any value to process, usually strings with a cache template, like `<%= foo %>` or `${foo}`. * `opts` **{*}**: Options to pass to Lo-Dash ` _.template`. **Example** ```js cache.process({a: '<%= b %>', b: 'c'}); //=> {a: 'c', b: 'c'} ``` ### [.flattenData](index.js#L122) If a `data` property is on the given `data` object (e.g. `data.data`, like when files named `data.json` or `data.yml` are used), `data.data` is flattened to just `data` **Params** * `data` **{Object}** * `returns` **{Object}**: Flattened object. ### [.extendData](index.js#L136) Extend the `cache.data` object with the given data. This method is chainable. * `returns` **{Object}** `PlasmaCache`: to enable chaining **Example** ```js cache .extendData({foo: 'bar'}, {baz: 'quux'}); .extendData({fez: 'bang'}); ``` ### [.data](index.js#L165) Extend the `cache.data` object with data from a JSON or YAML file, or by passing an object directly - glob patterns or file paths may be used. **Params** * `values` **{Object|Array|String}**: Values to pass to plasma. * `process` **{Boolean}**: If `true` is passed as the last argumemnt data will * `returns` **{Object}** `PlasmaCache`: to enable chaining **Example** ```js cache .data({a: 'b'}) .data({c: 'd'}); console.log(cache); //=> {data: {a: 'b', c: 'd'}} cache.data('*.{json,yml}'); // or cache.data('package.json'); //=> {name: 'plasma-cache', ...} // process config templates cache.data({a: '<%= b %>', b: 'z'}) //=> {data: {a: 'z', b: 'z'}} ``` ## Related projects * [config-cache](https://github.com/jonschlinkert/config-cache): General purpose JavaScript object storage methods. * [data-store](https://github.com/jonschlinkert/data-store): Easily get, set and persist config data. * [option-cache](https://github.com/jonschlinkert/option-cache): Simple API for managing options in JavaScript applications. * [plasma](https://github.com/jonschlinkert/plasma): Load data from globs or files or directly from objects. ## Running tests Install dev dependencies: ```sh $ npm i -d && npm test ``` ## Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/plasma-cache/issues/new) ## Author **Jon Schlinkert** + [github/jonschlinkert](https://github.com/jonschlinkert) + [twitter/jonschlinkert](http://twitter.com/jonschlinkert) ## License Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert) Released under the MIT license. *** _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on June 01, 2015._