question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal for additional $ properties

See original GitHub issue

I don’t want to submit a PR without some feedback about this first. Essentially, I’d like to add a handful of very useful $ properties.

Currently, we have $idx and $len. I’d like to add the following:

  • $first - equals true during the first iteration, false otherwise
  • $last - equals true during the last iteration, false otherwise
  • $count - equals the current count of the iteration (1-based index)

I’ve come across a number of circumstances where the corresponding helpers are too verbose or don’t work. Perhaps the biggest use case is being able to use the proposed properties with comparison helpers. Some examples:

{?$first}...{/$first}               <-- first item only
{^$first}...{/$first}               <-- everything except the first item

{?$last}...{/$last}                 <-- last item only
{^$last}...{/$last}                 <-- everything except the last item

{@eq key=$count value=4}{/eq}       <-- a specific item

I realize Dust aims to use “less logic”, but sometimes we don’t have complete control over datasets (or we have to go out of our way to adapt them). These special properties add a huge convenience to Dust templates.

Here is a simple modification to this section of the code that adds the propsed properties:

if (len > 0) {
  head = context.stack && context.stack.head || {};
  head.$len = len;
  for (i = 0; i < len; i++) {
    head.$first = i === 0;
    head.$last = i === len - 1;
    head.$count = i + 1;
    head.$idx = i;
    chunk = body(chunk, context.push(elem[i], i, len));
  }
  head.$idx = undefined;
  head.$len = undefined;
  head.$first = undefined;
  head.$last = undefined;
  head.$count = undefined;
  return chunk;
} else if (skip) {
  return skip(this, context);
}

I haven’t done extensive testing, but it’s a pretty straight-forward addition that seems to work well. (Of course, it works properly in nested loops as well.)

FWIW, I’d also suggest the aliases $index and $length for consistency and legibility, but that’s another discussion.

Interested to hear the developer’s and the community’s thoughts on this.

Re:

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sethkinastcommented, Jan 29, 2017

I would suggest not placing them on head itself. See #654.

I wish there were a slightly better way to bundle these metadata together but $ props seems fine. I’m a fan. @jimmyhchan?

0reactions
claviskacommented, Feb 1, 2017

@jimmyhchan Making properties extensible will be incredible, especially if defaults can be overwritten with custom props (like helpers and filters). Great idea. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Additional Properties Sample Clauses - Law Insider
Additional Properties. (a) Additional Real Estate Assets owned by the Borrower or by a Guarantor Subsidiary may become Mortgaged Properties with the approval...
Read more >
Property Management Proposal Template - PandaDoc
A property management proposal is a sales document that explains what you can do for your prospective client. Proposals outline real estate services...
Read more >
BUSINESS PROPOSAL FOR JPO REAL ESTATE, LLC by ...
The acquisition strategy will be to purchase properties at a deep discount whose values are reduced below market value due to foreclosure or...
Read more >
CE TSOs' proposal for additional properties of FCR ... - entso-e
The FCR additional properties proposal takes into account the general principles and goals set in the. 15.
Read more >
City of Watertown Vacant Homes Redevelopment Initiative ...
Note: If submitting proposals for more than one property, please submit a separate proposal form and supporting documentation for each property.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found