Skip to content

Commit

Permalink
replace lodash packages with es6 alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
xaksis committed Feb 28, 2021
1 parent 1e773f8 commit 1426874
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 121 deletions.
3 changes: 1 addition & 2 deletions dev/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ export default {
rowStyleClass: 'red',
searchTerm: '',
paginationOptions: {
mode: 'pages',
mode: 'records',
enabled: true,
perPage: 5,
perPageDropdown: [3, 50, 100, 200, 300, 500, 1000],
perPageDropdownEnabled: true,
setCurrentPage: 2,
// infoFn: (params) => `alala ${params.firstRecordOnPage} to ${params.lastRecordOnPage} of ${params.totalRecords}`,
},
columns: [
Expand Down
40 changes: 4 additions & 36 deletions package-lock.json

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

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
"dependencies": {
"date-fns": "^2.0.0-beta.4",
"diacriticless": "1.0.1",
"lodash.assign": "^4.2.0",
"lodash.clonedeep": "^4.5.0",
"lodash.filter": "^4.6.0",
"lodash.foreach": "^4.5.0",
"lodash.isequal": "^4.5.0"
},
"devDependencies": {
Expand Down
76 changes: 36 additions & 40 deletions src/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,6 @@
</template>

<script>
import each from 'lodash.foreach';
import assign from 'lodash.assign';
import cloneDeep from 'lodash.clonedeep';
import filter from 'lodash.filter';
import isEqual from 'lodash.isequal';
import defaultType from './types/default';
import VgtPagination from './pagination/VgtPagination.vue';
Expand All @@ -356,7 +352,7 @@ import * as CoreDataTypes from './types/index';
const dataTypes = {};
const coreDataTypes = CoreDataTypes.default;
each(Object.keys(coreDataTypes), (key) => {
Object.keys(coreDataTypes).forEach((key) => {
const compName = key.replace(/^\.\//, '').replace(/\.js/, '');
dataTypes[compName] = coreDataTypes[key].default;
});
Expand Down Expand Up @@ -635,8 +631,8 @@ export default {
selectedPageRows() {
const selectedRows = [];
each(this.paginated, (headerRow) => {
each(headerRow.children, (row) => {
this.paginated.forEach((headerRow) => {
headerRow.children.forEach((row) => {
if (row.vgtSelected) {
selectedRows.push(row);
}
Expand All @@ -647,8 +643,8 @@ export default {
selectedRows() {
const selectedRows = [];
each(this.processedRows, (headerRow) => {
each(headerRow.children, (row) => {
this.processedRows.forEach((headerRow) => {
headerRow.children.forEach((row) => {
if (row.vgtSelected) {
selectedRows.push(row);
}
Expand Down Expand Up @@ -694,17 +690,17 @@ export default {
return false;
},
totalRowCount() {
let total = 0;
each(this.processedRows, (headerRow) => {
total += headerRow.children ? headerRow.children.length : 0;
});
const total = this.processedRows.reduce((total, headerRow) => {
const childrenCount = headerRow.children ? headerRow.children.length : 0;
return total + childrenCount;
}, 0);
return total;
},
totalPageRowCount() {
let total = 0;
each(this.paginated, (headerRow) => {
total += headerRow.children ? headerRow.children.length : 0;
});
const total = this.paginated.reduce((total, headerRow) => {
const childrenCount = headerRow.children ? headerRow.children.length : 0;
return total + childrenCount;
}, 0);
return total;
},
wrapStyleClasses() {
Expand Down Expand Up @@ -762,12 +758,12 @@ export default {
// here also we need to de-construct and then
// re-construct the rows.
const allRows = [];
each(this.filteredRows, (headerRow) => {
this.filteredRows.forEach((headerRow) => {
allRows.push(...headerRow.children);
});
const filteredRows = [];
each(allRows, (row) => {
each(this.columns, (col) => {
allRows.forEach((row) => {
this.columns.forEach((col) => {
// if col does not have search disabled,
if (!col.globalSearchDisabled) {
// if a search function is provided,
Expand Down Expand Up @@ -809,11 +805,11 @@ export default {
// here we need to reconstruct the nested structure
// of rows
computedRows = [];
each(this.filteredRows, (headerRow) => {
this.filteredRows.forEach((headerRow) => {
const i = headerRow.vgt_header_id;
const children = filter(filteredRows, ['vgt_id', i]);
const children = filteredRows.filter((r) => r.vgt_id === i);
if (children.length) {
const newHeaderRow = cloneDeep(headerRow);
const newHeaderRow = JSON.parse(JSON.stringify(headerRow));
newHeaderRow.children = children;
computedRows.push(newHeaderRow);
}
Expand Down Expand Up @@ -868,7 +864,7 @@ export default {
//* flatten the rows for paging.
let paginatedRows = [];
each(this.processedRows, (childRows) => {
this.processedRows.forEach((childRows) => {
//* only add headers when group options are enabled.
if (this.groupOptions.enabled) {
paginatedRows.push(childRows);
Expand Down Expand Up @@ -903,7 +899,7 @@ export default {
//* header row?
if (flatRow.vgt_header_id !== undefined) {
this.handleExpanded(flatRow);
const newHeaderRow = cloneDeep(flatRow);
const newHeaderRow = JSON.parse(JSON.stringify(flatRow));
newHeaderRow.children = [];
reconstructedRows.push(newHeaderRow);
} else {
Expand All @@ -912,7 +908,7 @@ export default {
if (!hRow) {
hRow = this.processedRows.find(r => r.vgt_header_id === flatRow.vgt_id);
if (hRow) {
hRow = cloneDeep(hRow);
hRow = JSON.parse(JSON.stringify(hRow));
hRow.children = [];
reconstructedRows.push(hRow);
}
Expand All @@ -924,7 +920,7 @@ export default {
},
originalRows() {
const rows = cloneDeep(this.rows);
const rows = JSON.parse(JSON.stringify(this.rows));
let nestedRows = [];
if (!this.groupOptions.enabled) {
nestedRows = this.handleGrouped([
Expand All @@ -939,8 +935,8 @@ export default {
// we need to preserve the original index of
// rows so lets do that
let index = 0;
each(nestedRows, (headerRow, i) => {
each(headerRow.children, (row, j) => {
nestedRows.forEach((headerRow) => {
headerRow.children.forEach((row) => {
row.originalIndex = index++;
});
});
Expand All @@ -949,7 +945,7 @@ export default {
},
typedColumns() {
const columns = assign(this.columns, []);
const columns = this.columns;
for (let i = 0; i < this.columns.length; i++) {
const column = columns[i];
column.typeDef = this.dataTypes[column.type] || defaultType;
Expand Down Expand Up @@ -1037,8 +1033,8 @@ export default {
unselectAllInternal(forceAll) {
const rows =
this.selectAllByPage && !forceAll ? this.paginated : this.filteredRows;
each(rows, (headerRow, i) => {
each(headerRow.children, (row, j) => {
rows.forEach((headerRow, i) => {
headerRow.children.forEach((row, j) => {
this.$set(row, 'vgtSelected', false);
});
});
Expand All @@ -1051,16 +1047,16 @@ export default {
return;
}
const rows = this.selectAllByPage ? this.paginated : this.filteredRows;
each(rows, (headerRow) => {
each(headerRow.children, (row) => {
rows.forEach((headerRow) => {
headerRow.children.forEach((row) => {
this.$set(row, 'vgtSelected', true);
});
});
this.emitSelectedRows();
},
toggleSelectGroup(event, headerRow) {
each(headerRow.children, (row) => {
headerRow.children.forEach((row) => {
this.$set(row, 'vgtSelected', event.checked);
});
},
Expand Down Expand Up @@ -1203,7 +1199,7 @@ export default {
this.handleSearch();
// we reset the filteredRows here because
// we want to search across everything.
this.filteredRows = cloneDeep(this.originalRows);
this.filteredRows = JSON.parse(JSON.stringify(this.originalRows));
this.forceSearch = true;
this.sortChanged = true;
}
Expand Down Expand Up @@ -1317,7 +1313,7 @@ export default {
// this is invoked either as a result of changing filters
// or as a result of modifying rows.
this.columnFilters = columnFilters;
let computedRows = cloneDeep(this.originalRows);
let computedRows = JSON.parse(JSON.stringify(this.originalRows));
// do we have a filter to care about?
// if not we don't need to do anything
Expand Down Expand Up @@ -1359,7 +1355,7 @@ export default {
for (let i = 0; i < this.typedColumns.length; i++) {
const col = this.typedColumns[i];
if (this.columnFilters[fieldKey(col.field)]) {
computedRows = each(computedRows, (headerRow) => {
computedRows.forEach((headerRow) => {
const newChildren = headerRow.children.filter((row) => {
// If column has a custom filter, use that.
if (
Expand Down Expand Up @@ -1428,15 +1424,15 @@ export default {
},
handleGrouped(originalRows) {
each(originalRows, (headerRow, i) => {
originalRows.forEach((headerRow, i) => {
headerRow.vgt_header_id = i;
if (
this.groupOptions.maintainExpanded &&
this.expandedRowKeys.has(headerRow[this.groupOptions.rowKey])
) {
this.$set(headerRow, 'vgtIsExpanded', true);
}
each(headerRow.children, (childRow) => {
headerRow.children.forEach((childRow) => {
childRow.vgt_id = i;
});
});
Expand Down
1 change: 0 additions & 1 deletion src/components/VgtFilterRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
</template>

<script>
import isEqual from 'lodash.isequal';
export default {
name: 'VgtFilterRow',
Expand Down
9 changes: 4 additions & 5 deletions src/components/VgtTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@
</template>

<script>
import assign from 'lodash.assign';
import VgtFilterRow from './VgtFilterRow.vue';
import * as SortUtils from './utils/sort.js';
import { primarySort, secondarySort } from './utils/sort';
export default {
name: 'VgtTableHeader',
Expand Down Expand Up @@ -165,9 +164,9 @@ export default {
if (!this.isSortableColumn(column)) return;
if (e.shiftKey) {
this.sorts = SortUtils.secondarySort(this.sorts, column);
this.sorts = secondarySort(this.sorts, column);
} else {
this.sorts = SortUtils.primarySort(this.sorts, column);
this.sorts = primarySort(this.sorts, column);
}
this.$emit('on-sort-change', this.sorts);
},
Expand All @@ -193,7 +192,7 @@ export default {
},
getHeaderClasses(column, index) {
const classes = assign({}, this.getClasses(index, 'th'), {
const classes = Object.assign({}, this.getClasses(index, 'th'), {
sortable: this.isSortableColumn(column),
'sorting sorting-desc': this.getColumnSort(column) === 'desc',
'sorting sorting-asc': this.getColumnSort(column) === 'asc',
Expand Down
Loading

0 comments on commit 1426874

Please sign in to comment.