-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnorderedLibrarySystem.html
More file actions
89 lines (72 loc) · 2.34 KB
/
Copy pathUnorderedLibrarySystem.html
File metadata and controls
89 lines (72 loc) · 2.34 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
<script src="TinyTestMod.js"></script>
<script>
(function() {
var libraryStorage = {}
function librarySystem(libraryName, array, callback) {
if (arguments.length > 1) {
if (array.length === 0) {
libraryStorage[libraryName] = callback();
} else {
libraryStorage[libraryName] = {};
libraryStorage[libraryName]['callback'] = callback;
libraryStorage[libraryName]['array'] = array;
};
} else {
if (typeof libraryStorage[libraryName] !== 'object') {
return libraryStorage[libraryName];
} else {
var arrayOfValues = libraryStorage[libraryName]['array'].map(function(currentValue) {
return libraryStorage[currentValue];
});
return libraryStorage[libraryName]['callback'].apply(null,arrayOfValues)
}
};
}
window.librarySystem = librarySystem;
})()
tests({
'TEST1: it should run a callback function': function() {
librarySystem('test1', [], function() {
var test1 = 'ok';
eq(test1,'ok');
});
},
'TEST2: it should save a callback`s return value and return it back if provided with only 1 argument': function() {
librarySystem('test2', [], function() {
return 'value';
});
eq(librarySystem('test2'),'value');
},
'TEST3: it should run a callback with array items values as arguments': function() {
librarySystem('testKey', [], function() {
return 1;
});
librarySystem('testKey2', [], function() {
return 3;
});
librarySystem('test3', ['testKey','testKey2'], function(a,b) {
return a + b;
});
var b = librarySystem('test3');
eq(4,b)
},
'TEST4: it should update the library if the value of corresponding array item has changed': function() {
librarySystem('test4', ['I should change'], function(a) {
return a;
});
librarySystem('I should change', [], function() {
return 'I have changed';
});
eq(librarySystem('test4'),'I have changed')
},
'TEST5: the callback function for each library should run only once. ': function() {
var counter = 0;
librarySystem('test5', ['a'], function(a) {
counter++;
});
librarySystem('test5');
librarySystem('test5');
eq(counter,1)
},
});
</script>