Skip to content

fixing issue #355 #356

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

Merged
merged 1 commit into from
Feb 19, 2018
Merged
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
13 changes: 13 additions & 0 deletions src/app/common/services/utils/utils.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {inject, TestBed} from '@angular/core/testing';
import {UtilsService} from './utils.service';
import * as momentNs from 'moment';
import {IDate} from '../../models/date.model';

const moment = momentNs;

describe('Service: ObUtilsService', () => {
Expand Down Expand Up @@ -116,4 +117,16 @@ describe('Service: ObUtilsService', () => {
expect(service.datesStringToStringArray('14,01-1984| asdasd'))
.toEqual(['14,01-1984', 'asdasd']);
}));

it('check convertToString', inject([UtilsService], (service: UtilsService) => {
const format = 'MM/DD/YYYY';
expect(service.convertToString(null, format)).toEqual('');
expect(service.convertToString('', format)).toEqual('');
expect(service.convertToString(moment(), format)).toEqual(moment().format(format));
expect(service.convertToString([moment()], format)).toEqual(moment().format(format));
expect(service.convertToString([moment(), moment().add(1, 'd')], format))
.toEqual(moment().format(format) + ' | ' + moment().add(1, 'd').format(format));
expect(service.convertToString([moment().format(format), moment().add(1, 'd').format(format)], format))
.toEqual(moment().format(format) + ' | ' + moment().add(1, 'd').format(format));
}));
});
23 changes: 23 additions & 0 deletions src/app/common/services/utils/utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {IDate} from '../../models/date.model';
import {CalendarMode} from '../../types/calendar-mode';
import {DateValidator} from '../../types/validator.type';
import {ICalendarInternal} from '../../models/calendar.model';

const moment = momentNs;

export interface DateLimits {
Expand Down Expand Up @@ -127,6 +128,28 @@ export class UtilsService {
}
}

convertToString(value: CalendarValue, format: string): string {
let tmpVal: string[];

if (typeof value === 'string') {
tmpVal = [value];
} else if (Array.isArray(value)) {
if (value.length) {
tmpVal = (<SingleCalendarValue[]>value).map((v) => {
return this.convertToMoment(v, format).format(format);
});
} else {
tmpVal = <string[]>value;
}
} else if (moment.isMoment(value)) {
tmpVal = [value.format(format)];
} else {
return '';
}

return tmpVal.filter(Boolean).join(' | ');
}

// todo:: add unit test
clearUndefined<T>(obj: T): T {
if (!obj) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/date-picker/date-picker.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Directive: DpDayPicker', () => {
});
});

const directive = new DatePickerDirective(null, null, null, null, null);
const directive = new DatePickerDirective(null, null, null, null, null, null);

it('should create an instance', () => {
expect(directive).toBeTruthy();
Expand Down
8 changes: 6 additions & 2 deletions src/app/date-picker/date-picker.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {NgControl} from '@angular/forms';
import {CalendarValue} from '../common/types/calendar-value';
import {SingleCalendarValue} from '../common/types/single-calendar-value';
import {INavEvent} from '../common/models/navigation-event.model';
import {UtilsService} from '../common/services/utils/utils.service';
import {ECalendarValue} from '../common/types/calendar-value-enum';

@Directive({
exportAs: 'dpDayPicker',
Expand Down Expand Up @@ -164,7 +166,8 @@ export class DatePickerDirective implements OnInit {
public elemRef: ElementRef,
public componentFactoryResolver: ComponentFactoryResolver,
public service: DatePickerDirectiveService,
@Optional() public formControl: NgControl) {
@Optional() public formControl: NgControl,
public utilsService: UtilsService) {
}

ngOnInit(): void {
Expand All @@ -189,7 +192,8 @@ export class DatePickerDirective implements OnInit {

this.formControl.valueChanges.subscribe((value) => {
if (value !== this.datePicker.inputElementValue) {
this.datePicker.onViewDateChange(value);
const strVal = this.utilsService.convertToString(value, this.datePicker.componentConfig.format);
this.datePicker.onViewDateChange(strVal);
}
});

Expand Down
3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
"dom"
]
},
"include": [
"prebuild/app/index.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
Expand Down