12
12
angular . module ( 'environment' , [ ] ) .
13
13
provider ( 'envService' , function ( ) {
14
14
15
+ 'use strict' ;
16
+
17
+ var local = { } ;
18
+
19
+ local . pregQuote = function ( string , delimiter ) {
20
+ return ( string + '' ) . replace ( new RegExp ( '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + ( delimiter || '' ) + '-]' , 'g' ) , '\\$&' ) ;
21
+ } ;
22
+
23
+ local . stringToRegex = function ( string ) {
24
+ return new RegExp ( local . pregQuote ( string ) . replace ( / \\ \* / g, '.*' ) . replace ( / \\ \? / g, '.' ) , 'g' ) ;
25
+ } ;
26
+
15
27
this . environment = 'development' ; // default
16
28
this . data = { } ; // user defined environments data
17
29
@@ -55,11 +67,14 @@ angular.module('environment', []).
55
67
* @return {Void }
56
68
*/
57
69
this . read = function ( variable ) {
58
- if ( variable !== 'all' ) {
59
- return this . data . vars [ this . get ( ) ] [ variable ] ;
70
+ if ( typeof variable === 'undefined' || variable === '' || variable === 'all' ) {
71
+ return this . data . vars [ this . get ( ) ] ;
72
+ }
73
+ else if ( typeof this . data . vars [ this . get ( ) ] [ variable ] === 'undefined' ) {
74
+ return this . data . vars . defaults [ variable ] ;
60
75
}
61
76
62
- return this . data . vars [ this . get ( ) ] ;
77
+ return this . data . vars [ this . get ( ) ] [ variable ] ;
63
78
} ;
64
79
65
80
/**
@@ -82,13 +97,15 @@ angular.module('environment', []).
82
97
* @return {Void }
83
98
*/
84
99
this . check = function ( ) {
85
- var location = window . location . href ,
100
+ var location = window . location . host ,
86
101
self = this ;
87
102
88
103
angular . forEach ( this . data . domains , function ( v , k ) {
89
104
angular . forEach ( v , function ( v ) {
90
- if ( location . match ( '//' + v ) ) {
105
+ if ( location . match ( local . stringToRegex ( v ) ) ) {
91
106
self . environment = k ;
107
+
108
+ return false ;
92
109
}
93
110
} ) ;
94
111
} ) ;
0 commit comments