Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Date Time Range' type #32

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Type.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Term: Real
Term: Text
Term: Date
Term: Date Time
Term: Date Time Range
Term: Time
Term: Interval
Term: File
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Color from './types/color';
import * as ConceptType from './types/concept-type';
import * as Date from './types/date';
import * as DateTime from './types/date-time';
import * as DateTimeRange from './types/date-time-range';
import * as File from './types/file';
import * as ForeignKey from './types/foreign-key';
import * as Hashed from './types/hashed';
Expand All @@ -27,6 +28,7 @@ export = {
Color,
ConceptType,
'Date Time': DateTime,
'Date Time Range': DateTimeRange,
Date,
File,
ForeignKey,
Expand Down
58 changes: 58 additions & 0 deletions src/types/date-time-range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as TypeUtils from '../type-utils';

export const types = {
postgres: 'TSRANGE',
mysql: 'TEXT',
websql: 'TEXT',
odata: {
name: 'Edm.String',
},
};

export interface TSRange {
lowerInclusive: boolean;
lowerDate?: Date;
upperDate?: Date;
upperInclusive: boolean;
}

const regex = /(\[|\()(?:"((?:\\"|[^"])*)"|[^"]*),(?:"((?:\\"|[^"])*)"|[^"]*)(\]|\))/;

export async function fetchProcessing(data: null | undefined): Promise<null>;
export async function fetchProcessing(data: any): Promise<TSRange>;
export async function fetchProcessing(data: any): Promise<null | TSRange> {
if (data == null) {
return data;
}

const match = regex.exec(data);
if (!match) {
throw new Error(`Invalid date time range: '${data}`);
}

const [, lowerBound, lowerDate, upperDate, upperBound] = match;
return {
lowerInclusive: lowerBound === '[',
lowerDate: lowerDate == null ? undefined : new Date(lowerDate),
upperDate: upperDate == null ? undefined : new Date(upperDate),
upperInclusive: upperBound === ']',
};
}

export const validate = TypeUtils.validate.checkRequired(async value => {
let rangeObj: TSRange;
if (value == null) {
rangeObj = value;
} else if (typeof value === 'string') {
rangeObj = await fetchProcessing(value);
} else if (typeof value === 'object') {
rangeObj = value;
} else {
throw new Error('is neither a string or date time range object: ' + value);
}
const lowerBound = rangeObj.lowerInclusive ? '[' : '(';
const upperBound = rangeObj.upperInclusive ? ']' : ')';
const lowerDate = rangeObj.lowerDate?.toISOString() ?? '';
const upperDate = rangeObj.upperDate?.toISOString() ?? '';
return `${lowerBound}"${lowerDate}","${upperDate}"${upperBound}`;
});
62 changes: 62 additions & 0 deletions test/Date Time Range.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
helpers = require './helpers'

helpers.describe 'Date Time Range', (test) ->
describe 'fetchProcessing', ->
test.fetch('["2010-01-01 14:30:00","2010-01-01 15:30:00")', {
lowerInclusive: true,
lowerDate: new Date('2010-01-01 14:30:00'),
upperDate: new Date('2010-01-01 15:30:00'),
upperInclusive: false,
})
test.fetch('("2010-01-01 14:30:00",]', {
lowerInclusive: false,
lowerDate: new Date('2010-01-01 14:30:00'),
upperDate: undefined,
upperInclusive: true,
})
test.fetch('[,"2010-01-01 14:30:00"]', {
lowerInclusive: true,
lowerDate: undefined,
upperDate: new Date('2010-01-01 14:30:00'),
upperInclusive: true,
})
test.validate('[2010-01-01T14:30:00.000Z,2010-01-01T15:30:00.000Z)', {
lowerInclusive: true,
lowerDate: new Date('2010-01-01 14:30:00'),
upperDate: new Date('2010-01-01 15:30:00'),
upperInclusive: false,
})

describe 'validate', ->
test.validate({
lowerInclusive: true,
lowerDate: new Date('2010-01-01 14:30:00'),
upperDate: new Date('2010-01-01 15:30:00'),
upperInclusive: false,
}, true, '["2010-01-01T14:30:00.000Z","2010-01-01T15:30:00.000Z")')
test.validate({
lowerInclusive: false,
lowerDate: new Date('2010-01-01 14:30:00'),
upperDate: undefined,
upperInclusive: true,
}, true, '("2010-01-01T14:30:00.000Z",]')
test.validate({
lowerInclusive: true,
lowerDate: undefined,
upperDate: new Date('2010-01-01 14:30:00'),
upperInclusive: true,
}, true, '[,"2010-01-01T14:30:00.000Z"]')
# test.validate({
# r: 0
# g: 0
# b: 0
# a: 0
# }, true, 0)
# test.validate({
# r: 255
# g: 255
# b: 255
# a: 255
# }, true, -1)
# test.validate(0, true, 0)
# test.validate(-1, true, -1)