forked from svt/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.js
117 lines (103 loc) · 2.54 KB
/
variables.js
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
// SPDX-FileCopyrightText: 2025 Sveriges Television AB
//
// SPDX-License-Identifier: MIT
const objectPath = require('object-path')
const DIController = require('../shared/DIController')
/**
* The regex used to match
* variables in strings
*
* @example
* "My string $(my_variable)" -> MATCH
* "My string no variable" -> NO MATCH
*
* This should NOT be made global "/g" as that will trigger
* an issue where the expression will only match every
* other time it's used – this is known behaviour in
* multiple browsers
*
* @type { RegExp }
*/
const VARIABLE_REGEX = /\$\((.*?)\)/
class Variables {
#props
constructor (props) {
this.#props = props
}
/**
* Check if a string contains
* at least one variable
* @param { String } str
* @returns { Boolean }
*/
stringContainsVariable (str) {
if (typeof str !== 'string') {
return false
}
return VARIABLE_REGEX.test(str)
}
/**
* Set a variable's value
* @param { String } key
* @param { any } value
*/
setVariable (key, value) {
return this.#props.Commands.executeCommand('variables.setVariable', key, value)
}
/**
* Get a variable's value
* @param { String } key
* @returns { Promise.<any> }
*/
getVariable (key) {
return this.#props.Commands.executeCommand('variables.getVariable', key)
}
/**
* Get all variables' values
* @returns { Promise.<any> }
*/
async getAllVariables () {
return this.#props.State.get('variables')
}
/**
* Substitute variables for their
* values in a string
*
* @example
* "Hello $(my variable)" -> "Hello world"
*
* @param { String } str
* @param { any } data Data to substitute variables for,
* defaults to the local this.#props.State
* @param { any } overrideData Data that will override the
* default data rather than replace
* @returns { String }
*/
substituteInString (str, data = (this.#props.State.getLocalState()?.variables || {}), overrideData = {}) {
if (!str) {
return ''
}
const text = `${str}`.split(VARIABLE_REGEX)
const values = {
...data,
...overrideData
}
let out = ''
let i = 0
while (text.length > 0) {
if (i % 2 === 0) {
out += text.shift()
} else {
const path = text.shift()
const value = objectPath.get(values, path)
out += value || ''
}
i++
}
return out
}
}
DIController.main.register('Variables', Variables, [
'State',
'Commands'
])