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

prop-extractor

v1.0.1

Published

Get values from complicated data structures, nested arrays and objects, using request string like 'foo.[].bar.[].baz'.

Downloads

7

Readme

Contents

  1. Creating Extractor instance and test data
  2. Getting single values
    2.1. ... if path exists
    2.2. ... if path doesn't exist
    2.3. ... with checking of path existence
  3. Getting arrays of values
    3.1. ... if path exists
    3.2. ... if path doesn't exist
    3.3. ... with checking of path existence
  4. More examples
    4.1. Getting of array elements
    4.2. Getting a defined property of array elements (objects)
    4.3. Getting a defined property of array elements (objects) that are deeply nested
  5. Using filters
    5.1. Getting names of countries with population bigger than 300 millions
    5.2. Getting names of cities where name starts with ‘S’
    5.3. Getting cities with population bigger than 20 millions in countries with population bigger than 1 billion

1. Creating Extractor instance and test data

const Extractor = require('prop-extractor');
let extractor = new Extractor();

let data = {
    countries: [
        {
            name: 'Australia',
            population: 24641662,
            cities: [
                {name: 'Sydney', population: 5029768},
                {name: 'Melbourne', population: 4725316},
                {name: 'Canberra', population: 435019}
            ]
        },
        {
            name: 'Brazil',
            population: 211243220,
            cities: [
                {name: 'São Paulo', population: 12038175},
                {name: 'Rio de Janeiro', population: 6498837},
                {name: 'Salvador', population: 2938092}
            ]
        },
        {
            name: 'China',
            population: 1388232693,
            cities: [
                {name: 'Shanghai', population: 24152700},
                {name: 'Beijing', population: 21700000},
                {name: 'Guangzhou', population: 14043500}
            ]
        },
        {
            name: 'United States',
            population: 326474013,
            cities: [
                {name: 'New York', population: 8537673},
                {name: 'Los Angeles', population: 3976322},
                {name: 'Chicago', population: 2704958}
            ]
        }
    ],
    country_codes: ['AU', 'BR', 'CN', 'US'],
    count: 4
};

2. Getting single values

2.1. ... if path exists

extractor.setPath('data.count');
console.log(extractor.extractFrom(data).get());

// 4

2.2. ... if path doesn't exist

extractor.setPath('data.foo.bar.baz');
console.log(extractor.extractFrom(data).get());

// undefined

2.3. ... with checking of path existence

extractor.setPath('data.count');
let response = extractor.extractFrom(data);

if (response.has()) {
  console.log(response.get());
}

// 4

3. Getting arrays of values

3.1. ... if path exists

extractor.setPath('data.country_codes.[]');
console.log(extractor.extractFrom(data).get());

// ['AU', 'BR', 'CN', 'US']

3.2. ... if path doesn't exist

extractor.setPath('data.foo.[].bar');
console.log(extractor.extractFrom(data).get());

// []

3.3. ... with checking of path existence

extractor.setPath('data.country_codes.[]');
response = extractor.extractFrom(data);

if (response.has()) {
  console.log(response.get());
}

// ['AU', 'BR', 'CN', 'US']

4. More examples

4.1. Getting of array elements

extractor.setPath('data.countries.[]');
console.log(extractor.extractFrom(data).get());

// [ { name: 'Australia', population: 24641662, cities: [ ... ] },
// { name: 'Brazil', population: 211243220, cities: [ ... ] },
// { name: 'China', population: 1388232693, cities: [ ... ] },
// { name: 'United States', population: 326474013, cities: [ ... ] } ]

4.2. Getting a defined property of array elements (objects)

extractor.setPath('data.countries.[].name');
console.log(extractor.extractFrom(data).get());

// [ 'Australia', 'Brazil', 'China', 'United States' ]

4.3. Getting a defined property of array elements (objects) that are deeply nested

extractor.setPath('data.countries.[].cities.[].name');
console.log(extractor.extractFrom(data).get());

// [ 'Sydney', 'Melbourne', 'Canberra', 'São Paulo',
// 'Rio de Janeiro', 'Salvador', 'Shanghai', 'Beijing',
// 'Guangzhou', 'New York', 'Los Angeles', 'Chicago' ]

5. Using filters

5.1. Getting names of countries with population bigger than 300 millions

extractor.setPath('data.countries.[population].name');
extractor.removeAllFilters();
extractor.setFilter('population', (item) => {
  return (item.population > 300000000);
});
console.log(extractor.extractFrom(data).get());

// [ 'China', 'United States' ]

5.2. Getting names of cities where name starts with 'S'

extractor.setPath('data.countries.[].cities.[city_name].name');
extractor.removeAllFilters();
extractor.setFilter('city_name', (item) => {
  return (item.name[0] === 'S');
});
console.log(extractor.extractFrom(data).get());

// [ 'Sydney', 'São Paulo', 'Salvador', 'Shanghai' ]

5.3. Getting cities with population bigger than 20 millions in countries with population bigger than 1 billion

extractor.setPath('data.countries.[country_population].cities.[city_population]');
extractor.removeAllFilters();
extractor.setFilter('country_population', (item) => {
  return (item.population > 1000000000);
});
extractor.setFilter('city_population', (item) => {
  return (item.population > 20000000);
});
console.log(extractor.extractFrom(data).get());

// [ { name: 'Shanghai', population: 24152700 },
// { name: 'Beijing', population: 21700000 } ]