Skip to content

Commit

Permalink
Merge pull request #2 from tim-s-ccs/make-some-updates-around-dates
Browse files Browse the repository at this point in the history
Add some new features
  • Loading branch information
tim-s-ccs authored Feb 2, 2022
2 parents 4e4259a + 6e0e4d5 commit 1178b3d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/models/activeModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cast from '../utils/cast'
import Model from './model'
import StaticModel from './staticModel'
import StaticModelValidator from '../validation/validators/staticModelValidator'
Expand All @@ -8,6 +7,7 @@ import { addActiveRow, getActiveRow, getActiveTable, setActiveRow } from '../dat
import { ErrorMessages, GenericValidatorOptions } from '../types/validation/validator'
import { Request } from 'express'
import { TableRow } from '../types/data/tables'
import { utils } from '..'
import { ValidationSchema, ValidationScheme } from '../types/validation/validationSchema'

abstract class ActiveModel extends Model implements ActiveModelInterface {
Expand Down Expand Up @@ -132,14 +132,14 @@ abstract class ActiveModel extends Model implements ActiveModelInterface {
if (attributeConstructor.prototype instanceof ActiveModel) {
(this.data[attribute] as ActiveModel).assignAttributes(data[attribute])
} else if (attributeConstructor.prototype instanceof StaticModel) {
const id = cast(data[attribute], Number)
const id = utils.cast(data[attribute], Number)

if (this.data[attribute] === undefined || (this.data[attribute] as StaticModel).data.id !== id) {
// TODO: Chnage to somthing that is not any
this.data[attribute] = (attributeConstructor as any).find(id)
}
} else {
this.data[attribute] = cast(data[attribute], attributeConstructor as NumberConstructor|StringConstructor|BooleanConstructor)
this.data[attribute] = utils.cast(data[attribute], attributeConstructor as NumberConstructor|StringConstructor|BooleanConstructor)
}
}
}
Expand All @@ -150,6 +150,8 @@ abstract class ActiveModel extends Model implements ActiveModelInterface {

activeModelAttributes.forEach(activeModelAttribute => (this.data[activeModelAttribute] as ActiveModel)._save(req, dataInterface))

if ('updatedAt' in this.modelSchema) this.data['updatedAt'] = utils.getCurrentDate()

dataInterface(req, this.tableName, this.data.id, this.attributes())
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils/addLeadingZeros.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const addLeadingZeros = (num: number, length: number): string => {
return ('0'.repeat(length) + num).slice(-length)
}

export default addLeadingZeros
43 changes: 43 additions & 0 deletions src/utils/formatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import addLeadingZeros from './addLeadingZeros'

const monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]

const hourToTwelveHour = (hour: number): number => {
const twelveHour = hour % 12

if (twelveHour === 0) {
return 12
} else {
return twelveHour
}
}

const amOrPm = (hour: number): string => {
if (hour <= 12) {
return 'am'
} else {
return 'pm'
}
}

const datePart = (date: Date): string => {
return `${date.getDate()} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
}

const timePart = (date: Date): string => {
return `${hourToTwelveHour(date.getHours())}:${addLeadingZeros(date.getMinutes(), 2)}${amOrPm(date.getHours())}`
}

const formatDate = (date: Date, withTime: false): string => {
const dateString = datePart(date)

if (withTime) {
return `${dateString}, ${timePart(date)}`
} else {
return dateString
}
}

export default formatDate
6 changes: 3 additions & 3 deletions src/utils/getUpdatedAt.ts → src/utils/getCurrentDate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const getUpdatedAt = (): string => {
const getCurrentDate = (): string => {
let currentDate = new Date()
const offset = currentDate.getTimezoneOffset()
currentDate = new Date(currentDate.getTime() - (offset*60*1000))
return currentDate.toISOString().split('T')[0]
return currentDate.toISOString()
}

export default getUpdatedAt
export default getCurrentDate
8 changes: 6 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import addLeadingZeros from './addLeadingZeros'
import cast from './cast'
import getUpdatedAt from './getUpdatedAt'
import formatDate from './formatDate'
import getCurrentDate from './getCurrentDate'
import numberToCurrency from './numberToCurrency'
import pluralise from './pluralise'

export default {
addLeadingZeros: addLeadingZeros,
cast: cast,
getUpdatedAt: getUpdatedAt,
formatDate: formatDate,
getCurrentDate: getCurrentDate,
pluralise: pluralise,
numberToCurrency: numberToCurrency
}

0 comments on commit 1178b3d

Please sign in to comment.