-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathutilities.js
More file actions
115 lines (108 loc) · 3.33 KB
/
utilities.js
File metadata and controls
115 lines (108 loc) · 3.33 KB
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
/*
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
*/
export default {
methods: {
isFloat(str) {
// Regex to identify a string as a floating point number
if (/^\s*[-+]?\d*\.\d+\s*$/.test(str)) {
return true
}
// Regex to identify a string as a floating point number in scientific notation.
if (/^\s*[-+]?(\d+((\.\d+)?)|(\.\d+))[eE][-+]?\d+\s*$/.test(str)) {
return true
}
return false
},
isInt(str) {
// Regular expression to identify a String as an integer
if (/^\s*[-+]?\d+\s*$/.test(str)) {
return true
}
// # Regular expression to identify a String as an integer in hexadecimal format
if (/^\s*0[xX][\dabcdefABCDEF]+\s*$/.test(str)) {
return true
}
return false
},
isArray(str) {
// Regular expression to identify a String as an Array
if (/^\s*\[.*\]\s*$/.test(str)) {
return true
}
return false
},
removeQuotes(str) {
// Return the string with leading and trailing quotes removed
if (str.length < 2) {
return str
}
let firstChar = str.charAt(0)
if (firstChar !== '"' && firstChar !== "'") {
return str
}
let lastChar = str.charAt(str.length - 1)
if (firstChar !== lastChar) {
return str
}
return str.slice(1, -1)
},
convertToString(value) {
let i = 0
let returnValue = ''
if (Object.prototype.toString.call(value).slice(8, -1) === 'Array') {
let arrayLength = value.length
returnValue = '[ '
for (i = 0; i < arrayLength; i++) {
if (
Object.prototype.toString.call(value[i]).slice(8, -1) === 'String'
) {
returnValue += '"' + value[i] + '"'
} else {
returnValue += value[i]
}
if (i !== arrayLength - 1) {
returnValue += ', '
}
}
returnValue += ' ]'
} else if (
Object.prototype.toString.call(value).slice(8, -1) === 'Object'
) {
if (value.json_class === 'String' && value.raw) {
// This is binary data, display in hex.
returnValue = '0x'
for (i = 0; i < value.raw.length; i++) {
let nibble = value.raw[i].toString(16).toUpperCase()
if (nibble.length < 2) {
nibble = '0' + nibble
}
returnValue += nibble
}
} else if (value.json_class === 'Float' && value.raw) {
returnValue = value.raw
}
} else {
returnValue = String(value)
}
return returnValue
},
},
}