Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 7ddd1d5

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(formatters): add dateSeparator Formatter Option
- user can customize and choose a different date separator instead of the default one - example:: this.gridOptions = { formatterOptions: { dateSeparator: '.' }};
1 parent ac3bac5 commit 7ddd1d5

File tree

5 files changed

+107
-21
lines changed

5 files changed

+107
-21
lines changed

src/app/examples/grid-formatter.component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ export class GridFormatterComponent implements OnInit {
4646
},
4747
enableAutoResize: true,
4848
enableCellNavigation: true,
49-
enableExcelCopyBuffer: true
49+
enableExcelCopyBuffer: true,
50+
// you customize the date separator through "formatterOptions"
51+
/*
52+
formatterOptions: {
53+
dateSeparator: '.'
54+
},
55+
*/
5056
};
5157

5258
// mock a dataset
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { getAssociatedDateFormatter, getValueFromParamsOrGridOptions } from '../formatterUtilities';
2+
import { FieldType, Column, GridOption } from '../../models';
3+
4+
describe('formatterUtilities', () => {
5+
const gridStub = {
6+
getOptions: jest.fn()
7+
};
8+
9+
describe('getValueFromParamsOrGridOptions method', () => {
10+
it('should return options found in the Grid Option when not found in Column Definition "params" property', () => {
11+
const gridOptions = { formatterOptions: { minDecimal: 2 } } as GridOption;
12+
const gridSpy = gridStub.getOptions.mockReturnValue(gridOptions);
13+
14+
const output = getValueFromParamsOrGridOptions('minDecimal', {} as Column, gridStub, -1);
15+
16+
expect(gridSpy).toHaveBeenCalled();
17+
expect(output).toBe(2);
18+
});
19+
20+
it('should return options found in the Column Definition "params" even if exist in the Grid Option as well', () => {
21+
const gridOptions = { formatterOptions: { minDecimal: 2 } } as GridOption;
22+
const gridSpy = gridStub.getOptions.mockReturnValue(gridOptions);
23+
24+
const output = getValueFromParamsOrGridOptions('minDecimal', { params: { minDecimal: 3 } } as Column, gridStub, -1);
25+
26+
expect(gridSpy).toHaveBeenCalled();
27+
expect(output).toBe(3);
28+
});
29+
30+
it('should return default value when not found in "params" (columnDef) neither the "formatterOptions" (gridOption)', () => {
31+
const defaultValue = 5;
32+
const output = getValueFromParamsOrGridOptions('minDecimal', { field: 'column1' } as Column, {}, defaultValue);
33+
expect(output).toBe(defaultValue);
34+
});
35+
});
36+
37+
describe('getAssociatedDateFormatter method', () => {
38+
it('should return a Formatter function', () => {
39+
const formatterFn = getAssociatedDateFormatter(FieldType.dateIso, '-');
40+
const isFunction = typeof formatterFn === 'function';
41+
expect(isFunction).toBe(true);
42+
});
43+
44+
it('should return a formatted Date when calling the Formatter function', () => {
45+
const formatterFn = getAssociatedDateFormatter(FieldType.dateIso, '-');
46+
const gridSpy = jest.spyOn(gridStub, 'getOptions');
47+
48+
const output = formatterFn(1, 1, '2002-01-01T00:01:01', { type: FieldType.dateIso } as Column, {}, gridStub);
49+
50+
expect(gridSpy).toHaveBeenCalled();
51+
expect(output).toBe('2002-01-01');
52+
});
53+
54+
it('should return a formatted Date with a different separator when changing setting the "dateSeparator" in "formatterOptions"', () => {
55+
const formatterFn = getAssociatedDateFormatter(FieldType.dateIso, '-');
56+
const gridOptions = { formatterOptions: { dateSeparator: '.' } } as GridOption;
57+
const gridSpy = gridStub.getOptions.mockReturnValue(gridOptions);
58+
59+
const output = formatterFn(1, 1, '2002-01-01T00:01:01', { type: FieldType.dateIso } as Column, {}, gridStub);
60+
61+
expect(gridSpy).toHaveBeenCalled();
62+
expect(output).toBe('2002.01.01');
63+
});
64+
});
65+
});

src/app/modules/angular-slickgrid/formatters/formatterUtilities.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@ export function getValueFromParamsOrGridOptions(optionName: string, columnDef: C
2222
}
2323

