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

Code comments for tools.ts #416

Merged
merged 6 commits into from
Apr 23, 2020
Merged
Changes from 2 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
37 changes: 24 additions & 13 deletions packages/core/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export namespace Tools {
}

/**
* Returns an elements's x and y translations from attribute transform
* Gets elements's x and y translations from transform attribute or returns null
*
* @param {HTMLElement} element
* @returns an object containing the x and y translations or null
* @returns an object containing the translated x and y values or null
*/
export function getTranslationValues(elementRef: HTMLElement) {
// regex to ONLY get values for translate (instead of all rotate, translate, skew, etc)
Expand All @@ -64,7 +65,7 @@ export namespace Tools {
*************************************/

/**
* Gets x and y coordinates from a HTML transform attribute
* Gets x and y coordinates from HTML transform attribute
*
* @export
* @param {any} string the transform attribute string ie. transform(x,y)
Expand All @@ -85,20 +86,20 @@ export namespace Tools {
* Capitalizes first letter of a string
*
* @export
* @param {any} string the string whose first letter you'd like to capitalize
* @returns The input string with its first letter capitalized
* @param {any} string the input string to perform first letter capitalization with
* @returns The transformed string after first letter is capitalized
*/
export function capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}

/**
* Get the percentage of a datapoint compared to the entire data-set.
* Returns 1 significant digit if percentage is less than 1%.
*
* @export
* @param {any} item
* @param {any} fullData
* @returns The percentage in the form of a number
* @returns The percentage in the form of a number (1 significant digit if percentage is less than 1%.)
*/
export function convertValueToPercentage(item, fullData) {
const percentage = item / fullData.reduce((accum, val) => accum + val.value, 0) * 100;
Expand All @@ -108,13 +109,15 @@ export namespace Tools {
/**************************************
* Object/array related checks *
*************************************/

/**
* Get the difference between two arrays' items
* Compares two arrays to return the difference between two arrays' items.
*
* @export
* @param {any[]} oldArray
* @param {any[]} newArray
* @returns The items missing in newArray from oldArray, and items added to newArray compared to oldArray
* @param {any[]} oldArray the array to check for missing items
* @param {any[]} newArray the array to check for newly added items
* @returns An object containing items missing (existing in oldArray but not newArray)
* and items added (existing in newArray but not in oldArray). Object is of the form { missing: [], added: [] }
*/
export function arrayDifferences(oldArray: any[], newArray: any[]) {
const difference = {
Expand All @@ -138,7 +141,7 @@ export namespace Tools {
}

/**
* Lists out the duplicated keys in an array of data
* Gets the duplicated keys from an array of data
*
* @export
* @param {*} data - array of data
Expand All @@ -162,11 +165,12 @@ export namespace Tools {
// ================================================================================
// D3 Extensions
// ================================================================================

/**
* In D3, moves an element to the front of the canvas
*
* @export
* @param {any} element
* @param {any} element input element to moved in front
* @returns The function to be used by D3 to push element to the top of the canvas
*/
export function moveToFront(element) {
Expand All @@ -179,6 +183,13 @@ export namespace Tools {
// Style Helpers
// ================================================================================

/**
* Gets a speicified property from within an object.
*
* @param object the object containing the property to retrieve
* @param propPath nested properties used to extract the final property from within the object
* (i.e "style", "color" would retrieve the color property from within an object that has "color" nested within "style")
*/
export const getProperty = (object, ...propPath) => {
let position = object;
if (position) {
Expand Down