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

@hackylabs/obglob

v1.1.2

Published

Glob search an a nested object/array against patterns. Returns a copy of the original with only the matched properties/values included or excluded. Optionally modifies the values of the matched properties. Optionally flattens the result.

Downloads

20

Readme

Obglob

npm version GitHub license

Obglob is a library that allows you to extract, remove or modify values from an object using glob patterns to match key paths or values.

Uses safe-flat under the hood to flatten the object/array, while transforming circular references and other support values, before globbing over the flattened object/array using micromatch. Optionally, you can also use the returnFlattened option to return the flattened result instead of unflattening it.

ko-fi

Installation

npm install @hackylabs/obglob

Usage

// ./src/example.ts
import {obglob} from '@hackylabs/obglob'; // If you're using CommonJS, import with require('@hackylabs/obglob') instead. Both CommonJS and ESM support named and default imports.

// Glob over an object
const obj = {
  a: {
    foo: 'bar',
  },
  b: {
    bing: 'bong',
  },
}

const objResults = obglob(obj, { patterns: ['*/foo'] })
// { a: { foo: 'bar' } }

// Glob by value
const valueResults = obglob(obj, { patterns: ['ba*'], globBy: 'value' })
// { a: { foo: 'bar' } }

// Return as paths
const paths = obglob(obj, { patterns: ['*/foo'], returnAs: 'paths' })
// [ 'a/foo' ]

// Return as values
const values = obglob(obj, { patterns: ['*/foo'], returnAs: 'values' })
// [ 'bar' ]

// Exclude matches and return the rest
const unmatched = obglob(obj, { patterns: ['*/foo'], excludeMatched: true, includeUnmatched: true })
// { b: { bing: 'bong' } }

Main Options

| key | description | type | options | default | required | | --- | --- | --- | --- | --- | --- | | patterns | An array of glob patterns (as strings) to match key paths against. See micromatch for more information. An empty array will return the original value and log a warning as a helpful reminder. | array | | | Y | | delimiter | The delimiter to use when flattening and unflattening the object. Glob patterns will be matched against the flattened keys. When returnFlattened is true, the delimiter will appear in the key to delimit the path to the value in the original object. | string | | / | N | | globBy | When set to "path", match the keys of the object. When set to "value", match the values of the object. When set to "value", the values will be coerced to strings before matching. | string | path │ value | path | N | | returnAs | When set to "object", return the matched key/value pairs as an object. When set to "values", return an array of the matched values as a flat array. When set to "paths", return an array of the matched keys as a flat array. | string | object │ paths │ values | object | N | | excludeMatched | When true, exclude matched key/value pairs from the result. When true and includeUnmatched is false, an empty object will be returned. When true and includeUnmatched is false, an empty object will be returned and a warning will be logged as a helpful reminder. | boolean | | false | N | | includeUnmatched | When true, include unmatched key/value pairs in the result. When false and excludeMatched is true, an empty object will be returned. When false and excludeMatched is true, an empty object will be returned and a warning will be logged as a helpful reminder. | boolean | | false | N | | returnFlattened | When true, return the result as a flattened object. When false, return the result as a nested object. Uses safe-flat. When true, each key will be delimited by a forward slash (/) denoting the path to the value in the original object. | boolean | | false | N | | callback | A function to apply to the matched values. Optionally accepts the matched value as an argument. | function | | | N |