This repository has been archived by the owner on Jan 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
weaver.js
98 lines (89 loc) · 3.56 KB
/
weaver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Create a new instance of weaver.
*
* @constructor
* @this {Weaver}
*/
exports.Weaver = function () {
/**
* Find a function in an object by name
* @param {object} : Object contains the function
* @param {string} : function name
*/
var find = function (object, functionName) {
var objectFunction = null;
for (var prop in object) {
if (prop == functionName && typeof object[prop] == 'function') {
objectFunction = object[prop];
break;
}
}
return objectFunction;
};
/**
* Weave an aspect function after a list of joint points in the object
* @param {object} : Object in which weave
* @param {Array} : Array of jointPoints string (function names)
* @param {function} : Aspect function to weave after joint point functions
*/
this.after = function(object, jointPoints, aspect) {
if (jointPoints instanceof Array && typeof aspect == 'function') {
for (var i = 0; i < jointPoints.length; i++) {
var jointPoint = jointPoints[i];
if (typeof jointPoint == 'string') {
var oldFunction = find(object, jointPoint);
if (oldFunction != null) {
var replaceFunc = function () {
oldFunction.apply(this, arguments);
aspect.apply(this, arguments);
};
object[jointPoint] = replaceFunc;
} else {
throw new Error('Unknow function : ' + jointPoint);
}
} else {
throw new Error('jointPoint at index ' + i + ' is not a String => typeof=' + typeof jointPoint);
}
}
} else {
var errorMsg =
(jointPoints instanceof Array)
? 'JointPoints must be an array of string'
: 'Aspect must be a function';
throw new Error(errorMsg);
}
};
/**
* Weave an aspect function before a list of joint points in the object
* @param {object} : Object in which weave
* @param {Array} : Array of jointPoints string (function names)
* @param {function} : Aspect function to weave before joint point functions
*/
this.before = function(object, jointPoints, aspect) {
if (jointPoints instanceof Array && typeof aspect == 'function') {
for (var i = 0; i < jointPoints.length; i++) {
var jointPoint = jointPoints[i];
if (typeof jointPoint == 'string') {
var oldFunction = find(object, jointPoint);
if (oldFunction != null) {
var replaceFunc = function () {
aspect.apply(this, arguments);
oldFunction.apply(this, arguments);
};
object[jointPoint] = replaceFunc;
} else {
throw new Error('Unknow function : ' + jointPoint);
}
} else {
throw new Error('jointPoint at index ' + i + ' is not a String => typeof=' + typeof jointPoint);
}
}
} else {
var errorMsg =
(jointPoints instanceof Array)
? 'JointPoints must be an array of string'
: 'Aspect must be a function';
throw new Error(errorMsg);
}
};
};