blob: 10cb6ff01b4c13fe5084c88f88a724b07a6b5020 [file] [log] [blame]
Simon Hunt1eecfa22014-12-16 14:46:29 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Simon Hunt1eecfa22014-12-16 14:46:29 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Huntdc6362a2014-12-18 19:55:23 -080018 ONOS GUI -- Util -- General Purpose Functions
Simon Hunt1eecfa22014-12-16 14:46:29 -080019 */
Simon Huntdc6362a2014-12-18 19:55:23 -080020(function () {
Simon Hunt1eecfa22014-12-16 14:46:29 -080021 'use strict';
22
Simon Hunt59df0b22014-12-17 10:32:25 -080023 function isF(f) {
Simon Hunt404f6b22015-01-21 14:00:56 -080024 return typeof f === 'function' ? f : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080025 }
26
27 function isA(a) {
Simon Hunt404f6b22015-01-21 14:00:56 -080028 // NOTE: Array.isArray() is part of EMCAScript 5.1
29 return Array.isArray(a) ? a : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080030 }
31
32 function isS(s) {
33 return typeof s === 'string' ? s : null;
34 }
35
36 function isO(o) {
Simon Hunt404f6b22015-01-21 14:00:56 -080037 return (o && typeof o === 'object' && o.constructor === Object) ? o : null;
Simon Hunt59df0b22014-12-17 10:32:25 -080038 }
39
40 function contains(a, x) {
41 return isA(a) && a.indexOf(x) > -1;
42 }
43
Simon Hunt51fc40b2015-01-06 13:56:12 -080044 // Returns true if all names in the array are defined as functions
45 // on the given api object; false otherwise.
46 function areFunctions(api, fnNames) {
47 if (!isA(fnNames)) {
48 return false;
49 }
50 var n = fnNames.length,
51 i, name;
52 for (i=0; i<n; i++) {
53 name = fnNames[i];
54 if (!isF(api[name])) {
55 return false;
56 }
57 }
58 return true;
59 }
60
Simon Huntdc6362a2014-12-18 19:55:23 -080061 angular.module('onosUtil')
62 .factory('FnService', [function () {
63 return {
64 isF: isF,
65 isA: isA,
66 isS: isS,
67 isO: isO,
Simon Hunt51fc40b2015-01-06 13:56:12 -080068 contains: contains,
69 areFunctions: areFunctions
Simon Huntdc6362a2014-12-18 19:55:23 -080070 };
Simon Hunt1eecfa22014-12-16 14:46:29 -080071 }]);
72
Simon Huntdc6362a2014-12-18 19:55:23 -080073}());