-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-type: minor
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |