Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

adds a `data` method to base-methods. 100% unit test coverage and browserify-friendly lazy-caching.

License

NotificationsYou must be signed in to change notification settings

base-repos/base-data

Repository files navigation

adds adata method to base-methods.

(TOC generated byverb usingmarkdown-toc)

Install

Install withnpm:

$ npm install --save base-data

Usage

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

varBase=require('base');vardata=require('base-data');// instantiate `Base`varbase=newBase();// add `data` as a pluginbase.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 ontoapp.cache.data. This can be customized by passing the property to use when the plugin is initialized.

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

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

API

Register a data loader for loading data ontoapp.cache.data.

Params

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

Example

varyaml=require('js-yaml');app.dataLoader('yml',function(str,fp){returnyaml.safeLoad(str);});app.data('foo.yml');//=> loads and parses `foo.yml` as yaml

Load data ontoapp.cache.data

Params

  • key{String|Object}: Key of the value to set, or object to extend.
  • val{any}
  • returns{Object}: Returns the instance ofTemplate 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 arrayapp.data('e',['f']);// overwrite the arrayapp.data('e',['g']);// update the arrayapp.data('e',['h'],true);console.log(app.cache.data.e);//=> ['g', 'h']

Shallow extend an object ontoapp.cache.data.

Params

  • key{String|Object}: Property name or object to extend ontoapp.cache.data. Dot-notation may be used for extending nested properties.
  • value{Object}: The object to extend ontoapp.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'}

Deeply merge an object ontoapp.cache.data.

Params

  • key{String|Object}: Property name or object to merge ontoapp.cache.data. Dot-notation may be used for merging nested properties.
  • value{Object}: The object to merge ontoapp.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'}}}

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

Params

  • key{String}: Property name. Dot-notation may be used for nested properties.
  • array{Object}: The array to add or union onapp.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']

Set the given value ontoapp.cache.data.

Params

  • key{String|Object}: Property name or object to merge ontoapp.cache.data. Dot-notation may be used for nested properties.
  • val{any}: The value to set onapp.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']}

Get the value ofkey fromapp.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 ofkey

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-globapp.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 thatfoo.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){returnpath.basename(fp);}});console.log(app.cache.data);//=> {'foo.json': {a: 'b'}}

History

v0.6.0

v0.5.0

v0.4.0

  • Refactored

  • adds methods to.data for getting and setting data.

v0.3.6

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

About

Related 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 aconfig method for mapping declarative configuration values to other 'base…more |homepage
  • base-option: Adds a few options methods to base, likeoption,enable anddisable. 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: Adds 'smart plugin' support to your base application. |homepage
  • base-store: Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object…more |homepage
  • base: Framework for rapidly creating high quality node.js applications, using plugins like building blocks |homepage

Contributing

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

Contributors

CommitsContributor
69jonschlinkert
10doowb

Building docs

(This project's readme.md is generated byverb, please don't edit the readme directly. Any changes to the readme must be made in the.verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme&& verb

Running tests

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:

$ npm install&& npmtest

Author

Jon Schlinkert

License

Copyright © 2017,Jon Schlinkert.Released under theMIT License.


This file was generated byverb-generate-readme, v0.6.0, on July 20, 2017.

About

adds a `data` method to base-methods. 100% unit test coverage and browserify-friendly lazy-caching.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  
  •  

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp