Browse Source

Add data namespace

Data namespace includes access and datetime functions.
master
Allan Crisostomo 3 years ago
parent
commit
32f0770f65
3 changed files with 103 additions and 1 deletions
  1. +13
    -0
      src/data/access.ts
  2. +81
    -0
      src/data/datetime.ts
  3. +9
    -1
      src/plugin.ts

+ 13
- 0
src/data/access.ts View File

@@ -0,0 +1,13 @@
export enum Access {
'note:read' = 1,
'note:write' = 2,
'note:delete' = 3,
'note:import' = 4,
'note:export' = 5,
'folder:read' = 7,
'folder:write' = 8,
'social:publish' = 9,
'social:unpublish' = 10,
'social:share' = 11,
'userinfo:manage' = 12,
}

+ 81
- 0
src/data/datetime.ts View File

@@ -0,0 +1,81 @@
export enum TimeDivision {
MILLISECONDS,
SECONDS,
MINUTES,
HOURS,
DAYS
}

type GetTimeDifference = (a: Date, b: Date) => (c: TimeDivision) => number

export const getTimeDifference: GetTimeDifference = (a, b) => {
const ms = b.getTime() - a.getTime()
const absoluteMs = ms < 0 ? -ms : ms

return c => {
let divisionDifference = absoluteMs
if (c === TimeDivision.MILLISECONDS) {
return divisionDifference
}

divisionDifference /= 1000
if (c === TimeDivision.SECONDS) {
return divisionDifference
}

divisionDifference /= 60
if (c === TimeDivision.MINUTES) {
return divisionDifference
}

divisionDifference /= 60
if (c === TimeDivision.HOURS) {
return divisionDifference
}

divisionDifference /= 24
if (c === TimeDivision.DAYS) {
return divisionDifference
}

throw new Error('Unknown time division.')
}
}

type AddTime = (refDate: Date, increment: number) => (c: TimeDivision) => Date

export const addTime: AddTime = (refDate, increment) => {
const futureDate = new Date(refDate.getTime())
return c => {
let msIncrement = increment
if (c === TimeDivision.MILLISECONDS) {
futureDate.setMilliseconds(futureDate.getMilliseconds() + msIncrement)
return futureDate
}

msIncrement *= 1000
if (c === TimeDivision.SECONDS) {
futureDate.setMilliseconds(futureDate.getMilliseconds() + msIncrement)
return futureDate
}

msIncrement *= 60
if (c === TimeDivision.MINUTES) {
futureDate.setMilliseconds(futureDate.getMilliseconds() + msIncrement)
return futureDate
}

msIncrement *= 60
if (c === TimeDivision.HOURS) {
futureDate.setMilliseconds(futureDate.getMilliseconds() + msIncrement)
return futureDate
}

msIncrement *= 24
if (c === TimeDivision.DAYS) {
futureDate.setMilliseconds(futureDate.getMilliseconds() + msIncrement)
return futureDate
}
throw new Error('Unknown time division.')
}
}

+ 9
- 1
src/plugin.ts View File

@@ -2,4 +2,12 @@ export type State = {
currentUserId: string,
}

export type Plugin<T = {}> = (config: T) => (state: State) => void
export interface StoragePlugin {
save(collectionId: string, item: unknown): Promise<unknown>
load(collectionId: string, itemId?: unknown): Promise<unknown>
remove(collectionId: string, item: unknown): Promise<unknown>
}

type StoragePluginConstructor = new (state: State) => StoragePlugin

export type Plugin<T = {}> = (config?: T) => StoragePluginConstructor

Loading…
Cancel
Save