2424
/** From a FieldType, return the associated date Formatter */
25-
export function getAssociatedDateFormatter(fieldType: FieldType): Formatter {
26-
const FORMAT = mapMomentDateFormatWithFieldType(fieldType);
25+
export function getAssociatedDateFormatter(fieldType: FieldType, defaultSeparator: string): Formatter {
26+
const defaultDateFormat = mapMomentDateFormatWithFieldType(fieldType);
2727

28-
return (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
29-
const isDateValid = moment(value, FORMAT, false).isValid();
30-
return (value && isDateValid) ? moment(value).format(FORMAT) : value;
28+
return (row: number, cell: number, value: any, columnDef: Column, dataContext: any, grid: any) => {
29+
const gridOptions = ((grid && typeof grid.getOptions === 'function') ? grid.getOptions() : {}) as GridOption;
30+
const customSeparator = gridOptions && gridOptions.formatterOptions && gridOptions.formatterOptions.dateSeparator || defaultSeparator;
31+
32+
const isDateValid = moment(value, defaultDateFormat, false).isValid();
33+
let outputDate = (value && isDateValid) ? moment(value).format(defaultDateFormat) : value;
34+
35+
// user can customize the separator through the "formatterOptions"
36+
// if that is the case we need to replace the default "/" to the new separator
37+
if (outputDate && customSeparator !== defaultSeparator) {
38+
const regex = new RegExp(defaultSeparator, 'ig'); // find separator globally
39+
outputDate = outputDate.replace(regex, customSeparator);
40+
}
41+
42+
return outputDate;
3143
};
3244
}

src/app/modules/angular-slickgrid/formatters/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,40 @@ export const Formatters = {
8282
collectionEditor: collectionEditorFormatter,
8383

8484
/** Takes a Date object and displays it as an ISO Date format (YYYY-MM-DD) */
85-
dateIso: getAssociatedDateFormatter(FieldType.dateIso),
85+
dateIso: getAssociatedDateFormatter(FieldType.dateIso, '-'),
8686

8787
/** Takes a Date object and displays it as an ISO Date+Time format (YYYY-MM-DD HH:mm:ss) */
88-
dateTimeIso: getAssociatedDateFormatter(FieldType.dateTimeIso),
88+
dateTimeIso: getAssociatedDateFormatter(FieldType.dateTimeIso, '-'),
8989

9090
/** Takes a Date object and displays it as an ISO Date+Time (without seconds) format (YYYY-MM-DD HH:mm) */
91-
dateTimeShortIso: getAssociatedDateFormatter(FieldType.dateTimeShortIso),
91+
dateTimeShortIso: getAssociatedDateFormatter(FieldType.dateTimeShortIso, '-'),
9292

9393
/** Takes a Date object and displays it as an ISO Date+Time+(am/pm) format (YYYY-MM-DD h:mm:ss a) */
94-
dateTimeIsoAmPm: getAssociatedDateFormatter(FieldType.dateTimeIsoAmPm),
94+
dateTimeIsoAmPm: getAssociatedDateFormatter(FieldType.dateTimeIsoAmPm, '-'),
9595

9696
/** Takes a Date object and displays it as an Euro Date format (DD/MM/YYYY) */
97-
dateEuro: getAssociatedDateFormatter(FieldType.dateEuro),
97+
dateEuro: getAssociatedDateFormatter(FieldType.dateEuro, '/'),
9898

9999
/** Takes a Date object and displays it as an Euro Date+Time format (DD/MM/YYYY HH:mm:ss) */
100-
dateTimeEuro: getAssociatedDateFormatter(FieldType.dateTimeEuro),
100+
dateTimeEuro: getAssociatedDateFormatter(FieldType.dateTimeEuro, '/'),
101101

102102
/** Takes a Date object and displays it as an Euro Date+Time (without seconds) format (DD/MM/YYYY HH:mm) */
103-
dateTimeShortEuro: getAssociatedDateFormatter(FieldType.dateTimeShortEuro),
103+
dateTimeShortEuro: getAssociatedDateFormatter(FieldType.dateTimeShortEuro, '/'),
104104

105105
/** Takes a Date object and displays it as an Euro Date+Time+(am/pm) format (DD/MM/YYYY hh:mm:ss a) */
106-
dateTimeEuroAmPm: getAssociatedDateFormatter(FieldType.dateTimeEuroAmPm),
106+
dateTimeEuroAmPm: getAssociatedDateFormatter(FieldType.dateTimeEuroAmPm, '/'),
107107

108108
/** Takes a Date object and displays it as an US Date format (MM/DD/YYYY) */
109-
dateUs: getAssociatedDateFormatter(FieldType.dateUs),
109+
dateUs: getAssociatedDateFormatter(FieldType.dateUs, '/'),
110110

111111
/** Takes a Date object and displays it as an US Date+Time format (MM/DD/YYYY HH:mm:ss) */
112-
dateTimeUs: getAssociatedDateFormatter(FieldType.dateTimeUs),
112+
dateTimeUs: getAssociatedDateFormatter(FieldType.dateTimeUs, '/'),
113113

114114
/** Takes a Date object and displays it as an US Date+Time (without seconds) format (MM/DD/YYYY HH:mm:ss) */
115-
dateTimeShortUs: getAssociatedDateFormatter(FieldType.dateTimeShortUs),
115+
dateTimeShortUs: getAssociatedDateFormatter(FieldType.dateTimeShortUs, '/'),
116116

117117
/** Takes a Date object and displays it as an US Date+Time+(am/pm) format (MM/DD/YYYY hh:mm:ss a) */
118-
dateTimeUsAmPm: getAssociatedDateFormatter(FieldType.dateTimeUsAmPm),
118+
dateTimeUsAmPm: getAssociatedDateFormatter(FieldType.dateTimeUsAmPm, '/'),
119119

120120
/** Displays a Font-Awesome delete icon (fa-trash) */
121121
deleteIcon: deleteIconFormatter,
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
export interface FormatterOption {
2-
/** defaults to false, option to display negative numbers in parentheses, example: -$12.50 becomes ($12.50) */
2+
/** What separator to use to display a Date, for example using "." it could be "2002.01.01" */
3+
dateSeparator?: '/' | '-' | '.' | ' ';
4+
5+
/** Defaults to false, option to display negative numbers wrapped in parentheses, example: -$12.50 becomes ($12.50) */
36
displayNegativeNumberWithParentheses?: boolean;
47

5-
/** defaults to undefined, minimum number of decimals */
8+
/** Defaults to undefined, minimum number of decimals */
69
minDecimal?: number;
710

8-
/** defaults to undefined, maximum number of decimals */
11+
/** Defaults to undefined, maximum number of decimals */
912
maxDecimal?: number;
1013
}

0 commit comments

Comments
 (0)