fix bug - Service dependency - Can't select parent inputs that came from other instances
[sdc.git] / catalog-ui / src / app / utils / prototypes.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 interface String {
22     format(variables:Array<string>):string
23 }
24
25 interface Array<T> {
26     clean(o:T):Array<T>;
27 }
28
29
30 /**
31  * This function will replace the %<number> with strings (from array).
32  * Example: "Requested '%1' resource was not found.".format(["MyResource"]);
33  * Note: in case the array contains empty string the function will also remove the '' or the "".
34  */
35 if (!String.hasOwnProperty("format")) {
36     String.prototype["format"] = function (variables:Array<string>):string {
37
38         if (variables === null || variables === undefined || variables.length === 0) {
39             variables = [''];
40         }
41
42         for (let i = 0; i < variables.length; i++) {
43             if (variables[i] === '' || variables[i] === null) {
44                 variables[i] = '--DELETE--';
45             }
46         }
47
48         let res = this.replace(/%(\d+)/g, function (_, m) {
49             return variables[--m];
50         });
51
52         res = res.replace(" '--DELETE--' ", " ");
53         res = res.replace(" \"--DELETE--\" ", " ");
54         res = res.replace("'--DELETE--'", "");
55         res = res.replace("\"--DELETE--\"", "");
56         res = res.replace("--DELETE--", "");
57
58         return res;
59     };
60 }
61
62 if (!String.hasOwnProperty("capitalizeFirstLetter")) {
63     String.prototype["capitalizeFirstLetter"] = function () {
64         return this.charAt(0).toUpperCase() + this.slice(1);
65     };
66 }
67
68 if (!String.hasOwnProperty("replaceAll")) {
69     String.prototype["replaceAll"] = function (find:string, replace:string):string {
70         return this.replace(new RegExp(find, 'g'), replace);
71     };
72 }
73
74 if (!Array.hasOwnProperty("clean")) {
75     Array.prototype.clean = function (deleteValue) {
76         for (let i = 0; i < this.length; i++) {
77             if (this[i] == deleteValue) {
78                 this.splice(i, 1);
79                 i--;
80             }
81         }
82         return this;
83     };
84 }