JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 27431
  • Score
    100M100P100Q130985F
  • License MIT

adds a `data` method to base-methods.

Package Exports

  • base-data

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 (base-data) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

base-data NPM version NPM downloads Build Status

adds a data method to base-methods.

TOC

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install base-data --save

Usage

Adds a data method to base that can be used for setting, getting and loading data onto a specified object in your application.

var Base = require('base');
var data = require('base-data');

// instantiate `Base`
var base = new Base();
// add `data` as a plugin
base.use(data());

Examples

Add data:

app.data('a', 'b');
app.data({c: 'd'});
app.data('e', ['f']);
console.log(app.cache.data);
//=> {a: 'b', c: 'd', e: ['f']}

cache.data

By default, all data is loaded onto app.cache.data. This can be customized by passing the property to use when the plugin is initialized.

For example, the following set app.foo as object for storing data:

app.use(data('foo'));
app.data('a', 'b');
console.log(app.foo);
//=> {a: 'b'}

API

.dataLoader

Register a data loader for loading data onto app.cache.data.

Params

  • ext {String}: The file extension for to match to the loader
  • fn {Function}: The loader function.

Example

var yaml = require('js-yaml');

app.dataLoader('yml', function(str, fp) {
  return yaml.safeLoad(str);
});

app.data('foo.yml');
//=> loads and parses `foo.yml` as yaml

.data

Load data onto app.cache.data

Params

  • key {String|Object}: Key of the value to set, or object to extend.
  • val {any}
  • returns {Object}: Returns the instance of Template for chaining

Example

console.log(app.cache.data);
//=> {};

app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}

// set an array
app.data('e', ['f']);

// overwrite the array
app.data('e', ['g']);

// update the array
app.data('e', ['h'], true);
console.log(app.cache.data.e);
//=> ['g', 'h']

.data.extend

Shallow extend an object onto app.cache.data.

Params

  • key {String|Object}: Property name or object to extend onto app.cache.data. Dot-notation may be used for extending nested properties.
  • value {Object}: The object to extend onto app.cache.data
  • returns {Object}: returns the instance for chaining

Example

app.data({a: {b: {c: 'd'}}});
app.data.extend('a.b', {x: 'y'});
console.log(app.get('a.b'));
//=> {c: 'd', x: 'y'}

.data.merge

Deeply merge an object onto app.cache.data.

Params

  • key {String|Object}: Property name or object to merge onto app.cache.data. Dot-notation may be used for merging nested properties.
  • value {Object}: The object to merge onto app.cache.data
  • returns {Object}: returns the instance for chaining

Example

app.data({a: {b: {c: {d: {e: 'f'}}}}});
app.data.merge('a.b', {c: {d: {g: 'h'}}});
console.log(app.get('a.b'));
//=> {c: {d: {e: 'f', g: 'h'}}}

.data.union

Union the given value onto a new or existing array value on app.cache.data.

Params

  • key {String}: Property name. Dot-notation may be used for nested properties.
  • array {Object}: The array to add or union on app.cache.data
  • returns {Object}: returns the instance for chaining

Example

app.data({a: {b: ['c', 'd']}});
app.data.union('a.b', ['e', 'f']}});
console.log(app.get('a.b'));
//=> ['c', 'd', 'e', 'f']

.data.set

Set the given value onto app.cache.data.

Params

  • key {String|Object}: Property name or object to merge onto app.cache.data. Dot-notation may be used for nested properties.
  • val {any}: The value to set on app.cache.data
  • returns {Object}: returns the instance for chaining

Example

app.data.set('a.b', ['c', 'd']}});
console.log(app.get('a'));
//=> {b: ['c', 'd']}

.data.get

Get the value of key from app.cache.data. Dot-notation may be used for getting nested properties.

Params

  • key {String}: The name of the property to get.
  • returns {any}: Returns the value of key

Example

app.data({a: {b: {c: 'd'}}});
console.log(app.get('a.b'));
//=> {c: 'd'}

Glob patterns

Glob patterns may be passed as a string or array. All of these work:

app.data('foo.json');
app.data('*.json');
app.data(['*.json']);
// pass options to node-glob
app.data(['*.json'], {dot: true});

Namespacing

Namespacing allows you to load data onto a specific key, optionally using part of the file path as the key.

Example

Given that foo.json contains {a: 'b'}:

app.data('foo.json');
console.log(app.cache.data);
//=> {a: 'b'}

app.data('foo.json', {namespace: true});
console.log(app.cache.data);
//=> {foo: {a: 'b'}}

app.data('foo.json', {
  namespace: function(fp) {
    return path.basename(fp);
  }
});
console.log(app.cache.data);
//=> {'foo.json': {a: 'b'}}

History

v0.3.6

  • adds a basic loader that only calls the JSON.parse method, if no other loaders are defined
  • calls .isRegistered from base to ensure the plugin is only loaded once on an instance

You might also be interested in these projects:

  • base-cli: Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a… more | homepage
  • base-config: base-methods plugin that adds a config method for mapping declarative configuration values to other 'base'… more | homepage
  • base-option: Adds a few options methods to base, like option, enable and disable. See the readme… more | homepage
  • base-pipeline: base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | homepage
  • base-plugins: Upgrade's plugin support in base applications to allow plugins to be called any time after… more | homepage
  • base-store: Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object… more | homepage
  • base: base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with verb:

$ npm install verb && npm run docs

Or, if verb is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on April 20, 2016.