-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'renderOrder' property to schema to decide in which order components render #684
base: master
Are you sure you want to change the base?
Changes from all commits
e3a945c
27c5ccd
a3a55ac
08b09eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,4 +200,44 @@ describe(_.startCase(filename), function () { | |
expect(fn({ bar: 'foo' })).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('orderReferences', function () { | ||
const fn = lib[this.title], | ||
referenceObjects = { | ||
someComponent: { _ref: 'ref1' }, | ||
anotherComponent: { _ref: 'ref2' }, | ||
'componentList.0': { _ref: 'ref3' }, | ||
'componentList.1': { _ref: 'ref4' } | ||
}; | ||
|
||
it('maintains order without a schema', function () { | ||
const result = fn(referenceObjects); | ||
|
||
expect(result).to.have.deep.property('0.0', 'someComponent'); | ||
expect(result).to.have.deep.property('1.0', 'anotherComponent'); | ||
expect(result).to.have.deep.property('2.0', 'componentList.0'); | ||
expect(result).to.have.deep.property('3.0', 'componentList.1'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fyi this can be rewritten to
|
||
}); | ||
|
||
it('orders based on schema', function () { | ||
const schema = { | ||
someComponent: { | ||
_component: { | ||
renderOrder: 3 | ||
} | ||
}, | ||
componentList: { | ||
_componentList: { | ||
renderOrder: 2 | ||
} | ||
} | ||
}, | ||
result = fn(referenceObjects, schema); | ||
|
||
expect(result).to.have.deep.property('0.0', 'anotherComponent'); | ||
expect(result).to.have.deep.property('1.0', 'componentList.0'); | ||
expect(result).to.have.deep.property('2.0', 'componentList.1'); | ||
expect(result).to.have.deep.property('3.0', 'someComponent'); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,31 @@ function listDeepObjects(obj, filter) { | |
return list; | ||
} | ||
|
||
/** | ||
* Search through an object and find all deep key-value pairs matching a filter. | ||
* @param {object} obj | ||
* @param {Function} [filter=_.identity] Optional filter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So actually since this is only called by
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure. was just keeping the same definition as the |
||
* @param {array} [path] Path of this value | ||
* @returns {object} | ||
*/ | ||
function listDeepValuesByKey(obj, filter = _.identity, path = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately the order listDeepValuesByKey results in is different from listDeepObjects due to your use of recursion (depth vs breadth first). I tested on an article - and this matters because if you change the default order then when people update their version and rely on a certain rendering order, their code will behave differently and potentially break. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hey phil. tested on an article as well. the only thing that would be affected here are refs that are properties, which we already know order shouldn't be relied upon for those. array ordering is still maintained unless there is a ref inside of a ref (which afaik would never happen before data is populated) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll get an example to show what i mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so testing against this article listDeepObjects returns with the order of these refs In terms of keeping the public api of amphora stable, listDeepValuesByKey needs to preserve the ordering by default, regardless whether the order should be relied upon. For instance, we were relying upon the rendering order and if that changed with a minor update, that would be a frustrating bug to find. Ultimately it's up to the lib author to define the api and what constitutes semver changes, so if you disagree then we can let them decide. |
||
let result = {}; | ||
|
||
if (_.isObject(obj)) { | ||
// loop through any objects or arrays | ||
result = _.reduce(obj, (cumm, curr, key) => _.assign(cumm, listDeepValuesByKey(curr, filter, [...path, key])), {}); | ||
} | ||
|
||
if (_.iteratee(filter)(obj) && path.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was wrong about _.filter because that returns an array no matter what. However I would still suggest doing something to make this more readable because _.iteratee is not a common lodash function nor is it obvious what it's doing here. So if you use it then do something like this
Or more preferably separate out the function and string cases to make it more clear
|
||
// add the key to results if it matches the filter | ||
const key = path.join('.'); | ||
|
||
result[key] = obj; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* @param {string} version | ||
* @returns {function} | ||
|
@@ -155,3 +180,4 @@ module.exports.urlToUri = urlToUri; | |
module.exports.omitPageConfiguration = omitPageConfiguration; | ||
module.exports.getPageReferences = getPageReferences; | ||
module.exports.listDeepObjects = listDeepObjects; | ||
module.exports.listDeepValuesByKey = listDeepValuesByKey; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had to export this to use it for calling
resolveComponentReferences
in order to remove the breaking change for that call signature