Skip to content
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

fix(utilities): move findOrDefault to a function #29

Merged
merged 3 commits into from
Mar 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Column,
MultipleSelectOption,
SelectOption
} from './../models/index';
} from '../models/index';
import { findOrDefault } from '../services';
import * as $ from 'jquery';

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ export class SingleSelectEditor implements Editor {
* The current selected value from the collection
*/
get currentValue() {
return this.collection.findOrDefault(c =>
return findOrDefault(this.collection, (c: any) =>
c[this.valueName].toString() === this.$editorElm.val())[this.valueName];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { arrayToCsvFormatter } from './arrayToCsvFormatter';
import { Column, Formatter } from './../models/index';
import { findOrDefault } from '../services';

/**
* A formatter to show the label property value of a filter.collection
* A formatter to show the label property value of a params collection
*/
export const collectionFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
if (!value || !columnDef || !columnDef.filter || !columnDef.filter.collection
|| !columnDef.filter.collection.length) {
if (!value || !columnDef || !columnDef.params || !columnDef.params.collection
|| !columnDef.params.collection.length) {
return '';
}

const { filter, filter: { collection } } = columnDef;
const labelName = (filter.customStructure) ? filter.customStructure.label : 'label';
const valueName = (filter.customStructure) ? filter.customStructure.value : 'value';
const { params, params: { collection } } = columnDef;
const labelName = (params.customStructure) ? params.customStructure.label : 'label';
const valueName = (params.customStructure) ? params.customStructure.value : 'value';

if (Array.isArray(value)) {
return arrayToCsvFormatter(row,
cell,
value.map((v: any) => collection.findOrDefault((c: any) => c[valueName] === v)[labelName]),
value.map((v: any) => findOrDefault(collection, (c: any) => c[valueName] === v)[labelName]),
columnDef,
dataContext);
}

return collection.findOrDefault((c: any) => c[valueName] === value)[labelName] || '';
return findOrDefault(collection, (c: any) => c[valueName] === value)[labelName] || '';
};
12 changes: 11 additions & 1 deletion aurelia-slickgrid/src/aurelia-slickgrid/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ export const Formatters = {
/** Takes a complex data object and return the data under that property (for example: "user.firstName" will return the first name "John") */
complexObject: complexObjectFormatter,

/** Looks up values from the filter.collection property and convert it to a CSV or string */
/**
* Looks up values from the columnDefinition.params.collection property and displays the label in CSV or string format
* @example
* // the grid will display 'foo' and 'bar' and not 1 and 2 from your dataset
* {
* params: {
* collection: [{ value: 1, label: 'foo'}, {value: 2, label: 'bar' }]
* }
* }
* const dataset = [{ value: 1 },{ value: 2 }];
*/
collection: collectionFormatter,

/** Takes a Date object and displays it as an ISO Date format */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ declare interface StringConstructor {
titleCase(inputStr: string): string;
}

declare interface Array<T> {
findOrDefault(logic: (item: any) => boolean): {};
}

String.format = (format: string, ...args): string => {
// const args = (Array.isArray(arguments[1])) ? arguments[1] : Array.prototype.slice.call(arguments, 1);

Expand Down Expand Up @@ -45,14 +41,3 @@ String.allTitleCase = (inputStr: string): string => {
String.titleCase = (inputStr: string): string => {
return inputStr.charAt(0).toUpperCase() + inputStr.slice(1);
};

/**
* Uses the logic function to find an item in an array or returns the default
* value provided (empty object by default)
* @param function logic the logic to find the item
* @param any [defaultVal={}] the default value to return
* @return object the found object or deafult value
*/
Array.prototype.findOrDefault = function(logic: (item: any) => boolean, defaultVal = {}): any {
return this.find(logic) || defaultVal;
};
12 changes: 12 additions & 0 deletions aurelia-slickgrid/src/aurelia-slickgrid/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,15 @@ export function arraysEqual(a: any[], b: any[], orderMatters: boolean = false):

return true;
}

/**
* Uses the logic function to find an item in an array or returns the default
* value provided (empty object by default)
* @param any[] array the array to filter
* @param function logic the logic to find the item
* @param any [defaultVal={}] the default value to return
* @return object the found object or deafult value
*/
export function findOrDefault(array: any[], logic: (item: any) => boolean, defaultVal = {}): any {
return array.find(logic) || defaultVal;
}
2 changes: 1 addition & 1 deletion aurelia-slickgrid/src/examples/slickgrid/example3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class Example3 {
minWidth: 100,
params: {
formatters: [ Formatters.collection, Formatters.percentCompleteBar ],
collection: Array.from(Array(101).keys()).map(k => ({ value: k, label: `${k}%` }))
collection: Array.from(Array(101).keys()).map(k => ({ value: k, label: k }))
}
}, {
id: 'start',
Expand Down