@@ -8,12 +8,10 @@ const pushToStore = (collectionName, records, store) => {
8
8
recordFactory ( record , collectionName , store ) ) ;
9
9
} ;
10
10
11
- const deepMap = ( obj , fn ) => {
12
- return _ . mapValues ( obj , ( value , key ) => {
13
- if ( _ . isPlainObject ( value ) ) return deepMap ( value , fn ) ;
14
- return fn ( value ) ;
15
- } ) ;
16
- }
11
+ const deepMap = ( obj , fn ) => _ . mapValues ( obj , ( value ) => {
12
+ if ( _ . isPlainObject ( value ) ) return deepMap ( value , fn ) ;
13
+ return fn ( value ) ;
14
+ } ) ;
17
15
18
16
export class Database {
19
17
constructor ( ) {
@@ -23,15 +21,16 @@ export class Database {
23
21
all ( collectionName , raw = false ) {
24
22
this . checkFactoryPresence ( collectionName ) ;
25
23
const records = _ . cloneDeep ( this . store [ collectionName ] ) ;
26
- if ( raw ) { return records ; }
24
+ if ( raw ) { return records ; }
27
25
28
- return this . serialize ( records , collectionName )
26
+ return this . serialize ( records , collectionName ) ;
29
27
}
30
28
31
29
belongsTo ( collectionName , predicate ) {
32
- return ( ) => predicate ?
33
- this . find ( collectionName , predicate ) :
34
- this . randomRecord ( collectionName ) ;
30
+ return ( ) => {
31
+ if ( predicate ) { return this . find ( collectionName , predicate ) ; }
32
+ return this . randomRecord ( collectionName ) ;
33
+ } ;
35
34
}
36
35
37
36
hasMany ( collectionName , limit = randomIndex ( this . all ( collectionName ) ) + 1 ) {
@@ -48,11 +47,13 @@ export class Database {
48
47
this . checkFactoryPresence ( collectionName ) ;
49
48
50
49
const factory = this . factoryFor ( collectionName ) ;
51
- const records = this . store [ collectionName ] || [ ] ;
50
+ const records = this . store [ collectionName ] || [ ] ;
52
51
53
- while ( size -- ) {
54
- const record = deepMap ( factory ( faker ) , field =>
55
- _ . isFunction ( field ) ? field ( ) : field ) ;
52
+ for ( let idx = 0 ; idx < size ; ++ idx ) {
53
+ const record = deepMap ( factory ( faker ) , ( field ) => {
54
+ if ( _ . isFunction ( field ) ) { return field ( ) ; }
55
+ return field ;
56
+ } ) ;
56
57
57
58
records . push ( this . decorateRecord ( collectionName , record ) ) ;
58
59
}
@@ -62,7 +63,7 @@ export class Database {
62
63
63
64
decorateRecord ( collectionName , record ) {
64
65
this . checkFactoryPresence ( collectionName ) ;
65
- return _ . assign ( { } , record , { id : this . uuid ( collectionName ) } ) ;
66
+ return _ . assign ( { } , record , { id : this . uuid ( collectionName ) } ) ;
66
67
}
67
68
68
69
factoryFor ( collectionName ) {
@@ -101,8 +102,7 @@ export class Database {
101
102
push ( collectionName , record ) {
102
103
this . checkFactoryPresence ( collectionName ) ;
103
104
104
- const factory = this . factoryFor ( collectionName ) ;
105
- const records = this . store [ collectionName ] || [ ] ;
105
+ const records = this . store [ collectionName ] || [ ] ;
106
106
const content = _ . castArray ( record ) ;
107
107
108
108
records . push ( ...content ) ;
@@ -111,14 +111,14 @@ export class Database {
111
111
}
112
112
113
113
register ( collectionName , factory , serializer ) {
114
- this . factories [ collectionName ] = { factory, serializer} ;
114
+ this . factories [ collectionName ] = { factory, serializer } ;
115
115
this . store [ collectionName ] = [ ] ;
116
116
}
117
117
118
118
serialize ( record , collectionName ) {
119
119
const serializer = this . serializerFor ( collectionName ) ;
120
120
const records = _ . castArray ( record ) ;
121
- if ( ! serializer ) { return records ; }
121
+ if ( ! serializer ) { return records ; }
122
122
123
123
return records . map ( r => serializer ( r , collectionName ) ) ;
124
124
}
@@ -137,9 +137,8 @@ export class Database {
137
137
const all = this . all ( collectionName ) ;
138
138
const records = [ ] ;
139
139
140
- while ( limit ) {
140
+ for ( let idx = 0 ; idx < limit ; ++ idx ) {
141
141
records . push ( randomItem ( all ) ) ;
142
- limit -- ;
143
142
}
144
143
145
144
return records ;
@@ -152,14 +151,14 @@ export class Database {
152
151
setInitialState ( ) {
153
152
this . factories = { } ;
154
153
this . store = { } ;
155
- this . _uuids = { } ;
154
+ this . uuids = { } ;
156
155
}
157
156
158
157
uuid ( collectionName ) {
159
158
this . checkFactoryPresence ( collectionName ) ;
160
159
161
- const id = this . _uuids [ collectionName ] || 0 ;
162
- this . _uuids [ collectionName ] = id + 1 ;
160
+ const id = this . uuids [ collectionName ] || 0 ;
161
+ this . uuids [ collectionName ] = id + 1 ;
163
162
164
163
return id ;
165
164
}
0 commit comments