Skip to content

Commit

Permalink
Merge pull request #86 from adiun/adiun/lodash2
Browse files Browse the repository at this point in the history
Replacing whole-lib import of lodash with single method imports
  • Loading branch information
nomcopter authored Oct 30, 2018
2 parents b1ccb37 + 25d29c2 commit 180d007
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 270 deletions.
4 changes: 2 additions & 2 deletions demo/ExampleApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Classes, HTMLSelect } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import classNames from 'classnames';
import _ from 'lodash';
import dropRight from 'lodash/dropRight';
import React from 'react';

import {
Expand Down Expand Up @@ -127,7 +127,7 @@ export class ExampleApp extends React.PureComponent<{}, ExampleAppState> {
let { currentNode } = this.state;
if (currentNode) {
const path = getPathToCorner(currentNode, Corner.TOP_RIGHT);
const parent = getNodeAtPath(currentNode, _.dropRight(path)) as MosaicParent<number>;
const parent = getNodeAtPath(currentNode, dropRight(path)) as MosaicParent<number>;
const destination = getNodeAtPath(currentNode, path) as MosaicNode<number>;
const direction: MosaicDirection = parent ? getOtherDirection(parent.direction) : 'row';

Expand Down
10 changes: 4 additions & 6 deletions src/Mosaic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/
import classNames from 'classnames';
import _ from 'lodash';
import countBy from 'lodash/countBy';
import keys from 'lodash/keys';
import pickBy from 'lodash/pickBy';
import React from 'react';
import { DragDropContext } from 'react-dnd';
import HTML5 from 'react-dnd-html5-backend';
Expand Down Expand Up @@ -197,11 +199,7 @@ export class MosaicWithoutDragDropContext<T extends MosaicKey = string> extends

private validateTree(node: MosaicNode<T> | null) {
if (process.env.NODE_ENV !== 'production') {
const duplicates = _.chain(getLeaves(node))
.countBy()
.pickBy((n) => n > 1)
.keys()
.value();
const duplicates = keys(pickBy(countBy(getLeaves(node)), (n) => n > 1));

if (duplicates.length > 0) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions src/MosaicRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import _ from 'lodash';
import flatten from 'lodash/flatten';
import React from 'react';
import { MosaicContext } from './contextTypes';
import { Split } from './Split';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class MosaicRoot<T extends MosaicKey> extends React.PureComponent<MosaicR
if (isParent(node)) {
const splitPercentage = node.splitPercentage == null ? 50 : node.splitPercentage;
const { first, second } = BoundingBox.split(boundingBox, splitPercentage, node.direction);
return _.flatten(
return flatten(
[
this.renderRecursively(node.first, first, path.concat('first')),
this.renderSplit(node.direction, boundingBox, splitPercentage, path),
Expand Down
21 changes: 13 additions & 8 deletions src/MosaicWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import { Classes, Icon } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import classNames from 'classnames';
import _ from 'lodash';
import defer from 'lodash/defer';
import dropRight from 'lodash/dropRight';
import isEmpty from 'lodash/isEmpty';
import isEqual from 'lodash/isEqual';
import omit from 'lodash/omit';
import values from 'lodash/values';
import React from 'react';
import {
ConnectDragPreview,
Expand Down Expand Up @@ -145,16 +150,16 @@ export class InternalMosaicWindow<T extends MosaicKey> extends React.Component<
<div className="mosaic-window-additional-actions-bar">{additionalControls}</div>
{connectDragPreview(renderPreview!(this.props))}
<div className="drop-target-container">
{_.values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map(this.renderDropTarget)}
{values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map(this.renderDropTarget)}
</div>
</div>,
);
}

shouldComponentUpdate(nextProps: InternalMosaicWindowProps<T>, nextState: InternalMosaicWindowState): boolean {
return (
!_.isEqual(_.omit(this.props, PURE_RENDER_IGNORE), _.omit(nextProps, PURE_RENDER_IGNORE)) ||
!_.isEqual(this.state, nextState)
!isEqual(omit(this.props, PURE_RENDER_IGNORE), omit(nextProps, PURE_RENDER_IGNORE)) ||
!isEqual(this.state, nextState)
);
}

Expand Down Expand Up @@ -185,7 +190,7 @@ export class InternalMosaicWindow<T extends MosaicKey> extends React.Component<
titleDiv = connectDragSource(titleDiv) as React.ReactElement<any>;
}

const hasAdditionalControls = !_.isEmpty(additionalControls);
const hasAdditionalControls = !isEmpty(additionalControls);

return (
<div className={classNames('mosaic-window-toolbar', { draggable: draggableAndNotRoot })}>
Expand Down Expand Up @@ -260,7 +265,7 @@ const dragSource = {
): MosaicDragItem => {
// TODO: Actually just delete instead of hiding
// The defer is necessary as the element must be present on start for HTML DnD to not cry
const hideTimer = _.defer(() => component.context.mosaicActions.hide(component.props.path));
const hideTimer = defer(() => component.context.mosaicActions.hide(component.props.path));
return {
mosaicId: component.context.mosaicId,
hideTimer,
Expand All @@ -279,13 +284,13 @@ const dragSource = {
const dropResult: MosaicDropData = (monitor.getDropResult() || {}) as MosaicDropData;
const { mosaicActions } = component.context;
const { position, path: destinationPath } = dropResult;
if (position != null && destinationPath != null && !_.isEqual(destinationPath, ownPath)) {
if (position != null && destinationPath != null && !isEqual(destinationPath, ownPath)) {
mosaicActions.updateTree(createDragToUpdates(mosaicActions.getRoot(), ownPath, destinationPath, position));
} else {
// TODO: restore node from captured state
mosaicActions.updateTree([
{
path: _.dropRight(ownPath),
path: dropRight(ownPath),
spec: {
splitPercentage: {
$set: null,
Expand Down
4 changes: 2 additions & 2 deletions src/MosaicZeroState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Classes, Icon } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import classNames from 'classnames';
import _ from 'lodash';
import noop from 'lodash/noop';
import React from 'react';

import { MosaicActionsPropType, MosaicContext } from './contextTypes';
Expand Down Expand Up @@ -55,7 +55,7 @@ export class MosaicZeroState<T extends MosaicKey> extends React.PureComponent<Mo
private replace = () =>
Promise.resolve(this.props.createNode!())
.then((node) => this.context.mosaicActions.replaceWith([], node))
.catch(_.noop); // Swallow rejections (i.e. on user cancel)
.catch(noop); // Swallow rejections (i.e. on user cancel)
}

// Factory that works with generics
Expand Down
4 changes: 2 additions & 2 deletions src/RootDropTargets.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import _ from 'lodash';
import values from 'lodash/values';
import React from 'react';
import { DropTarget } from 'react-dnd';

Expand All @@ -19,7 +19,7 @@ class RootDropTargetsClass extends React.PureComponent<RootDropTargetsProps> {
'-dragging': this.props.isDragging,
})}
>
{_.values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map((position) => (
{values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map((position) => (
<MosaicDropTarget position={position} path={[]} key={position} />
))}
</div>
Expand Down
7 changes: 4 additions & 3 deletions src/Split.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* limitations under the License.
*/
import classNames from 'classnames';
import _ from 'lodash';
import clamp from 'lodash/clamp';
import throttle from 'lodash/throttle';
import React from 'react';

import { EnabledResizeOptions, MosaicDirection } from './types';
Expand Down Expand Up @@ -92,7 +93,7 @@ export class Split extends React.PureComponent<SplitProps> {
}
};

private onMouseMove = _.throttle((event: MouseEvent) => {
private onMouseMove = throttle((event: MouseEvent) => {
event.preventDefault();
event.stopPropagation();

Expand All @@ -115,6 +116,6 @@ export class Split extends React.PureComponent<SplitProps> {

const relativePercentage = BoundingBox.getRelativeSplitPercentage(boundingBox, absolutePercentage, direction);

return _.clamp(relativePercentage, minimumPaneSizePercentage!, 100 - minimumPaneSizePercentage!);
return clamp(relativePercentage, minimumPaneSizePercentage!, 100 - minimumPaneSizePercentage!);
}
}
4 changes: 2 additions & 2 deletions src/buttons/ReplaceButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { Classes } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import _ from 'lodash';
import noop from 'lodash/noop';
import React from 'react';

import { MosaicWindowContext } from '../contextTypes';
Expand All @@ -39,7 +39,7 @@ export class ReplaceButton<T extends MosaicKey> extends React.PureComponent<Mosa
this.props.onClick();
}
})
.catch(_.noop); // Swallow rejections (i.e. on user cancel)
.catch(noop); // Swallow rejections (i.e. on user cancel)
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/buttons/SplitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { Classes } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import _ from 'lodash';
import noop from 'lodash/noop';
import React from 'react';

import { MosaicWindowContext } from '../contextTypes';
Expand All @@ -39,7 +39,7 @@ export class SplitButton<T extends MosaicKey> extends React.PureComponent<Mosaic
this.props.onClick();
}
})
.catch(_.noop); // Swallow rejections (i.e. on user cancel)
.catch(noop); // Swallow rejections (i.e. on user cancel)
};
}

Expand Down
21 changes: 13 additions & 8 deletions src/util/mosaicUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
* limitations under the License.
*/
import update from 'immutability-helper';
import _ from 'lodash';
import drop from 'lodash/drop';
import dropRight from 'lodash/dropRight';
import isEqual from 'lodash/isEqual';
import last from 'lodash/last';
import set from 'lodash/set';
import take from 'lodash/take';
import { MosaicDropTargetPosition } from '../internalTypes';
import {
MosaicBranch,
Expand All @@ -39,7 +44,7 @@ export { MosaicParent };
*/
export function buildSpecFromUpdate<T extends MosaicKey>(mosaicUpdate: MosaicUpdate<T>): MosaicUpdateSpec<T> {
if (mosaicUpdate.path.length > 0) {
return _.set({}, mosaicUpdate.path, mosaicUpdate.spec);
return set({}, mosaicUpdate.path, mosaicUpdate.spec);
} else {
return mosaicUpdate.spec;
}
Expand Down Expand Up @@ -67,8 +72,8 @@ export function updateTree<T extends MosaicKey>(root: MosaicNode<T>, updates: Mo
* @returns {{path: T[], spec: {$set: MosaicNode<T>}}}
*/
export function createRemoveUpdate<T extends MosaicKey>(root: MosaicNode<T> | null, path: MosaicPath): MosaicUpdate<T> {
const parentPath = _.dropRight(path);
const nodeToRemove = _.last(path);
const parentPath = dropRight(path);
const nodeToRemove = last(path);
const siblingPath = parentPath.concat(getOtherBranch(nodeToRemove!));
const sibling = getAndAssertNodeAtPathExists(root, siblingPath);

Expand All @@ -81,7 +86,7 @@ export function createRemoveUpdate<T extends MosaicKey>(root: MosaicNode<T> | nu
}

function isPathPrefixEqual(a: MosaicPath, b: MosaicPath, length: number) {
return _.isEqual(_.take(a, length), _.take(b, length));
return isEqual(take(a, length), take(b, length));
}

/**
Expand All @@ -106,7 +111,7 @@ export function createDragToUpdates<T extends MosaicKey>(
if (destinationIsParentOfSource) {
// Must explicitly remove source from the destination node
destinationNode = updateTree(destinationNode, [
createRemoveUpdate(destinationNode, _.drop(sourcePath, destinationPath.length)),
createRemoveUpdate(destinationNode, drop(sourcePath, destinationPath.length)),
]);
} else {
// Can remove source normally
Expand Down Expand Up @@ -151,8 +156,8 @@ export function createDragToUpdates<T extends MosaicKey>(
* @returns {{path: T[], spec: {splitPercentage: {$set: number}}}}
*/
export function createHideUpdate<T extends MosaicKey>(path: MosaicPath): MosaicUpdate<T> {
const targetPath = _.dropRight(path);
const thisBranch = _.last(path);
const targetPath = dropRight(path);
const thisBranch = last(path);

let splitPercentage: number;
if (thisBranch === 'first') {
Expand Down
7 changes: 4 additions & 3 deletions src/util/mosaicUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import _ from 'lodash';
import clone from 'lodash/clone';
import get from 'lodash/get';
import { MosaicBranch, MosaicDirection, MosaicKey, MosaicNode, MosaicParent, MosaicPath } from '../types';

function alternateDirection<T extends MosaicKey>(
Expand Down Expand Up @@ -63,7 +64,7 @@ export function createBalancedTreeFromLeaves<T extends MosaicKey>(
return null;
}

let current: MosaicNode<T>[] = _.clone(leaves);
let current: MosaicNode<T>[] = clone(leaves);
let next: MosaicNode<T>[] = [];

while (current.length > 1) {
Expand Down Expand Up @@ -160,7 +161,7 @@ export function getLeaves<T extends MosaicKey>(tree: MosaicNode<T> | null): T[]
*/
export function getNodeAtPath<T extends MosaicKey>(tree: MosaicNode<T> | null, path: MosaicPath): MosaicNode<T> | null {
if (path.length > 0) {
return _.get(tree, path, null);
return get(tree, path, null);
} else {
return tree;
}
Expand Down
12 changes: 7 additions & 5 deletions test/utilitiesSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/
import { expect } from 'chai';
import _ from 'lodash';
import max from 'lodash/max';
import min from 'lodash/min';
import range from 'lodash/range';

import { getNodeAtPath, MosaicNode } from '../src/index';
import {
Expand Down Expand Up @@ -48,8 +50,8 @@ const FALSY_TREE: MosaicNode<number | string> = {
second: '',
};

const NINE_LEAVES = _.range(1, 10);
const THOUSAND_AND_ONE_LEAVES = _.range(1, 1002);
const NINE_LEAVES = range(1, 10);
const THOUSAND_AND_ONE_LEAVES = range(1, 1002);

const NUMERICAL_SORT = (a: number, b: number) => a - b;

Expand All @@ -58,8 +60,8 @@ function getTreeDepths(tree: MosaicNode<any>): { min: number; max: number } {
const first = getTreeDepths(tree.first);
const second = getTreeDepths(tree.second);
return {
min: _.min([first.min, second.min])! + 1,
max: _.max([first.max, second.max])! + 1,
min: min([first.min, second.min])! + 1,
max: max([first.max, second.max])! + 1,
};
} else {
return {
Expand Down
Loading

0 comments on commit 180d007

Please sign in to comment.