Skip to content

Commit

Permalink
Add 'Date Time Range' type
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
Page- committed Jul 1, 2020
1 parent 07981e2 commit ffcb476
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
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 @@ -5,6 +5,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 Down Expand Up @@ -40,6 +41,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)

0 comments on commit ffcb476

Please sign in to comment.