Skip to content

Commit 6e308e4

Browse files
committed
Rest '...' key support thanks to @illiano
json-schema-form/angular-schema-form#748 was the reason for this change, thanks @illiano
1 parent 583588b commit 6e308e4

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

docs/test.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,24 @@ should handle a wildcard * in the form definition.
8888
(0, _merge.merge)(schema, ['*']).should.be.deep.equal(stdForm.form);
8989
```
9090

91-
should not handle a wildcard * if the schema is a lookup.
91+
should not handle a wildcard * if the schema is a lookup and cannot be inserted.
9292

9393
```js
9494
(0, _merge.merge)(stdForm.lookup, ['*']).should.not.be.deep.equal(stdForm.form);
9595
```
9696

97+
should handle a rest "..." key in the form definition.
98+
99+
```js
100+
(0, _merge.merge)(schema, ['...', 'gender']).should.be.deep.equal(stdForm.form);
101+
```
102+
103+
should not handle a rest "..." key in the form definition when the schema is a lookup and cannot be inserted.
104+
105+
```js
106+
(0, _merge.merge)(stdForm.lookup, ['...', 'gender']).should.not.be.deep.equal(stdForm.form);
107+
```
108+
97109
should combine a schema and form definition, regardless of order.
98110

99111
```js

src/lib/merge.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import canonicalTitleMap from './canonical-title-map';
55
// export function merge(schema, form, schemaDefaultTypes, ignore, options, readonly, asyncTemplates) {
66
export function merge(lookup, form, ignore, options, readonly, asyncTemplates) {
77
let formItems = [];
8+
let formItemRest = [];
89
form = form || [];
910
let idx = form.indexOf('*');
1011
options = options || {};
12+
let stdForm = {};
1113

14+
let idxRest = form.indexOf('...');
1215
if(typeof lookup === 'object' && lookup.hasOwnProperty('properties')) {
1316
readonly = readonly || lookup.readonly || lookup.readOnly;
14-
const stdForm = defaultForm(lookup, createDefaults(), ignore, options);
17+
stdForm = defaultForm(lookup, createDefaults(), ignore, options);
18+
1519
let defaultFormLookup = stdForm.lookup;
1620

1721
lookup = defaultFormLookup || lookup;
@@ -20,6 +24,36 @@ export function merge(lookup, form, ignore, options, readonly, asyncTemplates) {
2024

2125
if (idx !== -1) {
2226
form = form.slice(0, idx).concat(formItems).concat(form.slice(idx + 1));
27+
}
28+
29+
//simple case, we have a "...", just put the formItemRest there
30+
if (stdForm.form && idxRest !== -1) {
31+
let formKeys = form.map(function(obj) {
32+
if (typeof obj === 'string'){
33+
return obj;
34+
}
35+
else if (obj.key) {
36+
return obj.key;
37+
};
38+
}).filter(function(element) {
39+
return element !== undefined;
40+
});
41+
42+
formItemRest = formItemRest.concat(
43+
stdForm.form.map(function(obj) {
44+
let isInside = formKeys.indexOf(obj.key[0]) !== -1;
45+
if (!isInside) {
46+
return obj;
47+
};
48+
})
49+
.filter(function(element) {
50+
return element !== undefined;
51+
})
52+
);
53+
};
54+
55+
if (idxRest !== -1) {
56+
form = form.slice(0, idxRest).concat(formItemRest).concat(form.slice(idxRest + 1));
2357
};
2458

2559
// ok let's merge!

src/lib/merge.spec.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,39 @@ describe('merge.js', () => {
6060

6161
describe('merge', () => {
6262
it('should handle a schema lookup or schema for first argument', () => {
63-
merge(stdForm.lookup, ['name','shoe','gender']).should.be.deep.equal(stdForm.form);
64-
merge(schema, ['*']).should.be.deep.equal(stdForm.form);
63+
merge(stdForm.lookup, [ 'name', 'shoe', 'gender' ]).should.be.deep.equal(stdForm.form);
64+
merge(schema, [ '*' ]).should.be.deep.equal(stdForm.form);
6565
});
6666

6767
it('should handle a wildcard * in the form definition', () => {
68-
merge(schema, ['*']).should.be.deep.equal(stdForm.form);
68+
merge(schema, [ '*' ]).should.be.deep.equal(stdForm.form);
6969
});
7070

71-
it('should not handle a wildcard * if the schema is a lookup', () => {
72-
merge(stdForm.lookup, ['*']).should.not.be.deep.equal(stdForm.form);
71+
it('should not handle a wildcard * if the schema is a lookup and cannot be inserted', () => {
72+
merge(stdForm.lookup, [ '*' ]).should.not.be.deep.equal(stdForm.form);
73+
});
74+
75+
it('should handle a rest "..." key in the form definition', () => {
76+
merge(schema, [ '...', 'gender' ]).should.be.deep.equal(stdForm.form);
77+
});
78+
79+
it('should not handle a rest "..." key in the form definition when the schema is a lookup and cannot be inserted', () => {
80+
merge(stdForm.lookup, [ '...', 'gender' ]).should.not.be.deep.equal(stdForm.form);
7381
});
7482

7583
it('should combine a schema and form definition, regardless of order', () => {
76-
merge(schema, ['name','shoe','gender']).should.be.deep.equal(stdForm.form);
77-
merge(schema, ['gender']).should.be.deep.equal([stdForm.form[2]]);
78-
merge(schema, ['gender','name']).should.be.deep.equal([stdForm.form[2],stdForm.form[0]]);
84+
merge(schema, [ 'name', 'shoe', 'gender' ]).should.be.deep.equal(stdForm.form);
85+
merge(schema, [ 'gender' ]).should.be.deep.equal([stdForm.form[2]]);
86+
merge(schema, [ 'gender', 'name' ]).should.be.deep.equal([stdForm.form[2],stdForm.form[0]]);
7987
});
8088

8189

8290
it('should allow items that are not in the schema', () => {
83-
merge(schema, ['*', { type:'fieldset' }]).should.be.deep.equal(stdForm.form.concat([{ type:'fieldset' }]));
91+
merge(schema, [ '*', { type:'fieldset' }]).should.be.deep.equal(stdForm.form.concat([{ type:'fieldset' }]));
8492
});
8593

8694
it('should translate "readOnly" in schema to "readonly" on the merged form defintion', () => {
87-
var merged = merge(schema, ['gender']);
95+
var merged = merge(schema, [ 'gender' ]);
8896
merged[0].should.have.property('readonly');
8997
merged[0].readonly.should.eq(true)
9098
});
@@ -113,7 +121,7 @@ describe('merge.js', () => {
113121
}
114122
};
115123

116-
var merged = merge(subschema, ['*']);
124+
var merged = merge(subschema, [ '*' ]);
117125

118126
//sub
119127
merged[0].should.have.property('readonly');

0 commit comments

Comments
 (0)