JSPM

babel-plugin-transform-expressive-loops

1.1.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 16
    • Score
      100M100P100Q32133F
    • License MIT

    Babel transform adds extra bindings to for-in and for-of statements

    Package Exports

    • babel-plugin-transform-expressive-loops

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-transform-expressive-loops) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    babel-plugin-transform-expressive-loops

    Babel plugin to transform enhanced for-in and for-of loops.
    Comes with added bindings for more terse yet powerful for statements.

    Install

    npm install babel-plugin-transform-expressive-loops

    .babelrc

    {
        "plugins": [
            "transform-expressive-loops"
        ]
    }

    What does this plugin do?

    This plugin allows for the convenient binding of variables commonly associated with iterator loops. For instance, if one wishes to bind the index, item, or a {property} in a single statement, they may do so with this transform.

    How does it do that?

    This plugin leverages the binary in opperator (e.g. prop in object) in addition to the one already in standard for(x in y) (or of) statements.

    This plugin won't affect for-statements not clearly using mechanics shown below, given that, it's quite safe. While the used syntax is technically legal in plain ECMAScript, it is otherwise inert or will lead to run-time errors, so there is no risk of collision† or conflation in natural code.

    Syntax

    The first keyword determines type, in or of, all subsequent keywords are in.

    For-In

    for ( key in object ) statement;
    
    for ( item in key in object ) statement;
    for ( { item_prop } in key in object ) statement;
    for ( { item_prop } in item in key in object) statement;
    
    `

    For-Of

    for ( item of iterable ) statement;
    for ( { item_prop } of iterable ) statement;
    
    for ( item of index of iterable ) statement;
    for ( { item_prop } of index in iterable ) statement;
    for ( { item_prop } of item in index in iterable) statement;
    
    for ( comma_delimited_word of List : StringLiteral ) statement;
    for ( number of Repetitions : IntegerLiteral ) statement;
    
    `

    Examples

    For-In Loops

    for(item in key in object){
        object[key] === item;
        //true
    }
    
    for({item_prop} in object){
        item_prop === object[_key];
        //true, if _key was available, which it isn't.
    }
    
    for({foo, bar} in item in key in object){
        item === object[key];
        foo === item.foo;
        bar === item.bar;
        //true
    }

    For-Of Loops

    for(item of index in iterable){
        item === iterable[index]
        //true
    }
    
    for({foo, bar} of item in index in iterable){
        item === iterable[index];
        foo === item.foo;
        bar === item.bar;
        //true
    }
    

    Convenience Features (Experimental!)

    keep in mind, this only works for literal strings and numbers

    for(item of i in "foo, bar, baz"){
    
        const equivalentTo = `foo, bar, baz`.split(", ");
        //Note: The split is done at compile time for StringLiteral, otherwise
        //      a split call is returned, for template expressions, as seen here.
        item === equivalentTo[i]
        //true
    }
    
    for(number of 3){
        console.log(number)
        // 0
        // 1
        // 2
    }
    

    License

    MIT License