Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Last active November 17, 2021 21:35
Show Gist options
  • Save themgoncalves/ce2493408b77017814cc1edf4f5186dc to your computer and use it in GitHub Desktop.
Save themgoncalves/ce2493408b77017814cc1edf4f5186dc to your computer and use it in GitHub Desktop.
import typeOf from './type-of';
const shallowCompare = (source, target) => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
return source.length === target.length;
} else if (typeOf(source) === 'object') {
return Object.keys(source).every((key) => source[key] === target[key]);
} else if (typeOf(source) === 'date') {
return source.getTime() === target.getTime();
}
return source === target;
};
@EJIqpEP
Copy link

EJIqpEP commented Nov 17, 2021

This logic doesn't look correct

shallowCompare([1], [2])

will return true...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment