Skip to content

Commit

Permalink
Adding prettier (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreardon authored Dec 17, 2018
1 parent 06bb861 commit f7b6a8e
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 232 deletions.
145 changes: 0 additions & 145 deletions .eslintrc

This file was deleted.

49 changes: 49 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module.exports = {
extends: [
'prettier',
'eslint:recommended',
'plugin:flowtype/recommended',
'prettier/flowtype',
],
parser: 'babel-eslint',
plugins: ['prettier', 'jest', 'flowtype'],
env: {
node: true,
browser: true,
es6: true,
'jest/globals': true,
},
// custom rules
rules: {
// Error on prettier violations
'prettier/prettier': 'error',

// New eslint style rules that is not disabled by prettier:
'lines-between-class-members': 'off',

// Allowing warning and error console logging
// use `invariant` and `warning`
'no-console': ['error'],

// Opting out of prefer destructuring (nicer with flow in lots of cases)
'prefer-destructuring': 'off',

// Disallowing the use of variables starting with `_` unless it called on `this`.
// Allowed: `this._secret = Symbol()`
// Not allowed: `const _secret = Symbol()`
'no-underscore-dangle': ['error', { allowAfterThis: true }],

// Cannot reassign function parameters but allowing modification
'no-param-reassign': ['error', { props: false }],

// Allowing ++ on numbers
'no-plusplus': 'off',

// Require // @flow at the top of files
'flowtype/require-valid-file-annotation': [
'error',
'always',
{ annotationStyle: 'line' },
],
},
};
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/*
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "all",
"semi": true,
"tabWidth": 2,
"useTabs": false,
"singleQuote": true
}
44 changes: 27 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ const identity = x => x;
const defaultMemoization = memoizeOne(identity);
const customMemoization = memoizeOne(identity, deepEqual);

const result1 = defaultMemoization({foo: 'bar'});
const result2 = defaultMemoization({foo: 'bar'});
const result1 = defaultMemoization({ foo: 'bar' });
const result2 = defaultMemoization({ foo: 'bar' });

result1 === result2 // false - difference reference
result1 === result2; // false - difference reference

const result3 = customMemoization({foo: 'bar'});
const result4 = customMemoization({foo: 'bar'});
const result3 = customMemoization({ foo: 'bar' });
const result4 = customMemoization({ foo: 'bar' });

result3 === result4 // true - arguments are deep equal
result3 === result4; // true - arguments are deep equal
```

### Custom equality function behaviour
Expand Down Expand Up @@ -129,7 +129,11 @@ Here is an example of a higher order function that allow you to pass an `index`

```js
// this function will do some special checking for the second argument
const customIsEqual = (newValue: mixed, oldValue: mixed, index: number): boolean => {
const customIsEqual = (
newValue: mixed,
oldValue: mixed,
index: number,
): boolean => {
if (index === 1) {
if (!isDate(newValue) || !isDate(oldValue)) {
return false;
Expand All @@ -142,7 +146,8 @@ const customIsEqual = (newValue: mixed, oldValue: mixed, index: number): boolean

const getMemoizedWithIndex = (fn: Function) => {
let argIndex: number = 0;
const withIndex = (newValue: mixed, oldValue: mixed) => customIsEqual(newValue, oldValue, argIndex++);
const withIndex = (newValue: mixed, oldValue: mixed) =>
customIsEqual(newValue, oldValue, argIndex++);
const memoized = memoizeOne(fn, withIndex);

// every time this function is called it will reset our argIndex
Expand All @@ -161,7 +166,12 @@ Here is an example of a higher order function that allow you to pass a `index` a

```js
// using this to only memoize calls with 3+ arguments
const customIsEqual = (newValue: mixed, oldValue: mixed, index: number, args: mixed[]): boolean => {
const customIsEqual = (
newValue: mixed,
oldValue: mixed,
index: number,
args: mixed[],
): boolean => {
if (args.length < 3) {
return false;
}
Expand All @@ -170,7 +180,8 @@ const customIsEqual = (newValue: mixed, oldValue: mixed, index: number, args: mi
const getMemoizedFn = (fn: Function) => {
let args: mixed[] = [];
let argIndex: number = 0;
const withIndex = (newValue: mixed, oldValue: mixed) => customIsEqual(newValue, oldValue, argIndex++, args);
const withIndex = (newValue: mixed, oldValue: mixed) =>
customIsEqual(newValue, oldValue, argIndex++, args);
const memoized = memoizeOne(fn, withIndex);

// every time this function is called it will reset our args and argIndex
Expand Down Expand Up @@ -211,15 +222,15 @@ const temp1 = {
};
const temp2 = {
a: 30,
}
};

getA.call(temp1); // 20
getA.call(temp2); // 30
```

Therefore, in order to prevent against unexpected results, `memoize-one` takes into account the current execution context (`this`) of the memoized function. If `this` is different to the previous invocation then it is considered a change in argument. [further discussion](https://github.com/alexreardon/memoize-one/issues/3).

Generally this will be of no impact if you are not explicity controlling the `this` context of functions you want to memoize with [explicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#explicit-binding) or [implicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#implicit-binding). `memoize-One` will detect when you are manipulating `this` and will then consider the `this` context as an argument. If `this` changes, it will re-execute the original function even if the arguments have not changed.
Generally this will be of no impact if you are not explicity controlling the `this` context of functions you want to memoize with [explicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#explicit-binding) or [implicit binding](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md#implicit-binding). `memoize-One` will detect when you are manipulating `this` and will then consider the `this` context as an argument. If `this` changes, it will re-execute the original function even if the arguments have not changed.

## When your result function `throw`s

Expand All @@ -230,11 +241,11 @@ If your result function `throw`s then the memoized function will also throw. The
```js
const canThrow = (name: string) => {
console.log('called');
if(name === 'throw') {
if (name === 'throw') {
throw new Error(name);
}
return { name };
}
};

const memoized = memoizeOne(canThrow);

Expand All @@ -249,7 +260,7 @@ console.log(value1 === value2);
try {
memoized('throw');
// console.log => 'called'
} catch(e) {
} catch (e) {
firstError = e;
}

Expand All @@ -258,13 +269,12 @@ try {
// console.log => 'called'
// the result function was called again even though it was called twice
// with the 'throw' string
} catch(e) {
} catch (e) {
secondError = e;
}

console.log(firstError !== secondError);


const value3 = memoized('Alex');
// result function not called as the original memoization cache has not been busted
console.log(value1 === value3);
Expand Down
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,39 @@
],
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.0",
"@babel/preset-flow": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "^23.6.0",
"cross-env": "^5.2.0",
"eslint": "5.10.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-flowtype": "^3.2.0",
"eslint-plugin-jest": "^22.1.2",
"flow-bin": "0.88.0",
"eslint-plugin-prettier": "^3.0.0",
"flow-bin": "0.89.0",
"jest": "^23.6.0",
"prettier": "1.15.3",
"rimraf": "2.6.2",
"rollup": "^0.67.4",
"rollup-plugin-babel": "^4.0.3",
"rollup": "^0.68.0",
"rollup-plugin-babel": "^4.1.0",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-uglify": "^6.0.0"
},
"config": {
"prettier_target": "*.{js,jsx,md,json} src/**/*.{js,jsx,md,json} test/**/*.{js,jsx,md,json}"
},
"scripts": {
"validate": "yarn run lint && yarn run typecheck",
"test": "cross-env NODE_ENV=test jest",
"typecheck": "flow check",
"lint": "eslint src test",
"prettier:check": "yarn prettier --debug-check $npm_package_config_prettier_target",
"prettier:write": "yarn prettier --write $npm_package_config_prettier_target",
"lint:eslint": "eslint src test",
"lint": "yarn lint:eslint && yarn prettier:check",
"build": "yarn run build:clean && yarn run build:dist && yarn run build:flow",
"build:clean": "rimraf dist",
"build:dist": "rollup -c",
Expand Down
Loading

0 comments on commit f7b6a8e

Please sign in to comment.