Giter Club home page Giter Club logo

vitorluizc / get Goto Github PK

View Code? Open in Web Editor NEW
17.0 4.0 2.0 64 KB

๐Ÿšš 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.

License: MIT License

TypeScript 89.26% JavaScript 10.74%
object-path object property-accessors typescript javascript ava dot-notation dot-prop dot-properties deep-property

get's Introduction

@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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.