All Projects → armanozak → snq

armanozak / snq

Licence: MIT license
A utility function to avoid type errors when traversing over arrays and object properties.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to snq

Bugsnag React Native
Error monitoring and reporting tool for native exceptions and JS errors in React Native apps
Stars: ✭ 374 (+1146.67%)
Mutual labels:  exception-handling
Larabug
Laravel error reporting tool
Stars: ✭ 84 (+180%)
Mutual labels:  exception-handling
Graphql Errors
Simple error handler for GraphQL Ruby ❗️
Stars: ✭ 170 (+466.67%)
Mutual labels:  exception-handling
Traceback with variables
Adds variables to python traceback. Simple, lightweight, controllable. Debug reasons of exceptions by logging or pretty printing colorful variable contexts for each frame in a stacktrace, showing every value. Dump locals environments after errors to console, files, and loggers. Works in Jupyter and IPython. Install with pip or conda.
Stars: ✭ 509 (+1596.67%)
Mutual labels:  exception-handling
Mjn
⚡️Like loadash.get, but in ~200 bytes
Stars: ✭ 69 (+130%)
Mutual labels:  exception-handling
Coderr.server
Replace logfiles with Coderr to correct bugs faster and more efficiently.
Stars: ✭ 92 (+206.67%)
Mutual labels:  exception-handling
Throwing Function
Checked Exceptions-enabled Java 8+ functional interfaces + adapters
Stars: ✭ 279 (+830%)
Mutual labels:  exception-handling
Dsl.scala
A framework to create embedded Domain-Specific Languages in Scala
Stars: ✭ 220 (+633.33%)
Mutual labels:  exception-handling
Laravel Whoops Editor
Laravel Whoops Editor helps to open your code editor from exception stack trace.
Stars: ✭ 83 (+176.67%)
Mutual labels:  exception-handling
Catch Exception
Stars: ✭ 137 (+356.67%)
Mutual labels:  exception-handling
Spring Guide
Spring 실전 가이드
Stars: ✭ 521 (+1636.67%)
Mutual labels:  exception-handling
Sosl
StackOverflow Search Library
Stars: ✭ 10 (-66.67%)
Mutual labels:  exception-handling
Faux Pas
A library that simplifies error handling for Functional Programming in Java
Stars: ✭ 100 (+233.33%)
Mutual labels:  exception-handling
Bugsnag Php
Bugsnag error monitoring and crash reporting tool for PHP apps
Stars: ✭ 475 (+1483.33%)
Mutual labels:  exception-handling
Object Oriented Programming Using Python
Python is a multi-paradigm programming language. Meaning, it supports different programming approach. One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).
Stars: ✭ 183 (+510%)
Mutual labels:  exception-handling
Aspnetcore Webapi Course
Professional REST API design with ASP.NET Core 3.1 WebAPI
Stars: ✭ 323 (+976.67%)
Mutual labels:  exception-handling
Android Defensecrash
Android runtime crash controller. defense java crash keep your application safety
Stars: ✭ 89 (+196.67%)
Mutual labels:  exception-handling
Express Typescript
Express + TypeScript + Boilerplate for Web / API App
Stars: ✭ 230 (+666.67%)
Mutual labels:  exception-handling
Exceptions4c
🐑 An exception handling framework for C
Stars: ✭ 189 (+530%)
Mutual labels:  exception-handling
Stacktracey
Parses call stacks. Reads sources. Clean & filtered output. Sourcemaps. Node & browsers.
Stars: ✭ 115 (+283.33%)
Mutual labels:  exception-handling

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].