Skip to content

Commit 6bf1e81

Browse files
committed
Rid off Ramda
1 parent 22e842d commit 6bf1e81

File tree

5 files changed

+95
-20
lines changed

5 files changed

+95
-20
lines changed

Diff for: package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"prepublish": "npm run build"
2626
},
2727
"dependencies": {
28-
"co": "^4.6.0",
29-
"ramda": "^0.18.0"
28+
"co": "^4.6.0"
3029
},
3130
"devDependencies": {
3231
"babel-cli": "^6.3.17",
@@ -39,7 +38,6 @@
3938
"co": "^4.6.0",
4039
"mocha": "*",
4140
"prop-types": "^15.6.0",
42-
"ramda": "^0.18.0",
4341
"react": "^16.1.1",
4442
"react-redux": "^4.0.0",
4543
"redux": "^3.0.0",

Diff for: src/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import PropTypes from 'prop-types';
2-
import R from 'ramda';
32
import React from 'react';
43
import co from 'co';
54
import { aggregateSelectors } from './selector_utils';
5+
import { mapObj, pick, merge, mergeAll } from './utils';
66
import { connect } from 'react-redux';
77

88
const toDispatchSymbol = Symbol('toDispatch');
@@ -103,23 +103,23 @@ export function runControllerGenerator(propsGetter) {
103103
*/
104104
export function controller(RootComponent, controllerGenerators, selectorBundles, controllerGeneratorRunner = runControllerGenerator) {
105105
// Combine selector bundles into one mapStateToProps function.
106-
const mapStateToProps = aggregateSelectors(R.mergeAll(R.flatten([selectorBundles])));
106+
const mapStateToProps = aggregateSelectors(mergeAll(selectorBundles));
107107
const selectorPropTypes = mapStateToProps.propTypes;
108108

109109
// All the controller method propTypes should simply be "function" so we can
110110
// synthensize those.
111-
const controllerMethodPropTypes = R.map(() => PropTypes.func.isRequired, controllerGenerators);
111+
const controllerMethodPropTypes = mapObj(() => PropTypes.func.isRequired, controllerGenerators);
112112

113113
// Declare the availability of all of the selectors and controller methods
114114
// in the React context for descendant components.
115-
const contextPropTypes = R.merge(selectorPropTypes, controllerMethodPropTypes);
115+
const contextPropTypes = merge(selectorPropTypes, controllerMethodPropTypes);
116116

117117
class Controller extends React.Component {
118118
constructor(...constructorArgs) {
119119
super(...constructorArgs);
120120

121121
const injectedControllerGeneratorRunner = controllerGeneratorRunner(() => this.props);
122-
this.controllerMethods = R.map(controllerGenerator =>
122+
this.controllerMethods = mapObj(controllerGenerator =>
123123
injectedControllerGeneratorRunner(controllerGenerator)
124124
, controllerGenerators);
125125

@@ -136,8 +136,8 @@ export function controller(RootComponent, controllerGenerators, selectorBundles,
136136
getChildContext() {
137137
// Rather than injecting all of the RootComponent props into the context,
138138
// we only explictly pass selector and controller method props.
139-
const selectorProps = R.pick(R.keys(selectorPropTypes), this.props);
140-
const childContext = R.merge(selectorProps, this.controllerMethods);
139+
const selectorProps = pick(Object.keys(selectorPropTypes), this.props);
140+
const childContext = merge(selectorProps, this.controllerMethods);
141141
return childContext;
142142
}
143143

@@ -148,7 +148,7 @@ export function controller(RootComponent, controllerGenerators, selectorBundles,
148148
}
149149
}
150150

151-
Controller.propTypes = R.merge(selectorPropTypes, RootComponent.propTypes || {});
151+
Controller.propTypes = merge(selectorPropTypes, RootComponent.propTypes || {});
152152
Controller.childContextTypes = contextPropTypes;
153153

154154
return connect(mapStateToProps)(Controller);

Diff for: src/selector_utils.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import R from 'ramda';
1+
import { mapObj } from './utils';
22

33
/**
44
* Combines bundle of selector functions into a single super selector function
@@ -44,8 +44,8 @@ import R from 'ramda';
4444
* of the selector outputs.
4545
*/
4646
export function aggregateSelectors(bundle) {
47-
const combinedSelector = state => R.map(selectorFunction => selectorFunction(state), bundle);
48-
combinedSelector.propTypes = R.map(selectorFunction => selectorFunction.propType, bundle);
47+
const combinedSelector = state => mapObj(selectorFunction => selectorFunction(state), bundle);
48+
combinedSelector.propTypes = mapObj(selectorFunction => selectorFunction.propType, bundle);
4949
return combinedSelector;
5050
}
5151

@@ -57,8 +57,8 @@ export function aggregateSelectors(bundle) {
5757
* annotated with a propType property.
5858
*/
5959
export function disaggregateSuperSelector(superSelector) {
60-
return R.mapObjIndexed((propType, propName) => {
61-
const singleSelector = R.pipe(superSelector, R.prop(propName));
60+
return mapObj((propType, propName) => {
61+
const singleSelector = store => superSelector(store)[propName];
6262
singleSelector.propType = propType;
6363
return singleSelector;
6464
}, superSelector.propTypes);

Diff for: src/utils.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Map, but for objects. Creates an object with the same keys as `obj` and values
3+
* generated by running each property of `obj` through `fn`. `fn` is passed three arguments: *(value, key, obj)*.
4+
* @param {Function} fn A function called for each property in `obj`. Its return value will
5+
* become a new property on the return object.
6+
* @param {Object} obj The object to iterate over.
7+
* @return {Object} A new object with the same keys as `obj` and values that are the result
8+
* of running each property through `fn`.
9+
*/
10+
export function mapObj(fn, obj) {
11+
const result = {};
12+
13+
const keys = Object.keys(obj);
14+
15+
for (let i = 0; i < keys.length; ++i) {
16+
result[keys[i]] = fn(obj[keys[i]], keys[i], obj);
17+
}
18+
19+
return result;
20+
}
21+
22+
/**
23+
* Returns a partial copy of an object containing only the keys specified. If the key does not exist, the
24+
* property is ignored.
25+
* @param {String[]} keys An array of String property names to copy onto a new object.
26+
* @param {Object} obj The object to copy from.
27+
* @return {Object} A new object with only properties from `keys` on it.
28+
*/
29+
export function pick(keys, obj) {
30+
const result = {};
31+
32+
for (let i = 0; i < keys.length; ++i) {
33+
if (keys[i] in obj) {
34+
result[keys[i]] = obj[keys[i]];
35+
}
36+
}
37+
38+
return result;
39+
}
40+
41+
/**
42+
* Create a new object with the own properties of `a`
43+
* merged with the own properties of object `b`.
44+
* @param {Object} a The first object.
45+
* @param {Object} b The second object.
46+
* @return {Object} A merged object.
47+
*/
48+
export function merge(a, b) {
49+
const result = {};
50+
51+
for (let i = 0, keys = Object.keys(a); i < keys.length; ++i) {
52+
result[keys[i]] = a[keys[i]];
53+
}
54+
55+
for (let i = 0, keys = Object.keys(b); i < keys.length; ++i) {
56+
result[keys[i]] = b[keys[i]];
57+
}
58+
59+
return result;
60+
}
61+
62+
/**
63+
* Merges a list of objects together into one object.
64+
* @param {Object|Object[]} objs An array of objects or a single object.
65+
* @return {Object} A merged object.
66+
*/
67+
export function mergeAll(objs) {
68+
const list = Array.isArray(objs) ? objs : [objs];
69+
70+
const result = {};
71+
72+
for (let i = 0; i < list.length; ++i) {
73+
const keys = Object.keys(list[i]);
74+
75+
for (let j = 0; j < keys.length; ++j) {
76+
result[keys[j]] = list[i][keys[j]];
77+
}
78+
}
79+
80+
return result;
81+
}

Diff for: yarn.lock

-4
Original file line numberDiff line numberDiff line change
@@ -1760,10 +1760,6 @@ [email protected]:
17601760
version "0.2.0"
17611761
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
17621762

1763-
ramda@^0.18.0:
1764-
version "0.18.0"
1765-
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.18.0.tgz#c6e3c5d4b9ab1f7906727fdeeb039152a85d4db3"
1766-
17671763
randomatic@^1.1.3:
17681764
version "1.1.7"
17691765
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.7.tgz#c7abe9cc8b87c0baa876b19fde83fd464797e38c"

0 commit comments

Comments
 (0)