Skip to content

Commit e6eb712

Browse files
committed
fix(*): replace lodash functions with own ones in order to avoid tree-shaking issues (closes #108)
1 parent e68892c commit e6eb712

8 files changed

Lines changed: 274 additions & 144 deletions

File tree

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
"post:publish": "npm run build:demo && gh-pages -d dist-demo",
3838
"start": "ng serve",
3939
"build:demo": "ng build",
40-
"test": "ng test --single-run",
41-
"test:w": "ng test --watch",
42-
"test:cov": "ng test --single-run --code-coverage",
40+
"test": "ng test -sr",
41+
"test:w": "ng test -w",
42+
"test:cov": "ng test -sr -cc",
4343
"lint": "ng lint --fix --type-check",
4444
"e2e": "ng e2e",
4545
"changelog": "conventional-changelog -i CHANGELOG.md -s -p angular",
@@ -66,7 +66,6 @@
6666
"@angular/platform-browser-dynamic": "4.1.3",
6767
"@angular/router": "4.1.3",
6868
"@types/jasmine": "2.5.51",
69-
"@types/lodash-es": "4.14.5",
7069
"@types/node": "7.0.29",
7170
"alertifyjs": "1.10.0",
7271
"codelyzer": "3.0.1",
@@ -86,8 +85,6 @@
8685
"karma-jasmine": "1.1.0",
8786
"karma-jasmine-html-reporter": "0.2.2",
8887
"karma-phantomjs-launcher": "1.0.4",
89-
"lodash": "4.17.4",
90-
"lodash-es": "4.17.4",
9188
"phantomjs-polyfill": "0.0.2",
9289
"phantomjs-prebuilt": "2.1.14",
9390
"pre-commit": "1.2.2",

publish.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ shell.exec('npm run post:publish');
1313

1414
function omit(obj, key) {
1515
return Object
16-
.keys(pkg)
16+
.keys(obj)
1717
.reduce((result, prop) => {
1818
if (prop === key) return result;
19-
return Object.assign(result, {[prop]: pkg[prop]})
19+
return Object.assign(result, {[prop]: obj[prop]})
2020
}, {});
2121
}

src/tree.ts

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
import _map from 'lodash-es/map';
2-
import _isEmpty from 'lodash-es/isEmpty';
3-
import _trim from 'lodash-es/trim';
4-
import _has from 'lodash-es/has';
5-
import _isFunction from 'lodash-es/isFunction';
6-
import _clone from 'lodash-es/clone';
7-
import _merge from 'lodash-es/merge';
8-
import _extend from 'lodash-es/extend';
9-
import _get from 'lodash-es/get';
10-
import _omit from 'lodash-es/omit';
11-
import _forEach from 'lodash-es/forEach';
12-
import _toString from 'lodash-es/toString';
13-
import _isArray from 'lodash-es/isArray';
14-
import _size from 'lodash-es/size';
15-
import _indexOf from 'lodash-es/indexOf';
16-
import _includes from 'lodash-es/includes';
17-
import _findIndex from 'lodash-es/findIndex';
18-
import _once from 'lodash-es/once';
1+
import {
2+
isEmpty,
3+
trim,
4+
has,
5+
isFunction,
6+
get,
7+
omit,
8+
size,
9+
once,
10+
includes
11+
} from './utils/fn.utils';
12+
1913
import { Observable, Observer } from 'rxjs/Rx';
2014
import { TreeModel, RenamableNode, FoldingType, TreeStatus, TreeModelSettings, ChildrenLoadingFunction } from './tree.types';
2115

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

33-
private _childrenAsyncOnce: () => Observable<Tree[]> = _once(() => {
27+
private _childrenAsyncOnce: () => Observable<Tree[]> = once(() => {
3428
return new Observable((observer: Observer<Tree[]>) => {
3529
setTimeout(() => {
3630
this._childrenLoadingState = ChildrenLoadingState.Loading;
3731
this._loadChildren((children: TreeModel[]) => {
38-
this._children = _map(children, (child: TreeModel) => new Tree(child, this));
32+
this._children = (children || []).map((child: TreeModel) => new Tree(child, this));
3933
this._childrenLoadingState = ChildrenLoadingState.Completed;
4034
observer.next(this.children);
4135
observer.complete();
@@ -56,7 +50,7 @@ export class Tree {
5650
* @static
5751
*/
5852
public static isValueEmpty(value: string): boolean {
59-
return _isEmpty(_trim(value));
53+
return isEmpty(trim(value));
6054
}
6155

6256
/**
@@ -66,18 +60,18 @@ export class Tree {
6660
* @static
6761
*/
6862
public static isRenamable(value: any): value is RenamableNode {
69-
return (_has(value, 'setName') && _isFunction(value.setName))
70-
&& (_has(value, 'toString') && _isFunction(value.toString) && value.toString !== Object.toString);
63+
return (has(value, 'setName') && isFunction(value.setName))
64+
&& (has(value, 'toString') && isFunction(value.toString) && value.toString !== Object.toString);
7165
}
7266

7367
private static cloneTreeShallow(origin: Tree): Tree {
74-
const tree = new Tree(_clone(origin.node));
68+
const tree = new Tree(Object.assign({}, origin.node));
7569
tree._children = origin._children;
7670
return tree;
7771
}
7872

7973
private static applyNewValueToRenamable(value: RenamableNode, newValue: string): RenamableNode {
80-
const renamableValue: RenamableNode = _merge({}, value as RenamableNode);
74+
const renamableValue: RenamableNode = Object.assign({}, value as RenamableNode);
8175
renamableValue.setName(newValue);
8276
return renamableValue;
8377
}
@@ -94,14 +88,14 @@ export class Tree {
9488

9589
private buildTreeFromModel(model: TreeModel, parent: Tree, isBranch: boolean): void {
9690
this.parent = parent;
97-
this.node = _extend(_omit(model, 'children') as TreeModel, {
98-
settings: TreeModelSettings.merge(model, _get(parent, 'node') as TreeModel)
91+
this.node = Object.assign(omit(model, 'children') as TreeModel, {
92+
settings: TreeModelSettings.merge(model, get(parent, 'node') as TreeModel)
9993
}) as TreeModel;
10094

101-
if (_isFunction(this.node.loadChildren)) {
95+
if (isFunction(this.node.loadChildren)) {
10296
this._loadChildren = this.node.loadChildren;
10397
} else {
104-
_forEach(_get(model, 'children') as TreeModel[], (child: TreeModel, index: number) => {
98+
get(model, 'children', []).forEach((child: TreeModel, index: number) => {
10599
this._addChild(new Tree(child, this), index);
106100
});
107101
}
@@ -200,11 +194,11 @@ export class Tree {
200194
return;
201195
}
202196

197+
const stringifiedValue = '' + value;
203198
if (Tree.isRenamable(this.value)) {
204-
const newValue = typeof value === 'string' ? value : _toString(value);
205-
this.node.value = Tree.applyNewValueToRenamable(this.value as RenamableNode, newValue);
199+
this.node.value = Tree.applyNewValueToRenamable(this.value as RenamableNode, stringifiedValue);
206200
} else {
207-
this.node.value = Tree.isValueEmpty(value as string) ? this.node.value : _toString(value);
201+
this.node.value = Tree.isValueEmpty(stringifiedValue) ? this.node.value : stringifiedValue;
208202
}
209203
}
210204

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

234-
private _addChild(child: Tree, position: number = _size(this._children) || 0): Tree {
228+
private _addChild(child: Tree, position: number = size(this._children) || 0): Tree {
235229
child.parent = this;
236230

237231
if (Array.isArray(this._children)) {
@@ -268,31 +262,31 @@ export class Tree {
268262
* @returns {number} The position inside a parent.
269263
*/
270264
public get positionInParent(): number {
271-
return _indexOf(this.parent.children, this);
265+
return this.parent.children ? this.parent.children.indexOf(this) : -1;
272266
}
273267

274268
/**
275269
* Check whether or not this tree is static.
276270
* @returns {boolean} A flag indicating whether or not this tree is static.
277271
*/
278272
public isStatic(): boolean {
279-
return _get(this.node.settings, 'static', false);
273+
return get(this.node.settings, 'static', false);
280274
}
281275

282276
/**
283277
* Check whether or not this tree has a left menu.
284278
* @returns {boolean} A flag indicating whether or not this tree has a left menu.
285279
*/
286280
public hasLeftMenu(): boolean {
287-
return !_get(this.node.settings, 'static', false) && _get(this.node.settings, 'leftMenu', false);
281+
return !get(this.node.settings, 'static', false) && get(this.node.settings, 'leftMenu', false);
288282
}
289283

290284
/**
291285
* Check whether or not this tree has a right menu.
292286
* @returns {boolean} A flag indicating whether or not this tree has a right menu.
293287
*/
294288
public hasRightMenu(): boolean {
295-
return !_get(this.node.settings, 'static', false) && _get(this.node.settings, 'rightMenu', false);
289+
return !get(this.node.settings, 'static', false) && get(this.node.settings, 'rightMenu', false);
296290
}
297291

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

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

339333
/**
@@ -343,7 +337,7 @@ export class Tree {
343337
* @returns {boolean} A flag indicating whether provided tree is a child or not.
344338
*/
345339
public hasChild(tree: Tree): boolean {
346-
return _includes(this._children, tree);
340+
return includes(this._children, tree);
347341
}
348342

349343
/**
@@ -352,7 +346,11 @@ export class Tree {
352346
* @param {Tree} tree - A tree that should be removed.
353347
*/
354348
public removeChild(tree: Tree): void {
355-
const childIndex = _findIndex(this._children, (child: Tree) => child === tree);
349+
if (!this.hasChildren()) {
350+
return;
351+
}
352+
353+
const childIndex = this._children.findIndex((child: Tree) => child === tree);
356354
if (childIndex >= 0) {
357355
this._children.splice(childIndex, 1);
358356
}
@@ -404,7 +402,7 @@ export class Tree {
404402
private _setFoldingType(): void {
405403
if (this.childrenShouldBeLoaded()) {
406404
this.node._foldingType = FoldingType.Collapsed;
407-
} else if (this._children && !_isEmpty(this._children)) {
405+
} else if (this._children && !isEmpty(this._children)) {
408406
this.node._foldingType = FoldingType.Expanded;
409407
} else if (Array.isArray(this._children)) {
410408
this.node._foldingType = FoldingType.Empty;
@@ -438,14 +436,14 @@ export class Tree {
438436
}
439437

440438
if (this.node._foldingType === FoldingType.Collapsed) {
441-
return _get(this.node.settings, 'cssClasses.collapsed', null);
439+
return get(this.node.settings, 'cssClasses.collapsed', null);
442440
} else if (this.node._foldingType === FoldingType.Expanded) {
443-
return _get(this.node.settings, 'cssClasses.expanded', null);
441+
return get(this.node.settings, 'cssClasses.expanded', null);
444442
} else if (this.node._foldingType === FoldingType.Empty) {
445-
return _get(this.node.settings, 'cssClasses.empty', null);
443+
return get(this.node.settings, 'cssClasses.empty', null);
446444
}
447445

448-
return _get(this.node.settings, 'cssClasses.leaf', null);
446+
return get(this.node.settings, 'cssClasses.leaf', null);
449447
}
450448

451449
/**
@@ -458,9 +456,9 @@ export class Tree {
458456

459457
private getTemplateFromSettings(): string {
460458
if (this.isLeaf()) {
461-
return _get(this.node.settings, 'templates.leaf', '');
459+
return get(this.node.settings, 'templates.leaf', '');
462460
} else {
463-
return _get(this.node.settings, 'templates.node', '');
461+
return get(this.node.settings, 'templates.node', '');
464462
}
465463
}
466464

@@ -470,7 +468,7 @@ export class Tree {
470468
*/
471469
public get leftMenuTemplate(): string {
472470
if (this.hasLeftMenu()) {
473-
return _get(this.node.settings, 'templates.leftMenu', '<span></span>');
471+
return get(this.node.settings, 'templates.leftMenu', '<span></span>');
474472
}
475473
return '';
476474
}

src/tree.types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import _defaultsDeep from 'lodash-es/defaultsDeep';
2-
import _get from 'lodash-es/get';
1+
import { get, defaultsDeep } from './utils/fn.utils';
32

43
export class FoldingType {
54
public static Expanded: FoldingType = new FoldingType('node-expanded');
@@ -84,7 +83,7 @@ export class TreeModelSettings {
8483
public static?: boolean;
8584

8685
public static merge(sourceA: TreeModel, sourceB: TreeModel): TreeModelSettings {
87-
return _defaultsDeep({}, _get(sourceA, 'settings'), _get(sourceB, 'settings'), {static: false, leftMenu: false, rightMenu: true});
86+
return defaultsDeep({}, get(sourceA, 'settings'), get(sourceB, 'settings'), {static: false, leftMenu: false, rightMenu: true});
8887
}
8988
}
9089

0 commit comments

Comments
 (0)