UNPKG

is-valid-instance

Version:

Returns true if a value is a valid instance of Base.

131 lines (89 loc) 4.68 kB
# is-valid-instance [![NPM version](https://img.shields.io/npm/v/is-valid-instance.svg?style=flat)](https://www.npmjs.com/package/is-valid-instance) [![NPM monthly downloads](https://img.shields.io/npm/dm/is-valid-instance.svg?style=flat)](https://npmjs.org/package/is-valid-instance) [![NPM total downloads](https://img.shields.io/npm/dt/is-valid-instance.svg?style=flat)](https://npmjs.org/package/is-valid-instance) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/is-valid-instance.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/is-valid-instance) > Returns true if a value is a valid instance of Base. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save is-valid-instance ``` ## Usage Use in your [base](https://github.com/node-base/base) plugin to ensure that the plugin is registered on a valid instance. ```js var isValidInstance = require('is-valid-instance'); function plugin(app) { if (!isValidInstance(app)) return; // do plugin stauff } ``` ## API **Params** * `val` **{Object}** * `names` **{Array|Function}**: One or more names to check for on the given instance. Example `app` will check for `app.isApp === true` or `app._name === 'app'`. * `fn` **{Function}**: Custom function for validating the instance. * `returns` **{Boolean}** **Example** ```js function plugin(app) { if (!isValidInstance(app)) return; // do plugin stuff } ``` ## Examples **Collection instance** Return true if the instance is a [templates](https://github.com/jonschlinkert/templates) view-collection (`views`): ```js function plugin(collection) { if (!isValidInstance(collection, ['views'])) { // optionally return the plugin function so the plugin is re-tried on // each instance until it finds the correct one return plugin; } // do plugin stauff } ``` **View instance** Return true if the instance is a [templates](https://github.com/jonschlinkert/templates) view: ```js function plugin(view) { if (!isValidInstance(view, ['view'])) { return plugin; } // do plugin stauff } ``` **View instance** Return true if the instance is either "app" or "collection": ```js function plugin(app) { if (!isValidInstance(app, ['app', 'collection'])) { return plugin; } // do plugin stauff } ``` ## About ### Related projects * [base-app](https://www.npmjs.com/package/base-app): Starting point for creating a base application, with a few light plugins for running tasks… [more](https://github.com/node-base/base-app) | [homepage](https://github.com/node-base/base-app "Starting point for creating a base application, with a few light plugins for running tasks and writing to the file system, and a functional CLI.") * [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base applications to allow plugins to be called any time after… [more](https://github.com/node-base/base-plugins) | [homepage](https://github.com/node-base/base-plugins "Upgrade's plugin support in base applications to allow plugins to be called any time after init.") * [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/node-base/base) | [homepage](https://github.com/node-base/base "base 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`.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). ### Building docs _(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.)_ To generate the readme, run the following command: ```sh $ 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: ```sh $ npm install && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.3, on March 24, 2017._