Coder Social home page Coder Social logo

TIP Strongly typed names about typescript-book HOT 5 OPEN

basarat avatar basarat commented on May 12, 2024
TIP Strongly typed names

from typescript-book.

Comments (5)

IanYates avatar IanYates commented on May 12, 2024 6

The one from the TS compiler doesn't always work after running through the fairly simple ASP.Net bundling & minification as it can strip the ';' off the end of the generated function's return statement. By that I mean you get
function(){return x.something}
rather than
function(){ return x.something; }
and the ';' makes all the difference

I've slightly tweaked it for my use now, with some brief smoke tests

    var nameExtractorRegex = /return (.*);/;
    var nameExtractorRegexNoTrailingSemi = /return (.*)/;
    /** Get the name using a lambda so that you don't have magic strings */
    export function getName(nameLambda: (o?:any) => any) {
        var m = nameExtractorRegex.exec(nameLambda + "");
        if (m == null) {
            m = nameExtractorRegexNoTrailingSemi.exec(nameLambda + "");
            if (m == null) {
                throw new Error("The function does not contain a statement matching 'return variableName'");
            }
        }
        var access = m[1].split('.');        
        var name = access[access.length - 1];

        //prevent trailing whitespace, '}'
        while (name[name.length - 1] === '}' || name[name.length - 1] === ' ') {
            name = name.substr(0, name.length - 1);
        }

        return name;
    }

from typescript-book.

IanYates avatar IanYates commented on May 12, 2024 1

Yes that's a better approach 👍
I'll update my code later and report back if I have issues.

from typescript-book.

IanYates avatar IanYates commented on May 12, 2024 1

My RegEx foo leaves a lot to be desired

from typescript-book.

IanYates avatar IanYates commented on May 12, 2024

Handy! Thanks :)

from typescript-book.

basarat avatar basarat commented on May 12, 2024

You can do const nameExtractorRegex = /return (.*);?/; to say that ; is optional but still not a part of the capture group 🌹

from typescript-book.

Related Issues (20)

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.