This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathjspath.coffee
More file actions
executable file
·111 lines (108 loc) · 3.53 KB
/
Copy pathjspath.coffee
File metadata and controls
executable file
·111 lines (108 loc) · 3.53 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
module.exports = class JsPath
primTypes = /^(string|number|boolean)$/
###
@constructor.
@signature: new JsPath(path, val)
@param: path - a dot-notation style "path" to identify a
nested JS object.
@description: Initialize a new js object with the provided
path. I've never actually used this constructor for any-
thing, and it is here for the sake of "comprehensiveness"
at this time, although I am incredulous as to it's overall
usefulness.
###
constructor:(path, val)->
return JsPath.setAt {}, path, val or {}
[
'forEach','indexOf','join','pop','reverse'
'shift','sort','splice','unshift','push'
].forEach (method)=>
@[method+'At'] = (obj, path, rest...)=>
target = @getAt obj, path
if 'function' is typeof target?[method] then return target[method] rest...
else throw new Error "Does not implement method #{method} at #{path}"
###
@method. property of the constructor.
@signature: JsPath.getAt(ref, path)
@param: ref - the object to traverse.
@param: path - a dot-notation style "path" to identify a
nested JS object.
@return: the object that can be found inside ref at the path
described by the second parameter or undefined if the path
is not valid.
###
@getAt =(ref, path)->
if 'function' is typeof path.split # ^1
path = path.split '.'
else
path = path.slice()
while ref? and prop = path.shift()
ref = ref[prop]
ref
###
@method. property of the constructor.
@signature: JsPath.getAt(ref, path)
@param: obj - the object to extend.
@param: path - a dot-notation style "path" to identify a
nested JS object.
@param: val - the value to assign to the path of the obj.
@return: the object that was extended.
@description: set a property to the path provided by the
second parameter with the value provided by the third
parameter.
###
@setAt =(obj, path, val)->
if 'function' is typeof path.split # ^1
path = path.split '.'
else
path = path.slice()
last = path.pop()
prev = []
ref = obj
while component = path.shift()
if primTypes.test typeof ref[component]
throw new Error \
"""
#{prev.concat(component).join '.'} is
primitive, and cannot be extended.
"""
ref = ref[component] or= {}
prev.push component
ref[last] = val
obj
@assureAt =(ref, path, initializer)->
if obj = JsPath.getAt ref, path then obj
else
JsPath.setAt ref, path, initializer
initializer
###
@method. property of the constructor.
@signature: JsPath.deleteAt(ref, path)
@param: obj - the object to extend.
@param: path - a dot-notation style "path" to identify a
nested JS object to dereference.
@return: boolean success.
@description: deletes the reference specified by the last
unit of the path from the object specified by other
components of the path, belonging to the provided object.
###
@deleteAt =(ref, path)->
if 'function' is typeof path.split # ^1
path = path.split '.'
else
path = path.slice()
prev = []
last = path.pop()
while component = path.shift()
if primTypes.test typeof ref[component]
throw new Error \
"""
#{prev.concat(component).join '.'} is
primitive; cannot drill any deeper.
"""
return no unless ref = ref[component]
prev.push component
delete ref[last] # TODO: should this return the deleted value.
###
Footnotes:
1 - if there's no .split() method, assume it's already an array