forked from gideo/CodeWars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5kyu_DelegatesInOurLife.js
71 lines (55 loc) · 2.26 KB
/
5kyu_DelegatesInOurLife.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
// 5kyu - Delegates In Our Life
// Story
// I think most of you heard about delegates in C#. Today we need to create our own delegate in JS. Some may think that it's hard, some that it isn't. But this Kata will give you experience in JS programming.
// What is needed? OR PLAN!
// 1. create add method
// First of all you need to fill with your code add method.
// It need to:
// Throw error if argument is not a function
// Throw error if functio already exists
// Compare number of parameters and throw Error if count of parameters not equal to delegate's parameters
// Add to list of functions
// 2. create main method
// Then you need to create main method.
// It need to:
// Compare parameters count
// Compare types of parameters with initialization types
// Execute all functions
// 3. create anything else
// Description
// Your delegate will receive variable amount of types as string
// You can get type of variable using preloaded function GetType( obj )
// Also all types that you need are preloaded. You can see list of types below:
// // BASIC TYPES
// TYPE_BOOL = GetType( true );
// TYPE_NUMBER = GetType( 0 );
// TYPE_STRING = GetType( '' );
// // COMPLEX TYPES
// TYPE_OBJECT = GetType( {} );
// TYPE_FUNCTION = GetType( function() {} );
// TYPE_ARRAY = GetType( [] );
// Now you know what you need to do. Let's begin the Kata!
function delegate(){
let args = [], functions = [];
for ( var i = 0; i < arguments.length; ++i ) args.push( arguments[i] );
let delegate = function() {
if (args.length != arguments.length) throw Error('Wrong parameters count');
if (args.some((t, i) => GetType(arguments[i]) != t)) throw Error('Wrong parameter type');
functions.forEach(f => f(...arguments));
};
delegate.add = function( func ) {
if (GetType(func) != TYPE_FUNCTION) throw Error('Not a function');
if (delegate.contains(func)) throw Error('Function already here');
if (func.length != args.length) throw Error('Wrong parameters count');
functions.push(func);
};
delegate.contains = function( func ) {
return functions.includes(func);
};
delegate.remove = function( func ) {
let index = functions.indexOf(func);
if (index == -1) throw Error('Nothing to remove');
functions.splice(index, 1);
};
return delegate;
}