Skip to content

Commit 19cee2d

Browse files
authored
fixing issue #355 (#356)
1 parent 09d8450 commit 19cee2d

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

src/app/common/services/utils/utils.service.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {inject, TestBed} from '@angular/core/testing';
22
import {UtilsService} from './utils.service';
33
import * as momentNs from 'moment';
44
import {IDate} from '../../models/date.model';
5+
56
const moment = momentNs;
67

78
describe('Service: ObUtilsService', () => {
@@ -116,4 +117,16 @@ describe('Service: ObUtilsService', () => {
116117
expect(service.datesStringToStringArray('14,01-1984| asdasd'))
117118
.toEqual(['14,01-1984', 'asdasd']);
118119
}));
120+
121+
it('check convertToString', inject([UtilsService], (service: UtilsService) => {
122+
const format = 'MM/DD/YYYY';
123+
expect(service.convertToString(null, format)).toEqual('');
124+
expect(service.convertToString('', format)).toEqual('');
125+
expect(service.convertToString(moment(), format)).toEqual(moment().format(format));
126+
expect(service.convertToString([moment()], format)).toEqual(moment().format(format));
127+
expect(service.convertToString([moment(), moment().add(1, 'd')], format))
128+
.toEqual(moment().format(format) + ' | ' + moment().add(1, 'd').format(format));
129+
expect(service.convertToString([moment().format(format), moment().add(1, 'd').format(format)], format))
130+
.toEqual(moment().format(format) + ' | ' + moment().add(1, 'd').format(format));
131+
}));
119132
});

src/app/common/services/utils/utils.service.ts

+23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {IDate} from '../../models/date.model';
88
import {CalendarMode} from '../../types/calendar-mode';
99
import {DateValidator} from '../../types/validator.type';
1010
import {ICalendarInternal} from '../../models/calendar.model';
11+
1112
const moment = momentNs;
1213

1314
export interface DateLimits {
@@ -127,6 +128,28 @@ export class UtilsService {
127128
}
128129
}
129130

131+
convertToString(value: CalendarValue, format: string): string {
132+
let tmpVal: string[];
133+
134+
if (typeof value === 'string') {
135+
tmpVal = [value];
136+
} else if (Array.isArray(value)) {
137+
if (value.length) {
138+
tmpVal = (<SingleCalendarValue[]>value).map((v) => {
139+
return this.convertToMoment(v, format).format(format);
140+
});
141+
} else {
142+
tmpVal = <string[]>value;
143+
}
144+
} else if (moment.isMoment(value)) {
145+
tmpVal = [value.format(format)];
146+
} else {
147+
return '';
148+
}
149+
150+
return tmpVal.filter(Boolean).join(' | ');
151+
}
152+
130153
// todo:: add unit test
131154
clearUndefined<T>(obj: T): T {
132155
if (!obj) {

src/app/date-picker/date-picker.directive.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Directive: DpDayPicker', () => {
1717
});
1818
});
1919

20-
const directive = new DatePickerDirective(null, null, null, null, null);
20+
const directive = new DatePickerDirective(null, null, null, null, null, null);
2121

2222
it('should create an instance', () => {
2323
expect(directive).toBeTruthy();

src/app/date-picker/date-picker.directive.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {NgControl} from '@angular/forms';
1919
import {CalendarValue} from '../common/types/calendar-value';
2020
import {SingleCalendarValue} from '../common/types/single-calendar-value';
2121
import {INavEvent} from '../common/models/navigation-event.model';
22+
import {UtilsService} from '../common/services/utils/utils.service';
23+
import {ECalendarValue} from '../common/types/calendar-value-enum';
2224

2325
@Directive({
2426
exportAs: 'dpDayPicker',
@@ -164,7 +166,8 @@ export class DatePickerDirective implements OnInit {
164166
public elemRef: ElementRef,
165167
public componentFactoryResolver: ComponentFactoryResolver,
166168
public service: DatePickerDirectiveService,
167-
@Optional() public formControl: NgControl) {
169+
@Optional() public formControl: NgControl,
170+
public utilsService: UtilsService) {
168171
}
169172

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

190193
this.formControl.valueChanges.subscribe((value) => {
191194
if (value !== this.datePicker.inputElementValue) {
192-
this.datePicker.onViewDateChange(value);
195+
const strVal = this.utilsService.convertToString(value, this.datePicker.componentConfig.format);
196+
this.datePicker.onViewDateChange(strVal);
193197
}
194198
});
195199

tsconfig.json

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
"dom"
1818
]
1919
},
20-
"include": [
21-
"prebuild/app/index.ts"
22-
],
2320
"exclude": [
2421
"node_modules",
2522
"**/*.spec.ts"

0 commit comments

Comments
 (0)