@@ -20,26 +20,38 @@ function getRandomIntInclusive(min, max) {
2020 return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ; //The maximum is inclusive and the minimum is inclusive
2121}
2222
23- function initFakeData ( ) {
23+ if ( hasSupportLocalStorage ( ) ) {
2424 try {
2525 data = JSON . parse ( localStorage . getItem ( DB_KEY ) ) ;
26+ // fallback to array when its null
27+ if ( ! data ) {
28+ data = [ ] ;
29+ }
2630 } catch ( error ) {
2731 data = [ ] ;
2832 }
29- if ( data && data . length === 0 ) {
30- for ( let index = 0 ; index < 5 ; index ++ ) {
31- data . push ( {
32- id : faker . random . uuid ( ) ,
33- name : faker . name . findName ( ) ,
34- age : getRandomIntInclusive ( 20 , 40 ) ,
35- nickname : faker . internet . userName ( )
36- } ) ;
33+ }
34+
35+ function initFakeData ( ) {
36+ const dev = process . env . NODE_ENV !== 'production' ;
37+ if ( dev ) {
38+ if ( data && data . length === 0 ) {
39+ for ( let index = 0 ; index < 5 ; index ++ ) {
40+ data . push ( {
41+ id : faker . random . uuid ( ) ,
42+ name : faker . name . findName ( ) ,
43+ age : getRandomIntInclusive ( 20 , 40 ) ,
44+ nickname : faker . internet . userName ( )
45+ } ) ;
46+ }
47+ hasSupportLocalStorage ( ) &&
48+ localStorage . setItem ( DB_KEY , JSON . stringify ( data ) ) ;
3749 }
38- hasSupportLocalStorage ( ) &&
39- localStorage . setItem ( DB_KEY , JSON . stringify ( data ) ) ;
4050 }
4151}
4252
53+ initFakeData ( ) ;
54+
4355function addUser ( user , callback ) {
4456 const newUser = { id : faker . random . uuid ( ) , ...user } ;
4557 data . push ( newUser ) ;
0 commit comments