From 5e851762156f11eef8df1dd1e6c0961ec529d59d Mon Sep 17 00:00:00 2001 From: Jonathan Apodaca Date: Tue, 30 Oct 2018 11:30:33 -0600 Subject: [PATCH] refactor: make lodash a devDependency --- package.json | 2 +- src/macros/OverlayView.jsx | 13 +++++----- src/macros/places/SearchBox.jsx | 9 ++++--- src/utils/MapChildHelper.js | 44 ++++++++++++++++++++------------- src/utils/OverlayViewHelper.js | 3 +-- src/withGoogleMap.jsx | 3 +-- src/withScriptjs.jsx | 3 +-- 7 files changed, 42 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index e3d34ff9..c1477b21 100644 --- a/package.json +++ b/package.json @@ -104,7 +104,6 @@ "can-use-dom": "^0.1.0", "google-maps-infobox": "^2.0.0", "invariant": "^2.2.1", - "lodash": "^4.16.2", "marker-clusterer-plus": "^2.1.4", "markerwithlabel": "^2.0.1", "prop-types": "^15.5.8", @@ -139,6 +138,7 @@ "isomorphic-fetch": "^2.2.1", "jscodeshift": "^0.3.32", "lint-staged": "^6.0.0", + "lodash": "^4.16.2", "make-fetch-happen": "^2.6.0", "mkdirp": "^0.5.1", "prettier": "^1.8.2", diff --git a/src/macros/OverlayView.jsx b/src/macros/OverlayView.jsx index d0e6878a..6a420957 100644 --- a/src/macros/OverlayView.jsx +++ b/src/macros/OverlayView.jsx @@ -1,5 +1,4 @@ /* global google */ -import _ from "lodash" import invariant from "invariant" import React from "react" import ReactDOM from "react-dom" @@ -69,10 +68,10 @@ export class OverlayView extends React.PureComponent { super(props, context) const overlayView = new google.maps.OverlayView() // You must implement three methods: onAdd(), draw(), and onRemove(). - overlayView.onAdd = _.bind(this.onAdd, this) - overlayView.draw = _.bind(this.draw, this) - overlayView.onRemove = _.bind(this.onRemove, this) - this.onPositionElement = _.bind(this.onPositionElement, this) + overlayView.onAdd = this.onAdd.bind(this) + overlayView.draw = this.draw.bind(this) + overlayView.onRemove = this.onRemove.bind(this) + this.onPositionElement = this.onPositionElement.bind(this) // You must call setMap() with a valid Map object to trigger the call to // the onAdd() method and setMap(null) in order to trigger the onRemove() method. overlayView.setMap(this.context[MAP]) @@ -119,7 +118,7 @@ export class OverlayView extends React.PureComponent { offset, this.props ) - _.assign(this.containerElement.style, layoutStyles) + Object.assign(this.containerElement.style, layoutStyles) } onRemove() { @@ -140,7 +139,7 @@ export class OverlayView extends React.PureComponent { updaterMap, prevProps ) - _.delay(this.state[OVERLAY_VIEW].draw) + setImmediate(this.state[OVERLAY_VIEW].draw) } componentWillUnmount() { diff --git a/src/macros/places/SearchBox.jsx b/src/macros/places/SearchBox.jsx index 3f3f0653..dfde72aa 100644 --- a/src/macros/places/SearchBox.jsx +++ b/src/macros/places/SearchBox.jsx @@ -1,5 +1,4 @@ /* global google */ -import _ from "lodash" import invariant from "invariant" import canUseDOM from "can-use-dom" import React from "react" @@ -110,7 +109,7 @@ export class SearchBox extends React.PureComponent { * @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#SearchBox */ const searchBox = new google.maps.places.SearchBox( - this.containerElement.querySelector('input') + this.containerElement.querySelector("input") ) construct(SearchBox.propTypes, updaterMap, this.props, searchBox) this.setState({ @@ -145,7 +144,7 @@ export class SearchBox extends React.PureComponent { const child = this.context[MAP].controls[ this.props.controlPosition ].removeAt(this.mountControlIndex) - if(child !== undefined){ + if (child !== undefined) { this.containerElement.appendChild(child) } } @@ -164,7 +163,9 @@ export class SearchBox extends React.PureComponent { export default SearchBox -const isValidControlPosition = _.isNumber +const isValidControlPosition = function(value) { + return typeof value === "number" +} const eventMap = {} diff --git a/src/utils/MapChildHelper.js b/src/utils/MapChildHelper.js index 8481fa17..923493ac 100644 --- a/src/utils/MapChildHelper.js +++ b/src/utils/MapChildHelper.js @@ -1,13 +1,21 @@ /* global google */ /* eslint-disable no-param-reassign */ -import _ from "lodash" + +const lowerFirst = s => s.substring(0, 1).toLowerCase() + s.substring(1) +const reduceMap = (map, reducer, initial) => { + let result = initial + for (const key of Object.keys(map)) { + result = reducer(result, map[key], key) + } + return result +} function rdcUncontrolledAndControlledProps(acc, value, key) { - if (_.has(acc.prevProps, key)) { + if (typeof acc.prevProps[key] !== "undefined") { const match = key.match(/^default(\S+)/) if (match) { - const unprefixedKey = _.lowerFirst(match[1]) - if (!_.has(acc.nextProps, unprefixedKey)) { + const unprefixedKey = lowerFirst(match[1]) + if (typeof acc.nextProps[unprefixedKey] === "undefined") { acc.nextProps[unprefixedKey] = acc.prevProps[key] } } else { @@ -18,7 +26,8 @@ function rdcUncontrolledAndControlledProps(acc, value, key) { } function applyUpdaterToNextProps(updaterMap, prevProps, nextProps, instance) { - _.forEach(updaterMap, (fn, key) => { + Object.keys(updaterMap).forEach(key => { + const fn = updaterMap[key] const nextValue = nextProps[key] if (nextValue !== prevProps[key]) { fn(instance, nextValue) @@ -27,10 +36,14 @@ function applyUpdaterToNextProps(updaterMap, prevProps, nextProps, instance) { } export function construct(propTypes, updaterMap, prevProps, instance) { - const { nextProps } = _.reduce(propTypes, rdcUncontrolledAndControlledProps, { - nextProps: {}, - prevProps, - }) + const { nextProps } = reduceMap( + propTypes, + rdcUncontrolledAndControlledProps, + { + nextProps: {}, + prevProps, + } + ) applyUpdaterToNextProps( updaterMap, { @@ -62,10 +75,10 @@ export function componentWillUnmount(component) { } function registerEvents(component, instance, eventMap) { - const registeredList = _.reduce( + const registeredList = reduceMap( eventMap, (acc, googleEventName, onEventName) => { - if (_.isFunction(component.props[onEventName])) { + if (typeof component.props[onEventName] === "function") { acc.push( google.maps.event.addListener( instance, @@ -79,12 +92,9 @@ function registerEvents(component, instance, eventMap) { [] ) - component.unregisterAllEvents = _.bind( - _.forEach, - null, - registeredList, - unregisterEvent - ) + component.unregisterAllEvents = () => { + registeredList.forEach(registered => unregisterEvent(registered)) + } } function unregisterEvent(registered) { diff --git a/src/utils/OverlayViewHelper.js b/src/utils/OverlayViewHelper.js index e6788a9c..27eb2760 100644 --- a/src/utils/OverlayViewHelper.js +++ b/src/utils/OverlayViewHelper.js @@ -1,5 +1,4 @@ /* global google */ -import _ from "lodash" export function getOffsetOverride(containerElement, props) { const { getPixelPositionOffset } = props @@ -7,7 +6,7 @@ export function getOffsetOverride(containerElement, props) { // Allows the component to control the visual position of the OverlayView // relative to the LatLng pixel position. // - if (_.isFunction(getPixelPositionOffset)) { + if (typeof getPixelPositionOffset === "function") { return getPixelPositionOffset( containerElement.offsetWidth, containerElement.offsetHeight diff --git a/src/withGoogleMap.jsx b/src/withGoogleMap.jsx index a4bf3c41..2c7562eb 100644 --- a/src/withGoogleMap.jsx +++ b/src/withGoogleMap.jsx @@ -1,5 +1,4 @@ /* global google */ -import _ from "lodash" import warning from "warning" import invariant from "invariant" import { getDisplayName } from "recompose" @@ -26,7 +25,7 @@ export function withGoogleMap(BaseComponent) { map: null, } - handleComponentMount = _.bind(this.handleComponentMount, this) + handleComponentMount = this.handleComponentMount.bind(this) getChildContext() { return { diff --git a/src/withScriptjs.jsx b/src/withScriptjs.jsx index 66f25cba..b4d7291c 100644 --- a/src/withScriptjs.jsx +++ b/src/withScriptjs.jsx @@ -1,4 +1,3 @@ -import _ from "lodash" import invariant from "invariant" import canUseDOM from "can-use-dom" import { getDisplayName } from "recompose" @@ -26,7 +25,7 @@ export function withScriptjs(BaseComponent) { isUnmounted = false - handleLoaded = _.bind(this.handleLoaded, this) + handleLoaded = this.handleLoaded.bind(this) handleLoaded() { if (this.isUnmounted) {