Skip to content

Commit

Permalink
adding support for the target property.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Sep 27, 2017
1 parent 07956f4 commit 20c5738
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/helpers/columnSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ function ColumnSet(columns, options) {
const info = npm.utils.getIfHas(source, a.name);
a.exists = !!info.has;
if (a.exists) {
a.target = info.target; // TODO: target needs to be documented
a.value = info.value;
} else {
a.value = 'def' in column ? column.def : undefined;
Expand Down
13 changes: 10 additions & 3 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,40 @@ function InternalError(error) {
this.error = error;
}

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Parses a property name, and gets its name from the object,
// if the property exists. Returns object {valid, has, value}
// if the property exists. Returns object {valid, has, target, value}:
// - valid - true/false, whether the syntax is valid
// - has - whether the property exists; it is set when 'valid' = true
// - target - the target object that contains the property, set when 'has' = true
// - value - the value; set when 'has' = true
function getIfHas(obj, prop) {
const result = {valid: true};
if (prop.indexOf('.') === -1) {
result.has = prop in obj;
result.target = obj;
if (result.has) {
result.value = obj[prop];
}
} else {
const names = prop.split('.');
let missing;
let missing, target;
for (let i = 0; i < names.length; i++) {
const n = names[i];
if (!n) {
result.valid = false;
return result;
}
if (!missing && obj && typeof obj === 'object' && n in obj) {
target = obj;
obj = obj[n];
} else {
missing = true;
}
}
result.has = !missing;
if (result.has) {
result.target = target;
result.value = obj;
}
}
Expand Down

0 comments on commit 20c5738

Please sign in to comment.