Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
2683c204
...
2683c20417e16eb2277df7b7c7d0bbdfabc12ca8
authored
2012-06-16 10:54:50 +0200
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
added utils.getPropertyPath()
1 parent
918e9906
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
0 deletions
modules/utils.js
tests/suites/utils.js
modules/utils.js
View file @
2683c20
...
...
@@ -128,6 +128,32 @@
exports
.
format
=
format
;
/**
* Retrieves the value of an Object foreign property using a bot-separated
* path string.
*
* Beware, this function doesn't handle object key names containing a dot.
*
* @param Object obj The source object
* @param String path Dot separated path, eg. "x.y.z"
*/
function
getPropertyPath
(
obj
,
path
)
{
if
(
!
isObject
(
obj
)
||
!
isString
(
path
))
{
return
undefined
;
}
var
value
=
obj
;
path
.
split
(
'.'
).
forEach
(
function
(
property
)
{
if
(
typeof
value
===
"object"
&&
property
in
value
)
{
value
=
value
[
property
];
}
else
{
value
=
undefined
;
return
;
}
});
return
value
;
}
exports
.
getPropertyPath
=
getPropertyPath
;
/**
* Inherit the prototype methods from one constructor into another.
*
* @param {function} ctor Constructor function which needs to inherit the
...
...
tests/suites/utils.js
View file @
2683c20
...
...
@@ -29,6 +29,43 @@ t.comment('fillBlanks()');
}
})();
t
.
comment
(
'getPropertyPath()'
);
(
function
()
{
testCases
=
[
{
input
:
utils
.
getPropertyPath
({},
'a.b.c'
),
output
:
undefined
},
{
input
:
utils
.
getPropertyPath
([
1
,
2
,
3
],
'a.b.c'
),
output
:
undefined
},
{
input
:
utils
.
getPropertyPath
({
a
:
{
b
:
{
c
:
1
}
},
c
:
2
},
'a.b.c'
),
output
:
1
},
{
input
:
utils
.
getPropertyPath
({
a
:
{
b
:
{
c
:
1
}
},
c
:
2
},
'a.b.x'
),
output
:
undefined
},
{
input
:
utils
.
getPropertyPath
({
a
:
{
b
:
{
c
:
1
}
},
c
:
2
},
'a.b'
),
output
:
{
c
:
1
}
},
{
input
:
utils
.
getPropertyPath
({
'a-b'
:
{
'c-d'
:
1
}
},
'a-b.c-d'
),
output
:
1
},
{
input
:
utils
.
getPropertyPath
({
'a.b'
:
{
'c.d'
:
1
}
},
'a.b.c.d'
),
output
:
undefined
}
];
testCases
.
forEach
(
function
(
testCase
)
{
t
.
assertEquals
(
testCase
.
input
,
testCase
.
output
,
'getPropertyPath() gets a property using a path'
);
});
})();
t
.
comment
(
'isArray()'
);
(
function
()
{
t
.
assertEquals
(
utils
.
isArray
([]),
true
,
'isArray() checks for an Array'
);
...
...
Please
register
or
sign in
to post a comment