-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from pat310/28filteringSorting
28filtering sorting
- Loading branch information
Showing
31 changed files
with
2,155 additions
and
1,345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Construct an object of field names with the unique values for each field | ||
* @param {!Array<Object>} data | ||
* @returns {!Object} Keys are field names, each value is an object with the unique values as keys | ||
*/ | ||
export function createUniqueValues(data) { | ||
return data.reduce((acc, curr) => { | ||
Object.keys(curr).forEach((fieldName) => { | ||
if (!acc[fieldName]) acc[fieldName] = {}; | ||
/** by using an object instead of an array, we avoid extra work checking the array for duplicates*/ | ||
acc[fieldName][curr[fieldName]] = true; | ||
}); | ||
|
||
return acc; | ||
}, {}); | ||
}; | ||
|
||
/** | ||
* Filter a provided data set | ||
* @param {!Array<Object>} data | ||
* @param {string|function} fieldName Callback function or a field name | ||
* @param {Array<string>} filterValues Values to perform filter on | ||
* @param {string} filterType Enumerated string either 'include' or 'exclude' | ||
* @returns {!Array<Object>} filtered data set | ||
*/ | ||
export function filter(data, fieldName, filterValues, filterType) { | ||
/** check that fieldName isn't actually a callback */ | ||
if (typeof fieldName !== 'function') { | ||
/** filter out the data set based on the provided fieldName and filterValues */ | ||
return data.filter(({[fieldName]: value}) => { | ||
switch (filterType) { | ||
case ('include'): { | ||
return filterValues.indexOf(value) !== -1; | ||
} | ||
|
||
case ('exclude'): { | ||
return filterValues.indexOf(value) === -1; | ||
} | ||
|
||
default: { | ||
return filterValues.indexOf(value) === -1; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** if custom callback provided, filter using the callback */ | ||
return data.filter((dataRow, index, array) => { | ||
return fieldName(dataRow, index, array); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.