Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Created August 19, 2021 00:46
Show Gist options
  • Save MeetMartin/975978390a90ab6e22319d756d53a1a5 to your computer and use it in GitHub Desktop.
Save MeetMartin/975978390a90ab6e22319d756d53a1a5 to your computer and use it in GitHub Desktop.
// pure function
// depends only on its input
// and produces only its output
const pureFunction = input => input + 1;
const pureFunction2 = function(input) {
return input + 1;
};
// impure function
// depends on external state or changes it
// like databases, cookies, files, console...
let someNumber = 0;
const impureFunction = () => {
someNumber = someNumber + 1;
}; // mutates someNumber creating a side effect
// pure functions are easier to unit test
// because you don't have to mock, proxy, or inject
const identity = a => a;
test('identity output is the same as input.', () => {
expect(identity('7urtle')).toBe('7urtle');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment