Giter Site home page Giter Site logo

armanozak / snq Goto Github PK

View Code? Open in Web Editor NEW
29.0 1.0 3.0 1.07 MB

A utility function to avoid type errors when traversing over arrays and object properties.

Home Page: https://stackblitz.com/edit/snq

License: MIT License

TypeScript 80.74% JavaScript 19.26%
optional-chaining safe-navigation elvis-operator null-coalescing-operator null-conditional-operator type-error null-safety exception-handling typescript

snq's Introduction

snq (Safe Navigation Query)

Bundle Size MIT License Follow the Author on Twitter

Now that optional chaining and nullish coalescing operators are available, libraries like snq have become redundant. Please use them instead.

snq is a utility function to safely navigate arrays and object properties without getting type errors. It is not an original idea at all and is actually adapted and only slightly different from idx. The main differences are as follows:

  • snq returns undefined whenever a TypeError happens, regardless of the reason for the error and throws an error only if it is not a TypeError. idx returns null, if the cause of the error is a null value and throws an error if the error is not caused by an undefined or null value.
  • snq has an optional second parameter which works as default value to return instead of undefined.
  • idx requires the source object as a first parameter. snq does not.
  • idx has a Babel plugin for replacing idx instances with conventional traversing in order to improve performance. Although it is not benchmarked yet, due to lack of reason checks, it is safe to say that snq is faster than idx. Thus, a Babel plugin could prove insignificant for snq.
  • snq is written in TypeScript and, unlike idx, it does not support Flow types.

Installation

Run the following code in your terminal:

yarn add snq

or if you are using npm:

npm install --save snq

Setup

import snq from 'snq';

Usage

Consider the following interfaces as products list:

interface Price {
  amount: number;
  currency: string;
  symbol?: string;
}

interface Product {
  id: number;
  name: string;
  inStock: boolean;
  price?: {
    final: Price;
    original?: Price;
  };
}

This is how it would probably look like when you want to get original price symbol of first product:

products.length &&
  products[0].price &&
  products[0].price.original &&
  products[0].price.original.symbol;

Otherwise, you will get a type error. Using snq, it is safe to write the following:

const symbol = snq(() => products[0].price.original.symbol);

// symbol is undefined if a type error happens, actual value if not

There is an optional second argument which represents the default value to return when a type error happens.

const symbol = snq(() => products[0].price.original.symbol, '$');

// symbol is "$" if a type error happens, actual value if not

The type of the symbol returned will be inferred as string in both cases.

Check the demo application out.

snq's People

Contributors

armanozak avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.