summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/shared/utils/utils.ts
blob: c3f38e526904b8fbec69be005750b019470e90ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
    Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

export class Util {
    // Time formatting milliseconds to normal
    dateformater(vmstime) {
        if (!vmstime) {
            return ''
        }
        let mstime = Number((vmstime + '').slice(0, 13));
        let time = new Date(mstime);
        let year = time.getFullYear();
        let month = time.getMonth() + 1;
        let day = time.getDate();
        let hours = time.getHours();
        let minutes = time.getMinutes();
        let seconds = time.getSeconds();
        let formattime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
        return formattime;
    }
    
    getRulesText(words: string, title: string, index: number, rulesText: any[]){
        return rulesText[index] = words + title
    };
    validator(title: string="", key: string="", val: any="", index: number=0, rulesText: any[]=[], validateRulesShow: any[]=[]) {
        let maxNumberReg = /^([1-9]\d{0,4}|100000)$/, // Check integer between 1 ~ 100000
            expDataRateReg  = /^([1-9]\d{2}|[1-3]\d{3}|3000)$/, // Check integer between 100 ~ 3000
            latencyReg = /^1[0-9]$|^[2-9]\d$|^1\d{2}$|^200$/; // Check integers between 10 and 200
        if (val === null || val.replace(/\s*/g, '').length <= 0) {
            validateRulesShow[index] = true;
            this.getRulesText('Please enter ', title, index,rulesText);
            return false
        } else {
            validateRulesShow[index] = false;
        }
        if (isNaN(val) && (key === 'maxNumberofUEs' || key === 'expDataRateDL' || key === 'expDataRateUL' || key === 'latency')) {
            validateRulesShow[index] = true;
            this.getRulesText('Only numbers can be entered', '', index,rulesText);
            return false
        }
        if (!isNaN(val) && key === 'maxNumberofUEs' && !maxNumberReg.test(val)) {
            validateRulesShow[index] = true;
            this.getRulesText('Scope: 1-100000', '', index,rulesText);
            return false
        } else {
            validateRulesShow[index] = false;
        }
        if ( !isNaN(val) && (key === 'expDataRateDL' || key === 'expDataRateUL') && !expDataRateReg.test(val)) {
            validateRulesShow[index] = true;
            this.getRulesText('Scope: 100-3000', '', index,rulesText);
            return false
        } else {
            validateRulesShow[index] = false;
        }
        if (!isNaN(val) && key === 'latency' && !latencyReg.test(val)) {
            validateRulesShow[index] = true;
            this.getRulesText('Scope: 10-200', '', index,rulesText);
            return false
        } else {
            validateRulesShow[index] = false;
        }
    }
    isInteger (value: any) : boolean{
        // for common string and undefined, eg '123a3'
		if (isNaN(value)) {
			return false;
		} else if (isNaN(parseInt(value))) {
			return false;
		} else if (Number(value) < 0 || (Number(value)%1 !== 0)){
			return false;
		} else {
			return true;
		}
    }
    judgeType (a: any) : string {
		return Object.prototype.toString.call(a)
    }
    isEmpty (a: any): boolean {
        const type = this.judgeType(a);
        if (type === 'object Null' || type === '[object undefined]' || a === '') {
            return true;
        } else {
            return false;
        }
    }
    deepCheck (target: any) : boolean{
        //used to verify that each item is not '' or undefined in a object or an array
        let type = this.judgeType(target);
		if (type === '[object Array]') {
			for (let i = 0; i < target.length; i++) {
				if (!this.deepCheck(target[i])) {
					return false;
				}
			}
		} else if (type === '[object Object]') {
			for (const prop in target) {
				if (target.hasOwnProperty(prop)) {
                    if (!this.deepCheck(target[prop])) {
                        return false;
                    }
				}
			}
		} else {
			if (this.isEmpty(target)) { // '', undefined, null, false
				return false;
			} else {
				return true;
			}
		}
		return true;
    }
    pick(obj: object, arr: Array<string>): Object {
        return arr.reduce((iter, val) => {
            if(val in obj) {
                iter[val] = obj[val];
            }
            return iter;
        }, {});
      }

    intersection(inputs: any[]) : any[]{
        return inputs.reduce((a, b) => a.filter(c => b.includes(c)))
    }

    getUuid() {
        let s = []
        let hexDigits = '0123456789abcdef'
        for (let i = 0; i < 36; i++) {
          let _temp=Math.floor(Math.random() * 0x10)
          s[i] = hexDigits.substring(_temp, _temp + 1)
        }
        s[14] = '4'
        let _temp2=(s[19] & 0x3) | 0x8
        s[19] = hexDigits.substring(_temp2, _temp2 + 1)
        s[8] = s[13] = s[18] = s[23] = '-'
        return s.join('')
      }
}