npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flourish/transform-data

v2.1.0

Published

Reshape data

Downloads

6,017

Readme

Transform data

This module exports a collection of functions for transforming data (arrays of objects and arrays of arrays) that may be useful either in the visualisation editor or when using the API.

pivotObjects(input_data, headers_column, values_column)

Takes an array of objects, input_data, and two strings that should refer to properties in the input_data object. The output data is then a pivotted version of input_data with properties from the headers_column and values from the values_column. This is similar to the Excel pivot table operation and will generally turn "long" data into "wide data". It also adds a column_names property to the output array that is useful with the Flourish API.

pivotArrays(input_data, headers_index, values_index)

Takes an array of arrays, input_data, and two numbers that should refer to indexes within each inner array. The first element of input_data should contain the headers. The output data is then a pivotted version of input_data with properties from the headers_index entries and values from the values_index entries. This is similar to the Excel pivot table operation and will generally turn "long" data into "wide data".

meltObjects(input_data, melt_columns, opts)

Takes an array of objects, input_data, and an array of property names, melt_columns. The melt_columns are then used to create multiple rows for each previous row. This is the reverse operation of pivotObjects and will generally turn "wide data" into "long data". It also adds a column_names property to the output array that is useful with the Flourish API. The opts argument, if present, should be an object that can have up to three (useful) properties:

  • variable_name: The name applied to the new column based on the melt_columns properties (headers);
  • value_name: The name applied to the new column based on the melt_columns values;
  • keep_empty_strings: If true, keeps rows that would otherwise by ignored because the associated value is an empty string.

meltArrays(input_data, melt_columns, opts)

Takes an array of arrays, input_data, and an array of indexes, melt_columns. The melt_columns are then used to create multiple rows for each previous row. The first element of input_data should contain the headers. This is the reverse operation of pivotArrays and will generally turn "wide data" into "long data". The opts argument, if present, should be an object that can have up to three (useful) properties:

  • variable_name: The name given to the new column based on the melt_columns properties (headers);
  • value_name: The name given to the new column based on the melt_columns values;
  • keep_empty_strings: If true, keeps rows that would otherwise by ignored because the associated value is an empty string.

flourishifyObjects(input_data, column_bindings, columns_bindings)

Takes an array of objects, input_data, and up to two objects, column_bindings and columns_bindings. These two objects then act as dictionaries to transform the input_data into output data that better matches the structure used in a Flourish template. Each key of the column_bindings object should match a key of type: column in the template and the values should match keys in the input data. Similarly, each key of the columns_bindings should match a key in the template of type: columns while the values should be arrays whose elements match keys in the input data (you can also use a string in place of an array if you only want to map one column to a given key). This function will also add a column_names property to the output array.

flourishifyArrays(input_data, column_bindings, columns_bindings)

Takes an array of arrays, input_data, and up to two objects, column_bindings and columns_bindings. These two objects then act as dictionaries to transform the input_data into an array of objects that better matches the structure used in a Flourish template and by the API. Each key of the column_bindings object should match a key of type: column in the template and the values should match indexes in the input data. Similarly, each key of the columns_bindings should match a key in the template of type: columns while the values should be arrays whose elements match indexes in the input data (you can also use a single index in place of an array if you only want to map one column to a given key). This function will also add a column_names property to the output array.

flourishify(input_data, column_bindings, columns_bindings)

Calls and returns the results of either flourishifyArrays or flourishifyObjects (passing in all arguments) based on whether input_data[0] is an array or not.

leftJoin(left_data, right_data, [options])

Expects two arrays of arrays - left_data and right_data - plus options and returns an array of arrays that is the left join of left_data and right_data. options properties are the left_index for the left_data column to join on (defaults to the last column in left_data), right_index for the right_data column to join on (defaults to the first column, index 0, in right_data), left_keep for whether to keep the left_index column in the merged data (defaults to true) and right_keep for whether to keep the right_index column in the merged data (defaults to true).

rightJoin(left_data, right_data, [options])

Expects two arrays of arrays - left_data and right_data - plus options and returns an array of arrays that is the right outer join of left_data and right_data. options properties are the left_index for the left_data column to join on (defaults to the last column in left_data), right_index for the right_data column to join on (defaults to the first column, index 0, in right_data), left_keep for whether to keep the left_index column in the merged data (defaults to true) and right_keep for whether to keep the right_index column in the merged data (defaults to true).

fullJoin(left_data, right_data, [options])

Expects two arrays of arrays - left_data and right_data - plus options and returns an array of arrays that is the full outer join of left_data and right_data. options properties are the left_index for the left_data column to join on (defaults to the last column in left_data), right_index for the right_data column to join on (defaults to the first column, index 0, in right_data), left_keep for whether to keep the left_index column in the merged data (defaults to true) and right_keep for whether to keep the right_index column in the merged data (defaults to true).

innerJoin(left_data, right_data, [options])

Expects two arrays of arrays - left_data and right_data- plus options and returns an array of arrays that is the inner join of left_data and right_data. options properties are the left_index for the left_data column to join on (defaults to the last column in left_data), right_index for the right_data column to join on (defaults to the first column, index 0, in right_data), left_keep for whether to keep the left_index column in the merged data (defaults to true) and right_keep for whether to keep the right_index column in the merged data (defaults to true).

*Join.getLastUnmatched()

The four *Join methods all have an attached getLastUnmatched method. These methods return an unmatched object giving information on rows that had no matches the last time the parent function was invoked. The unmatched object contains two properties: left and right whose values are arrays of objects. Each object in the left or right array represents an unmatched row from the appropriate left_data or right_data dataset. The properties of these objects are input_row (the row index in the input data), output_row (the row index in the output data or null if it does not appear) and the value that had no match.