Skip to content

Commit

Permalink
fix(*): replace lodash functions with own ones in order to avoid tree…
Browse files Browse the repository at this point in the history
…-shaking issues (closes #108)
  • Loading branch information
rychkog committed Jul 1, 2017
1 parent e68892c commit e6eb712
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 144 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"post:publish": "npm run build:demo && gh-pages -d dist-demo",
"start": "ng serve",
"build:demo": "ng build",
"test": "ng test --single-run",
"test:w": "ng test --watch",
"test:cov": "ng test --single-run --code-coverage",
"test": "ng test -sr",
"test:w": "ng test -w",
"test:cov": "ng test -sr -cc",
"lint": "ng lint --fix --type-check",
"e2e": "ng e2e",
"changelog": "conventional-changelog -i CHANGELOG.md -s -p angular",
Expand All @@ -66,7 +66,6 @@
"@angular/platform-browser-dynamic": "4.1.3",
"@angular/router": "4.1.3",
"@types/jasmine": "2.5.51",
"@types/lodash-es": "4.14.5",
"@types/node": "7.0.29",
"alertifyjs": "1.10.0",
"codelyzer": "3.0.1",
Expand All @@ -86,8 +85,6 @@
"karma-jasmine": "1.1.0",
"karma-jasmine-html-reporter": "0.2.2",
"karma-phantomjs-launcher": "1.0.4",
"lodash": "4.17.4",
"lodash-es": "4.17.4",
"phantomjs-polyfill": "0.0.2",
"phantomjs-prebuilt": "2.1.14",
"pre-commit": "1.2.2",
Expand Down
4 changes: 2 additions & 2 deletions publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ shell.exec('npm run post:publish');

function omit(obj, key) {
return Object
.keys(pkg)
.keys(obj)
.reduce((result, prop) => {
if (prop === key) return result;
return Object.assign(result, {[prop]: pkg[prop]})
return Object.assign(result, {[prop]: obj[prop]})
}, {});
}
98 changes: 48 additions & 50 deletions src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import _map from 'lodash-es/map';
import _isEmpty from 'lodash-es/isEmpty';
import _trim from 'lodash-es/trim';
import _has from 'lodash-es/has';
import _isFunction from 'lodash-es/isFunction';
import _clone from 'lodash-es/clone';
import _merge from 'lodash-es/merge';
import _extend from 'lodash-es/extend';
import _get from 'lodash-es/get';
import _omit from 'lodash-es/omit';
import _forEach from 'lodash-es/forEach';
import _toString from 'lodash-es/toString';
import _isArray from 'lodash-es/isArray';
import _size from 'lodash-es/size';
import _indexOf from 'lodash-es/indexOf';
import _includes from 'lodash-es/includes';
import _findIndex from 'lodash-es/findIndex';
import _once from 'lodash-es/once';
import {
isEmpty,
trim,
has,
isFunction,
get,
omit,
size,
once,
includes
} from './utils/fn.utils';

import { Observable, Observer } from 'rxjs/Rx';
import { TreeModel, RenamableNode, FoldingType, TreeStatus, TreeModelSettings, ChildrenLoadingFunction } from './tree.types';

Expand All @@ -30,12 +24,12 @@ export class Tree {
private _loadChildren: ChildrenLoadingFunction;
private _childrenLoadingState: ChildrenLoadingState = ChildrenLoadingState.NotStarted;

private _childrenAsyncOnce: () => Observable<Tree[]> = _once(() => {
private _childrenAsyncOnce: () => Observable<Tree[]> = once(() => {
return new Observable((observer: Observer<Tree[]>) => {
setTimeout(() => {
this._childrenLoadingState = ChildrenLoadingState.Loading;
this._loadChildren((children: TreeModel[]) => {
this._children = _map(children, (child: TreeModel) => new Tree(child, this));
this._children = (children || []).map((child: TreeModel) => new Tree(child, this));
this._childrenLoadingState = ChildrenLoadingState.Completed;
observer.next(this.children);
observer.complete();
Expand All @@ -56,7 +50,7 @@ export class Tree {
* @static
*/
public static isValueEmpty(value: string): boolean {
return _isEmpty(_trim(value));
return isEmpty(trim(value));
}

/**
Expand All @@ -66,18 +60,18 @@ export class Tree {
* @static
*/
public static isRenamable(value: any): value is RenamableNode {
return (_has(value, 'setName') && _isFunction(value.setName))
&& (_has(value, 'toString') && _isFunction(value.toString) && value.toString !== Object.toString);
return (has(value, 'setName') && isFunction(value.setName))
&& (has(value, 'toString') && isFunction(value.toString) && value.toString !== Object.toString);
}

private static cloneTreeShallow(origin: Tree): Tree {
const tree = new Tree(_clone(origin.node));
const tree = new Tree(Object.assign({}, origin.node));
tree._children = origin._children;
return tree;
}

private static applyNewValueToRenamable(value: RenamableNode, newValue: string): RenamableNode {
const renamableValue: RenamableNode = _merge({}, value as RenamableNode);
const renamableValue: RenamableNode = Object.assign({}, value as RenamableNode);
renamableValue.setName(newValue);
return renamableValue;
}
Expand All @@ -94,14 +88,14 @@ export class Tree {

private buildTreeFromModel(model: TreeModel, parent: Tree, isBranch: boolean): void {
this.parent = parent;
this.node = _extend(_omit(model, 'children') as TreeModel, {
settings: TreeModelSettings.merge(model, _get(parent, 'node') as TreeModel)
this.node = Object.assign(omit(model, 'children') as TreeModel, {
settings: TreeModelSettings.merge(model, get(parent, 'node') as TreeModel)
}) as TreeModel;

if (_isFunction(this.node.loadChildren)) {
if (isFunction(this.node.loadChildren)) {
this._loadChildren = this.node.loadChildren;
} else {
_forEach(_get(model, 'children') as TreeModel[], (child: TreeModel, index: number) => {
get(model, 'children', []).forEach((child: TreeModel, index: number) => {
this._addChild(new Tree(child, this), index);
});
}
Expand Down Expand Up @@ -200,11 +194,11 @@ export class Tree {
return;
}

const stringifiedValue = '' + value;
if (Tree.isRenamable(this.value)) {
const newValue = typeof value === 'string' ? value : _toString(value);
this.node.value = Tree.applyNewValueToRenamable(this.value as RenamableNode, newValue);
this.node.value = Tree.applyNewValueToRenamable(this.value as RenamableNode, stringifiedValue);
} else {
this.node.value = Tree.isValueEmpty(value as string) ? this.node.value : _toString(value);
this.node.value = Tree.isValueEmpty(stringifiedValue) ? this.node.value : stringifiedValue;
}
}

Expand All @@ -215,7 +209,7 @@ export class Tree {
* @returns {Tree} A newly inserted sibling, or null if you are trying to make a sibling for the root.
*/
public addSibling(sibling: Tree, position?: number): Tree {
if (_isArray(_get(this.parent, 'children'))) {
if (Array.isArray(get(this.parent, 'children'))) {
return this.parent.addChild(sibling, position);
}
return null;
Expand All @@ -231,7 +225,7 @@ export class Tree {
return this._addChild(Tree.cloneTreeShallow(child), position);
}

private _addChild(child: Tree, position: number = _size(this._children) || 0): Tree {
private _addChild(child: Tree, position: number = size(this._children) || 0): Tree {
child.parent = this;

if (Array.isArray(this._children)) {
Expand Down Expand Up @@ -268,31 +262,31 @@ export class Tree {
* @returns {number} The position inside a parent.
*/
public get positionInParent(): number {
return _indexOf(this.parent.children, this);
return this.parent.children ? this.parent.children.indexOf(this) : -1;
}

/**
* Check whether or not this tree is static.
* @returns {boolean} A flag indicating whether or not this tree is static.
*/
public isStatic(): boolean {
return _get(this.node.settings, 'static', false);
return get(this.node.settings, 'static', false);
}

/**
* Check whether or not this tree has a left menu.
* @returns {boolean} A flag indicating whether or not this tree has a left menu.
*/
public hasLeftMenu(): boolean {
return !_get(this.node.settings, 'static', false) && _get(this.node.settings, 'leftMenu', false);
return !get(this.node.settings, 'static', false) && get(this.node.settings, 'leftMenu', false);
}

/**
* Check whether or not this tree has a right menu.
* @returns {boolean} A flag indicating whether or not this tree has a right menu.
*/
public hasRightMenu(): boolean {
return !_get(this.node.settings, 'static', false) && _get(this.node.settings, 'rightMenu', false);
return !get(this.node.settings, 'static', false) && get(this.node.settings, 'rightMenu', false);
}

/**
Expand All @@ -316,7 +310,7 @@ export class Tree {
* @returns {boolean} A flag indicating whether or not this tree has children.
*/
public hasChildren(): boolean {
return !_isEmpty(this._children) || this.childrenShouldBeLoaded();
return !isEmpty(this._children) || this.childrenShouldBeLoaded();
}

/**
Expand All @@ -333,7 +327,7 @@ export class Tree {
* @returns {boolean} A flag indicating whether or not provided tree is the sibling of the current one.
*/
public hasSibling(tree: Tree): boolean {
return !this.isRoot() && _includes(this.parent.children, tree);
return !this.isRoot() && includes(this.parent.children, tree);
}

/**
Expand All @@ -343,7 +337,7 @@ export class Tree {
* @returns {boolean} A flag indicating whether provided tree is a child or not.
*/
public hasChild(tree: Tree): boolean {
return _includes(this._children, tree);
return includes(this._children, tree);
}

/**
Expand All @@ -352,7 +346,11 @@ export class Tree {
* @param {Tree} tree - A tree that should be removed.
*/
public removeChild(tree: Tree): void {
const childIndex = _findIndex(this._children, (child: Tree) => child === tree);
if (!this.hasChildren()) {
return;
}

const childIndex = this._children.findIndex((child: Tree) => child === tree);
if (childIndex >= 0) {
this._children.splice(childIndex, 1);
}
Expand Down Expand Up @@ -404,7 +402,7 @@ export class Tree {
private _setFoldingType(): void {
if (this.childrenShouldBeLoaded()) {
this.node._foldingType = FoldingType.Collapsed;
} else if (this._children && !_isEmpty(this._children)) {
} else if (this._children && !isEmpty(this._children)) {
this.node._foldingType = FoldingType.Expanded;
} else if (Array.isArray(this._children)) {
this.node._foldingType = FoldingType.Empty;
Expand Down Expand Up @@ -438,14 +436,14 @@ export class Tree {
}

if (this.node._foldingType === FoldingType.Collapsed) {
return _get(this.node.settings, 'cssClasses.collapsed', null);
return get(this.node.settings, 'cssClasses.collapsed', null);
} else if (this.node._foldingType === FoldingType.Expanded) {
return _get(this.node.settings, 'cssClasses.expanded', null);
return get(this.node.settings, 'cssClasses.expanded', null);
} else if (this.node._foldingType === FoldingType.Empty) {
return _get(this.node.settings, 'cssClasses.empty', null);
return get(this.node.settings, 'cssClasses.empty', null);
}

return _get(this.node.settings, 'cssClasses.leaf', null);
return get(this.node.settings, 'cssClasses.leaf', null);
}

/**
Expand All @@ -458,9 +456,9 @@ export class Tree {

private getTemplateFromSettings(): string {
if (this.isLeaf()) {
return _get(this.node.settings, 'templates.leaf', '');
return get(this.node.settings, 'templates.leaf', '');
} else {
return _get(this.node.settings, 'templates.node', '');
return get(this.node.settings, 'templates.node', '');
}
}

Expand All @@ -470,7 +468,7 @@ export class Tree {
*/
public get leftMenuTemplate(): string {
if (this.hasLeftMenu()) {
return _get(this.node.settings, 'templates.leftMenu', '<span></span>');
return get(this.node.settings, 'templates.leftMenu', '<span></span>');
}
return '';
}
Expand Down
5 changes: 2 additions & 3 deletions src/tree.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _defaultsDeep from 'lodash-es/defaultsDeep';
import _get from 'lodash-es/get';
import { get, defaultsDeep } from './utils/fn.utils';

export class FoldingType {
public static Expanded: FoldingType = new FoldingType('node-expanded');
Expand Down Expand Up @@ -84,7 +83,7 @@ export class TreeModelSettings {
public static?: boolean;

public static merge(sourceA: TreeModel, sourceB: TreeModel): TreeModelSettings {
return _defaultsDeep({}, _get(sourceA, 'settings'), _get(sourceB, 'settings'), {static: false, leftMenu: false, rightMenu: true});
return defaultsDeep({}, get(sourceA, 'settings'), get(sourceB, 'settings'), {static: false, leftMenu: false, rightMenu: true});
}
}

Expand Down
Loading

0 comments on commit e6eb712

Please sign in to comment.