-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (86 loc) · 2.1 KB
/
index.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
var set = function(key, value) {
this.beforeAll(function() {
this.__jasmine_memoize__ = clone(this.__jasmine_memoize__ || {});
this.__jasmine_memoize__[key] = {
value: value,
cache: null,
populated: false
};
});
};
var clone = function(object) {
var value = {};
Object.keys(object).forEach(function(key) {
value[key] = object[key];
});
return value;
};
var define = function(context, key, value) {
Object.defineProperty(context, key, {
get: function() {
if (value.populated) {
return value.cache;
}
value.populated = true;
return value.cache = value.value.call(this);
},
enumerable: true,
configurable: false
});
};
function globalObject() {
if (typeof window !== "undefined") {
return window;
} else if (typeof global !== "undefined") {
return global;
}
}
function installHooks(env) {
var last;
env.beforeEach(function() {
if (this.__jasmine_memoize__) {
Object.keys(this.__jasmine_memoize__).forEach(function(key) {
var value = this.__jasmine_memoize__[key];
define(this, key, value);
}.bind(this));
last = this.__jasmine_memoize__;
delete this.__jasmine_memoize__;
}
});
env.afterEach(function() {
this.__jasmine_memoize__ = last;
if (this.__jasmine_memoize__) {
Object.keys(this.__jasmine_memoize__).forEach(function(key) {
var value = this.__jasmine_memoize__[key];
value.cache = null;
value.populated = false;
}.bind(this));
}
last = null;
});
}
function install(env) {
var exports = globalObject();
var installGlobally = false;
if (!env) {
env = exports.jasmine.getEnv();
installGlobally = true;
}
env.set = set.bind(env);
installHooks(env);
if (installGlobally) {
exports.set = env.set;
}
return {
set: env.set
};
}
if (typeof define == "function" && define.amd) {
define(function() {
return {install: install}
});
} else if (typeof module !== "undefined" && module.exports) {
module.exports = {install: install};
} else {
window.jasmineMemoize = {install: install};
}