Skip to content

Commit 3c29832

Browse files
committed
feat(app): implement util module for thin, reusable functions
1 parent 2fab1f5 commit 3c29832

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

Diff for: app/templates/client/components/auth(auth)/auth.module(js).js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
angular.module('<%= scriptAppName %>.auth', [
44
'<%= scriptAppName %>.constants',
5+
'<%= scriptAppName %>.util',
56
'ngCookies'<% if (filters.ngroute) { %>,
67
'ngRoute'<% } if (filters.uirouter) { %>,
78
'ui.router'<% } %>

Diff for: app/templates/client/components/auth(auth)/auth.service(js).js

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@
22

33
(function() {
44

5-
function AuthService($location, $http, $cookies, $q, appConfig, User) {
6-
/**
7-
* Return a callback or noop function
8-
*
9-
* @param {Function|*} cb - a 'potential' function
10-
* @return {Function}
11-
*/
12-
var safeCb = function(cb) {
13-
return (angular.isFunction(cb)) ? cb : angular.noop;
14-
},
15-
16-
currentUser = {},
17-
userRoles = appConfig.userRoles || [];
5+
function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
6+
var safeCb = Util.safeCb;
7+
var currentUser = {};
8+
var userRoles = appConfig.userRoles || [];
189

1910
if ($cookies.get('token') && $location.path() !== '/logout') {
2011
currentUser = User.get();

Diff for: app/templates/client/components/util/util.module.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
angular.module('<%= scriptAppName %>.util', []);

Diff for: app/templates/client/components/util/util.service.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
(function() {
4+
5+
/**
6+
* The Util service is for thin, globally reusable, utility functions
7+
*/
8+
function UtilService($window) {
9+
10+
var Util = {
11+
12+
/**
13+
* Return a callback or noop function
14+
*
15+
* @param {Function|*} cb - a 'potential' function
16+
* @return {Function}
17+
*/
18+
safeCb: function(cb) {
19+
return (angular.isFunction(cb)) ? cb : angular.noop;
20+
},
21+
22+
/**
23+
* Parse a given url with the use of an anchor element
24+
*
25+
* @param {String} url - the url to parse
26+
* @return {Object} - the parsed url, anchor element
27+
*/
28+
urlParse: function(url) {
29+
var a = document.createElement('a');
30+
a.href = url;
31+
return a;
32+
},
33+
34+
/**
35+
* Test whether or not a given url is same origin
36+
*
37+
* @param {String} url - url to test
38+
* @param {String|String[]} [origins] - additional origins to test against
39+
* @return {Boolean} - true if url is same origin
40+
*/
41+
isSameOrigin: function(url, origins) {
42+
url = Util.urlParse(url);
43+
origins = (origins && [].concat(origins)) || [];
44+
origins = origins.map(Util.urlParse);
45+
origins.push($window.location);
46+
origins = origins.filter(function(o) {
47+
return url.hostname === o.hostname &&
48+
url.port === o.port &&
49+
url.protocol === o.protocol;
50+
});
51+
return (origins.length >= 1);
52+
}
53+
54+
};
55+
56+
return Util;
57+
}
58+
59+
angular.module('<%= scriptAppName %>.util')
60+
.factory('Util', UtilService);
61+
62+
})();

Diff for: test/test-file-creation.js

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ describe('angular-fullstack generator', function () {
182182
'client/components/navbar/navbar.' + markup,
183183
'client/components/navbar/navbar.controller.' + script,
184184
'client/components/navbar/navbar.directive.' + script,
185+
'client/components/util/util.module.' + script,
186+
'client/components/util/util.service.' + script,
185187
'server/.jshintrc',
186188
'server/.jshintrc-spec',
187189
'server/app.js',

0 commit comments

Comments
 (0)