Skip to content

Commit

Permalink
feat(formatters): add AlignRight Formatter & alias AlignCenter=>Center (
Browse files Browse the repository at this point in the history
#161)

* feat(formatters): add AlignRight Formatter & alias AlignCenter=>Center
- center already existed but we can add alignCenter to be in line with the other alignRight formatter
- note also that there's no need for alignLeft since everything is aligned to the left in SlickGrid
  • Loading branch information
ghiscoding authored Nov 17, 2020
1 parent 7b7202b commit 831580d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Column } from '../../interfaces/index';
import { alignRightFormatter } from '../alignRightFormatter';

describe('Right Alignment Formatter', () => {
it('should return an empty string when no value is passed', () => {
const output = alignRightFormatter(1, 1, '', {} as Column, {});
expect(output).toBe('<div style="float: right"></div>');
});

it('should return an empty string when value is null or undefined', () => {
const output1 = alignRightFormatter(1, 1, null, {} as Column, {});
const output2 = alignRightFormatter(1, 1, undefined, {} as Column, {});

expect(output1).toBe('<div style="float: right"></div>');
expect(output2).toBe('<div style="float: right"></div>');
});

it('should return a string all in uppercase', () => {
const output = alignRightFormatter(1, 1, 'hello', {} as Column, {});
expect(output).toBe('<div style="float: right">hello</div>');
});

it('should return a number as a string', () => {
const output = alignRightFormatter(1, 1, 99, {} as Column, {});
expect(output).toBe('<div style="float: right">99</div>');
});

it('should return a boolean as a string all in uppercase', () => {
const output = alignRightFormatter(1, 1, false, {} as Column, {});
expect(output).toBe('<div style="float: right">false</div>');
});
});
11 changes: 10 additions & 1 deletion packages/common/src/formatters/__tests__/centerFormatter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Column } from '../../interfaces/index';
import { centerFormatter } from '../centerFormatter';

describe('the Uppercase Formatter', () => {
describe('Center Alignment Formatter', () => {
it('should return an empty string when no value is passed', () => {
const output = centerFormatter(1, 1, '', {} as Column, {});
expect(output).toBe('<center></center>');
});

it('should return an empty string when value is null or undefined', () => {
const output1 = centerFormatter(1, 1, null, {} as Column, {});
const output2 = centerFormatter(1, 1, undefined, {} as Column, {});

expect(output1).toBe('<center></center>');
expect(output2).toBe('<center></center>');
});


it('should return a string all in uppercase', () => {
const output = centerFormatter(1, 1, 'hello', {} as Column, {});
expect(output).toBe('<center>hello</center>');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const gridStub = {
getOptions: jest.fn(),
} as unknown as SlickGrid;

describe('the Uppercase Formatter', () => {
describe('Tree Formatter', () => {
let dataset;
let mockGridOptions: GridOption;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Column } from '../../interfaces/index';
import { yesNoFormatter } from '../yesNoFormatter';

describe('the Uppercase Formatter', () => {
describe('Yes/No Formatter', () => {
it('should return a "Yes" string when value is passed', () => {
const output = yesNoFormatter(1, 1, 'blah', {} as Column, {});
expect(output).toBe('Yes');
Expand Down
10 changes: 10 additions & 0 deletions packages/common/src/formatters/alignRightFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Formatter } from './../interfaces/index';

export const alignRightFormatter: Formatter = (_row: number, _cell: number, value: string | any): string => {
let outputValue = value;

if (value === null || value === undefined) {
outputValue = '';
}
return `<div style="float: right">${outputValue}</div>`;
};
9 changes: 5 additions & 4 deletions packages/common/src/formatters/centerFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Formatter } from './../interfaces/index';

export const centerFormatter: Formatter = (_row: number, _cell: number, value: string | any): string => {
// make sure the value is a string
if (value !== undefined && typeof value !== 'string') {
value = value + '';
let outputValue = value;

if (value === null || value === undefined) {
outputValue = '';
}
return `<center>${value}</center>`;
return `<center>${outputValue}</center>`;
};
7 changes: 7 additions & 0 deletions packages/common/src/formatters/formatters.index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FieldType } from '../enums/index';
import { getAssociatedDateFormatter } from './formatterUtilities';
import { alignRightFormatter } from './alignRightFormatter';
import { arrayObjectToCsvFormatter } from './arrayObjectToCsvFormatter';
import { arrayToCsvFormatter } from './arrayToCsvFormatter';
import { boldFormatter } from './boldFormatter';
Expand Down Expand Up @@ -38,6 +39,12 @@ import { yesNoFormatter } from './yesNoFormatter';

/** Provides a list of different Formatters that will change the cell value displayed in the UI */
export const Formatters = {
/** Align cell value to the center (alias to Formatters.center) */
alignCenter: centerFormatter,

/** Align cell value to the right */
alignRight: alignRightFormatter,

/**
* Takes an array of complex objects converts it to a comma delimited string.
* Requires to pass an array of "propertyNames" in the column definition the generic "params" property
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './alignRightFormatter';
export * from './arrayObjectToCsvFormatter';
export * from './arrayToCsvFormatter';
export * from './boldFormatter';
Expand Down

0 comments on commit 831580d

Please sign in to comment.