Comparing version 1.2.2 to 1.3.0
@@ -0,1 +1,6 @@ | ||
### 1.3.0 | ||
* [`meld()`](docs/api.md#adding-aspects) is now a function that adds aspects. | ||
* **DEPRECATED:** `meld.add()`. Use `meld()` instead. | ||
### 1.2.2 | ||
@@ -2,0 +7,0 @@ |
@@ -10,4 +10,4 @@ # meld.js API | ||
* [meld.after](#meldafter) | ||
1. [Adding Multiple Advices](#adding-multiple-advices) | ||
* [meld.add](#meldadd) | ||
1. [Adding Aspects](#adding-aspects) | ||
* [meld](#meld) | ||
1. [Removing Method Advice](#removing-method-advice) | ||
@@ -179,10 +179,12 @@ 1. [Accessing the Joinpoint](#accessing-the-joinpoint) | ||
# Adding Multiple Advices | ||
# Adding Aspects | ||
Meld.js allows you to add any number of advices to a method, function, or constructor. In addition to adding them individually, as [shown here](#advising-methods), using the individual advice methods (e.g. meld.before, meld.after, etc.), you can also add several advices at once using `meld.add()`. | ||
Meld.js allows you to add any number of advices to a method, function, or constructor. In addition to adding them individually, as [shown here](#advising-methods), using the individual advice methods (e.g. meld.before, meld.after, etc.), you can also add several advices at once using `meld()`. | ||
For example, the [bundled aspects](aspects.md) are implemented this way, and can be added using `meld.add()`. | ||
For example, the [bundled aspects](aspects.md) are implemented this way, and can be added using `meld()`. | ||
## meld.add | ||
## meld | ||
**DEPRECATED ALIAS:** meld.add() | ||
```js | ||
@@ -202,3 +204,3 @@ // Supply any or all of the advice types at once | ||
var remover = meld.add(object, match, advices); | ||
var remover = meld(object, match, advices); | ||
``` | ||
@@ -209,5 +211,5 @@ | ||
```js | ||
var advisedFunction = meld.add(functionToAdvise, advices); | ||
var advisedFunction = meld(functionToAdvise, advices); | ||
var AdvisedConstructor = meld.add(ConstructorToAdvise, advices); | ||
var AdvisedConstructor = meld(ConstructorToAdvise, advices); | ||
``` | ||
@@ -214,0 +216,0 @@ |
@@ -10,3 +10,3 @@ # meld.js Bundled Aspects | ||
Besides adding individual advices, such as before, afterReturning, etc., meld supports adding aspects, which are essentially one or more pieces of advice that work together to implement some functionality. Aspects can be added using [meld.add](api.md#adding-multiple-advices) | ||
Besides adding individual advices, such as before, afterReturning, etc., meld supports adding aspects, which are essentially one or more pieces of advice that work together to implement some functionality. Aspects can be added using [meld](api.md#adding-aspects) | ||
@@ -20,5 +20,5 @@ Meld comes with several aspects, in the `aspect` dir, that you can use, and that serve as examples for implementing your own. | ||
var traced = meld.add(object, pointcut, trace()); | ||
var traced = meld(object, pointcut, trace()); | ||
// or | ||
var traced = meld.add(func, trace()); | ||
var traced = meld(func, trace()); | ||
``` | ||
@@ -57,3 +57,3 @@ | ||
var traced = meld.add(object, pointcut, trace(myReporter)); | ||
var traced = meld(object, pointcut, trace(myReporter)); | ||
``` | ||
@@ -66,5 +66,5 @@ | ||
var memoized = meld.add(object, pointcut, memoize()); | ||
var memoized = meld(object, pointcut, memoize()); | ||
// or | ||
var memoized = meld.add(func, memoize()); | ||
var memoized = meld(func, memoize()); | ||
``` | ||
@@ -85,3 +85,3 @@ | ||
var memoized = meld.add(object, pointcut, memoize(myKeyGenerator)); | ||
var memoized = meld(object, pointcut, memoize(myKeyGenerator)); | ||
``` | ||
@@ -94,5 +94,5 @@ | ||
var cached = meld.add(object, pointcut, cache(storage)); | ||
var cached = meld(object, pointcut, cache(storage)); | ||
// or | ||
var cached = meld.add(func, cache(storage)); | ||
var cached = meld(func, cache(storage)); | ||
``` | ||
@@ -130,3 +130,3 @@ | ||
var cached = meld.add(object, pointcut, cache(storage, myKeyGenerator)); | ||
var cached = meld(object, pointcut, cache(storage, myKeyGenerator)); | ||
``` |
200
meld.js
@@ -14,3 +14,3 @@ /** @license MIT License (c) copyright 2011-2013 original author or authors */ | ||
* @author John Hann | ||
* @version 1.2.2 | ||
* @version 1.3.0 | ||
*/ | ||
@@ -20,5 +20,2 @@ (function (define) { | ||
var meld, currentJoinpoint, joinpointStack, | ||
ap, prepend, append, iterators, slice, isArray, defineProperty, objectCreate; | ||
// | ||
@@ -28,68 +25,63 @@ // Public API | ||
meld = { | ||
// General add aspect | ||
// Returns a function that will remove the newly-added aspect | ||
add: addAspect, | ||
// Add a single, specific type of advice | ||
// returns a function that will remove the newly-added advice | ||
meld.before = adviceApi('before'); | ||
meld.around = adviceApi('around'); | ||
meld.on = adviceApi('on'); | ||
meld.afterReturning = adviceApi('afterReturning'); | ||
meld.afterThrowing = adviceApi('afterThrowing'); | ||
meld.after = adviceApi('after'); | ||
// Add a single, specific type of advice | ||
// returns a function that will remove the newly-added advice | ||
before: adviceApi('before'), | ||
around: adviceApi('around'), | ||
on: adviceApi('on'), | ||
afterReturning: adviceApi('afterReturning'), | ||
afterThrowing: adviceApi('afterThrowing'), | ||
after: adviceApi('after'), | ||
// Access to the current joinpoint in advices | ||
meld.joinpoint = joinpoint; | ||
// Access to the current joinpoint in advices | ||
joinpoint: joinpoint | ||
}; | ||
// DEPRECATED: meld.add(). Use meld() instead | ||
// Returns a function that will remove the newly-added aspect | ||
meld.add = function() { return meld.apply(null, arguments); }; | ||
// TOOD: Freeze joinpoints when v8 perf problems are resolved | ||
// freeze = Object.freeze || function (o) { return o; }; | ||
/** | ||
* Add an aspect to all matching methods of target, or to target itself if | ||
* target is a function and no pointcut is provided. | ||
* @param {object|function} target | ||
* @param {string|array|RegExp|function} [pointcut] | ||
* @param {object} aspect | ||
* @param {function?} aspect.before | ||
* @param {function?} aspect.on | ||
* @param {function?} aspect.around | ||
* @param {function?} aspect.afterReturning | ||
* @param {function?} aspect.afterThrowing | ||
* @param {function?} aspect.after | ||
* @returns {{ remove: function }|function} if target is an object, returns a | ||
* remover { remove: function } whose remove method will remove the added | ||
* aspect. If target is a function, returns the newly advised function. | ||
*/ | ||
function meld(target, pointcut, aspect) { | ||
var pointcutType, remove; | ||
joinpointStack = []; | ||
if(arguments.length < 3) { | ||
return addAspectToFunction(target, pointcut); | ||
} else { | ||
if (isArray(pointcut)) { | ||
remove = addAspectToAll(target, pointcut, aspect); | ||
} else { | ||
pointcutType = typeof pointcut; | ||
ap = Array.prototype; | ||
prepend = ap.unshift; | ||
append = ap.push; | ||
slice = ap.slice; | ||
if (pointcutType === 'string') { | ||
if (typeof target[pointcut] === 'function') { | ||
remove = addAspectToMethod(target, pointcut, aspect); | ||
} | ||
isArray = Array.isArray || function(it) { | ||
return Object.prototype.toString.call(it) == '[object Array]'; | ||
}; | ||
} else if (pointcutType === 'function') { | ||
remove = addAspectToAll(target, pointcut(target), aspect); | ||
// Check for a *working* Object.defineProperty, fallback to | ||
// simple assignment. | ||
defineProperty = definePropertyWorks() | ||
? Object.defineProperty | ||
: function(obj, prop, descriptor) { | ||
obj[prop] = descriptor.value; | ||
}; | ||
} else { | ||
remove = addAspectToMatches(target, pointcut, aspect); | ||
} | ||
} | ||
objectCreate = Object.create || | ||
(function() { | ||
function F() {} | ||
return function(proto) { | ||
F.prototype = proto; | ||
var instance = new F(); | ||
F.prototype = null; | ||
return instance; | ||
}; | ||
}()); | ||
return remove; | ||
} | ||
iterators = { | ||
// Before uses reverse iteration | ||
before: forEachReverse, | ||
around: false | ||
}; | ||
} | ||
// All other advice types use forward iteration | ||
// Around is a special case that uses recursion rather than | ||
// iteration. See Advisor._callAroundAdvice | ||
iterators.on | ||
= iterators.afterReturning | ||
= iterators.afterThrowing | ||
= iterators.after | ||
= forEach; | ||
function Advisor(target, func) { | ||
@@ -370,35 +362,2 @@ | ||
function addAspect(target, pointcut, aspect) { | ||
// pointcut can be: string, Array of strings, RegExp, Function(targetObject): Array of strings | ||
// advice can be: object, Function(targetObject, targetMethodName) | ||
var pointcutType, remove; | ||
if(arguments.length < 3) { | ||
return addAspectToFunction(target, pointcut); | ||
} else { | ||
if (isArray(pointcut)) { | ||
remove = addAspectToAll(target, pointcut, aspect); | ||
} else { | ||
pointcutType = typeof pointcut; | ||
if (pointcutType === 'string') { | ||
if (typeof target[pointcut] === 'function') { | ||
remove = addAspectToMethod(target, pointcut, aspect); | ||
} | ||
} else if (pointcutType === 'function') { | ||
remove = addAspectToAll(target, pointcut(target), aspect); | ||
} else { | ||
remove = addAspectToMatches(target, pointcut, aspect); | ||
} | ||
} | ||
return remove; | ||
} | ||
} | ||
/** | ||
@@ -478,6 +437,6 @@ * Add an aspect to a pure function, returning an advised version of it. | ||
aspect[type] = method; | ||
return addAspect(target, aspect); | ||
return meld(target, aspect); | ||
} else { | ||
aspect[type] = adviceFunc; | ||
return addAspect(target, method, aspect); | ||
return meld(target, method, aspect); | ||
} | ||
@@ -558,2 +517,53 @@ }; | ||
var currentJoinpoint, joinpointStack, | ||
ap, prepend, append, iterators, slice, isArray, defineProperty, objectCreate; | ||
// TOOD: Freeze joinpoints when v8 perf problems are resolved | ||
// freeze = Object.freeze || function (o) { return o; }; | ||
joinpointStack = []; | ||
ap = Array.prototype; | ||
prepend = ap.unshift; | ||
append = ap.push; | ||
slice = ap.slice; | ||
isArray = Array.isArray || function(it) { | ||
return Object.prototype.toString.call(it) == '[object Array]'; | ||
}; | ||
// Check for a *working* Object.defineProperty, fallback to | ||
// simple assignment. | ||
defineProperty = definePropertyWorks() | ||
? Object.defineProperty | ||
: function(obj, prop, descriptor) { | ||
obj[prop] = descriptor.value; | ||
}; | ||
objectCreate = Object.create || | ||
(function() { | ||
function F() {} | ||
return function(proto) { | ||
F.prototype = proto; | ||
var instance = new F(); | ||
F.prototype = null; | ||
return instance; | ||
}; | ||
}()); | ||
iterators = { | ||
// Before uses reverse iteration | ||
before: forEachReverse, | ||
around: false | ||
}; | ||
// All other advice types use forward iteration | ||
// Around is a special case that uses recursion rather than | ||
// iteration. See Advisor._callAroundAdvice | ||
iterators.on | ||
= iterators.afterReturning | ||
= iterators.afterThrowing | ||
= iterators.after | ||
= forEach; | ||
function forEach(array, func) { | ||
@@ -560,0 +570,0 @@ for (var i = 0, len = array.length; i < len; i++) { |
{ | ||
"name": "meld", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "AOP for JS with before, around, on, afterReturning, afterThrowing, after advice, and pointcut support", | ||
@@ -5,0 +5,0 @@ "keywords": ["aop", "aspect", "cujo"], |
@@ -76,2 +76,7 @@ [![Build Status](https://secure.travis-ci.org/cujojs/meld.png)](http://travis-ci.org/cujojs/meld) | ||
### 1.3.0 | ||
* [`meld()`](docs/api.md#adding-aspects) is now a function that adds aspects. | ||
* **DEPRECATED:** `meld.add()`. Use `meld()` instead. | ||
### 1.2.2 | ||
@@ -78,0 +83,0 @@ |
@@ -26,3 +26,3 @@ (function(buster, meld, createCache) { | ||
meld.add(advised, 'method', createCache(cache)); | ||
meld(advised, 'method', createCache(cache)); | ||
@@ -46,3 +46,3 @@ advised.method(param); | ||
meld.add(advised, 'method', createCache(cache)); | ||
meld(advised, 'method', createCache(cache)); | ||
@@ -65,3 +65,3 @@ assert.same(advised.method(param), sentinel); | ||
meld.add(advised, 'method', createCache(cache)); | ||
meld(advised, 'method', createCache(cache)); | ||
@@ -86,3 +86,3 @@ advised.method(param); | ||
meld.add(advised, 'method', createCache(cache, stubKeyGenerator)); | ||
meld(advised, 'method', createCache(cache, stubKeyGenerator)); | ||
@@ -89,0 +89,0 @@ advised.method(param, 1); |
@@ -19,3 +19,3 @@ (function(buster, meld, createMemoizer) { | ||
meld.add(advised, 'method', createMemoizer()); | ||
meld(advised, 'method', createMemoizer()); | ||
@@ -33,3 +33,3 @@ advised.method(param); | ||
meld.add(advised, 'method', createMemoizer()); | ||
meld(advised, 'method', createMemoizer()); | ||
@@ -49,3 +49,3 @@ advised.method(param); | ||
meld.add(advised, 'method', createMemoizer()); | ||
meld(advised, 'method', createMemoizer()); | ||
@@ -64,3 +64,3 @@ advised.method(param, 1); | ||
meld.add(advised, 'method', createMemoizer(stubKeyGenerator)); | ||
meld(advised, 'method', createMemoizer(stubKeyGenerator)); | ||
@@ -67,0 +67,0 @@ advised.method(param, 1); |
@@ -20,3 +20,3 @@ (function(buster, meld, createTracer) { | ||
meld.add(advised, 'method', createTracer(reporter)); | ||
meld(advised, 'method', createTracer(reporter)); | ||
@@ -37,3 +37,3 @@ advised.method(sentinel); | ||
meld.add(advised, 'method', createTracer(reporter)); | ||
meld(advised, 'method', createTracer(reporter)); | ||
@@ -56,3 +56,3 @@ advised.method(); | ||
meld.add(advised, 'method', createTracer(reporter)); | ||
meld(advised, 'method', createTracer(reporter)); | ||
@@ -59,0 +59,0 @@ assert.exception(function() { |
@@ -35,3 +35,3 @@ (function(buster, meld) { | ||
advised = meld.add(f, { | ||
advised = meld(f, { | ||
before: spyBefore, | ||
@@ -38,0 +38,0 @@ around: function(call) { |
@@ -21,3 +21,3 @@ (function(buster, meld) { | ||
meld.add(target, 'method1', { | ||
meld(target, 'method1', { | ||
before: before | ||
@@ -47,3 +47,3 @@ }); | ||
meld.add(target, ['method1', 'method3'], { | ||
meld(target, ['method1', 'method3'], { | ||
before: before | ||
@@ -78,3 +78,3 @@ }); | ||
meld.add(target, /method[13]/, { | ||
meld(target, /method[13]/, { | ||
before: before | ||
@@ -109,3 +109,3 @@ }); | ||
meld.add(target, | ||
meld(target, | ||
function() { | ||
@@ -112,0 +112,0 @@ return ['method1', 'method3']; |
@@ -101,3 +101,3 @@ (function(buster, meld) { | ||
fixture.method = this.spy(); | ||
ref = meld.add(fixture, 'method', aspect); | ||
ref = meld(fixture, 'method', aspect); | ||
@@ -104,0 +104,0 @@ fixture.method(); |
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
91961
1790
114