Skip to content

Commit

Permalink
domain comprobation/matching fix
Browse files Browse the repository at this point in the history
  • Loading branch information
juanpablob committed Apr 21, 2017
1 parent e227242 commit 3cd588e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
15 changes: 10 additions & 5 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ angular.module('acme', ['environment']).
// set the domains and variables for each environment
envServiceProvider.config({
domains: {
development: ['localhost', 'dev.local'],
production: ['acme.com', 'acme.net', 'acme.org']
// anotherStage: []
development: ['localhost', 'local.dev'],
production: ['acme.com', '*.dev.prod'],
test: ['acme.test', 'acme.net', 'acme.*.test']
},
vars: {
development: {
Expand All @@ -16,8 +16,13 @@ angular.module('acme', ['environment']).
// antoherCustomVar: ''
},
production: {
apiUrl: '//api.acme.com/v2',
staticUrl: '//static.acme.com'
apiUrl: '//api.acme.dev.prod/v1',
staticUrl: '//static.acme.dev.prod'
// antoherCustomVar: ''
},
test: {
apiUrl: '//api.acme.dev.test/v1',
staticUrl: '//static.acme.test.prod'
// antoherCustomVar: ''
}
}
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script>
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../dist/angular-environment.js"></script>
<script src="app.js"></script>
</head>
Expand Down
16 changes: 14 additions & 2 deletions src/angular-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
angular.module('environment', []).
provider('envService', function() {

var private = {};

private.pregQuote = function(string, delimiter) {
return (string + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
};

private.stringToRegex = function(string) {
return new RegExp(private.pregQuote(string).replace(/\\\*/g, '.*').replace(/\\\?/g, '.'), 'g');
};

this.environment = 'development'; // default
this.data = {}; // user defined environments data

Expand Down Expand Up @@ -82,13 +92,15 @@ angular.module('environment', []).
* @return {Void}
*/
this.check = function() {
var location = window.location.href,
var location = window.location.host,
self = this;

angular.forEach(this.data.domains, function(v, k) {
angular.forEach(v, function(v) {
if (location.match('//' + v)) {
if (location.match(private.stringToRegex(v))) {
self.environment = k;

return false;
}
});
});
Expand Down

0 comments on commit 3cd588e

Please sign in to comment.