aboutsummaryrefslogtreecommitdiffstats
path: root/core/src/OC/get_set.js
blob: 0c909ad04fd2b78970a6f34a1ed43c4940106152 (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
/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

export const get = context => name => {
	const namespaces = name.split('.')
	const tail = namespaces.pop()

	for (let i = 0; i < namespaces.length; i++) {
		context = context[namespaces[i]]
		if (!context) {
			return false
		}
	}
	return context[tail]
}

/**
 * Set a variable by name
 *
 * @param {string} context context
 * @return {Function} setter
 * @deprecated 19.0.0 use https://lodash.com/docs#set
 */
export const set = context => (name, value) => {
	const namespaces = name.split('.')
	const tail = namespaces.pop()

	for (let i = 0; i < namespaces.length; i++) {
		if (!context[namespaces[i]]) {
			context[namespaces[i]] = {}
		}
		context = context[namespaces[i]]
	}
	context[tail] = value
	return value
}