Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object iterators added. #67

Closed
wants to merge 1 commit into from
Closed

Object iterators added. #67

wants to merge 1 commit into from

Conversation

milichev
Copy link

Added support of object property iterators, e.g. for (key in obj) {...}

Notation is similar to array iterators, '@' char is used instead of '~':

{{@ {one: 1, two: 2, three: 3} :val :key}}
    {{=key}} = {{=val}}
{{@}}

Also, this can iterate only own properties – with additional '@':

{{ obj = (function() {
    function O() {
        this.four = 4;
    }
    O.prototype = { one: 1, two: 2, three: 3 };
    return new O;
})(); }}

{{@@ obj :val :key}}
    {{=key}} = {{=val}}
{{@@}}

See advancedsnippet.txt
@milichev
Copy link
Author

And tests for this:

describe("iterate object (for..in)", function() {
    function Obj() {
        this.awesome = "doT.js";
    }
    Obj.prototype = {
        one: 1,
        2: "two",
        THREE: 3
    };

    var obj;

    beforeEach(function() {
        obj = new Obj();
    });

    it("should iterate all object properties", function() {
        var tpl = doT.compile('{{@ it :val :key}}[{{=key}}={{=val}}]{{@}}');
        var exp = '';
        for (var k in obj) {
            exp += "[" + k + "=" + obj[k] + "]";
        }
        expect(tpl(obj)).toBe(exp);
    });

    it("should iterate only own properties", function() {
        var tpl = doT.compile('{{@@ it :val :key}}[{{=key}}={{=val}}]{{@@}}');
        var exp = '';
        for (var k in obj) {
            if (obj.hasOwnProperty(k)) {
                exp += "[" + k + "=" + obj[k] + "]";
            }
        }
        expect(tpl(obj)).toBe(exp);
    });
});

@nijikokun
Copy link

Why hasn't this been pulled?

@wake
Copy link

wake commented Apr 17, 2013

Need a simple and friendly object iterators please. 👍

@milichev
Copy link
Author

Yep, we use them a lot.

Laura, what's with the pull?

tnx.

@fearphage
Copy link

Bump.

@yckart
Copy link

yckart commented Sep 13, 2013

👍

@fearphage
Copy link

It would be nice if the tests where a part of the commit.

@olado Any word on this making it to master?

@satazor
Copy link

satazor commented Oct 12, 2013

Bump.

@lhwparis
Copy link

bump! @olado do you still support this project?

@klimashkin
Copy link

+1

@VitalyKovalev
Copy link

same q to @Olando, do you support this project?

@zevero
Copy link

zevero commented Jul 9, 2015

Is there a case, where you would like to iterate without checking for own properties?

Please pull!!!

@night-codes
Copy link

👍

2 similar comments
@epoberezkin
Copy link
Collaborator

👍

@amigolargo
Copy link

+1

@epoberezkin
Copy link
Collaborator

@milichev see #124 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet