Skip to content

Conversation

LorenzoSantoro94
Copy link
Contributor

Description

Instead of simply copying each selected key to result object, it breaks each select arg string by dots and recursively copies each key.

Before

.select('a.b') => { 'a.b': ... }

After

.select('a.b') => {a: {b: ... }}

Related issues

Fixes #199

Limitations

If two paths inside same property are selected (e.g. "a.b" and "a.c") result will carry only the latest selected path.
See skipped test case

@sbatson5 sbatson5 requested review from sbatson5 and Copilot May 19, 2025 02:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for selecting nested fields via dot paths in query.select, transforming flat key selections into a nested object structure.

  • Introduces lodash.merge and a new buildDocFromPath helper to recursively build nested selections.
  • Updates buildDocFromHash.js to apply selectFields.reduce with the new helper.
  • Adds new test cases in query.test.js to cover nested selections, missing values, and multiple nested fields.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/mocks/helpers/buildDocFromHash.js Import merge, implement nested select logic, add buildDocFromPath helper
tests/query.test.js Add tests for nested select behavior and streamline query chaining
Files not reviewed (1)
  • package.json: Language not supported
Comments suppressed due to low confidence (1)

src/mocks/helpers/buildDocFromHash.js:67

  • When the root property doesn’t exist on data, buildDocFromPath currently builds nested empty objects (e.g., {size: {height: {}}}) instead of returning {}. Add a guard: if data[root] is undefined or null, return {} immediately.
const [root, ...subPath] = path;

@@ -56,3 +58,15 @@ module.exports = function buildDocFromHash(hash = {}, id = 'abc123', selectField
},
};
};

Copy link
Preview

Copilot AI May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a JSDoc comment above buildDocFromPath to explain its parameters, return value, and overall behavior for better maintainability.

Suggested change
/**
* Recursively builds a nested object from a given data object and a path array.
*
* @param {Object} data - The source object containing the data to extract.
* @param {string[]} path - An array of strings representing the path to the desired data.
* Each string corresponds to a key in the object hierarchy.
* @returns {Object} A new object containing the data at the specified path. If the path
* does not exist in the data, an empty object is returned.
*/

Copilot uses AI. Check for mistakes.

@sbatson5 sbatson5 merged commit 90e4cb2 into sbatson5:master May 22, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Path select selects nothing
2 participants