forked from ffMathy/FluffySpoon.JavaScript.Testing.Faking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArguments.ts
More file actions
70 lines (55 loc) · 2.01 KB
/
Copy pathArguments.ts
File metadata and controls
70 lines (55 loc) · 2.01 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
export class Argument<T> {
constructor(
private description: string,
private matchingFunction: (arg: T) => boolean
){}
matches(arg: T) {
return this.matchingFunction(arg);
}
toString() {
return this.description;
}
inspect() {
return this.description;
}
}
export class AllArguments extends Argument<any> {
constructor() {
super('{all}', () => true);
}
}
export class Arg {
private static _all: AllArguments;
static all() {
return this._all = (this._all || new AllArguments());
}
static any(): Argument<any> & any
static any<T extends 'string'>(type: T): Argument<string> & string
static any<T extends 'number'>(type: T): Argument<number> & number
static any<T extends 'boolean'>(type: T): Argument<boolean> & boolean
static any<T extends 'array'>(type: T): Argument<any[]> & any[]
static any<T extends 'function'>(type: T): Argument<Function> & Function
static any<T extends 'string'|'number'|'boolean'|'symbol'|'undefined'|'object'|'function'|'array'>(type: T): Argument<any> & any
static any(type?: string): Argument<any> & any {
const description = !type ? '{any arg}' : '{type ' + type + '}';
return new Argument<any>(description, x => {
if(!type)
return true;
if(typeof x === 'undefined')
return true;
if(type === 'array')
return x && Array.isArray(x);
return typeof x === type;
});
}
static is<T>(predicate: (input: any) => boolean): Argument<T> & T {
return new Argument<T>('{predicate ' + this.toStringify(predicate) + '}', predicate) as Argument<T> & T;
}
private static toStringify(obj: any) {
if(typeof obj.inspect === 'function')
return obj.inspect();
if(typeof obj.toString === 'function')
return obj.toString();
return obj;
}
}