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

@gmbeard/only-data

v0.1.3

Published

Reduces objects and object graphs to just their data properties

Downloads

4

Readme

Only Data

Only Data will strip input down to just its data properties, removing "non-data" types such as functions and symbols. It works on primitive types, objects, arrays of objects, and nested graphs of objects.

Features:

  • Circular reference protection is provided by either throwing (the default), or removing them.
  • Ability to use a user-provided "reduction" function, allowing fine-grained control over the output.

Signature

onlyData(input[, options])

Parameters:
    input: Primitive | Object | Array<Any>
    options: Object | Array<String> | Function

Returns:
    Primitive | Object | Array<Any>

Usage

const { onlyData } = require("only-data");

const input = {
    name: "object",
    value: 42,
    invoke: function() { ... }
};

const data = onlyData(input);

// data: { name: "object", value: 42 }

Options

If options is a Array<String> then only properties matching these values will be included in the output.

If options is a Function then the function will be used as the reducer. See the Custom Reducer section.

If options is an object, it can contain the following settings...

Circular Reference Behaviour

By default, Only Data will throw an error whenever it encounters a circular reference. This will prevent costly stack overflows. You can control this behaviour using the circularReferences option.

circularReferences: "empty": Circular reference objects will be replaced with an empty object ({ })

const a = { name: "A" };
const b = { name: "B", val: a };
a.val = b; // This closes the loop and causes a circular reference

const data = onlyData(a, { circularReferences: "empty" });

// data: {
//   name: "A",
//   val: {
//     name: "B",
//     val: { }
//   }
// }

circularReferences: "error": An error will be thrown when a circular reference is detected. This is the default behaviour.

circularReferences: "indicate": Indicates circular references in the output by offending objects with { __circular: true }.

const a = { name: "A" };
const b = { name: "B", val: a };
a.val = b; // This closes the loop and causes a circular reference

const data = onlyData(a, { circularReferences: "remove" });

// data: {
//   name: "A",
//   val: {
//     name: "B",
//     val: { __circular: true }
//   }
// }

circularReferences: "remove": Removes circular reference objects from the graph altogether.

const a = { name: "A" };
const b = { name: "B", val: a };
a.val = b; // This closes the loop and causes a circular reference

const data = onlyData(a, { circularReferences: "remove" });

// data: {
//   name: "A",
//   val: {
//     name: "B",
//   }
// }

Custom Reducer

A custom reduction function has the signature (key: String, value: Any) -> Any. It will be called for each property of an object that needs to be reduced. Returning undefined from the custom reducer will cause the property to be ignored in the output.

function customReducer(key, value) {
    if (key === "propertyToIgnore")
        return;

    return value;
}

const data = onlyData(input, customReducer);