diff --git a/lib/constants/sortTypes.d.ts b/lib/constants/sortTypes.d.ts new file mode 100644 index 0000000..2209a1a --- /dev/null +++ b/lib/constants/sortTypes.d.ts @@ -0,0 +1,9 @@ +declare const sortDirections: { + ASCENDING: string; + DESCENDING: string; +}; +declare const sortValueTypes: { + NUMERIC: string; + ALPHABETIC: string; +}; +export { sortDirections, sortValueTypes }; diff --git a/lib/constants/sortTypes.js b/lib/constants/sortTypes.js new file mode 100644 index 0000000..8797cca --- /dev/null +++ b/lib/constants/sortTypes.js @@ -0,0 +1,13 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.sortValueTypes = exports.sortDirections = void 0; +const sortDirections = { + ASCENDING: 'ASCENDING', + DESCENDING: 'DESCENDING' +}; +exports.sortDirections = sortDirections; +const sortValueTypes = { + NUMERIC: 'NUMERIC', + ALPHABETIC: 'ALPHABETIC' +}; +exports.sortValueTypes = sortValueTypes; diff --git a/lib/entities/nodules/FilterNodule.d.ts b/lib/entities/nodules/FilterNodule.d.ts index 43c687e..0e1459c 100644 --- a/lib/entities/nodules/FilterNodule.d.ts +++ b/lib/entities/nodules/FilterNodule.d.ts @@ -5,7 +5,7 @@ declare class FilterNodule extends Nodule { filterParams: filterParams; constructor(props: filterNoduleConstructionProps); addFilter: (params: filterParams) => void; - setFilterType: (filterType: filterType) => void; + setFilterType: (filterType: string) => void; export: () => import("../../types/tableTypes").tableRow[]; private createFilterMethods; private validateFilters; diff --git a/lib/entities/nodules/GroupByNodule.d.ts b/lib/entities/nodules/GroupByNodule.d.ts index 1738b58..e24cf5a 100644 --- a/lib/entities/nodules/GroupByNodule.d.ts +++ b/lib/entities/nodules/GroupByNodule.d.ts @@ -7,7 +7,7 @@ declare class GroupByNodule extends Nodule { asTables: () => Table[]; asTable: () => never; export: () => never; - exportTables: () => groupedByRows; + exportRowGroups: () => groupedByRows; setGroupByValue: (value: string) => void; private validateGroupByValue; } diff --git a/lib/entities/nodules/SortNodule.d.ts b/lib/entities/nodules/SortNodule.d.ts new file mode 100644 index 0000000..5796bb2 --- /dev/null +++ b/lib/entities/nodules/SortNodule.d.ts @@ -0,0 +1,11 @@ +import { sortDirection, sortKey, sortNoduleConstructorProps, sortValueType } from '../../types/noduleTypes'; +import { tableRow } from '../../types/tableTypes'; +import Nodule from '../Nodule'; +declare class SortNodule extends Nodule { + sortValueType: sortValueType; + sortDirection: sortDirection; + sortKey: sortKey; + constructor(props: sortNoduleConstructorProps); + export: () => tableRow[]; +} +export default SortNodule; diff --git a/lib/entities/nodules/SortNodule.js b/lib/entities/nodules/SortNodule.js new file mode 100644 index 0000000..3fa290c --- /dev/null +++ b/lib/entities/nodules/SortNodule.js @@ -0,0 +1,42 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const sortTypes_1 = require("../../constants/sortTypes"); +const Nodule_1 = require("../Nodule"); +class SortNodule extends Nodule_1.default { + sortValueType = sortTypes_1.sortValueTypes.ALPHABETIC; + sortDirection = sortTypes_1.sortDirections.ASCENDING; + sortKey = ''; + constructor(props) { + super(props); + const { sortValueType, sortDirection, sortKey } = props; + if (sortValueType) + this.sortValueType = sortValueType; + if (sortDirection) + this.sortDirection = sortDirection; + if (sortKey) + this.sortKey = sortKey; + } + export = () => { + const { sortValueType, sortDirection, sortKey } = this; + const { NUMERIC } = sortTypes_1.sortValueTypes; + const { DESCENDING } = sortTypes_1.sortDirections; + let sortMethod; + if (sortValueType === NUMERIC) + sortMethod = (a, b) => { + const aValue = a[sortKey]; + const bValue = b[sortKey]; + return aValue - bValue; + }; + else + sortMethod = (a, b) => { + const aValue = a[sortKey]; + const bValue = b[sortKey]; + return aValue.toLowerCase().localeCompare(bValue.toLowerCase()); + }; + let rows = this.tables.map(t => t.export()).flat().sort(sortMethod); + if (sortDirection === DESCENDING) + rows = rows.reverse(); + return rows; + }; +} +exports.default = SortNodule; diff --git a/lib/entities/nodules/TransformNodule.js b/lib/entities/nodules/TransformNodule.js index df99682..4996b09 100644 --- a/lib/entities/nodules/TransformNodule.js +++ b/lib/entities/nodules/TransformNodule.js @@ -30,7 +30,7 @@ class TransformNodule extends Nodule_1.default { const err = { status: 'ERR', error: { - label: 'Ptructure Parameters are not valid', + label: 'Structure Parameters are not valid', messages: [] } }; diff --git a/lib/types/noduleTypes.d.ts b/lib/types/noduleTypes.d.ts index f72f0f8..fea87e8 100644 --- a/lib/types/noduleTypes.d.ts +++ b/lib/types/noduleTypes.d.ts @@ -1,3 +1,5 @@ +import filterTypes from "../constants/filterTypes"; +import { sortValueTypes, sortDirections } from "../constants/sortTypes"; import Table from "../entities/Table"; import { tableRow } from "./tableTypes"; declare type noduleConstructorProps = { @@ -6,7 +8,9 @@ declare type noduleConstructorProps = { type?: 'Nodule'; tables?: Table[]; }; -declare type filterType = 'EQUAL' | 'GREATER' | 'GREATEREQUAL' | 'LESSER' | 'LESSEREQUAL'; +declare type filterKeys = keyof typeof filterTypes; +declare type filterValues = typeof filterTypes[filterKeys]; +declare type filterType = filterValues; declare type filterParams = Record; declare type filterNoduleConstructionProps = noduleConstructorProps & { filterType: filterType; @@ -33,4 +37,16 @@ declare type groupByNoduleConstructorProps = noduleConstructorProps & { groupByValue: string; }; declare type groupedByRows = Record; -export { noduleConstructorProps, filterNoduleConstructionProps, filterType, filterParams, joinParam, joinBy, joinNoduleConstructionProps, transformStruct, transformNoduleConstructionProps, groupByNoduleConstructorProps, groupedByRows }; +declare type sortDirectionKeys = keyof typeof sortDirections; +declare type sortDirectionValues = typeof sortDirections[sortDirectionKeys]; +declare type sortDirection = sortDirectionValues; +declare type sortValueTypeKeys = keyof typeof sortValueTypes; +declare type sortValueTypeValues = typeof sortValueTypes[sortValueTypeKeys]; +declare type sortValueType = sortValueTypeValues; +declare type sortKey = string; +declare type sortNoduleConstructorProps = noduleConstructorProps & { + sortDirection: sortDirection; + sortValueType: sortValueType; + sortKey: sortKey; +}; +export { noduleConstructorProps, filterNoduleConstructionProps, filterType, filterParams, joinParam, joinBy, joinNoduleConstructionProps, transformStruct, transformNoduleConstructionProps, groupByNoduleConstructorProps, groupedByRows, sortDirection, sortValueType, sortKey, sortNoduleConstructorProps, }; diff --git a/src/entities/nodules/GroupByNodule.ts b/src/entities/nodules/GroupByNodule.ts index 36b5da1..421e77d 100644 --- a/src/entities/nodules/GroupByNodule.ts +++ b/src/entities/nodules/GroupByNodule.ts @@ -13,7 +13,7 @@ class GroupByNodule extends Nodule { } asTables = (): Table[] => { - const exports = this.exportTables() + const exports = this.exportRowGroups() const tables = [] for (let key in exports) { const newTableProps: tableConstructorProps = { @@ -35,7 +35,7 @@ class GroupByNodule extends Nodule { throw new Error('"export()" can not be called by GroupByNodule. Call "exportTables()"') } - exportTables = (): groupedByRows => { + exportRowGroups = (): groupedByRows => { const { groupByValue } = this const rows = this.tables.map(t => t.export() ).flat() const groupedByRows = rows.reduce((groups: groupedByRows, r) => { diff --git a/tests/core/nodules/groupByNoduleTests.js b/tests/core/nodules/groupByNoduleTests.js index a703f64..1204ccb 100644 --- a/tests/core/nodules/groupByNoduleTests.js +++ b/tests/core/nodules/groupByNoduleTests.js @@ -42,7 +42,7 @@ const groupByTest = () => { return false } - const groupedRows = groupByNodule.exportTables() + const groupedRows = groupByNodule.exportRowGroups() if (JSON.stringify(groupedRows) === JSON.stringify(expectedOutput)) { return true } else {