Last active
September 3, 2021 02:16
-
-
Save themgoncalves/e191e9cccbcc0cf0c923f9b69ae82927 to your computer and use it in GitHub Desktop.
Making JavaScript typeof() work - typescript demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typeOf from './type-of'; | |
const shallowCompare: (source: Record<string, unknown | unknown[]>, target: Record<string, unknown | unknown[]>): boolean => { | |
if (typeOf(source) !== typeOf(target)) { | |
return false; | |
} | |
return Object.keys(source).every((key) => { | |
if (typeOf(source[key]) !== typeOf(target[key])) { | |
return false; | |
} | |
if (Array.isArray(source[key])) { | |
return (source[key] as unknown[]).length === (target[key] as unknown[]).length; | |
} | |
return source[key] === target[key]; | |
}); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dfdfd