11'use strict' ;
22
3- ( function ( ng ) {
4- var injector = ng . injector ( [ 'configuration' , 'ng' ] ) ;
5- var environment = injector . get ( 'ENV' ) ;
3+ var normal_distribution = function ( ) {
4+ return ( Math . random ( ) * 2 - 1 ) + ( Math . random ( ) * 2 - 1 ) + ( Math . random ( ) * 2 - 1 ) ;
5+ } ;
6+ var exponential_distribution = function ( min , max ) {
7+ var increment = ( max - min ) / 6 ;
8+ var num ;
9+ do {
10+ var u = Math . random ( ) ;
11+ var t = ( - 1 * Math . log ( u ) ) / 1 ;
12+ num = min + ( t * increment ) ;
13+ }
14+ while ( num <= min || num >= max ) ;
15+ return Math . floor ( num ) ;
16+ } ;
17+ var distributed_random = function ( min , max ) {
18+ var mean = ( min + max ) / 2 ;
19+ var deviation = mean / 3 ;
20+ var num ;
21+ do {
22+ num = Math . floor ( ( normal_distribution ( ) * deviation ) + mean ) ;
23+ }
24+ while ( num <= min || num >= max ) ;
25+ return num ;
26+ } ;
627
7- var generateInvestors = function ( investorCount ) {
8- var investorList = [ ] ;
928
10- for ( var i = 0 ; i < investorCount ; i ++ ) {
11- investorList . push ( {
12- id : i ,
13- name : 'investor' + i ,
14- invested_company_ids : [ ] ,
15- invested_category_ids : [ ]
16- } ) ;
17- }
29+ ( function ( ng , fk ) {
30+ var injector = ng . injector ( [ 'configuration' , 'ng' ] ) ;
31+ var environment = injector . get ( 'ENV' ) ;
1832
19- return investorList ;
33+ /**
34+ * Generate a company object with random, usable attributes.
35+ *
36+ * @param {number } [id] Not required. Define an ID for the returned company.
37+ * @return {object } A company with randomly generated data values.
38+ */
39+ var randomCompany = function ( id ) {
40+ var name = fk . Company . companyName ( ) ;
41+ id = id || 0 ;
42+ return {
43+ id : id ,
44+ name : name ,
45+ permalink : name . toLowerCase ( ) ,
46+ category_id : 0 ,
47+ total_funding : distributed_random ( 1 , 6e9 ) , //Random between 1 and 6 billion
48+ latitude : 1.0 ,
49+ longitude : 1.0 ,
50+ investor_ids : [ ] ,
51+ funding_rounds : [ ]
52+ } ;
2053 } ;
2154
22- var generateCategories = function ( categoryCount ) {
23- var categoryList = [ ] ;
24-
25- for ( var i = 0 ; i < categoryCount ; i ++ ) {
26- categoryList . push ( {
27- id : i ,
28- name : 'category' + i ,
29- company_ids : [ ] ,
30- investor_ids : [ ]
31- } ) ;
32- }
33-
34- return categoryList ;
55+ /**
56+ * Generate an investor object with random, usable attributes.
57+ *
58+ * @param {number } [id] Not required. Define an ID for the returned investor.
59+ * @return {object } An investor with randomly generated data values.
60+ */
61+ var randomInvestor = function ( id ) {
62+ var type = Math . random ( ) < 0.5 ? 'company' : 'person' ;
63+ var name = type === 'company' ? fk . Company . companyName ( ) : fk . Name . findName ( ) ;
64+ id = id || 0 ;
65+ return {
66+ id : id ,
67+ name : name ,
68+ investor_type : type ,
69+ permalink : name . toLowerCase ( ) ,
70+ invested_company_ids : [ ] ,
71+ invested_category_ids : [ ]
72+ } ;
3573 } ;
3674
37- var generateCompanies = function ( categories , investors , companyCount ) {
38- var getRandomInRange = function ( from , to , fixed ) {
39- return ( Math . random ( ) * ( to - from ) + from ) . toFixed ( fixed ) * 1 ;
75+ /**
76+ * Generate a category object with random, usable attributes.
77+ *
78+ * @param {number } [id] Not required. Define an ID for the returned category.
79+ * @return {object } A category with randomly generated data values.
80+ */
81+ var randomCategory = function ( id ) {
82+ id = id || 0 ;
83+ return {
84+ id : id ,
85+ name : fk . random . bs_noun ( ) ,
86+ permalink : name . toLowerCase ( ) ,
87+ company_ids : [ ] ,
88+ investor_ids : [ ]
4089 } ;
41- var companyList = [ ] ;
42-
43- for ( var i = 0 ; i < companyCount ; i ++ ) {
44- var company = { } ;
45- var associationLimit = Math . floor ( Math . random ( ) * 10 ) ;
46- company . id = i ;
47- company . name = 'company' + i ;
48- company . zip_code = Math . floor ( Math . random ( ) * 90000 ) + 10000 ;
49- company . total_funding = Math . floor ( Math . random ( ) * 9900000 ) + 100000 ;
50- company . latitude = getRandomInRange ( 22 , 49 , 3 ) ;
51- company . longitude = getRandomInRange ( - 124 , - 66 , 3 ) ;
52- var category = categories [ Math . floor ( Math . random ( ) * categories . length ) ] ;
53- company . category_code = category ;
54-
55- if ( category . company_ids . indexOf ( company . id ) === - 1 ) {
56- category . company_ids . push ( company . id ) ;
57- }
58-
59- company . investor_ids = [ ] ;
60- company . funding_rounds = [ ] ;
61-
62- for ( var j = 0 ; j < associationLimit ; j ++ ) {
63- var fundingRound = { } ;
64- fundingRound . id = j ;
65- fundingRound . raised_amount = '$1000' ;
66- fundingRound . funded_on = '2013-01-01' ;
67-
68- for ( var k = 0 ; k < associationLimit ; k ++ ) {
69- var investor = investors [ Math . floor ( Math . random ( ) * investors . length ) ] ;
70- investor . invested_company_ids . push ( company . id ) ;
71- investor . invested_category_ids . push ( category . id ) ;
72-
73- if ( category . investor_ids . indexOf ( investor . id ) === - 1 ) {
74- category . investor_ids . push ( investor . id ) ;
75- }
90+ } ;
7691
77- if ( company . investor_ids . indexOf ( investor . id ) === - 1 ) {
78- company . investor_ids . push ( investor . id ) ;
79- }
80- }
81- company . funding_rounds . push ( fundingRound ) ;
82- }
83- companyList . push ( company ) ;
92+ /**
93+ * Generate a list of data based on a supplied function
94+ *
95+ * @param {number } [count] How many items should be returned in the data list
96+ * @param {function } [genFn] A function that when called returns an instance of a single item in the list
97+ * @return {array } A list of data generated from the supplied function
98+ */
99+ var generateDataList = function ( count , genFn ) {
100+ var generated = [ ] ;
101+ for ( var i = 0 ; i < count ; i ++ ) {
102+ generated . push ( genFn ( i ) ) ;
84103 }
104+ return generated ;
105+ } ;
85106
86- return companyList ;
107+ /**
108+ * Link a generated list of data together
109+ *
110+ * @param {array } [companies] An unlinked list of companies
111+ * @param {array } [investors] An unlinked list of investors
112+ * @param {array } [categories] An unlinked list of categories
113+ */
114+ var linkGeneratedLists = function ( companies , investors , categories ) {
115+ _ . each ( companies , function ( company ) {
116+ var category = categories [ exponential_distribution ( 0 , categories . length ) ] ;
117+ company . category_id = category . id ;
118+ category . company_ids . push ( company . id ) ;
119+
120+ _ ( exponential_distribution ( 1 , 10 ) ) . times ( function ( ) {
121+ var investor = investors [ exponential_distribution ( 0 , investors . length ) ] ;
122+ company . investor_ids . push ( investor . id ) ;
123+ category . investor_ids . push ( investor . id ) ;
124+ investor . invested_company_ids . push ( company . id ) ;
125+ investor . invested_category_ids . push ( company . category_id ) ;
126+ } ) ;
127+ } ) ;
87128 } ;
88129
130+ /**
131+ * Initiate and respond with fake backend data instead of querying an actual API
132+ */
89133 var setupStubbedBackend = function ( ) {
90- var investors = generateInvestors ( 20 ) ;
91- var categories = generateCategories ( 10 ) ;
92- var companies = generateCompanies ( categories , investors , 500 ) ;
134+ var categories = generateDataList ( 42 , randomCategory ) ;
135+ var investors = generateDataList ( 9987 , randomInvestor ) ;
136+ var companies = generateDataList ( 16635 , randomCompany ) ;
137+ linkGeneratedLists ( companies , investors , categories ) ;
93138
94139 ng . module ( 'crunchinatorApp' )
95140 . config ( [ '$provide' , function ( $provide ) {
96- $provide . decorator ( '$httpBackend' , angular . mock . e2e . $httpBackendDecorator ) ;
141+ $provide . decorator ( '$httpBackend' , ng . mock . e2e . $httpBackendDecorator ) ;
97142 } ] ) . run ( [ '$httpBackend' , function ( $httpBackend ) {
98143 $httpBackend . when ( 'GET' , '/companies.json' ) . respond ( { companies : companies } ) ;
99144 $httpBackend . when ( 'GET' , '/categories.json' ) . respond ( { categories : categories } ) ;
119164 }
120165
121166 ng . module ( 'crunchinatorApp.models' ) . constant ( 'API_BASE_URL' , base_url ) ;
122- } ) ( angular ) ;
167+ } ) ( angular , window . Faker ) ;
0 commit comments