From 3cd588eb6de979fb3f6dae5310e82f91979e2a8d Mon Sep 17 00:00:00 2001 From: "Juan Pablo [Lipibook]" Date: Fri, 21 Apr 2017 20:07:03 +1200 Subject: [PATCH] domain comprobation/matching fix --- demo/app.js | 15 ++++++++++----- demo/index.html | 2 +- src/angular-environment.js | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/demo/app.js b/demo/app.js index e4372e0..b527ab7 100644 --- a/demo/app.js +++ b/demo/app.js @@ -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: { @@ -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: '' } } diff --git a/demo/index.html b/demo/index.html index f42facf..23331b1 100644 --- a/demo/index.html +++ b/demo/index.html @@ -8,7 +8,7 @@ - + diff --git a/src/angular-environment.js b/src/angular-environment.js index a10ff4e..d7e993e 100644 --- a/src/angular-environment.js +++ b/src/angular-environment.js @@ -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 @@ -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; } }); });