Code Monkey home page Code Monkey logo

ts-get's Introduction

ts-get

npm version Build Status codecov

Archived. Use optional chaining instead :)

Alternative to lodash.get that makes it typed and cool as if optional chaining proposal is there.

Means you're not only safely navigate object, but you're also getting 100% autocomplete and type-safeness ๐ŸŽ‰

Important comment from the author :)

Please, be aware of how this is working, and take a look limitations below, this is good to get only primitive values not an objects! (at least without generics/castings etc, which makes no sense and you can use lodash then). You better use ts-optchain if browsers your are supporting, have Proxy support. If not, use ts-optchain anyway but with typescript transformer or babel-plugin that you can find in their docs. More than that, optional chaining proposal has moved to Stage 3 recently, meaning it will be in TS very soon (version 3.7.0 to be precise)

This was nice experiment, but performance limitations of try catch and problems with type inference if object is accessed is making this thing dangerous and not as cool as I thought when I wrote it initially and started using it ๐Ÿ˜‡

Usage and examples

import get from 'ts-get'

 type SomeType = {
  optionalField?: string
  nested?: {
    dangerousAccess?: string
  }
 } | undefined | null
 
 const emptyObject: SomeType = {}
 const withOneOptionalField: SomeType = {
    optionalField: "value",
 }
 
 get(emptyObject, it => it.optionalField, "default") // -> "default"
 get(withOneOptionalField, it => it.optionalField, "default") // -> "value"
 get(withOneOptionalField, it => it.nested.dangerousAccess, "default") // -> "default"
 get(withOneOptionalField, it => it.unknownField, "default") // -> Type error, `unknownField` doesn't exist on type
 get(withOneOptionalField, it => it.optionalField, 5) // -> Type error, third argument is not assignable to type `string`

Difference with lodash.get behavior

  • If your path gets null at the end, it will bail out to defaultValue or undefined. If you would like to get null returned anyway, just pass it as a defaultValue

Known issues/limitations:

  • If your type field is of type null and only null or undefined your field will be of type {}[]. I have no idea how to fix it ๐Ÿคทโ€โ™‚๏ธ PR Welcome ๐Ÿ˜‡๐Ÿ™
type A = {
  field: null | undefined// -> {}[] inside of the callback and as return type too
}
  • If you return not a primitive but an object, all its nested fields will be Required e.g. all undefined and null will be removed.
import get from 'ts-get'
type A = {
  field?: {
    optional?: string | null
  }
}
const input: A = {}
const res = get(input, it => it.field)
res // <== Will be inferred as { optional: string }, without null and ? (undefined) which is wrong, but seems to be impossible to infer.

You can solve this issue passing down generics implicitly

import get from 'ts-get'
type A = {
  field?: {
    optional?: string | null
  }
}
const input: A = {}
const res = get<A, A['field']>(input, it => it.field)
res // <== Will be inferred properly

ts-get's People

Contributors

dependabot[bot] avatar rip21 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  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  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  avatar

ts-get's Issues

Performance test doesn't seem accurate, seems much slower than lodash.get.

Hi, thanks for creating this, I've been hoping a better _.get existed for TypeScript.

I was confused how try/catch could be faster than lodash's get, so I simplified the performance test and got very different results, suggesting ts-get is many times slower than _.get:

Screen Shot 2019-04-09 at 4 23 51 PM

https://jsperf.com/ts-get-vs-get/4

The problem with your original tests:

  • Only one level deep.
  • Not testing the fail case.
  • Comparing a single run to an array of runs.

It's possible I did something wrong in the tests, it took me a couple tries to get it right.

Feature request: Add Ramda-compatible composable export

This would be similar to the existing export, except with the params flipped, curryable, and defaults split into another export:

import { composableGet as get, composableGetOr as getOr } from 'ts-get';

// these are the same statement
get(obj => obj.a.b.c, inputObject);
get(obj => obj.a.b.c)(inputObject);

// these are the same statement
getOr(defaultValue, obj => obj.a.b.c, inputObject);
getOr(defaultValue, obj => obj.a.b.c)(inputObject);
getOr(defaultValue)(obj => obj.a.b.c, inputObject);
getOr(defaultValue)(obj => obj.a.b.c)(inputObject);

This would allow use with Ramda for things like the following (real simple example):

import { pipe, equals } from 'ramda';
import { composableGet as get } from 'ts-get';

import { getMetadataForIndex } from './whatever-utils';

export function satisfiesCriteria(index: number, type: string): boolean {
    return pipe(
        getMetadataForIndex, // this is passed index
        get(metadata => metadata.a.b.c), // this is passed the output of getMetadataForIndex
        equals(type) // this is passed the output of get
    )(index);
};

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.