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

structure-anal

v1.0.1

Published

A small utility library for analysis of fields in JSON-like data structures.

Downloads

21

Readme

structure-anal github, npm

A small library for analysis of fields in JSON-like data structures.

Consumes a number of JSON-like entities and returns a summary of fields encontered across any entities along with their unique values. Contains helpers for converting that summary to text.

See npx cli command structure-anal-cli for console use: github, npm.

Usage

For options, see types below this example. All options are optional. See an example file.

import { 
  StructureAnalyzer, 
  makePathReducer, 
  printAnalyzerEntries 
} from 'structure-anal';

const anal = new StructureAnalyzer({
  pathReducer: makePathReducer(['children']),
});

anal.processEntity(exampleData);

console.log("Processed", anal.entitiesProcessed, "entities");

const entries = anal.sorted(); 
// array of entries [key: string, info: FieldInfo]

printAnalyzerEntries(entries, {
  omitObjectOnlyEntries: true,
  printFn: paths => '---\n' + paths.join("\n")
});

Types

Available types

import {
  StructureAnalyzer,    // Main class
  makePathReducer,      // Helper for making collapsing path reducers
  printAnalyzerEntries, // Helper for printing entries
  fieldInfoSummary,     // Helper for text representation of an entry
} from 'structure-anal';

import type { 
  StructureAnalyzerOptions,
  PathReducer,
  FieldInfo,
  PrintAnalyzerEntriesOptions,
  PrintFn,
} from 'structure-anal'

Results

Results are returned as arrays/iterables of entries [string, FieldInfo].

interface FieldInfo {
  entitiesWithFieldCount: number,
  fieldOccurancesTotalCount: number,
  uniqueValues: Array<[value: any, count: number]> | null,
}

Main class StructureAnalyzer

class StructureAnalyzer {
  constructor(options: StructureAnalyzerOptions = {})

  /** Get total number of entities processed */
  get entitiesProcessed() : number
  
  /** Get all fields, by default sorts unique values by how many 
   *  times they occur. Entries are not sorted by key. */
  entries(sortFieldValuesDescendingCount = true) : Iterable<[string, FieldInfo]>

  /** Like `entries`, but sorts them alphabetically by their key */
  sorted(sortFieldValuesDescendingCount = true) : [string, FieldInfo][]
  
  /** Process many entries */
  processEntities(entities: Iterable<any>) : void

  /** Process single entry */
  processEntity(entity: any) : void
}

StructureAnalyzerOptions (constructor parameter)

/** Maximum number of unique values for a given field to gather.
 *  If more values are encountered, `null` is returned
 *  for `uniqueValues`. Default: 1000 */
maxUniqueValuesPerField?: number,
/** If set to false, numeric keys will not be collapsed to `#`. 
 *  Default: `true` */
collapseNumericKeys?: boolean,
/** Custom path reducer for generating field keys.
 *  See `makePathReducer` helper.
 *  Default: `path => path.join('.')` */
pathReducer?: PathReducer, // (path: string[]) => string

Printing results

printAnalyzerEntries(
  entries: Iterable<[string, FieldInfo]>, 
  options: PrintAnalyzerEntriesOptions = {}
) : void

fieldInfoSummary(field: FieldInfo, valueLimit?: number): string[]

PrintAnalyzerEntriesOptions (printAnalyzerEntries parameter)

/** If set to true, will not print fields with `uniqueValues: null` 
 *  (contained more unique values than `maxUniqueValuesPerField`).
 *  Default: false */
omitTooManyValues?: boolean,
/** If set to true, will not print fields with only one unique 
 *  value `"[object]"`. Default: false */
omitObjectOnlyEntries?: boolean,
/** Maximum unique number of values to display. Number of
 *  omitted values will be shown. Default: 10 */
valueLimit?: number,
/** Function called with few parts representing the entry.
 *  You may want to join these parts by a newline or space.
 *  Default: `(parts) => console.log(...parts)` */
printFn?: PrintFn, // (parts: string[]) => void

Contributing

Please feel free to open a pull request or an issue if you find something wrong or would like to contribute an improvement.