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

@uzelux/colget

v2.3.1

Published

Get Nested Attributes with Better experience

Downloads

27

Readme

Collection Get!

This is just a mini-helper to help you retrieve nested attributes!

V 2.3.1

  • Added Regular Expression Handling!

V 2.2.1

  • Fixed Getter not returning anything

V 2.2.0

  • Included Has/Exist functions!
  • Added Special Handling for null and undefined values!
  • Added JSDocs!

V. 2.1.2

  • Minor update on exporting function

V. 2.1.1

  • We have Git Now!

V. 2.1.0

  • Performance Update
  • Update default value for default in get() to undefined

Usage

const ColGet = require('@uzelux/colget');

Sample Object

const sample = {
  foo: 'bar',
  baz: { john: 'doe' },
  nullValue: null,
  undefinedValue: undefined,
};

Retrieve Values (discard null and undefined values)

ColGet.get(sample, 'foo');
// 'bar'

ColGet.get(sample, 'baz.john');
// 'doe'

ColGet.get(sample, 'baz');
// {john: 'doe'}

ColGet.get(sample, 'non.exist');
// undefined

ColGet.get(sample, 'nullValue');
// undefined

ColGet.get(sample, 'undefinedValue');
// undefined

ColGet.get(sample, 'non.exist', 'default');
// 'default'

ColGet.getOrFail(sample, 'non.exist');
// ReferenceError: Element non does not exist in object;

ColGet.getOrFail(sample, 'nullValue');
// ReferenceError: Element nullValue does not exist in object;

ColGet.getOrFail(sample, 'undefinedValue');
// ReferenceError: Element undefinedValue does not exist in object;

Retrieve Values (persist null and undefined values)

ColGet.value(sample, 'foo');
// 'bar'

ColGet.value(sample, 'baz.john');
// 'doe'

ColGet.value(sample, 'baz');
// {john: 'doe'}

ColGet.value(sample, 'non.exist');
// undefined

ColGet.value(sample, 'nullValue');
// null

ColGet.value(sample, 'undefinedValue');
// undefined

ColGet.value(sample, 'non.exist', 'default');
// 'default'

ColGet.valueOrFail(sample, 'non.exist');
// ReferenceError: Element non does not exist in object;

ColGet.valueOrFail(sample, 'nullValue');
// null

ColGet.valueOrFail(sample, 'undefinedValue');
// undefined

Retrieve Values with RegExp

ColGet.regexp(sample, /foo/);
// [ {foo: 'bar'} ]

ColGet.regexp(sample, /baz/, /j\w+/);
// [ {john: 'doe'} ]

ColGet.regexp(sample, /BAZ/i);
// [ {baz: {john: 'doe'}} ]

ColGet.regexp(sample, /nonexist/);
// []

Check Accessible and Defined

ColGet.hasValue(sample, 'foo');
// true

ColGet.hasValue(sample, 'baz.john');
// true

ColGet.hasValue(sample, 'baz');
// true

ColGet.hasValue(sample, 'non.exist');
// false

ColGet.hasValue(sample, 'nullValue');
// false

ColGet.hasValue(sample, 'undefinedValue');
// false

ColGet.hasValueOrFail(sample, 'non.exist');
// ReferenceError: Element non does not exist in object;

ColGet.hasValueOrFail(sample, 'nullValue');
// ReferenceError: Element nullValue does not exist in object;

ColGet.hasValueOrFail(sample, 'undefinedValue');
// ReferenceError: Element undefinedValue does not exist in object;

Check Accessible

ColGet.has(sample, 'foo');
// true

ColGet.has(sample, 'baz.john');
// true

ColGet.has(sample, 'baz');
// true

ColGet.has(sample, 'non.exist');
// false

ColGet.has(sample, 'nullValue');
// true

ColGet.has(sample, 'undefinedValue');
// true

ColGet.hasOrFail(sample, 'non.exist');
// ReferenceError: Element non does not exist in object;

ColGet.hasOrFail(sample, 'nullValue');
// true

ColGet.hasOrFail(sample, 'undefinedValue');
// true