Skip to content

Commit

Permalink
unit test deepValueGetter. fix swimlane#596 prop field name with dots.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlowhite authored and Arlo White committed Apr 20, 2017
1 parent 46db440 commit 3da6ae4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
53 changes: 53 additions & 0 deletions src/utils/column-prop-getters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { deepValueGetter } from './column-prop-getters';

describe('deepValueGetter', () => {

it('should get values one level deep', () => {
let data = {
a: {
value: 123
}
};
expect(deepValueGetter(data, 'a.value')).toEqual(123);
});

it('should get values two levels deep', () => {
let data = {
a: {
b: {
value: 'foo'
}
}
};
expect(deepValueGetter(data, 'a.b.value')).toEqual('foo');
});

it('should return empty string on missing nested field', () => {
let data = {
a: {}
};
expect(deepValueGetter(data, 'a.x.value')).toEqual('');
});

it('should return empty string on missing final field', () => {
let data = {
a: {}
};
expect(deepValueGetter(data, 'a.value')).toEqual('');
});

it('should return empty string on missing root field', () => {
let data = {
a: {}
};
expect(deepValueGetter(data, 'x.value')).toEqual('');
});

it('should check for root-level fields with dots in name', () => {
let data = {
"a.b.value": 5
};
expect(deepValueGetter(data, 'a.b.value')).toEqual(5);
});

});
15 changes: 10 additions & 5 deletions src/utils/column-prop-getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ValueGetter = (obj: any, prop: TableColumnProp) => any;
* Always returns the empty string ''
* @returns {string}
*/
export function emptyStringGetter() {
export function emptyStringGetter(): string {
return '';
}

Expand Down Expand Up @@ -37,7 +37,7 @@ export function getterForProp(prop: TableColumnProp): ValueGetter {
* @param index numeric index
* @returns {any} or '' if invalid index
*/
export function numericIndexGetter(row: any[], index: number) {
export function numericIndexGetter(row: any[], index: number): any {
// mimic behavior of deepValueGetter
if (!row || index == null) return row;

Expand All @@ -53,7 +53,7 @@ export function numericIndexGetter(row: any[], index: number) {
* @param fieldName field name string
* @returns {any}
*/
export function shallowValueGetter(obj: object, fieldName: string) {
export function shallowValueGetter(obj: object, fieldName: string): any {
if(!obj || !fieldName) return obj;

const value = obj[fieldName];
Expand All @@ -66,10 +66,15 @@ export function shallowValueGetter(obj: object, fieldName: string) {
* @param {object} obj
* @param {string} path
*/
export function deepValueGetter(obj: object, path: string) {
export function deepValueGetter(obj: object, path: string): any {
if(!obj || !path) return obj;

let current = obj;
// check if path matches a root-level field
// { "a.b.c": 123 }
let current = obj[path];
if (current !== undefined) return current;

current = obj;
const split = path.split('.');

if(split.length) {
Expand Down

0 comments on commit 3da6ae4

Please sign in to comment.