index.js | |
---|---|
Dotty makes it easy to programmatically access arbitrarily nested objects and their properties. | |
Returns | var exists = module.exports.exists = function exists(object, path) {
if (typeof path === "string") {
path = path.split(".");
}
if (!(path instanceof Array) || path.length === 0) {
return false;
}
path = path.slice();
var key = path.shift();
if (typeof object !== "object" || object === null) {
return false;
}
if (path.length === 0) {
return Object.hasOwnProperty.apply(object, [key]);
} else {
return exists(object[key], path);
}
}; |
These arguments are the same as those for The return value, however, is the property you're trying to access, or
| var get = module.exports.get = function get(object, path) {
if (typeof path === "string") {
path = path.split(".");
}
if (!(path instanceof Array) || path.length === 0) {
return;
}
path = path.slice();
var key = path.shift();
if (typeof object !== "object" || object === null) {
return;
}
if (path.length === 0) {
return object[key];
}
if (path.length) {
return get(object[key], path);
}
}; |
Arguments are similar to The return value is an array of values where the key path matches the specified criterion. If none match, an empty array will be returned. | var search = module.exports.search = function search(object, path) {
if (typeof path === "string") {
path = path.split(".");
}
if (!(path instanceof Array) || path.length === 0) {
return;
}
path = path.slice();
var key = path.shift();
if (typeof object !== "object" || object === null) {
return;
}
if (key === "*") {
key = ".*";
}
if (typeof key === "string") {
key = new RegExp(key);
}
if (path.length === 0) {
return Object.keys(object).filter(key.test.bind(key)).map(function(k) { return object[k]; });
} else {
return Array.prototype.concat.apply([], Object.keys(object).filter(key.test.bind(key)).map(function(k) { return search(object[k], path); }));
}
}; |
The first two arguments for The third argument is a value to The return value is | var put = module.exports.put = function put(object, path, value) {
if (typeof path === "string") {
path = path.split(".");
}
if (!(path instanceof Array) || path.length === 0) {
return false;
}
path = path.slice();
var key = path.shift();
if (typeof object !== "object" || object === null) {
return false;
}
if (path.length === 0) {
object[key] = value;
} else {
if (typeof object[key] === "undefined") {
object[key] = {};
}
if (typeof object[key] !== "object" || object[key] === null) {
return false;
}
return put(object[key], path, value);
}
};
|