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

webiny-data-extractor

v1.15.1

Published

A small library for easy async data extraction, using dot and square brackets notation.

Downloads

53

Readme

webiny-data-extractor

code style: prettier PRs Welcome

A small library for easy async data extraction, using dot and square brackets notation.

Documentation

Table of Contents

Installation

yarn add webiny-data-extractor

Get started

Data extractor support several ways to extract data, which are demonstrated in the following examples.

Sample data

const data = {
	"firstName": "John",
	"lastName": "Doe",
	"age": 30,
	"enabled": true,
	"company": {
		"name": "Webiny LTD",
		"city": "London"
	},
	"subscription": {
		"name": "Free",
		"price": 0,
		"commitment": {
			"expiresOn": "never",
			"startedOn": 2018,
			"enabled": true
		}
	},
	"simpleCollection": [
		{id: 1, name: "one"},
		{id: 2, name: "two"},
		{id: 3, name: "three"},
		{id: 4, name: "four"}
	],
	promised: new Promise(resolve => {
		setTimeout(() => {
			resolve(100);
		}, 5);
	})
};

Simple extraction

const extractor = require("webiny-data-extractor");
await extractor.get(data, "firstName,lastName,age,company");

This will return the following result:

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 30,
    "company": {
        "name": "Webiny LTD",
        "city": "London"
    }
}

Notice how the company was returned completely with all nested keys. But we can also return only specific nested keys.

Nested keys with dot notation

const extractor = require("webiny-data-extractor");
await extractor.get(data, "firstName,lastName,age,company.city");

This will return the following result:

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 30,
    "company": {
        "city": "London"
    }
}

Another example:

const extractor = require("webiny-data-extractor");
await extractor.get(data, "subscription.name,subscription.price,subscription.commitment");

This will return the following result:

{
    "subscription": {
        "name": "Free",
        "price": 0,
        "commitment": {
            "expiresOn": "never",
            "startedOn": 2018,
            "enabled": true
        }
    }
}

Nested keys with square brackets notation

From the previous example, listing keys using subscription.name,subscription.price,subscription.commitment can become tiring. Alternatively, square brackets can be used.

const extractor = require("webiny-data-extractor");
await extractor.get(data, "subscription[name,price,commitment]");

This will return the same as in previous example.

More advanced example:

const extractor = require("webiny-data-extractor");
await extractor.get(data, "age,subscription[name,price,commitment[expiresOn,enabled]]");

This will return the following result:

{
    "age": 30,
    "subscription": {
        "name": "Free",
        "price": 0,
        "commitment": {
            "expiresOn": "never",
            "enabled": true
        }
    }
}

Multi-line supported

When having many keys to extract, keys can be specified over multiple lines by using template literals. This way the code becomes more readable by not having to specify all keys in one line.

const extractor = require("webiny-data-extractor");
await extractor.get(data, `
    firstName,lastName,age,enabled,
    subscription[name,price,commitment],
    simpleCollection[id,name]
`);

Extracting arrays

Data extractor recognizes when a specified key is an array, in which case it will iterate and execute extraction over each item.

const extractor = require("webiny-data-extractor");
await extractor.get(data, "simpleCollection[name]");

This will return the following result:

{
    simpleCollection: [
        {name: "one"},
        {name: "two"},
        {name: "three"},
        {name: "four"}
    ]
}

Extracting promises

In case key in given data represents an unresolved promise, data extractor will make sure it is first resolved.

const extractor = require("webiny-data-extractor");
await extractor.get(data, "promised");

This will return the following result:

{
    promised: 100
}

Classes

DataExtractor

packages-utils/webiny-data-extractor/src/index.js:8-200

Data extractor class.

get

packages-utils/webiny-data-extractor/src/index.js:16-22

Returns extracted data.

Parameters

  • data {} Data object on which the extraction will be performed.
  • keys string Comma-separated keys which need to be extracted. For nested keys, dot and square brackets notation is available.
  • options ExtractionOptions Extraction options.

Returns Promise<{}>

Flow types

The following are Flow types used in this package:

ExtractionOptions

packages-utils/webiny-data-extractor/types.js:8-11

All possible data extraction options.

Properties

  • includeUndefined boolean? By default, if extracted value is undefined, its key will be omitted in the final output. Set to true if this behavior is not desired.
  • onRead Function? A callback function, which gets triggered when data extractor tries to read a key from given data.