Skip to content

Commit

Permalink
Merge pull request #62 from tschettler/use-edm-models
Browse files Browse the repository at this point in the history
refactor(models): updates data types to use edm models
  • Loading branch information
tschettler authored Mar 8, 2022
2 parents e2e3a0d + 795e3ac commit fefe25b
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 95 deletions.
8 changes: 5 additions & 3 deletions src/datatypes/models/edm-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class EdmDate {
private _year: number;

private constructor(source: Date | string) {
source instanceof Date ? this.parseDate(source) : this.parseString(source);
source instanceof Date ? this.parseDate(source) : this.parseString(source.toString());
}

/**
Expand Down Expand Up @@ -77,7 +77,9 @@ export class EdmDate {
public static create(source: EdmDate | Date | string = new Date()): EdmDate {
const result = source instanceof EdmDate
? source
: new EdmDate(source);
: source === null
? null
: new EdmDate(source);

return result;
}
Expand All @@ -98,7 +100,7 @@ export class EdmDate {
const result = sign + [year, month, day]
.map((x, i) => Math.abs(x).toString().padStart(partLengths[i], '0'))
.join('-');

return result;
}

Expand Down
6 changes: 4 additions & 2 deletions src/datatypes/models/edm-datetimeoffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const DateTimeOffsetRegex = new RegExp(/^(.+)T(.+)((?:\+|-)\d{2}:\d{2}|Z)$/);
*/
export class EdmDateTimeOffset {
protected constructor(source: Date | string) {
const dateString = source instanceof Date ? source.toISOString() : source;
const dateString = source instanceof Date ? source.toISOString() : source.toString();
this.parseDateTime(dateString);
}

Expand Down Expand Up @@ -52,7 +52,9 @@ export class EdmDateTimeOffset {
public static create(source: EdmDateTimeOffset | Date | string = new Date()): EdmDateTimeOffset {
const result = source instanceof EdmDateTimeOffset
? source
: new EdmDateTimeOffset(source);
: source === null
? null
: new EdmDateTimeOffset(source);

return result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/datatypes/models/edm-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class EdmDuration {
private _seconds: number;

private constructor(source: string) {
this.parseString(source);
this.parseString(source.toString());
}

/**
Expand Down Expand Up @@ -145,7 +145,9 @@ export class EdmDuration {
public static create(source: EdmDuration | string = Default): EdmDuration {
const result = source instanceof EdmDuration
? source
: new EdmDuration(source);
: source === null
? null
: new EdmDuration(source);

return result;
}
Expand Down
8 changes: 5 additions & 3 deletions src/datatypes/models/edm-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class EdmOffset {
private _totalMinutes = 0;

private constructor(source: string) {
this.parseString(source);
this.parseString(source.toString());
}

/**
Expand All @@ -30,7 +30,9 @@ export class EdmOffset {
public static create(source: EdmOffset | string = Zulu): EdmOffset {
const result = source instanceof EdmOffset
? source
: new EdmOffset(source);
: source === null
? null
: new EdmOffset(source);

return result;
}
Expand Down Expand Up @@ -67,7 +69,7 @@ export class EdmOffset {
const result = this.sign + [this.hour, this.minute]
.map((x, i) => Math.abs(x).toString().padStart(partLengths[i], '0'))
.join(':');

return result;
}

Expand Down
8 changes: 5 additions & 3 deletions src/datatypes/models/edm-timeofday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class EdmTimeOfDay {
private _seconds: number;

private constructor(source: Date | string) {
source instanceof Date ? this.parseDate(source) : this.parseString(source);
source instanceof Date ? this.parseDate(source) : this.parseString(source.toString());
}

/**
Expand All @@ -35,7 +35,9 @@ export class EdmTimeOfDay {
public static create(source: EdmTimeOfDay | Date | string = '00:00'): EdmTimeOfDay {
const result = source instanceof EdmTimeOfDay
? source
: new EdmTimeOfDay(source);
: source === null
? null
: new EdmTimeOfDay(source);

return result;
}
Expand Down Expand Up @@ -118,7 +120,7 @@ export class EdmTimeOfDay {
const result = sign + [hours, minutes, seconds]
.map((x, i) => Math.abs(x).toString().padStart(partLengths[i], '0'))
.join(':')
+ Math.abs(fractionalSeconds).toString().substring(1);
+ (fractionalSeconds ? Math.abs(fractionalSeconds).toString().substring(1) : '.').padEnd(4, '0');

return result;
}
Expand Down
30 changes: 4 additions & 26 deletions src/datatypes/setups/date-datatype-setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataType } from 'breeze-client';
import { EdmDate } from '../models';

import { BaseDataTypeSetup } from './base-datatype-setup';

Expand All @@ -13,36 +14,13 @@ const DateRegex = /(-\d)?\d{4}-\d{2}-\d{2}/;
export class DateDataTypeSetup extends BaseDataTypeSetup {
public name = 'Date';

private parseValue = (val: any) => {
if (!val) {
return null;
}

let result = val;
if (typeof val === 'string') {
const matchResult = DateRegex.exec(val);
if (matchResult) {
return matchResult[0];
}
}

const dateVal = val instanceof Date ? val : new Date(`${val} `);
if (isNaN(dateVal as any)) {
this.handleInvalidValue(val);
}

result = dateVal.toISOString().split('T')[0];

return result;
}

public addSymbol = () => {
const result = new DataType({
defaultValue: '0000-01-01',
name: this.name,
fmtOData: (val: any) => val ? EdmDate.create(val).toString() : null,
isDate: true,
fmtOData: this.parseValue,
parseRawValue: this.parseValue
name: this.name,
parseRawValue: EdmDate.create
});

DataType[this.name] = result;
Expand Down
11 changes: 3 additions & 8 deletions src/datatypes/setups/datetimeoffset-datatype-setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EdmDateTimeOffset } from '../models';
import { BaseDataTypeSetup } from './base-datatype-setup';

/**
Expand All @@ -8,14 +9,8 @@ export class DateTimeOffsetDataTypeSetup extends BaseDataTypeSetup {
public name = 'DateTimeOffset';

public fmtOData = (val: any) => {
if (!val) {
return null;
}
const result = val ? EdmDateTimeOffset.create(val).toString() : null;

try {
return val.toISOString();
} catch (e) {
this.handleInvalidValue(val);
}
return result;
}
}
24 changes: 8 additions & 16 deletions src/datatypes/setups/duration-datatype-setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { core, DataType } from 'breeze-client';
import { EdmDuration } from '../models';

import { BaseDataTypeSetup } from './base-datatype-setup';

Expand All @@ -8,23 +9,14 @@ import { BaseDataTypeSetup } from './base-datatype-setup';
export class DurationDataTypeSetup extends BaseDataTypeSetup {
public name = 'Duration';

public fmtOData = (val: any) => {
if (!val) {
return null;
}

if (!core.isDuration(val)) {
throw new Error(`${val} is not a valid ISO 8601 duration`);
}

return val;
}

public addSymbol = () => {
const result = new DataType({...DataType.Time,

name: this.name,
parseRawValue: DataType.parseTimeFromServer});
const result = new DataType({
...DataType.Time,
fmtOData: (val: any) => val ? EdmDuration.create(val).toString() : null,
name: this.name,
parseRawValue: EdmDuration.create
});

DataType[this.name] = result;

return result;
Expand Down
5 changes: 3 additions & 2 deletions src/datatypes/setups/timeofday-datatype-setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataType, Validator } from 'breeze-client';
import { EdmTimeOfDay } from '../models';

import { BaseDataTypeSetup } from './base-datatype-setup';

Expand All @@ -11,9 +12,9 @@ export class TimeOfDayDataTypeSetup extends BaseDataTypeSetup {
public addSymbol = () => {
const result = new DataType({
defaultValue: '00:00',
fmtOData: DataType.String.fmtOData,
parse: DataType.String.parse,
fmtOData: (val: any) => val ? EdmTimeOfDay.create(val).toString() : null,
name: this.name,
parseRawValue: EdmTimeOfDay.create,
validatorCtor: Validator.string
});

Expand Down
2 changes: 1 addition & 1 deletion tests/breeze-odata4.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('BreezeOData4', () => {

expect(() => {
DataType.DateTimeOffset.fmtOData(123.45);
}).toThrowError('is not a valid DateTimeOffset');
}).toThrowError('\'123.45\' is not a valid EdmDateTimeOffset');
});

it('should return null when calling Time.fmtOData for null', () => {
Expand Down
10 changes: 9 additions & 1 deletion tests/datatypes/models/edm-date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ describe('EdmDate', () => {
expect(result).toBeInstanceOf(EdmDate);
});

it('should return null with null', () => {
const input: string = null;

const result = EdmDate.create(input);

expect(result).toBeNull();
});

it('should throw error with invalid date string', () => {
const input = 'abc';

Expand Down Expand Up @@ -182,7 +190,7 @@ describe('EdmDate', () => {
input: new Date('2022-11-23'),
expected: '2022-11-23'
},
{
{
name: 'should return correct value with string',
input: '2022-11-23',
expected: '2022-11-23'
Expand Down
12 changes: 10 additions & 2 deletions tests/datatypes/models/edm-datetimeoffset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ describe('EdmDateTimeOffset', () => {
expect(result).toBeInstanceOf(EdmDateTimeOffset);
});

it('should return null with null', () => {
const input: string = null;

const result = EdmDateTimeOffset.create(input);

expect(result).toBeNull();
});

it('should throw error with invalid date string', () => {
const input = 'abc';

Expand Down Expand Up @@ -77,7 +85,7 @@ describe('EdmDateTimeOffset', () => {
const testCases = [
{
input: '0001-01-01T00:00:00.000Z',
expected: '0001-01-01T00:00:00Z'
expected: '0001-01-01T00:00:00.000Z'
},
{
input: '9999-12-31T23:59:59.999999999999Z',
Expand Down Expand Up @@ -141,7 +149,7 @@ describe('EdmDateTimeOffset', () => {

it('toString should return correct value', () => {
const result = sut.toString();
expect(result).toEqual('0001-01-01T00:00:00Z');
expect(result).toEqual('0001-01-01T00:00:00.000Z');
});
});

Expand Down
9 changes: 9 additions & 0 deletions tests/datatypes/models/edm-offset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ describe('EdmOffset', () => {
expect(() => EdmOffset.create(input)).toThrowError(`'${input}' is not a valid EdmOffset`);
});


it('should return null with null', () => {
const input: string = null;

const result = EdmOffset.create(input);

expect(result).toBeNull();
});

it('should throw error with invalid string', () => {
const input = 'abc';

Expand Down
Loading

1 comment on commit fefe25b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 100% 1396/1396
🟢 Branches 100% 517/517
🟢 Functions 100% 366/366
🟢 Lines 100% 1357/1357

Test suite run success

765 tests passing in 40 suites.

Report generated by 🧪jest coverage report action from fefe25b

Please sign in to comment.