-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem_ver2.html
More file actions
145 lines (112 loc) · 3.96 KB
/
Copy pathLibrarySystem_ver2.html
File metadata and controls
145 lines (112 loc) · 3.96 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<script src="TinyTestMod.js"></script>
<script>
(function() {
var libraryStorage = {};
function librarySystem(libraryName, array, callback) {
if (arguments.length > 1) {
saveLibrary(libraryName, array, callback);
} else {
return loadLibrary(libraryName);
}
}
function saveLibrary(libraryName, array, callback) {
var libraryName = libraryStorage[libraryName] = {};
var dependencies = libraryName['dependencies'] = array;
var dependencies_ids = libraryName['dependencies_ids'] = [];
if (array.length === 0) {
libraryName['string'] = callback();
} else {
for (var i = 0; i < array.length; i++) {
dependencies_ids.push(uuid()); //generates unique id string for each dependency
}
libraryName['string'] = callback.apply(null, dependencies_ids); //saves a string with unique ids, later to be replaced by real values.
}
}
function loadLibrary(libraryName) {
if (!libraryStorage[libraryName]) {
return 'There is no such library'
} else {
var libraryName = libraryStorage[libraryName];
var dependencies = libraryName['dependencies'];
var dependencies_ids = libraryName['dependencies_ids'];
var string = libraryName['string'];
var dependenciesValues = dependencies.map(function(currentValue) {
if (!libraryStorage[currentValue]) {
return 'undefined';
} else {
return libraryStorage[currentValue]['string'];
}
});
for (var i = 0; i < dependencies_ids.length; i++) {
string = string.replace(dependencies_ids[i], dependenciesValues[i]) //replaces unique ids with real values
}
return string;
}
}
function uuid() {
var i, random;
var uuid = '';
for (i = 0; i < 32; i++) {
random = Math.random() * 16 | 0;
if (i === 8 || i === 12 || i === 16 || i === 20) {
uuid += '-';
}
uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random)).toString(16);
}
return uuid;
}
window.librarySystem = librarySystem;
})();
tests({
'TEST1: it should save a callback`s return value and return it back if provided with only 1 argument': function() {
librarySystem('app', [], function() {
return 'ppapp';
});
eq(librarySystem('app'), 'ppapp');
},
'TEST2: it should return a string with a `dependency` library value': function() {
librarySystem('dependency', [], function() {
return 'loaded dependency';
});
librarySystem('app', ['dependency'], function(dependency) {
return 'app with ' + dependency;
});
eq(librarySystem('app'),'app with loaded dependency');
},
'TEST3: it should accept more than one `dependencies`': function() {
librarySystem('name', [], function() {
return 'Gordon';
});
librarySystem('company', [], function() {
return 'Watch and Code';
});
librarySystem('workBlurb', ['name', 'company'], function(name, company) {
return name + ' works at ' + company;
});
eq(librarySystem('workBlurb'),'Gordon works at Watch and Code');
},
'TEST4: it should update the library if the value of `dependency` library has changed': function() {
librarySystem('workBlurb', ['name', 'company'], function(name, company) {
return name + ' works at ' + company;
});
librarySystem('name', [], function() {
return 'Jake';
});
librarySystem('company', [], function() {
return 'Google';
});
eq(librarySystem('workBlurb'),'Jake works at Google');
},
'TEST5: the callback function for each library should run only once. ': function() {
var counter = 0;
librarySystem('test5', ['something'], function(a) {
counter++;
return a;
});
librarySystem('test5');
librarySystem('test5');
librarySystem('test5');
eq(counter, 1);
},
});
</script>