Package Exports
- config-cache
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (config-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
config-cache 
General purpose JavaScript cache methods.
Install
Install with npm:
npm i config-cache --save-dev
Usage
var Config = require('config-cache');
var config = new Config();
API
Initialize a new Cache
obj
{Object}: Optionally pass an object to initialize with.
var cache = new Cache();
Set or get an option.
key
{String}: The option name.value
{*}: The value to set.- returns {*|Object}: Returns
value
ifkey
is supplied, orCache
for chaining when an option is set.
cache.option('a', true)
cache.option('a')
// => true
Assign value
to key
or return the value of key
.
key
{String}value
{*}expand
{Boolean}: Resolve template strings with expander- returns {Cache}: for chaining
cache.set(key, value);
If expand
is defined as true, the value will be set using expander.
Examples:
// as a key-value pair
cache.set('a', {b: 'c'});
// or as an object
cache.set({a: {b: 'c'}});
// chaining is possible
cache
.set({a: {b: 'c'}})
.set('d', 'e');
Expand template strings with expander:
cache.set('a', {b: '${c}', c: 'd'}, true);
Visit the expander docs for more info.
Return the stored value of key
. If the value does not exist on the cache, you may pass true
as a second parameter to tell getobject to initialize the value as an empty object.
key
{*}create
{Boolean}- returns: {*}
cache.set('foo', 'bar');
cache.get('foo');
// => "bar"
Set a constant on the cache.
key
{String}value
{*}
Example
cache.constant('site.title', 'Foo');
Check if key
is enabled (truthy).
key
{String}- returns: {Boolean}
cache.enabled('foo')
// => false
cache.enable('foo')
cache.enabled('foo')
// => true
Check if key
is disabled.
key
{String}- returns: {Boolean}
cache.disabled('foo')
// => true
cache.enable('foo')
cache.disabled('foo')
// => false
Enable key
.
key
{String}- returns {Cache}: for chaining
Example
cache.enable('foo');
Disable key
.
key
{String}- returns {Cache}: for chaining
Example
cache.disable('foo');
Return true
if the element exists. Dot notation may be used for nested properties.
key
{String}- returns: {Boolean}
Example
cache.exists('author.name');
//=> true
Add values to an array on the cache
. This method is chainable.
- returns {Cache}: for chaining
Example
// config.cache['foo'] => ['a.hbs', 'b.hbs']
cache
.union('foo', ['b.hbs', 'c.hbs'], ['d.hbs']);
.union('foo', ['e.hbs', 'f.hbs']);
// config.cache['foo'] => ['a.hbs', 'b.hbs', 'c.hbs', 'd.hbs', 'e.hbs', 'f.hbs']
Extend the cache
with the given object. This method is chainable.
- returns {Cache}: for chaining
Example
cache
.defaults({foo: 'bar'}, {baz: 'quux'});
.defaults({fez: 'bang'});
Or define the property to defaults:
cache
// defaults `cache.a`
.defaults('a', {foo: 'bar'}, {baz: 'quux'})
// defaults `cache.b`
.defaults('b', {fez: 'bang'})
// defaults `cache.a.b.c`
.defaults('a.b.c', {fez: 'bang'});
Extend the cache
with the given object. This method is chainable.
- returns {Cache}: for chaining
Example
cache
.extend({foo: 'bar'}, {baz: 'quux'});
.extend({fez: 'bang'});
Or define the property to extend:
cache
// extend `cache.a`
.extend('a', {foo: 'bar'}, {baz: 'quux'})
// extend `cache.b`
.extend('b', {fez: 'bang'})
// extend `cache.a.b.c`
.extend('a.b.c', {fez: 'bang'});
Extend the cache with the given object. This method is chainable.
- returns {Cache}: for chaining
Example
cache
.merge({foo: 'bar'}, {baz: 'quux'});
.merge({fez: 'bang'});
Return the keys on this.cache
.
- returns: {Boolean}
cache.keys();
Return true if key
is an own, enumerable property of this.cache
or the given obj
.
key
{String}obj
{Object}: Optionally pass an object to check.- returns: {Boolean}
cache.hasOwn([key]);
Clone the given obj
or cache
.
obj
{Object}: Optionally pass an object to clone.- returns: {Boolean}
cache.clone();
Return methods on this.cache
or the given obj
.
obj
{Object}- returns: {Array}
cache.methods('foo')
//=> ['set', 'get', 'enable', ...]
Call fn
on each property in this.cache
.
fn
{Function}obj
{Object}: Optionally pass an object to iterate over.- returns {Object}: Resulting object.
cache.each(fn, obj);
Traverse each own property of this.cache
or the given object, recursively calling fn
on child objects.
obj
{Object|Function}: Optionally pass an object.fn
{Function}- returns {Object}: Return the resulting object.
cache.visit(obj, fn);
Methods for reading data files, processing template strings and extending the
cache.data
object.
lookup
{*}: Any value to process, usually strings with a cache template, like<%= foo %>
or${foo}
.opts
{*}: Options to pass to Lo-Dash_.template
.
Use expander to recursively expand template strings into their resolved values.
Example
cache.process({a: '<%= b %>', b: 'c'});
//=> {a: 'c', b: 'c'}
Extend the cache.data
object with the given data. This method is chainable.
- returns {Cache}: for chaining
Example
cache
.extendData({foo: 'bar'}, {baz: 'quux'});
.extendData({fez: 'bang'});
Extend the data
object with the value returned by plasma.
data
{Object|String|Array}: File path(s), glob pattern, or object of data.options
{Object}: Options to pass to plasma.
Example:
cache
.plasma({foo: 'bar'}, {baz: 'quux'});
.plasma({fez: 'bang'});
See the plasma documentation for all available options.
Expects file path(s) or glob pattern(s) to any JSON or YAML files to be merged onto the data object. Any data files read in by the .namespace()
method will extend the data
object with an object named after the basename of each file.
patterns
{String|Array}: Filepaths or glob patterns.- returns: {null}
Example
cache.namespace(['alert.json', 'nav*.json']);
The data from each file is namespaced using the name of the file:
{
alert: {},
navbar: {}
}
See the plasma documentation for all available options.
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.
values
{Object|Array|String}: Values to pass to plasma.process
{Boolean}: Iftrue
,- returns {Cache}: for chaining
cache
.data({a: 'b'})
.data({c: 'd'});
console.log(config.cache);
//=> {data: {a: 'b', c: 'd'}}
When true
is passed as the last argumemnt data will
be processed by expander before extending cache.data
.
cache.data({a: '<%= b %>', b: 'z'})
//=> {data: {a: 'z', b: 'z'}}
Methods for clearing the cache, removing or reseting specific values on the cache.
- returns {Cache}: for chaining
Omit properties and their from the cache
.
Example:
cache
.omit('foo');
.omit('foo', 'bar');
.omit(['foo']);
.omit(['foo', 'bar']);
Remove key
from the cache, or if no value is specified the entire cache is reset.
Example:
cache.clear();
Author
Jon Schlinkert
Brian Woodward
License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
This file was generated by verb-cli on August 16, 2014.