-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixins.ts
More file actions
68 lines (60 loc) · 1.89 KB
/
Copy pathmixins.ts
File metadata and controls
68 lines (60 loc) · 1.89 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
export class Name {
constructor(
public firstName: string,
public lastName: string
) {}
get fullName(): string {
return `${this.firstName} ${this.lastName}`
}
}
type Constructor = new (...args: any[]) => {} // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-empty-object-type
function applyFlattening<TBase extends Constructor>(Base: TBase) {
return class Flattener extends Base {
flatten(): string {
return Object.entries(this).reduce(
(
flattened: string,
[_ /* eslint-disable-line @typescript-eslint/no-unused-vars*/, value]
): string => {
return flattened + String(value)
},
''
)
}
}
}
export const FlattenableName = applyFlattening(Name)
type NameConstructor = new (
...args: any[] // eslint-disable-line @typescript-eslint/no-explicit-any
) => {
firstName: string
lastName: string
}
function applyNameFlattening<TBase extends NameConstructor>(Base: TBase) {
return class NameFlattener extends Base {
flatten(): string {
return this.firstName + this.lastName
}
}
}
export const FlattenableDualName = applyNameFlattening(Name)
export class ShortName {
constructor(public firstName: string) {}
}
//export const FlattenableShortName = applyNameFlattening(ShortName) // Argument of type 'typeof ShortName' is not assignable to parameter of type 'NameConstructor'.
function applyArrayifier<TBase extends Constructor>(Base: TBase) {
return class Arrayifier extends Base {
arrayify(): string[] {
return Object.entries(this).reduce(
(
arrayified: string[],
[_ /* eslint-disable-line @typescript-eslint/no-unused-vars*/, value]
): string[] => {
return arrayified.concat(String(value).split(''))
},
[]
)
}
}
}
export const ArrayableFlattenableName = applyArrayifier(FlattenableName)