From 32f0770f65a60698c2b23968a1946b40feb1abec Mon Sep 17 00:00:00 2001 From: Allan Crisostomo Date: Mon, 30 Nov 2020 16:26:18 +0800 Subject: [PATCH] Add data namespace Data namespace includes access and datetime functions. --- src/data/access.ts | 13 +++++++ src/data/datetime.ts | 81 ++++++++++++++++++++++++++++++++++++++++++++ src/plugin.ts | 10 +++++- 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/data/access.ts create mode 100644 src/data/datetime.ts diff --git a/src/data/access.ts b/src/data/access.ts new file mode 100644 index 0000000..201499b --- /dev/null +++ b/src/data/access.ts @@ -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, +} diff --git a/src/data/datetime.ts b/src/data/datetime.ts new file mode 100644 index 0000000..132c6cd --- /dev/null +++ b/src/data/datetime.ts @@ -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.') + } +} diff --git a/src/plugin.ts b/src/plugin.ts index 0e7ef4a..304e2c3 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2,4 +2,12 @@ export type State = { currentUserId: string, } -export type Plugin = (config: T) => (state: State) => void +export interface StoragePlugin { + save(collectionId: string, item: unknown): Promise + load(collectionId: string, itemId?: unknown): Promise + remove(collectionId: string, item: unknown): Promise +} + +type StoragePluginConstructor = new (state: State) => StoragePlugin + +export type Plugin = (config?: T) => StoragePluginConstructor