Skip to content
Permalink
Browse files

feat(datatable): normalize keys for displayValue

accepts now any combination of dot-seperated strings and arrays of those
  • Loading branch information...
doktordirk committed Jul 21, 2016
1 parent 54833ec commit 86fe55949016425e44a2efdfdaed92b501250a20
Showing with 22 additions and 3 deletions.
  1. +22 −3 src/datatable.js
@@ -217,16 +217,35 @@ export class DataTable {
}
}

displayValue(row, propertyName) {
return fetchFrom(row, propertyName);
displayValue(row, ...propertyName) {
return fetchFrom(row, normalizeKey(...propertyName));
}
}


/**
* Used to normalize keys of mixed array and dot-separated string to a single array of undotted strings
* @param {string|Array<keys>} keys (dot-separated) string(s) or array of keys
* @param {Array} ...rest Rest of the arguments
* @return {Array} that The key normalized to an array of strings
*/
function normalizeKey(key, ...rest) {
/*
* First, we split the arguments in key and rest
* then, if key is an array, we need to normalize it as well
* else it can be split into an array directly
* if the rest was empty, we're done
* if not we concat our normalized key with the normalized rest
*/
let normalized = Array.isArray(key) ? normalizeKey(...key) : key.split('.');
return rest.length === 0 ? normalized : normalized.concat(normalizeKey(...rest));
}


/**
* Fetches value from (nested) object with a normalized key
* @param {Object} data The data to fetch data from
* @param {string|Array<keys>} keys (dot-separated) string(s) or array of keys
* @param {string|Array<keys>} keys string or array of keys
* @param {Array<key>} ...rest Rest of the arguments
* @return {any} that The retrieved value from the data
*/

0 comments on commit 86fe559

Please sign in to comment.
You can’t perform that action at this time.