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

Different return values from higher order functions generate the same hash #98

Open
TheLudd opened this issue Jun 9, 2020 · 0 comments

Comments

@TheLudd
Copy link

TheLudd commented Jun 9, 2020

If function A takes an argument and returns another function, the returned function will always generate the same hash regardless of which arguments were passed to A.

Example (also available here https://runkit.com/embed/x2fle9t0ymt1):

const objectHash = require("object-hash")

function prop (propName) {
  return (obj) => obj[propName]
}

const getFoo = prop('foo')
const getBar = prop('bar')

const anObject = {
  foo: 'string one',
  bar: 'string 2',
}

const results = [
  objectHash(getFoo) !== objectHash(getBar),
  getFoo(anObject) !== getBar(anObject),
]
// result is [ false, true ]

This is because calling toString on the returned function will always give the same string back.

I guess the most wanted behavior would be:

const fn1 = someFn('foo')
const fn2 = someFn('bar')

objectHash(fn1) === objectHash(fn1) // true
objectHash(fn1) === objectHash(fn2) // false
objectHash(fn1) === objectHash(someFn('foo')) // true

I am not sure if this is possible however...

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

1 participant