Skip to content

Commit

Permalink
Change ref description. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 12, 2019
1 parent 031dbf6 commit dfda451
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 165 deletions.
59 changes: 39 additions & 20 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,7 @@ exports.describe = function (schema) {

case 'default':
case 'failover':
if (Common.isResolvable(value)) {
description.flags[flag] = value.describe();
}
else if (typeof value === 'function') {
description.flags[flag] = {
description: value.description,
function: value
};
}
else {
description.flags[flag] = { value };
}

description.flags[flag] = internals.describe(value);
break;

case 'label':
Expand Down Expand Up @@ -111,7 +99,7 @@ exports.describe = function (schema) {
continue;
}

item.args[key] = Common.isResolvable(arg) || Common.isSchema(arg) ? arg.describe() : arg;
item.args[key] = internals.describe(arg, key);
}

if (!Object.keys(item.args).length) {
Expand Down Expand Up @@ -204,16 +192,20 @@ exports.describe = function (schema) {
};


internals.inner = function (item) {

if (typeof item.describe === 'function') {
return item.describe();
}
internals.inner = function (item, assign) {

if (typeof item !== 'object') {
return item;
}

if (typeof item.describe === 'function') {
if (assign === 'ref') {
return item.describe().ref;
}

return item.describe();
}

if (Array.isArray(item)) {
return item;
}
Expand All @@ -231,8 +223,35 @@ internals.inner = function (item) {
continue;
}

normalized[key] = internals.inner(value);
normalized[key] = internals.inner(value, key);
}

return normalized;
};


internals.describe = function (value, key) {

if (typeof value === 'function') {
return {
description: value.description,
function: value
};
}

if (typeof value !== 'object' ||
key === 'options') {

return value;
}

if (typeof value.describe === 'function') {
if (key === 'ref') {
return value.describe().ref;
}

return value.describe();
}

return { value };
};
34 changes: 23 additions & 11 deletions lib/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.create = function (key, options = {}) {
const ref = {
adjust: options.adjust || null,
ancestor: options.ancestor,
iterables: options.iterables || false,
iterables: options.iterables || null,
map: options.map ? new Map(options.map) : null,
separator: Common.default(options.separator, '.')
};
Expand Down Expand Up @@ -75,7 +75,7 @@ internals.Ref = class {

constructor(ref) {

Object.assign(this, ref); // adjust, ancestor, display, iterables, map, path, separator, type
Object.assign(this, ref); // adjust, ancestor, iterables, map, path, separator, type

this.depth = this.path.length;
this.key = this.path.length ? this.path.join(this.separator) : null;
Expand Down Expand Up @@ -121,21 +121,33 @@ internals.Ref = class {

describe() {

const about = {
ref: this.type,
key: this.key,
path: this.path
};
const ref = { path: this.path };

if (this.adjust) {
about.adjust = this.adjust;
if (this.type !== 'value') {
ref.type = this.type;
}

if (this.separator !== '.') {
ref.separator = this.separator;
}

if (this.type === 'value' &&
this.ancestor !== 1) {

ref.ancestor = this.ancestor;
}

if (this.map) {
about.map = [...this.map];
ref.map = [...this.map];
}

for (const key of ['adjust', 'iterables']) {
if (this[key] !== null) {
ref[key] = this[key];
}
}

return about;
return { ref };
}

updateDisplay() {
Expand Down
14 changes: 13 additions & 1 deletion test/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,19 @@ describe('extension', () => {
expect(schema.describe()).to.equal({
type: 'myType',
rules: [
{ name: 'foo', args: { bar: 'bar', baz: 42, qux: { ref: 'value', key: 'a.b', path: ['a', 'b'] }, quux: { ref: 'global', key: 'c.d', path: ['c', 'd'] } } }
{
name: 'foo',
args: {
bar: 'bar',
baz: 42,
qux: {
ref: { path: ['a', 'b'] }
},
quux: {
ref: { type: 'global', path: ['c', 'd'] }
}
}
}
]
});
});
Expand Down
10 changes: 4 additions & 6 deletions test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ describe('Manifest', () => {
max: {
type: 'string',
flags: {
default: { value: 0 },
failover: { value: 1 }
default: 0,
failover: 1
},
invalids: [''],
rules: [{ name: 'max', args: { limit: 3 } }]
Expand Down Expand Up @@ -165,9 +165,7 @@ describe('Manifest', () => {
type: 'string',
flags: {
default: {
ref: 'value',
key: 'xor',
path: ['xor']
ref: { path: ['xor'] }
}
},
invalids: ['']
Expand Down Expand Up @@ -224,7 +222,7 @@ describe('Manifest', () => {

const description = schema.describe();
expect(description).to.equal(result);
expect(description.children.defaultRef.flags.default).to.equal({ ref: 'value', key: 'xor', path: ['xor'] });
expect(description.children.defaultRef.flags.default).to.equal({ ref: { path: ['xor'] } });
expect(description.children.defaultFn.flags.default.description).to.equal('testing');
expect(description.children.defaultDescribedFn.flags.default.description).to.equal('described test');
});
Expand Down
Loading

0 comments on commit dfda451

Please sign in to comment.