Giter VIP home page Giter VIP logo

ts-simple-nameof's People

Contributors

beej126 avatar dependabot-preview[bot] avatar ircraziesttaxi 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

Watchers

 avatar  avatar  avatar  avatar

ts-simple-nameof's Issues

Support anonymous functions with ES5

Our company has a shared library with style components to make sure different products have the same look. I'm experiencing an issue with the following piece of code when the project is compiled for ES5. When Es6 is used, it works fine.

public ngOnChanges(changes: SimpleChanges): void {
    if (changes[nameof(() => this.min)]) {
        this.minDateTime = this.toStartOf(this.min);
    }
}

The underlying issue is that the regex used in the ES5 prop selector in this library does not support the use of anonymous functions. In Javascript, the input for the nameof-function is function(){return t.min}. However, the regex expects the format function (x) { return x.prop; }.

My suggestion would be to make the argument optional by changing the regex to /function\s*\([\w+]?\)\s*\{[\r\n\s]*return\s+\w+\.((\w+\.)*(\w+))/i

Webpack - Fails in production mode with Terser

Issue:
Using nameof throws error ts-simple-nameof: Invalid function.

Issue is with how the parser works.

if (fnStr.startsWith("function (")) { will not capture functions with format
function(x) { return x.value }
Lack of space between function and left parentheses.

Resolution.
Can be resolved by adding the extra condition.

I can raise a PR for this if required.

Issue with enums

Got the following code:

export enum TicketPriorityEnum {
  'Low' = 'Low',
  'Medium' = 'Medium',
  'High' = 'High',
  'Critical' = 'Critical'
}
...
import { TicketPriorityEnum } from '../../services';
nameof(TicketPriorityEnum)

And the following error:

Error	TS2769	(TS) No overload matches this call.
  Overload 1 of 2, '(selector: (obj: Object) => any, options?: NameofOptions): string', gave the following error.
    Argument of type 'typeof TicketPriorityEnum' is not assignable to parameter of type '(obj: Object) => any'.
      Type 'typeof TicketPriorityEnum' provides no match for the signature '(obj: Object): any'.
  Overload 2 of 2, '(classType: new (...params: any[]) => Object): string', gave the following error.
    Argument of type 'typeof TicketPriorityEnum' is not assignable to parameter of type 'new (...params: any[]) => Object'.
      Type 'typeof TicketPriorityEnum' provides no match for the signature 'new (...params: any[]): Object'.

I guess enums are not supported. Are they?

Ability to retrieve name of interface instead of class

It would be nice to be able to retrieve the name of a given interface, instead of only concrete classes or property selectors of an interface/class.

For example:

interface User {
    firstName: string;
    lastName: string;
}

const interfaceName = nameof<User>();

I haven't looked at the code yet, but I'm not sure if that would force an API change with the current nameof<TClass>(TClass) signature. Based on the related package ts-nameof, I would imagine it's possible.

Failed with mixing arrow function and return clause

Hi,

Thanks for the library. I am maintaining of TypeScript AutoMapper and I also leverage getting the property path from a lambda function: d => d.foo.bar.

I've been trying to make my library smaller. Today, I've looked at my current implementation of getting the property path and I happen to find this "simple" (pun-intended) library. I dropped the relevant code from this library in my AutoMapper and all the tests passed but one. Considering the following case:

nameOf<Foo>(f => {
   return f.foo.bar;
});

Theoretically, the above case is valid JavaScript, and for whatever reason, it might happen. However, nameOf() failed to extract foo.bar because the path where nameOf() checks for Arrow function => is a little too simple. In my library, I have this extra replace

replace(/(?:\s|;|{|}|\(|\)|)+/gm, '');

I'm not very good with RegExp so I'm creating this issue to notify you about this weird case and maybe you'll be able to come up with a better solution.

Cheers

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.