Skip to content

Commit

Permalink
Change get function (#11)
Browse files Browse the repository at this point in the history
* Updating the get function to accepts the node type
  • Loading branch information
jmedarametla authored Sep 21, 2023
1 parent 7d51d74 commit 672cfa8
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 10 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.4
0.3.5
53 changes: 44 additions & 9 deletions src/get.brs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
' * @memberof module:rodash
' * @instance
' * @description
' * Resolve a nested 'dot' notation path safely. This is primarily allow things like
' * "myValue = a.b.c.d.e.f" to run without crashing the VM when an intermediate value is invalid.
' * Resolve a nested 'dot' notation path safely. This is primarily allow below things like
' * "myValue = a.b.c.d.e.f"
' * "myValue = node.getChild(0).id"
' * "myValue = node.value1.value2"
' * to run without crashing the VM when an intermediate value is invalid.
' *
' * @example
' *
Expand All @@ -18,29 +21,61 @@
' * value = _.get(data, "a[0]")
' * ' => invalid
' *
' * data = <Component: roSGNode:Group> = {
' * opacity: 1.0
' * id: "my_component"
' * configs: <Component: roAssociativeArray>
' * viewList: invalid (roArray)
' * ...... other common fields of node
' * }

' * value = _.get(data, "opacity")
' *
' * value = _.get(data, "0.id") ' value of data's first child's id field
' *
' * value = _.get(data, "0.1.color") ' value of data's first child's, second child's color field.
' *
' * value = _.get(data, "configs.showFeature")
' *
' * value = _.get(data, "viewList.0")
'
' */
Function rodash_get_(array, path, default=invalid)

if array = invalid or not (type(array) = "roAssociativeArray" or type(array) = "roArray") then return default
if array = invalid or not (type(array) = "roAssociativeArray" or type(array) = "roArray" or type(array) = "roSGNode") then return default

segments = m.pathAsArray_(path)
if segments = invalid then return default

result = invalid

while segments.count() > 0
key = segments.shift()
value = array[key]
if value = invalid
exit while

if type(array) = "roSGNode" and (type(key) = "roInt" or type(key) = "roInteger" or type(key) = "Integer")
if key < 0 or key >= array.getChildCount() then
result = invalid
exit while
end if

result = array.getChild(key)
else
result = array[key]
end if

if segments.count() = 0
result = value
exit while
end if
if not (type(value) = "roAssociativeArray" or type(value) = "roArray")

if result = invalid
exit while
end if
array = value

if not (type(result) = "roAssociativeArray" or type(result) = "roArray" or type(result) = "roSGNode")
exit while
end if

array = result
end while

if result = invalid then return default
Expand Down
136 changes: 136 additions & 0 deletions test/source/tests/test_get.brs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ Function testSuite_get()
this.addTest("get_badAADefault", testCase_get_badAADefault)
this.addTest("get_badPath", testCase_get_badPath)
this.addTest("get_badPathDefault", testCase_get_badPathDefault)
this.addTest("get_simpleField", testCase_get_simpleField)
this.addTest("get_ChildNode", testCase_get_ChildNode)
this.addTest("get_defaultForNode", testCase_get_defaultForNode)
this.addTest("get_badNode", testCase_get_badNode)
this.addTest("get_badNodeDefault", testCase_get_badNodeDefault)
this.addTest("get_childInvalid", testCase_get_childInvalid)
this.addTest("get_deepChildInvalid", testCase_get_deepChildInvalid)
this.addTest("get_deepChildFromNode", testCase_get_deepChildFromNode)
this.addTest("get_arrayFieldFromNode", testCase_get_arrayFieldFromNode)
this.addTest("get_AAFieldFromNode", testCase_get_AAFieldFromNode)
this.addTest("get_arrayFieldFromDeepChildNode", testCase_get_arrayFieldFromDeepChildNode)
this.addTest("get_AAFieldFromDeepChildNode", testCase_get_AAFieldFromDeepChildNode)
return this
End Function

Expand Down Expand Up @@ -134,3 +146,127 @@ End Function
Function testCase_get_badPathDefault()
return m.AssertEqual(m._.get({a:1}, invalid, "default"), "default")
End Function

Function testCase_get_simpleField()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"

value = m._.get(source, "id")
return m.AssertTrue(value = "parentNode")
End Function

Function testCase_get_ChildNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"

value = m._.get(source, "0.id")
return m.AssertTrue(value = "abc")
End Function

Function testCase_get_defaultForNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"

value = m._.get(source, "0.0.id", "def")
return m.AssertEqual(value, "def")
End Function

Function testCase_get_badNode()
return m.AssertInvalid(m._.get(invalid, "id"))
End Function

Function testCase_get_badNodeDefault()
return m.AssertEqual(m._.get(invalid, "id", "default"), "default")
End Function

Function testCase_get_childInvalid()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"

value = m._.get(source, "0.id")
return m.AssertInvalid(value)
End Function

Function testCase_get_deepChildInvalid()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"
child2 = source.createChild("ContentNode")
child2.id = "def"
grandchild1 = child2.createChild("ContentNode")
grandchild1.id = "ghi"

value = m._.get(source, "0.3.id")
return m.AssertInvalid(value)
End function

Function testCase_get_deepChildFromNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"
child2 = source.createChild("ContentNode")
child2.id = "def"
grandchild1 = child2.createChild("ContentNode")
grandchild1.id = "ghi"
grandchild2 = child2.createChild("ContentNode")
grandchild2.id = "jkl"

value = m._.get(source, "1.1.id")
return m.AssertEqual(value, "jkl")
End Function

Function testCase_get_arrayFieldFromNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
source.abcArray = ["a","b","c"]

value = m._.get(source, "abcArray[0]")
return m.AssertEqual(value, "a")
End Function

Function testCase_get_AAFieldFromNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
source.abcAA = {a: 1, b: 2, c: 3}

value = m._.get(source, "abcAA.b")
return m.AssertEqual(value, 2)
End Function

Function testCase_get_arrayFieldFromDeepChildNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"
child2 = source.createChild("ContentNode")
child2.id = "def"
grandchild1 = child2.createChild("ContentNode")
grandchild1.id = "ghi"
grandchild1.abcArray = ["a","b","c"]

value = m._.get(source, "1.0.abcArray[0]")
return m.AssertEqual(value, "a")
End Function

Function testCase_get_AAFieldFromDeepChildNode()
source = CreateObject("roSGNode", "ContentNode")
source.id = "parentNode"
child1 = source.createChild("ContentNode")
child1.id = "abc"
child2 = source.createChild("ContentNode")
child2.id = "def"
grandchild1 = child2.createChild("ContentNode")
grandchild1.id = "ghi"
grandchild1.abcAA = {a: 1, b: 2, c: 3}

value = m._.get(source, "1.0.abcAA.a")
return m.AssertEqual(value, 1)
End Function

0 comments on commit 672cfa8

Please sign in to comment.