diff --git a/package.json b/package.json index a291509fcb..66981b7906 100644 --- a/package.json +++ b/package.json @@ -81,5 +81,11 @@ "*.js" ] } - ] + ], + "dependencies": { + "@f/compose": "^0.1.2", + "@f/is-object": "^1.1.2", + "@f/map-obj": "^1.2.2", + "@f/pick": "^1.1.2" + } } diff --git a/src/createStore.js b/src/createStore.js index b46ff9554b..84aa948fd7 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -1,4 +1,4 @@ -import isPlainObject from './utils/isPlainObject' +import isPlainObject from '@f/is-object' /** * These are private action types reserved by Redux. diff --git a/src/index.js b/src/index.js index 1b11c79714..72a3525299 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import createStore from './createStore' import combineReducers from './utils/combineReducers' import bindActionCreators from './utils/bindActionCreators' import applyMiddleware from './utils/applyMiddleware' -import compose from './utils/compose' +import compose from '@f/compose' export { createStore, diff --git a/src/utils/applyMiddleware.js b/src/utils/applyMiddleware.js index 35846b66fb..36e0a0d491 100644 --- a/src/utils/applyMiddleware.js +++ b/src/utils/applyMiddleware.js @@ -1,4 +1,4 @@ -import compose from './compose' +import compose from '@f/compose' /** * Creates a store enhancer that applies middleware to the dispatch method diff --git a/src/utils/bindActionCreators.js b/src/utils/bindActionCreators.js index 111c426f0b..225971cbf3 100644 --- a/src/utils/bindActionCreators.js +++ b/src/utils/bindActionCreators.js @@ -1,4 +1,4 @@ -import mapValues from './mapValues' +import mapValues from '@f/map-obj' function bindActionCreator(actionCreator, dispatch) { return (...args) => dispatch(actionCreator(...args)) @@ -37,7 +37,7 @@ export default function bindActionCreators(actionCreators, dispatch) { ) } - return mapValues(actionCreators, actionCreator => + return mapValues(actionCreator => bindActionCreator(actionCreator, dispatch) - ) + , actionCreators) } diff --git a/src/utils/combineReducers.js b/src/utils/combineReducers.js index 49cfd386c2..21f87b7d8c 100644 --- a/src/utils/combineReducers.js +++ b/src/utils/combineReducers.js @@ -1,7 +1,7 @@ import { ActionTypes } from '../createStore' -import isPlainObject from './isPlainObject' -import mapValues from './mapValues' -import pick from './pick' +import isPlainObject from '@f/is-object' +import mapValues from '@f/map-obj' +import pick from '@f/pick' /* eslint-disable no-console */ @@ -95,7 +95,7 @@ function assertReducerSanity(reducers) { */ export default function combineReducers(reducers) { - var finalReducers = pick(reducers, (val) => typeof val === 'function') + var finalReducers = pick((val) => typeof val === 'function', reducers) var sanityError try { @@ -117,7 +117,7 @@ export default function combineReducers(reducers) { } var hasChanged = false - var finalState = mapValues(finalReducers, (reducer, key) => { + var finalState = mapValues((reducer, key) => { var previousStateForKey = state[key] var nextStateForKey = reducer(previousStateForKey, action) if (typeof nextStateForKey === 'undefined') { @@ -126,7 +126,7 @@ export default function combineReducers(reducers) { } hasChanged = hasChanged || nextStateForKey !== previousStateForKey return nextStateForKey - }) + }, finalReducers) return hasChanged ? finalState : state } diff --git a/src/utils/compose.js b/src/utils/compose.js deleted file mode 100644 index d26752b712..0000000000 --- a/src/utils/compose.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Composes single-argument functions from right to left. - * - * @param {...Function} funcs The functions to compose. - * @returns {Function} A function obtained by composing functions from right to - * left. For example, compose(f, g, h) is identical to arg => f(g(h(arg))). - */ -export default function compose(...funcs) { - return (...args) => { - if (funcs.length === 0) { - return args[0] - } - - const last = funcs[funcs.length - 1] - const rest = funcs.slice(0, -1) - - return rest.reduceRight((composed, f) => f(composed), last(...args)) - } -} diff --git a/src/utils/isPlainObject.js b/src/utils/isPlainObject.js deleted file mode 100644 index 3921c3045c..0000000000 --- a/src/utils/isPlainObject.js +++ /dev/null @@ -1,24 +0,0 @@ -var fnToString = (fn) => Function.prototype.toString.call(fn) -var objStringValue = fnToString(Object) - -/** - * @param {any} obj The object to inspect. - * @returns {boolean} True if the argument appears to be a plain object. - */ -export default function isPlainObject(obj) { - if (!obj || typeof obj !== 'object') { - return false - } - - var proto = typeof obj.constructor === 'function' ? Object.getPrototypeOf(obj) : Object.prototype - - if (proto === null) { - return true - } - - var constructor = proto.constructor - - return typeof constructor === 'function' - && constructor instanceof constructor - && fnToString(constructor) === objStringValue -} diff --git a/src/utils/mapValues.js b/src/utils/mapValues.js deleted file mode 100644 index 165d6667ad..0000000000 --- a/src/utils/mapValues.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Applies a function to every key-value pair inside an object. - * - * @param {Object} obj The source object. - * @param {Function} fn The mapper function that receives the value and the key. - * @returns {Object} A new object that contains the mapped values for the keys. - */ -export default function mapValues(obj, fn) { - return Object.keys(obj).reduce((result, key) => { - result[key] = fn(obj[key], key) - return result - }, {}) -} diff --git a/src/utils/pick.js b/src/utils/pick.js deleted file mode 100644 index a61e1f1e7b..0000000000 --- a/src/utils/pick.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Picks key-value pairs from an object where values satisfy a predicate. - * - * @param {Object} obj The object to pick from. - * @param {Function} fn The predicate the values must satisfy to be copied. - * @returns {Object} The object with the values that satisfied the predicate. - */ -export default function pick(obj, fn) { - return Object.keys(obj).reduce((result, key) => { - if (fn(obj[key])) { - result[key] = obj[key] - } - return result - }, {}) -} diff --git a/test/utils/isPlainObject.spec.js b/test/utils/isPlainObject.spec.js index 3314c5ff6e..f71554a5cf 100644 --- a/test/utils/isPlainObject.spec.js +++ b/test/utils/isPlainObject.spec.js @@ -1,5 +1,5 @@ import expect from 'expect' -import isPlainObject from '../../src/utils/isPlainObject' +import isPlainObject from '@f/is-object' import vm from 'vm' describe('isPlainObject', () => { diff --git a/test/utils/mapValues.spec.js b/test/utils/mapValues.spec.js index 3aef6fe3b6..16b9374a01 100644 --- a/test/utils/mapValues.spec.js +++ b/test/utils/mapValues.spec.js @@ -1,5 +1,5 @@ import expect from 'expect' -import mapValues from '../../src/utils/mapValues' +import mapValues from '@f/map-obj' describe('mapValues', () => { it('returns object with mapped values', () => { @@ -7,7 +7,8 @@ describe('mapValues', () => { a: 'c', b: 'd' } - expect(mapValues(test, (val, key) => val + key)).toEqual({ + + expect(mapValues((val, key) => val + key, test)).toEqual({ a: 'ca', b: 'db' }) diff --git a/test/utils/pick.spec.js b/test/utils/pick.spec.js index 745ba72c29..314b75ae32 100644 --- a/test/utils/pick.spec.js +++ b/test/utils/pick.spec.js @@ -1,5 +1,5 @@ import expect from 'expect' -import pick from '../../src/utils/pick' +import pick from '@f/pick' describe('pick', () => { it('returns object with picked values', () => { @@ -8,7 +8,7 @@ describe('pick', () => { age: 20 } expect( - pick(test, x => typeof x === 'string') + pick(x => typeof x === 'string', test) ).toEqual({ name: 'lily' })