Skip to content
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

require that inputs be plain objects (or arrays of) #133

Merged
merged 4 commits into from
Mar 26, 2024

Conversation

yutak23
Copy link
Contributor

@yutak23 yutak23 commented Jan 12, 2024

This resolves the issue #121.

I am updating the README to match the TypeScript type definitions.

Then update any[] to ReadonlyArray<Record<string, unknown>> to accept only the key-value record type of the array.

@@ -41,7 +41,6 @@ expectType<{ foo_bar: boolean }[]>(snakecaseKeys([{ fooBar: true }]));
expectAssignable<Array<{ [key: string]: boolean }>>(
snakecaseKeys([{ fooBar: true }])
);
expectType<string[]>(snakecaseKeys(["name 1", "name 2"]));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed the unsupported array format test.

@yutak23
Copy link
Contributor Author

yutak23 commented Jan 15, 2024

@bendrucker, I would appreciate your confirmation.

Copy link
Owner

@bendrucker bendrucker left a comment

Choose a reason for hiding this comment

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

Please go ahead and separate the documentation and type concerns since I'm left unsure as to whether there's any legitimate issue here.

I do not put TypeScript in readmes as ultimately these are intended as JavaScript packages and should meet that lowest common denominator. The types given are JS pseudo-code. I do accept PRs with user-contributed types but TypeScript fluency should not be required to read the docs.

If you want to drop support for arrays you'll need to do more to make the case for that breaking change and make the change comprehensively. Obviously it's a bit silly to try to snake case an array of strings. Presumably this was a possible path because arrays are objects in JS and could be passed to map-obj without erroring. Dropping support for arrays also means the JavaScript code should also reject them.

@@ -29,9 +29,9 @@ snakecaseKeys({'foo-bar': true, nested: {fooBaz: 'bar'}});
##### obj

*Required*
Type: `object`
Copy link
Owner

Choose a reason for hiding this comment

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

This is an informal pseudo-JS notation. I am not trying to use TypeScript types here. See exclude for example.

@bendrucker
Copy link
Owner

Happened to be peeking at map-obj for another PR

sindresorhus/map-obj#39

This happened well after I started using map-obj. Sindre went with a patch. This is one of the many ambiguous semver cases where it's strictly breaking but you can surely argue that "this wasn't supposed to work."

I also haven't updated map-obj since v5 is ESM only though, so you'd still have to implement the array checking here until/if I get around to an ESM conversion that can apply the patch to the aforementioned issue.

@yutak23
Copy link
Contributor Author

yutak23 commented Jan 19, 2024

Please go ahead and separate the documentation and type concerns since I'm left unsure as to whether there's any legitimate issue here.

I do not put TypeScript in readmes as ultimately these are intended as JavaScript packages and should meet that lowest common denominator. The types given are JS pseudo-code. I do accept PRs with user-contributed types but TypeScript fluency should not be required to read the docs.

If you want to drop support for arrays you'll need to do more to make the case for that breaking change and make the change comprehensively. Obviously it's a bit silly to try to snake case an array of strings. Presumably this was a possible path because arrays are objects in JS and could be passed to map-obj without erroring. Dropping support for arrays also means the JavaScript code should also reject them.

Happened to be peeking at map-obj for another PR

sindresorhus/map-obj#39

This happened well after I started using map-obj. Sindre went with a patch. This is one of the many ambiguous semver cases where it's strictly breaking but you can surely argue that "this wasn't supposed to work."

I also haven't updated map-obj since v5 is ESM only though, so you'd still have to implement the array checking here until/if I get around to an ESM conversion that can apply the patch to the aforementioned issue.

@bendrucker , Thank you for your review. Are you sure that you want to follow the following steps to correct this issue?

  1. create a PR with only TypeScript type definitions
  2. modify the implementation to support arrays in the README and JS code (support only plain objects)

@bendrucker
Copy link
Owner

Yeah, let's do this in multiple distinct steps if possible for clarity. If there's any changes you think should be made right now to the TypeScript definitions, based on what the JS code supports, please send that as one PR. If the TS already accurately reflects the inputs that the underlying JS implementation supports, no change is needed here.

Separately, you can send another PR dropping support for arrays, which will involve both TS and JS changes. The TS definition can make arrays a compile error, but also the JS should reject arrays with a TypeError to ensure uniform behavior. Perhaps I should have waited to release v6 and included this, but I didn't think of it. Given the presence of a type test I'd probably err on the side of v7 rather than 6.0.1.

@yutak23 yutak23 force-pushed the feature/fix-type-and-readme branch from 3384901 to abca2b5 Compare March 21, 2024 06:37
@yutak23 yutak23 requested a review from bendrucker March 21, 2024 06:44
@yutak23
Copy link
Contributor Author

yutak23 commented Mar 21, 2024

Yeah, let's do this in multiple distinct steps if possible for clarity. If there's any changes you think should be made right now to the TypeScript definitions, based on what the JS code supports, please send that as one PR. If the TS already accurately reflects the inputs that the underlying JS implementation supports, no change is needed here.

Separately, you can send another PR dropping support for arrays, which will involve both TS and JS changes. The TS definition can make arrays a compile error, but also the JS should reject arrays with a TypeError to ensure uniform behavior. Perhaps I should have waited to release v6 and included this, but I didn't think of it. Given the presence of a type test I'd probably err on the side of v7 rather than 6.0.1.

@bendrucker, sorry for the late reply, I have changed the PR to fix the TS type definitions as a set with the JS code fixes.
I would appreciate your confirmation.

@bendrucker bendrucker changed the title fix: change any[] to ReadonlyArray<Record<string, unknown>> and update readme require that inputs be plain objects (or arrays of) Mar 26, 2024
@bendrucker bendrucker merged commit 313e5aa into bendrucker:master Mar 26, 2024
2 checks passed
@bendrucker
Copy link
Owner

Thanks for your work on this! Out of caution I'm going to release this as a new major version since both the JS and typing changes have potential to break people's code.

@yutak23 yutak23 deleted the feature/fix-type-and-readme branch March 26, 2024 01:34
@hhanesand
Copy link

I think this may have introduced a bug with arrays. This test no longer functions, because it throws Error: obj must be an plain object

it("should return array of snake_case objects", function () {
  const test = [{ camelCase: "foo" }];
  expect(snake(test)).to.deep.equal([{ camel_case: "foo" }]);
});

Most likely the check for the array input type should be

if (is array) {
   assert array contents
} else {
   assert is object
}

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.

None yet

3 participants