-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.coffee
63 lines (59 loc) · 1.92 KB
/
path.coffee
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
{isString, isArray, isFunction} = require("./_helpers")
splittedToObjects = (splitted, obj) ->
return splitted.reduce ((arr, name, i) ->
arr.push arr[i][name]
return arr
), [obj]
module.exports =
_name: "path"
_v: 1
_rebind: "$path"
methods:
$path:
toValue: (o) ->
unless o.value?
if o.parent and o.name
o.value = o.parent[o.name]
else
o.obj ?= @
o.value = splittedToObjects(o.path.split("."), o.obj).pop()
return o
getValue: (path) -> @$path.toValue(path:path).value
resolveValue: (val) ->
if isString(val)
val = @$path.getValue((tmp = val))
val = @ if not val? and (tmp == "this" or tmp == "@")
return val
resolveMultiple: (o, arr) ->
for str in arr
o[str] = @$path.resolveValue(o[str]) if o[str]
setValue: (o) ->
if o.value?
@$path.toNameAndParent(o)
o.parent[o.name] = o.value
toNameAndParent: (o) ->
return o if o.name and o.parent
splitted = o.path.split(".")
o.obj ?= @
o.name = splitted.pop()
o.parent = splittedToObjects(splitted, o.obj).pop()
return o
test module.exports, {
methods:some:nested:path:"test"
}, (el) ->
path = "some.nested.path"
it "should convert path to name and parent", ->
obj = el.$path.toNameAndParent({path:path})
obj.name.should.equal "path"
obj.parent.should.equal el.some.nested
it "should convert path to value", ->
obj = el.$path.toValue({path:path})
obj.value.should.equal el.some.nested.path
it "should set value by path", ->
obj = el.$path.setValue({path:path,value:"test2"})
el.some.nested.path.should.equal "test2"
it "should convert parent and name to value", ->
obj = el.$path.toNameAndParent({path:path})
delete obj.path
obj = el.$path.toValue(obj)
obj.value.should.equal el.some.nested.path