Skip to content

Commit

Permalink
drop lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
Darmody committed Apr 1, 2016
1 parent 01107f8 commit 64d6e91
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 47 deletions.
6 changes: 3 additions & 3 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"electron-cookies": "^1.1.0",
"electron-json-storage": "^2.0.0",
"humps": "^1.0.0",
"lodash": "^4.6.1",
"lodash.sample": "^4.1.1",
"material-design-icons-iconfont": "^2.0.4",
"moment": "^2.12.0",
Expand Down
25 changes: 9 additions & 16 deletions src/middlewares/apiMiddlewareHook.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { compose } from 'redux';
import { CALL_API, isValidRSAA } from 'redux-api-middleware';
import { decamelizeKeys } from 'humps';
import _transform from 'lodash/transform';
import _join from 'lodash/join';
import _isPlainObject from 'lodash/isPlainObject';
import _includes from 'lodash/includes';
import _ from 'ramda';
import {
FETCH_REQUEST as CHANNEL_FETCH_REQUEST,
BAN_REQUEST as CHANNEL_BAN_REQUEST,
Expand All @@ -31,24 +28,20 @@ const decamelizeBody = (action) => {
};

const serializeFormBody = (action) => {

const callAPI = action[CALL_API];
const { body, headers } = callAPI;

if (!body || headers['Content-Type'] !== 'application/x-www-form-urlencoded') {
return action;
}

/* eslint no-param-reassign: 0*/
const fieldsArray = _transform(body, (result, value, key) => {
result.push(`${key}=${encodeURIComponent(value)}`);
}, []);
if (!body || headers['Content-Type'] !== 'application/x-www-form-urlencoded') return action;

const parsedBody = _.compose(
_.join('&'), _.map(_.join('=')), _.toPairs,
_.mapObjIndexed((value) => encodeURIComponent(value))
)(body);
return {
...action,
[CALL_API]: {
...callAPI,
body: _join(fieldsArray, '&'),
body: parsedBody,
}
};
};
Expand All @@ -57,7 +50,7 @@ const stringifyJsonBody = (action) => {
const callAPI = action[CALL_API];
const { body } = callAPI;

if (!body || !_isPlainObject(body)) return action;
if (!body || !_.is(Object, body)) return action;

return {
...action,
Expand All @@ -72,7 +65,7 @@ const pendingRefuse = (action, store) => {
if (!store) return action;

const callAPI = action[CALL_API];
if (_includes([CHANNEL_FETCH_REQUEST, CHANNEL_BAN_REQUEST], callAPI.types[0])) {
if (_.contains(callAPI.types[0], [CHANNEL_FETCH_REQUEST, CHANNEL_BAN_REQUEST])) {
if (store.getState().channel.loading) return { type: CHANNEL_REFUSE };
}
if (FAVORITE_BAN_REQUEST === callAPI.types[0]) {
Expand Down
13 changes: 6 additions & 7 deletions src/reducers/__tests__/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { expect } from 'chai';
import nock from 'nock';
import { apiMiddleware } from 'redux-api-middleware';
import thunk from 'redux-thunk';
import _ from 'ramda';
import apiMiddlewareHook from '../../middlewares/apiMiddlewareHook';
import camelizeState from '../../middlewares/camelizeState';
import _last from 'lodash/last';
import _join from 'lodash/join';
import {
LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT, VERIFY_SUCCESS, VERIFY_FAILURE,
} from '../../actionTypes/auth';
Expand Down Expand Up @@ -35,7 +34,7 @@ describe('Auth Actions', function actions() {
'captcha_id=captchaId'
];
nock('http://douban.fm/')
.post('/j/login', _join(params, '&'))
.post('/j/login', _.join('&', params))
.reply(200, {
user_info: {
uid: 1,
Expand All @@ -49,9 +48,9 @@ describe('Auth Actions', function actions() {
captchaSolution: 'captcha', captchaId: 'captchaId'
}));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(LOGIN_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(LOGIN_SUCCESS);
done();
}, 50);
}, 20);
});

it('LOGIN_FAILURE', function loginFailure(done) {
Expand All @@ -64,7 +63,7 @@ describe('Auth Actions', function actions() {
'captcha_id=captchaId'
];
nock('http://douban.fm/')
.post('/j/login', _join(params, '&'))
.post('/j/login', _.join('&', params))
.reply(200, { err_msg: 'wrong_password' });

const store = mockStore({ user: { id: 0 } });
Expand All @@ -88,7 +87,7 @@ describe('Auth Actions', function actions() {
const store = mockStore({ user: { id: 0 } });
store.dispatch(verify());
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(VERIFY_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(VERIFY_SUCCESS);
done();
}, 20);
});
Expand Down
4 changes: 2 additions & 2 deletions src/reducers/__tests__/captcha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { expect } from 'chai';
import nock from 'nock';
import { apiMiddleware } from 'redux-api-middleware';
import thunk from 'redux-thunk';
import _ from 'ramda';
import apiMiddlewareHook from '../../middlewares/apiMiddlewareHook';
import camelizeState from '../../middlewares/camelizeState';
import _last from 'lodash/last';
import { FETCH_SUCCESS } from '../../actionTypes/captcha';
import captcha, { fetch } from '../captcha';

Expand All @@ -27,7 +27,7 @@ describe('Captcha Actions', function actions() {
const store = mockStore({ code: '' });
store.dispatch(fetch());
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(FETCH_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(FETCH_SUCCESS);
done();
}, 20);
});
Expand Down
15 changes: 8 additions & 7 deletions src/reducers/__tests__/channel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import nock from 'nock';
import Immutable from 'seamless-immutable';
import { apiMiddleware } from 'redux-api-middleware';
import thunk from 'redux-thunk';
import _ from 'ramda';
import apiMiddlewareHook from '../../middlewares/apiMiddlewareHook';
import camelizeState from '../../middlewares/camelizeState';
import _last from 'lodash/last';
import {
FETCH_SUCCESS, FETCH_FAILURE, LIKE_SUCCESS, DISLIKE_SUCCESS,
BAN_SUCCESS, BAN_FAILURE, PLAY, PAUSE, JUMP, REFUSE,
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Channel Actions', function actions() {
});
store.dispatch(fetch(0, 0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(FETCH_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(FETCH_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -90,7 +90,8 @@ describe('Channel Actions', function actions() {
});
store.dispatch(fetch(0, 0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(REFUSE);
console.log(store.getActions());
expect(_.last(store.getActions()).type).to.equal(REFUSE);
done();
}, 20);
});
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('Channel Actions', function actions() {
});
store.dispatch(like(0, 0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(LIKE_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(LIKE_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -150,7 +151,7 @@ describe('Channel Actions', function actions() {
});
store.dispatch(dislike(0, 0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(DISLIKE_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(DISLIKE_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -182,7 +183,7 @@ describe('Channel Actions', function actions() {
});
store.dispatch(ban(0, 0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(BAN_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(BAN_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -215,7 +216,7 @@ describe('Channel Actions', function actions() {
});
store.dispatch(ban(0, 0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(REFUSE);
expect(_.last(store.getActions()).type).to.equal(REFUSE);
done();
}, 20);
});
Expand Down
15 changes: 7 additions & 8 deletions src/reducers/__tests__/favorite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import nock from 'nock';
import Immutable from 'seamless-immutable';
import { apiMiddleware } from 'redux-api-middleware';
import thunk from 'redux-thunk';
import _ from 'ramda';
import apiMiddlewareHook from '../../middlewares/apiMiddlewareHook';
import camelizeState from '../../middlewares/camelizeState';
import _last from 'lodash/last';
import _join from 'lodash/join';
import {
FETCH_ALL_SUCCESS, LIKE_SUCCESS, DISLIKE_SUCCESS, BAN_SUCCESS, BAN_FAILURE, REFUSE,
PLAY, PAUSE, NEXT, JUMP,
Expand Down Expand Up @@ -46,7 +45,7 @@ describe('Favorite Actions', function actions() {
}]
});
nock('http://douban.fm')
.post('/j/v2/redheart/songs', _join(['bps=192', 'sids=1', 'ck='], '&'))
.post('/j/v2/redheart/songs', _.join('&', ['bps=192', 'sids=1', 'ck=']))
.reply(200, [{
sid: 1,
title: '浮夸',
Expand All @@ -66,7 +65,7 @@ describe('Favorite Actions', function actions() {
});
store.dispatch(fetchAll());
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(FETCH_ALL_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(FETCH_ALL_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -96,7 +95,7 @@ describe('Favorite Actions', function actions() {
});
store.dispatch(like(0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(LIKE_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(LIKE_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -126,7 +125,7 @@ describe('Favorite Actions', function actions() {
});
store.dispatch(dislike(0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(DISLIKE_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(DISLIKE_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -158,7 +157,7 @@ describe('Favorite Actions', function actions() {
});
store.dispatch(ban(0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(BAN_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(BAN_SUCCESS);
done();
}, 20);
});
Expand Down Expand Up @@ -191,7 +190,7 @@ describe('Favorite Actions', function actions() {
});
store.dispatch(ban(0));
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(REFUSE);
expect(_.last(store.getActions()).type).to.equal(REFUSE);
done();
}, 20);
});
Expand Down
4 changes: 2 additions & 2 deletions src/reducers/__tests__/updater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import nock from 'nock';
import Immutable from 'seamless-immutable';
import { apiMiddleware } from 'redux-api-middleware';
import thunk from 'redux-thunk';
import _ from 'ramda';
import apiMiddlewareHook from '../../middlewares/apiMiddlewareHook';
import camelizeState from '../../middlewares/camelizeState';
import _last from 'lodash/last';
import { FETCH_SUCCESS } from '../../actionTypes/updater';
import updater, { check } from '../updater';

Expand All @@ -32,7 +32,7 @@ describe('Updater Actions', function actions() {
});
store.dispatch(check());
setTimeout(() => {
expect(_last(store.getActions()).type).to.equal(FETCH_SUCCESS);
expect(_.last(store.getActions()).type).to.equal(FETCH_SUCCESS);
done();
}, 20);
});
Expand Down
5 changes: 4 additions & 1 deletion src/utils/apiMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ function fetch(actionTypes, channel, lastSongId = 0, type = 's') {
sid: lastSongId,
type,
};
const queryString = _.compose(_.join('&'), _.map(_.join('=')), _.toPairs)(params);
const queryString = _.compose(
_.join('&'), _.map(_.join('=')), _.toPairs,
_.mapObjIndexed((value) => encodeURIComponent(value))
)(params);

return dispatch => {
dispatch({
Expand Down

0 comments on commit 64d6e91

Please sign in to comment.