Skip to content

Commit

Permalink
helpers seem fully functional now.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Sep 27, 2017
1 parent 20c5738 commit afd50bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/helpers/columnSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,14 @@ function ColumnSet(columns, options) {
const target = {};
this.columns.forEach(c => {
const a = colDesc(c, source);
if (c.init) {
// TODO: Cannot do it directly for nested properties:
target[a.name] = c.init.call(source, a);
} else {
if (a.exists || 'def' in c) {
// TODO: Cannot do it directly for nested properties:
target[a.name] = a.value;
if (a.info.valid) {
if (c.init) {
npm.utils.assignValue(target, a.info.path, c.init.call(source, a));
} else {
if (a.exists || 'def' in c) {
target[a.name] = a.value;
npm.utils.assignValue(target, a.info.path, a.value);
}
}
}
});
Expand All @@ -543,11 +544,11 @@ function ColumnSet(columns, options) {
source,
name: column.prop || column.name
};
const info = npm.utils.getIfHas(source, a.name);
a.exists = !!info.has;
a.info = npm.utils.getIfHas(source, a.name);
a.exists = !!a.info.has;
if (a.exists) {
a.target = info.target; // TODO: target needs to be documented
a.value = info.value;
a.target = a.info.target; // TODO: target needs to be documented
a.value = a.info.value;
} else {
a.value = 'def' in column ? column.def : undefined;
}
Expand Down
19 changes: 19 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ function InternalError(error) {
// Parses a property name, and gets its name from the object,
// if the property exists. Returns object {valid, has, target, value}:
// - valid - true/false, whether the syntax is valid
// - path - array of all property names; it is set when 'valid' = true
// - 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.path = [prop];
result.has = prop in obj;
result.target = obj;
if (result.has) {
Expand All @@ -176,6 +178,7 @@ function getIfHas(obj, prop) {
missing = true;
}
}
result.path = names;
result.has = !missing;
if (result.has) {
result.target = target;
Expand All @@ -185,7 +188,23 @@ function getIfHas(obj, prop) {
return result;
}

/////////////////////////////////////////////
// Assigns a value to the property based on
// the full name path in the target object.
function assignValue(obj, path, value) {
const lastIdx = path.length - 1;
for (let i = 0; i < lastIdx; i++) {
const key = path[i];
if (!(key in obj)) {
obj[key] = {};
}
obj = obj[key];
}
obj[path[lastIdx]] = value;
}

const exp = {
assignValue,
getIfHas,
InternalError,
getLocalStack,
Expand Down

0 comments on commit afd50bf

Please sign in to comment.