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

L.get has unexpected behavior #217

Open
pete-murphy opened this issue Jul 21, 2019 · 2 comments
Open

L.get has unexpected behavior #217

pete-murphy opened this issue Jul 21, 2019 · 2 comments

Comments

@pete-murphy
Copy link

pete-murphy commented Jul 21, 2019

Unless I'm missing something, L.get behaves differently from Lodash's _.get (and from an equivalent implementation in Ramda) in this minimal example (using @apollo/react-hooks): https://codesandbox.io/s/thirsty-einstein-frv16

...
import * as L from "partial.lenses";
import * as _ from "lodash/fp";

...

function App() {
  const { data } = useQuery(EXAMPLE, { client });
  return (
    <div className="App">
      <div>
        <h2>This works</h2>
        <Pre>{_.get(["continents"])(data)}</Pre>
      </div>
      <div>
        <h2>This doesn't work</h2>
        <Pre>{L.get(["continents"])(data)}</Pre>
      </div>
    </div>
  );
}
@polytypic
Copy link
Member

This is due to "naked" objects.

Unfortunately, at the moment, Partial Lenses does not support them. That is because naked objects are not instances of objects (specifically Object.create(null) instanceof Object is false), which is the test that Partial Lenses currently uses to test for object like thing. Support for naked objects has been on the list for some time now #127.

Initially Partial Lenses actually required that objects must have Object as the constructor. The reasoning behind that is that I wanted optics to work on plain / pure data and avoid dealing with any sort of object-oriented style objects. Of course, it is very common to have custom constructors even for essentially plain data objects so that requirement had to be relaxed. Unfortunately at that point I didn't know about the possibility of naked objects.

@pete-murphy
Copy link
Author

Ah, I hadn't heard of "naked" objects. So a quick fix would be to convert them over to regular objects with Object.assign or spread syntax:

-        <Pre>{L.get(["continents"])(data)}</Pre>
+        <Pre>{L.get(["continents"])({...data})}</Pre>

Thank you!

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

No branches or pull requests

2 participants