Comparing version 5.0.0 to 5.1.0
@@ -462,18 +462,30 @@ /*global window*/ | ||
MagicPen.prototype.installPlugin = function (plugin) { | ||
var alreadyInstalled = this.installedPlugins.some(function (installedPlugin) { | ||
return installedPlugin === plugin.name; | ||
MagicPen.prototype.use = function (plugin) { | ||
var existingPlugin = utils.findFirst(this.installedPlugins, function (installedPlugin) { | ||
if (installedPlugin === plugin) { | ||
return true; | ||
} else if (typeof plugin === 'function' && typeof installedPlugin === 'function') { | ||
var pluginName = utils.getFunctionName(plugin); | ||
return pluginName !== '' && pluginName === utils.getFunctionName(installedPlugin); | ||
} else { | ||
return installedPlugin.name === plugin.name; | ||
} | ||
}); | ||
if (alreadyInstalled) { | ||
return; | ||
if (existingPlugin) { | ||
if (existingPlugin === plugin) { | ||
// No-op | ||
return this; | ||
} else { | ||
throw new Error("Another instance of the plugin '" + plugin.name + "' is already installed. " + | ||
"Please check your node_modules folder for unmet peerDependencies."); | ||
} | ||
} | ||
if (typeof plugin !== 'object' || | ||
typeof plugin.name !== 'string' || | ||
typeof plugin.installInto !== 'function' || | ||
(plugin.dependencies && !Array.isArray(plugin.dependencies))) { | ||
throw new Error('Plugins must adhere to the following interface\n' + | ||
if ((typeof plugin !== 'function' && (typeof plugin !== 'object' || typeof plugin.installInto !== 'function')) || | ||
(typeof plugin.name !== 'undefined' && typeof plugin.name !== 'string') || | ||
(typeof plugin.dependencies !== 'undefined' && !Array.isArray(plugin.dependencies))) { | ||
throw new Error('Plugins must be functions or adhere to the following interface\n' + | ||
'{\n' + | ||
' name: <plugin name>,\n' + | ||
' name: <an optional plugin name>,\n' + | ||
' dependencies: <an optional list of dependencies>,\n' + | ||
@@ -488,3 +500,3 @@ ' installInto: <a function that will update the given magicpen instance>\n' + | ||
return !installedPlugins.some(function (plugin) { | ||
return plugin === dependency; | ||
return plugin.name === dependency; | ||
}); | ||
@@ -502,5 +514,8 @@ }); | ||
this.installedPlugins = this.installedPlugins.slice(); | ||
this.installedPlugins.push(plugin.name); | ||
plugin.installInto(this); | ||
this.installedPlugins.push(plugin); | ||
if (typeof plugin === 'function') { | ||
plugin(this); | ||
} else { | ||
plugin.installInto(this); | ||
} | ||
@@ -510,2 +525,4 @@ return this; // for chaining | ||
MagicPen.prototype.installPlugin = MagicPen.prototype.use; // Legacy alias | ||
function replaceText(output, outputArray, regexp, cb) { | ||
@@ -512,0 +529,0 @@ var replacedOutput = output; |
@@ -66,2 +66,29 @@ var utils = { | ||
return text.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1"); | ||
}, | ||
findFirst: function (arr, predicate, thisObj) { | ||
var scope = thisObj || null; | ||
for (var i = 0 ; i < arr.length ; i += 1) { | ||
if (predicate.call(scope, arr[i], i, arr)) { | ||
return arr[i]; | ||
} | ||
} | ||
return null; | ||
}, | ||
getFunctionName: function (f) { | ||
if (typeof f.name === 'string') { | ||
return f.name; | ||
} | ||
var matchFunctionName = Function.prototype.toString.call(f).match(/function ([^\(]+)/); | ||
if (matchFunctionName) { | ||
return matchFunctionName[1]; | ||
} | ||
if (f === Object) { | ||
return 'Object'; | ||
} | ||
if (f === Function) { | ||
return 'Function'; | ||
} | ||
} | ||
@@ -68,0 +95,0 @@ }; |
@@ -802,18 +802,30 @@ /*! | ||
MagicPen.prototype.installPlugin = function (plugin) { | ||
var alreadyInstalled = this.installedPlugins.some(function (installedPlugin) { | ||
return installedPlugin === plugin.name; | ||
MagicPen.prototype.use = function (plugin) { | ||
var existingPlugin = utils.findFirst(this.installedPlugins, function (installedPlugin) { | ||
if (installedPlugin === plugin) { | ||
return true; | ||
} else if (typeof plugin === 'function' && typeof installedPlugin === 'function') { | ||
var pluginName = utils.getFunctionName(plugin); | ||
return pluginName !== '' && pluginName === utils.getFunctionName(installedPlugin); | ||
} else { | ||
return installedPlugin.name === plugin.name; | ||
} | ||
}); | ||
if (alreadyInstalled) { | ||
return; | ||
if (existingPlugin) { | ||
if (existingPlugin === plugin) { | ||
// No-op | ||
return this; | ||
} else { | ||
throw new Error("Another instance of the plugin '" + plugin.name + "' is already installed. " + | ||
"Please check your node_modules folder for unmet peerDependencies."); | ||
} | ||
} | ||
if (typeof plugin !== 'object' || | ||
typeof plugin.name !== 'string' || | ||
typeof plugin.installInto !== 'function' || | ||
(plugin.dependencies && !Array.isArray(plugin.dependencies))) { | ||
throw new Error('Plugins must adhere to the following interface\n' + | ||
if ((typeof plugin !== 'function' && (typeof plugin !== 'object' || typeof plugin.installInto !== 'function')) || | ||
(typeof plugin.name !== 'undefined' && typeof plugin.name !== 'string') || | ||
(typeof plugin.dependencies !== 'undefined' && !Array.isArray(plugin.dependencies))) { | ||
throw new Error('Plugins must be functions or adhere to the following interface\n' + | ||
'{\n' + | ||
' name: <plugin name>,\n' + | ||
' name: <an optional plugin name>,\n' + | ||
' dependencies: <an optional list of dependencies>,\n' + | ||
@@ -828,3 +840,3 @@ ' installInto: <a function that will update the given magicpen instance>\n' + | ||
return !installedPlugins.some(function (plugin) { | ||
return plugin === dependency; | ||
return plugin.name === dependency; | ||
}); | ||
@@ -842,5 +854,8 @@ }); | ||
this.installedPlugins = this.installedPlugins.slice(); | ||
this.installedPlugins.push(plugin.name); | ||
plugin.installInto(this); | ||
this.installedPlugins.push(plugin); | ||
if (typeof plugin === 'function') { | ||
plugin(this); | ||
} else { | ||
plugin.installInto(this); | ||
} | ||
@@ -850,2 +865,4 @@ return this; // for chaining | ||
MagicPen.prototype.installPlugin = MagicPen.prototype.use; // Legacy alias | ||
function replaceText(output, outputArray, regexp, cb) { | ||
@@ -1269,2 +1286,29 @@ var replacedOutput = output; | ||
return text.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1"); | ||
}, | ||
findFirst: function (arr, predicate, thisObj) { | ||
var scope = thisObj || null; | ||
for (var i = 0 ; i < arr.length ; i += 1) { | ||
if (predicate.call(scope, arr[i], i, arr)) { | ||
return arr[i]; | ||
} | ||
} | ||
return null; | ||
}, | ||
getFunctionName: function (f) { | ||
if (typeof f.name === 'string') { | ||
return f.name; | ||
} | ||
var matchFunctionName = Function.prototype.toString.call(f).match(/function ([^\(]+)/); | ||
if (matchFunctionName) { | ||
return matchFunctionName[1]; | ||
} | ||
if (f === Object) { | ||
return 'Object'; | ||
} | ||
if (f === Function) { | ||
return 'Function'; | ||
} | ||
} | ||
@@ -1370,2 +1414,3 @@ }; | ||
process.version = ''; // empty string to avoid regexp issues | ||
process.versions = {}; | ||
@@ -1372,0 +1417,0 @@ function noop() {} |
{ | ||
"name": "magicpen", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -16,3 +16,3 @@ "main": "./lib/MagicPen.js", | ||
"test": "npm run lint && mocha -R spec", | ||
"travis": "npm test && npm run coverage && <coverage/lcov.info coveralls", | ||
"travis": "npm test && npm run coverage && (<coverage/lcov.info coveralls || true)", | ||
"prepublish": "(echo '/*!' && <LICENSE sed -e's/^/ * /' | sed -e's/\\s+$//' && echo ' */' && browserify -p bundle-collapser/plugin -e lib/MagicPen -s weknowhow.MagicPen) > magicpen.js", | ||
@@ -19,0 +19,0 @@ "coverage": "NODE_ENV=development istanbul cover _mocha -- --reporter dot" |
@@ -538,3 +538,3 @@ # MagicPen | ||
### raw | ||
### raw(...) | ||
@@ -598,9 +598,9 @@ In case you know the format of the pen you are working with, you can append | ||
### installPlugin(plugin) | ||
### use(plugin) | ||
MagicPen plugins are objects that adhere to the following interface: | ||
MagicPen plugins are functions or objects that adhere to the following interface: | ||
```js | ||
{ | ||
name: <plugin name>, | ||
name: <optional plugin name>, | ||
dependencies: <an optional list of dependencies>, | ||
@@ -611,2 +611,6 @@ installInto: <a function that will update the given magicpen instance> | ||
If a function is passed, it will be used like `installInto`, and the | ||
name of the function will be used as the plugin name, unless the | ||
function is anonymous. | ||
The name of the plugin should be the same at the NPM package name. | ||
@@ -620,4 +624,4 @@ | ||
The `installInto` function receives an instance of unexpected and uses | ||
uses the `addStyle` method to add new custom styles to the MagicPen | ||
The `installInto` function receives an instance of `MagicPen` and uses | ||
the `addStyle` method to add new custom styles to the `MagicPen` | ||
instance. | ||
@@ -627,3 +631,3 @@ | ||
var pen = magicpen(); | ||
pen.installPlugin({ | ||
pen.use({ | ||
name: 'starPlugin', | ||
@@ -642,4 +646,4 @@ installInto: function (pen) { | ||
MagicPen have support for theming text styles differently for each | ||
format. A theme is just a hash of aliases to build in text styles or | ||
MagicPen has support for theming text styles differently for each | ||
format. A theme is just a hash of aliases to built in text styles or | ||
aliases to other theme entries. You install the theme for one or more | ||
@@ -646,0 +650,0 @@ formats. |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
568946
4131
794