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

supick

v1.1.0

Published

Easy to use lodash Pick alternative with configurable and extensive schema

Downloads

60,653

Readme

Supick (super pick)

Empowered but easy to use lodash Pick alternative with configurable and extensive schema and

Supick creates an object/array composed of the deeply picked object properties/array of picked object properties.

Quick start

npm i supick

const pick = require("supick");

const person = {
  name: "John Doe",
  car: {
    model: "BMW X5",
    year: 2019,
    color: "black",

  },
  hobby: "music"
};

pick(person, "name, car: {model, year}"); // ==> { name: "John Doe", { car: { model: "BMW X5", year: 2019 } } }

Schema format rules:

  1. Schema could be string, array of strings(lodash pick style), object(mongodb projection style) or customizer function
  2. You may use trailing whitespaces, newlines and carret returns - they are ignored.
  3. You may enclose schema in brackets: '{a, b, c, d: {d_a, d_b}}'
  4. Use commas to separate prop names on each level. Trailing comma is ignored.
  5. Use colon ":" and curly brackets ("{", "}") to specify inner properties.
  6. You may use supick both for objects and arrays, for example: schema "a: 1, b: 2" works this way:
  • pick({ a: 1, b: 2, c: 3 }, 'a, b') => { a: 1, b: 2 }
  • pick([{ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }], 'a, b') => [{ a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }]
  1. Customizer function is very flexible way to add some extra logic. Customizer function must return truthy or falsy value to determine if property should be picked. Function receives 4 arguments: key(property name), value(property value), path(parent properties concatenated with dot delimiter), depth(depth of current nested property).

Schema examples:

  1. String: '{ a, b, c: { d, e }, f: { g: { h, i }, j } }'.
  2. Array of strings(lodash-style): ['a', 'b', 'c: { d, e }', 'f: { g: { h, i }, j }'].
  3. Object(mongodb projection-style): { a: true, b: true, c: { d: true, e: true }, f: { g: { h: true, i: true }, j: true } } // "true" could be replaced to 1
  4. Customizer function:
function (key, value, path, depth) {
    if (['a', 'b', 'c', 'e'].includes(key)) return true; // pick all properties with names a, b, c, e
    if (path.startsWith('f')) return true; // pick f property and all its nested properties
    if (depth === 0) return true; // pick all top level properties
    return false;
}

Big example with nested objects and arrays:

const person = {
    "name" : "John Doe",
    "age": 35,
    "family" : {
    	"wife" : {
            "name": "Susan",
            "age": 32
        },
        "children": [
            {
                "name": "Alex",
                "age": 5,
                "hobby": "dancing"
            },
            {
                "name": "Alice",
                "age": 3,
                "hobby": "music"
            }
        ]
    },
    "cars": [
        {
            "model": "BMW X5",
            "year": 2015
        },
        {
            "model": "Tesla Model S",
            "year": 2018
        }
    ]
};

const schema = `
{
	name,
	family: {
		wife,
		children: {
			name,
			age
		}
    },
	cars: { model }    
}`;

pick(obj, schema); will result in this =>

{
    "name" : "John Doe",
    "family" : {
    	"wife" : { "name": "Susan", "age": 32 },
        "children": [
            { "name": "Alex", "age": 5 },
            { "name": "Alice", "age": 3 }
        ]
    },
    "cars": [
        { "model": "BMW X5" },
        { "model": "Tesla Model S" }
    ]
};

More examples in test file index.test.js