Skip to content

add key path param - #1

Draft
maa-dar wants to merge 7 commits into
ljharb:mainfrom
maa-dar:patch-1
Draft

add key path param#1
maa-dar wants to merge 7 commits into
ljharb:mainfrom
maa-dar:patch-1

Conversation

@maa-dar

@maa-dar maa-dar commented Apr 13, 2023

Copy link
Copy Markdown

add key path to parameters sent to compare and replacer as this info is sometimes very crutial.

Why?

Often one would like to customize the printing of json based on where the json is in the tree like for example take:

{
  users: [
    {id:5. name: 'john', familyName: 'doe'}, ...
  ],
  asset: [
    {id:5, family: 'tools', name: 'hammer' }, ...
  ]
}

so here the family of the asset i want it before the name while in the user i want the family name to be before. (Note this is a theoratical example just to better explain my point)

add key path to parameters sent to compare and replacer as this info is sometimes very crutial

@ljharb ljharb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you elaborate on the use case here?

Either way it'd need documentation and tests.

Comment thread index.js Outdated
var seen = [];
return (function stringify(parent, key, node, level) {
var indent = space ? '\n' + new Array(level + 1).join(space) : '';
return (function stringify(parent, key, node, path = []) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

using this syntax would be a breaking change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
return (function stringify(parent, key, node, path = []) {
return (function stringify(parent, key, node, path = []) {
path = typeof(path) === 'number' ? new Array(path) : path;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

the breaking syntax is default parameters - = in a function signature.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

cool then

Suggested change
return (function stringify(parent, key, node, path = []) {
return (function stringify(parent, key, node, path = []) {
path = typeof(path) === 'number' ? new Array(path) : (path == null ? [] : path);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

again, it's the path = [] in the signature that can't work.

Comment thread index.js Outdated
Comment thread index.js Outdated
Comment thread index.js Outdated
@ljharb
ljharb marked this pull request as draft April 13, 2023 05:29
maa-dar and others added 6 commits June 5, 2023 08:27
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
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.

2 participants