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

qtools-object-flattener

v1.0.5

Published

flattens complex object into tsv with columns named as the dotted path to the value

Downloads

8

Readme

qtools-object-flattener

Collapses JS complex, multi-level JS object to single level object with dotted path property names. Additional utility functions, resurrect() (reverses the process) and convertArray() (operates on an array of objects).

Is useful for puttting Javascript objects into relational databases when you don't want to use a JSON field type. Column names become dotted.paths. Also for analysis of complicated objects.

Also consider qtools-functional-library with its objectInstance.getSurePath('some.dotted.path') and objectInstance.putDottedPath('another.path', value)

Usage:

Flatten an object

const jsonFlattener = require('qtools-object-flattener');

const flatObject=jsonFlattener.convert(someObject, [options], [callback]);

Works when executed on an Array but it doesn't make too much sense.

Without a callback, the result is returned.

Flatten an array of objects

const flatArray=jsonFlattener.convertArray(someArrayOfObjects, [options], [callback]); 

Optional parameters in the options parameter object.

nameTransformer - a function that operates on the generated, dotted path in any way that is useful and produces a new string maxDepth - recursion limit, default is 20

EG,

const flatArray=jsonFlattener.convertArray(someArrayOfObjects, {nameTransformer:dottedPath>dottedPath.replace(/\./g, '\_')}); 

const flatObject=jsonFlattener.convert(someObject, {nameTransformer:dottedPath=>dottedPath.replace(/\./g, '\_')});

The closest there is to a test is:

npm run demo

Which has examples of the main function as illustrated below.

Examples:

Flattening an object:

const flatObject=jsonFlattener.convert(someObject);

someObject={ name: 'TQ White II', tv1:{ brand: 'Sony', inches: 60, internet: true }, websites:[ 'https://tqwhite.com', 'https://tech.genericWhite.com', 'https://jsLightning.com' ] }

**Flattens to:**

const flatObject={
name: 'TQ White II',
'tv1.brand': 'Sony',
'tv1.inches': 60,
'tv1.internet': true,
'websites[0]': 'https://tqwhite.com',
'websites[1]': 'https://tech.genericWhite.com',
'websites[2]': 'https://jsLightning.com'
}

Flattening an array of objects into an array of flattened objects

const flatObject=jsonFlattener.convert(someObject);

const someArray = [
{
name: 'TQ White II',
tv1: {
brand: 'Sony',
inches: 60,
internet: true
},
websites: [
'https://tqwhite.com',
'https://tech.genericWhite.com',
'https://jsLightning.com'
]
},
{
name: 'Debbie White',
tv1: {
brand: 'Sony',
inches: 60,
internet: true
},
websites: [
'https://tqwhite.com',
'https://tech.genericWhite.com',
'https://jsLightning.com'
]
}
];

Flattens to:

const flattenedArray=[
{
name: 'TQ White II',
'tv1.brand': 'Sony',
'tv1.inches': 60,
'tv1.internet': true,
'websites[0]': 'https://tqwhite.com',
'websites[1]': 'https://tech.genericWhite.com',
'websites[2]': 'https://jsLightning.com'
},
{
name: 'Debbie White',
'tv1.brand': 'Sony',
'tv1.inches': 60,
'tv1.internet': true,
'websites[0]': 'https://tqwhite.com',
'websites[1]': 'https://tech.genericWhite.com',
'websites[2]': 'https://jsLightning.com'
}
]

Change Log

v1.0.5 Updated README only. No code changes. v1.0.4 Updated README only. No code changes.