feat: sort nodule

This commit is contained in:
joshuashoemaker 2022-06-22 23:04:55 -05:00
parent bdad848f45
commit 69b8ae5687
6 changed files with 274 additions and 3 deletions

View File

@ -0,0 +1,11 @@
const sortDirections = {
ASCENDING: 'ASCENDING',
DESCENDING: 'DESCENDING'
}
const sortValueTypes = {
NUMERIC: 'NUMERIC',
ALPHABETIC: 'ALPHABETIC'
}
export { sortDirections, sortValueTypes }

View File

@ -0,0 +1,44 @@
import { sortDirections, sortValueTypes } from '../../constants/sortTypes'
import { sortDirection, sortKey, sortNoduleConstructorProps, sortValueType } from '../../types/noduleTypes'
import { tableRow } from '../../types/tableTypes'
import Nodule from '../Nodule'
class SortNodule extends Nodule {
sortValueType: sortValueType = sortValueTypes.ALPHABETIC
sortDirection: sortDirection = sortDirections.ASCENDING
sortKey: sortKey = ''
constructor (props: sortNoduleConstructorProps) {
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 } = sortValueTypes
const { DESCENDING } = sortDirections
let sortMethod
if (sortValueType === NUMERIC) sortMethod = (a: tableRow, b: tableRow) => {
const aValue = a[sortKey] as number
const bValue = b[sortKey] as number
return aValue - bValue
}
else sortMethod = (a: tableRow, b: tableRow) => {
const aValue = a[sortKey] as string
const bValue = b[sortKey] as string
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
}
}
export default SortNodule

View File

@ -34,7 +34,7 @@ class TransformNodule extends Nodule {
const err: errType = { const err: errType = {
status: 'ERR', status: 'ERR',
error: { error: {
label: 'Ptructure Parameters are not valid', label: 'Structure Parameters are not valid',
messages: [] messages: []
} }
} }

View File

@ -1,4 +1,5 @@
import filterTypes from "../constants/filterTypes" import filterTypes from "../constants/filterTypes"
import { sortValueTypes, sortDirections } from "../constants/sortTypes"
import Table from "../entities/Table" import Table from "../entities/Table"
import { tableRow } from "./tableTypes" import { tableRow } from "./tableTypes"
@ -26,6 +27,17 @@ type groupByNoduleConstructorProps = noduleConstructorProps & { groupByValue: st
type groupedByRows = Record<string, tableRow[]> type groupedByRows = Record<string, tableRow[]>
type sortDirectionKeys = keyof typeof sortDirections
type sortDirectionValues = typeof sortDirections[sortDirectionKeys]
type sortDirection = sortDirectionValues
type sortValueTypeKeys = keyof typeof sortValueTypes
type sortValueTypeValues = typeof sortValueTypes[sortValueTypeKeys]
type sortValueType = sortValueTypeValues
type sortKey = string
type sortNoduleConstructorProps = noduleConstructorProps & { sortDirection: sortDirection, sortValueType: sortValueType, sortKey: sortKey }
export { export {
noduleConstructorProps, noduleConstructorProps,
filterNoduleConstructionProps, filterNoduleConstructionProps,
@ -37,5 +49,9 @@ export {
transformStruct, transformStruct,
transformNoduleConstructionProps, transformNoduleConstructionProps,
groupByNoduleConstructorProps, groupByNoduleConstructorProps,
groupedByRows groupedByRows,
sortDirection,
sortValueType,
sortKey,
sortNoduleConstructorProps,
} }

View File

@ -0,0 +1,198 @@
import SortNodule from '../../../src/entities/nodules/SortNodule'
import Table from '../../../src/entities/Table'
const sortNumericAscending = () => {
const expectedOutput = [
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
]
})
} catch (err) {
return false
}
let sortNodule = {}
try {
sortNodule = new SortNodule({
id: 'ABC',
label: 'Test Nodule',
tables: [table],
sortValueType: 'NUMERIC',
sortDirection: 'ASCENDING',
sortKey: 'someNumber'
})
} catch (err) {
console.log(err)
return false
}
const sortedRows = sortNodule.export()
if (JSON.stringify(sortedRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
const sortNumericDesscending = () => {
const expectedOutput = [
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
]
})
} catch (err) {
return false
}
let sortNodule = {}
try {
sortNodule = new SortNodule({
id: 'ABC',
label: 'Test Nodule',
tables: [table],
sortValueType: 'NUMERIC',
sortDirection: 'DESCENDING',
sortKey: 'someNumber'
})
} catch (err) {
console.log(err)
return false
}
const sortedRows = sortNodule.export()
if (JSON.stringify(sortedRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
const sortAlphabeticAscending = () => {
const expectedOutput = [
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
]
})
} catch (err) {
return false
}
let sortNodule = {}
try {
sortNodule = new SortNodule({
id: 'ABC',
label: 'Test Nodule',
tables: [table],
sortValueType: 'ALPHABETIC',
sortDirection: 'ASCENDING',
sortKey: 'contractor'
})
} catch (err) {
console.log(err)
return false
}
const sortedRows = sortNodule.export()
if (JSON.stringify(sortedRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
const sortAlphabeticDescending = () => {
const expectedOutput = [
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
]
let table = {}
try {
table = new Table({
id: 'XYZ',
label: 'Test Table',
rows: [
{ id: 'abc', data: 'row', contractor: 'AshBritt', someNumber: 9 },
{ id: 'qwe', data: 'lh', contractor: 'AshBritt', someNumber: 11 },
{ id: 'XYZ', data: 'row', contractor: 'HeyDay', someNumber: 4 },
{ id: 'XYZ', data: 'row', contractor: 'AshBritt', someNumber: 1 },
]
})
} catch (err) {
return false
}
let sortNodule = {}
try {
sortNodule = new SortNodule({
id: 'ABC',
label: 'Test Nodule',
tables: [table],
sortValueType: 'ALPHABETIC',
sortDirection: 'DESCENDING',
sortKey: 'contractor'
})
} catch (err) {
console.log(err)
return false
}
const sortedRows = sortNodule.export()
if (JSON.stringify(sortedRows) === JSON.stringify(expectedOutput)) {
return true
} else {
return false
}
}
export default [
{ name: 'Entities | Sort Nodule Numeric Ascending', test: sortNumericAscending },
{ name: 'Entities | Sort Nodule Numeric Descending', test: sortNumericDesscending },
{ name: 'Entities | Sort Nodule Alphabetic Ascending', test: sortAlphabeticAscending },
{ name: 'Entities | Sort Nodule Alphabetic Descending', test: sortAlphabeticDescending },
]

View File

@ -6,6 +6,7 @@ import filterNoduleTests from './core/nodules/filterNoduleTests.js'
import joinNoduleTests from './core/nodules/joinNoduleTests.js' import joinNoduleTests from './core/nodules/joinNoduleTests.js'
import transformNoduleTests from './core/nodules/transformNoduleTests.js' import transformNoduleTests from './core/nodules/transformNoduleTests.js'
import groupByNoduleTests from './core/nodules/groupByNoduleTests.js' import groupByNoduleTests from './core/nodules/groupByNoduleTests.js'
import sortNoduleTests from './core/nodules/sortNoduleTests.js'
type unitTest = { name: string, test: Function } type unitTest = { name: string, test: Function }
@ -38,7 +39,8 @@ const testsArray = [
filterNoduleTests, filterNoduleTests,
joinNoduleTests, joinNoduleTests,
transformNoduleTests, transformNoduleTests,
groupByNoduleTests groupByNoduleTests,
sortNoduleTests,
] ]
init (testsArray.flat()) init (testsArray.flat())