-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname-your-space.js
51 lines (38 loc) · 1.67 KB
/
name-your-space.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
/**
* http://www.codewars.com/kata/514b6c44a337752e67000077/
*
Finish the namespace function so that it sets or gets the value at the path specified.
The first argument should be the root object that the path belongs to. The 2nd argument
is the path itself and the 3rd optional argument is the value to set at the path.
If a value is provided then the path will be set to that value. Any objects not present
within the path will be created automatically in order for the path to be successfully set.
If no value is provided the the function will return the value at the path given. If the
path is not valid/present then undefined will be returned.
*
*/
let tests = require('./lib/framework.js');
let Test = tests.Test, describe = tests.describe, it = tests.it, before = tests.before, after = tests.after;
function namespaceSet(obj, key, value) {
let keys = key.split('.');
if (typeof obj[keys[0]] === 'undefined') {
obj[keys[0]] = {};
}
if (keys.length === 1) {
obj[keys[0]] = value;
} else {
namespace(obj[keys[0]], keys.slice(1).join('.'), value);
}
return obj;
}
function namespaceGet(obj, key) {
let keys = key.split('.');
if (!obj) return undefined;
return keys.length === 1 ? obj[key] : namespaceGet(obj[keys[0]], keys.slice(1).join('.'));
}
function namespace(obj, key, value) {
return typeof (value) === 'undefined' ? namespaceGet(obj, key) : namespaceSet(obj, key, value);
}
var stuff = {};
namespace(stuff, 'moreStuff.name', 'the stuff');
Test.expect(stuff.moreStuff.name === 'the stuff');
Test.expect(namespace(stuff, 'otherStuff.id') === undefined);