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

object2ext

v0.0.4

Published

a small toolkit to converts js array of objects to other extension format most importantly. you can save to disk or return as string

Downloads

3

Readme

status latest release status

Convert array of objects into a CSV file

Converts an array of JavaScript objects into the CSV and XLS. You can save the files or return them as string.

The keys in the first object of the array will be used as column names.

Any special characters in the values (such as commas) will be properly escaped.

conversion to excel format is coming soon.

Usage

const Object2Ext = require("object2ext");

// Sample data - two columns, three rows:
const tests = [
  {
    id: 56,
    name: "taro",
    designId: 1,
    designName: "design1",
  },
  {
    id: 1,
    name: "taro",
    designId: 2,
    designName: "design2",
  },
];

// If you use "await", code must be inside an asynchronous function:
(async () => {
  const csv = new Object2Ext(tests);

  // Save to file:
  await csv.toDisk("./tests.csv");

  // Return the CSV file as string:
  console.log(await csv.toString());
})();

Methods

The API provides two methods, toDisk(filename) and toString().

async toDisk(filename, options)

Converts the data and saves the CSV file to disk. The filename must include the path as well.

The options is an optional parameter which is an object that contains the settings. Supported options:

  • append - whether to append to the file. Default is false (overwrite the file). Set to true to append. Column names will be added only once at the beginning of the file. If the file does not exist, it will be created.
  • bom - whether to add the Unicode Byte Order Mark at the beginning of the file. Default is false; set to true to be able to view Unicode in Excel properly. Otherwise Excel will display Unicode incorrectly.
  • allColumns - whether to check all array items for keys to convert to columns rather than only the first. This will sort the columns alphabetically. Default is false; set to true to check all items for potential column names.
const Object2Ext = require("object2ext");
const sampleData = [{ id: 1, text: "this is a test" }];

// Run asynchronously, without awaiting:
new Object2Ext(sampleData).toDisk("./tests.csv");

// Alternatively, you can append to the existing file:
new Object2Ext(sampleData).toDisk("./tests.csv", { append: true });

// `allColumns: true` collects column names from all objects in the array,
// instead of only using the first one. In this case the CSV file will
// contain three columns:
const mixedData = [
  { id: 1, name: "California" },
  { id: 2, description: "A long description." },
];
new Object2Ext(mixedData).toDisk("./tests.csv", { allColumns: true });

async toString(header = true, allColumns = false)

Returns the CSV file as a string.

Two optional parameters are available:

  • header controls whether the column names will be returned as the first row of the file. Default is true. Set it to false to get only the data rows, without the column names.
  • allColumns controls whether to check every item for potential keys to process, rather than only the first item; this will sort the columns alphabetically by key name. Default is false. Set it to true to process keys that may not be present in the first object of the array.
const Object2Ext = require("object2ext");
const sampleData = [{ id: 1, text: "this is a test" }];

async function printCsv(data) {
  console.log(await new Object2Ext(data).toString());
}

printCsv(sampleData);

Requirements

Note tested again older node version

Use Node.js version 12 and above.