1
1
import * as fs from 'fs' ;
2
- import * as _ from 'underscore' ;
3
2
4
3
export enum ActionOnInvalid {
5
4
THROW = 'throw' ,
@@ -26,9 +25,13 @@ export interface Cluster {
26
25
}
27
26
28
27
export function newClusters ( a : any , opts ?: Partial < ConfigOptions > ) : Cluster [ ] {
28
+ if ( ! Array . isArray ( a ) ) {
29
+ return [ ] ;
30
+ }
31
+
29
32
const options = Object . assign ( defaultNewConfigOptions ( ) , opts || { } ) ;
30
33
31
- return _ . compact ( _ . map ( a , clusterIterator ( options . onInvalidEntry ) ) ) ;
34
+ return a . map ( clusterIterator ( options . onInvalidEntry ) ) . filter ( Boolean ) as Cluster [ ] ;
32
35
}
33
36
34
37
export function exportCluster ( cluster : Cluster ) : any {
@@ -44,8 +47,10 @@ export function exportCluster(cluster: Cluster): any {
44
47
} ;
45
48
}
46
49
47
- function clusterIterator ( onInvalidEntry : ActionOnInvalid ) : _ . ListIterator < any , Cluster | null > {
48
- return ( elt : any , i : number , list : _ . List < any > ) : Cluster | null => {
50
+ function clusterIterator (
51
+ onInvalidEntry : ActionOnInvalid ,
52
+ ) : ( elt : any , i : number , list : any [ ] ) => Cluster | null {
53
+ return ( elt : any , i : number , list : any [ ] ) : Cluster | null => {
49
54
try {
50
55
if ( ! elt . name ) {
51
56
throw new Error ( `clusters[${ i } ].name is missing` ) ;
@@ -90,9 +95,13 @@ export interface User {
90
95
}
91
96
92
97
export function newUsers ( a : any , opts ?: Partial < ConfigOptions > ) : User [ ] {
98
+ if ( ! Array . isArray ( a ) ) {
99
+ return [ ] ;
100
+ }
101
+
93
102
const options = Object . assign ( defaultNewConfigOptions ( ) , opts || { } ) ;
94
103
95
- return _ . compact ( _ . map ( a , userIterator ( options . onInvalidEntry ) ) ) ;
104
+ return a . map ( userIterator ( options . onInvalidEntry ) ) . filter ( Boolean ) as User [ ] ;
96
105
}
97
106
98
107
export function exportUser ( user : User ) : any {
@@ -112,8 +121,8 @@ export function exportUser(user: User): any {
112
121
} ;
113
122
}
114
123
115
- function userIterator ( onInvalidEntry : ActionOnInvalid ) : _ . ListIterator < any , User | null > {
116
- return ( elt : any , i : number , list : _ . List < any > ) : User | null => {
124
+ function userIterator ( onInvalidEntry : ActionOnInvalid ) : ( elt : any , i : number , list : any [ ] ) => User | null {
125
+ return ( elt : any , i : number , list : any [ ] ) : User | null => {
117
126
try {
118
127
if ( ! elt . name ) {
119
128
throw new Error ( `users[${ i } ].name is missing` ) ;
@@ -161,9 +170,13 @@ export interface Context {
161
170
}
162
171
163
172
export function newContexts ( a : any , opts ?: Partial < ConfigOptions > ) : Context [ ] {
173
+ if ( ! Array . isArray ( a ) ) {
174
+ return [ ] ;
175
+ }
176
+
164
177
const options = Object . assign ( defaultNewConfigOptions ( ) , opts || { } ) ;
165
178
166
- return _ . compact ( _ . map ( a , contextIterator ( options . onInvalidEntry ) ) ) ;
179
+ return a . map ( contextIterator ( options . onInvalidEntry ) ) . filter ( Boolean ) as Context [ ] ;
167
180
}
168
181
169
182
export function exportContext ( ctx : Context ) : any {
@@ -173,8 +186,10 @@ export function exportContext(ctx: Context): any {
173
186
} ;
174
187
}
175
188
176
- function contextIterator ( onInvalidEntry : ActionOnInvalid ) : _ . ListIterator < any , Context | null > {
177
- return ( elt : any , i : number , list : _ . List < any > ) : Context | null => {
189
+ function contextIterator (
190
+ onInvalidEntry : ActionOnInvalid ,
191
+ ) : ( elt : any , i : number , list : any [ ] ) => Context | null {
192
+ return ( elt : any , i : number , list : any [ ] ) : Context | null => {
178
193
try {
179
194
if ( ! elt . name ) {
180
195
throw new Error ( `contexts[${ i } ].name is missing` ) ;
0 commit comments