@use/core
Add use() function for easy plugin ability.
Allows default options and plugin specific options.
Accepts module name or path to use()
and will require()
it for you.
Can assign it to any name.
Table of Contents
Install
npm install use --save
Usage: Applying 'use'
The simplest way to use this module is to require()
it and assign it to the use
property on your object.
You may use any property name, like, 'load', 'add', 'plugin', or 'enhance'.
Use it by supplying something which can be given to require()
to get a function, or, supply a function.
var thing = getThing()
, use = require('@use/core')
thing.use = use
var somePluginName = 'some-name'
, pluginRequired = require('another-plugin')
, pluginFunction = function() { }
, someOptions = {}
thing.use(somePluginName, someOptions)
thing.use(pluginRequired, { })
thing.use(pluginFunction )
Usage: Example Plugin
function plugin(options, thing) {
this.blah = 'example'
thing.blah = 'example'
}
Usage: Example
var thing = {}
thing.use = require('@use/core')
thing.use(function addProcess() {
this.process = function process(string) {
console.log('I am processing string:', string)
}
})
thing.process('blah')
thing.use(function wrapInput(options) {
var realProcess = this.process
, prefix = '['
, suffix = ']'
if (options) {
if (options.prefix) prefix = options.prefix
if (options.suffix) suffix = options.suffix
}
this.process = function wrappedProcess(string) {
string = prefix + string + suffix
return realProcess.call(this, string)
}
})
thing.process('bleh')
Usage: Specify Default Options
var use = require('@use/core')
, thing = {}
thing.use = use.withOptions({
prefix: '(',
suffix: ')'
})
thing.use(addProcess)
thing.use(wrapInput)
thing.process('blarg')
Usage: Specify Plugin Options
var use = require('@use/core')
, thing = {}
thing.use = use.withOptions({
prefix: '(',
suffix: ')'
})
thing.use(addProcess)
thing.use(wrapInput, {
prefix: '\'',
suffix: '\''
})
thing.process('bling')
Usage: Enhance Itself
It's possible to use plugins to enhance the use
instance itself.
var use = require('@use/core')
use.use(function (options, scope) {
scope.combine = function(defaults, options) {
return _.extend({}, options, defaults)
}
})
Usage: Generate Your Own to Enhance
All of the above examples use the default use
instance. So, if you use the enhance it then everyone using the default instance will have those customizations.
To avoid that you can generate your own instance:
var use = require('@use/core')
use = use.gen()
Usage: Custom Generation
It's possible to supply an object to the gen()
function to add properties to the internal scope
or override the default internal functions.
The object you provide is used as the scope
and if new functions aren't specified then the default ones are placed into it.
var use = require('@use/core')
, customScope = {
some: 'value',
combine: function (defaultOptions, options) {
return _.extend({}, options, defaults)
}
}
use = use.gen(customScope)
use = use.gen({}, {
some: 'base options'
})
use = use.withOptions({ some: 'default options'})
thing = { use:use }
thing.use('some plugin', { some: 'plugin options'})
MIT License