Socket
Socket
Sign inDemoInstall

@bitty/get

Package Overview
Dependencies
0
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @bitty/get

A really small and type-safe (requires TypeScript >= 4.1.3) function, that gets a nested value from an object using a path string (like 'a.b[0].d'). If value is 'undefined' or unreachable returns the placeholder instead.


Version published
Weekly downloads
38
increased by90%
Maintainers
2
Install size
23.4 kB
Created
Weekly downloads
 

Readme

Source

@bitty/get

A really small and type-safe (requires TypeScript >= 4.1.3) function, that gets a nested value from an object using a path string (like "a.b[0].d"). If value is undefined or unreachable returns the placeholder instead.

  • 📦 Distributions in ESM, CommonJS, UMD and UMD minified formats.

    • Supports both NodeJS used with ESM (import/export) and CommonJS (require/module.exports).
  • ⚡ Lightweight:

    • Weighs less than 150 bytes (when minified and gzipped).
    • Tree-shakeable.
    • Side-effects free.
  • 🔋 Batteries included:

    • No dependencies.
    • Its not based on newer browser's APIs or es2015+ features.
  • 🏷 Safe:

    • JSDocs and type declarations for IDEs and editor's autocomplete/intellisense.
    • Made with TypeScript as strict as possible.
    • Unit tests with AVA (types was also tested).

It's approach is based on idx, from Facebook, and an insight from a Vue.js meetup where @IgorHalfeld showed a way to get nested values using eval.

Install

This library is published in the NPM registry and can be installed using any compatible package manager.

npm install --save @bitty/get

# If you're using Yarn use the command below.
yarn add @bitty/get

Installation from CDN

This module has a UMD bundle available through JSDelivr and Unpkg CDNs.

<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/@bitty/get"></script>

<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/@bitty/get"></script>

<script>
  // UMD module is exposed through the "get" global function.
  console.log(get);
  //=> "[Function: get]"

  var users = [{ name: 'Carlos Marcos' }];

  console.log(get(users, '[0].name', 'Unknown'));
  //=> "Carlos Marcos"
</script>

Usage

Just import get function and gets a value from object using a path expression.

import get from '@bitty/get';

// ...

const name = get(response, 'data.user.name', 'Unknown');

You can use brackets, even on first property.

const title = get(paragraphs, '[0].children[0].textContent');

License

Released under MIT License.

Keywords

FAQs

Last updated on 09 Jan 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc