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

Support Map for iteration and nested paths #1679

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/handlebars/helpers/each.js
Expand Up @@ -3,7 +3,8 @@ import {
blockParams,
createFrame,
isArray,
isFunction
isFunction,
isMap
} from '../utils';
import Exception from '../exception';

Expand Down Expand Up @@ -33,7 +34,7 @@ export default function(instance) {
data = createFrame(options.data);
}

function execIteration(field, index, last) {
function execIteration(field, value, index, last) {
if (data) {
data.key = field;
data.index = index;
Expand All @@ -47,22 +48,25 @@ export default function(instance) {

ret =
ret +
fn(context[field], {
fn(value, {
data: data,
blockParams: blockParams(
[context[field], field],
[contextPath + field, null]
)
blockParams: blockParams([value, field], [contextPath + field, null])
});
}

if (context && typeof context === 'object') {
if (isArray(context)) {
for (let j = context.length; i < j; i++) {
if (i in context) {
execIteration(i, i, i === context.length - 1);
execIteration(i, context[i], i, i === context.length - 1);
}
}
} else if (isMap(context)) {
const j = context.size - 1;
for (const [key, value] of context) {
execIteration(key, value, i, i === j);
i++;
}
} else if (global.Symbol && context[global.Symbol.iterator]) {
const newContext = [];
const iterator = context[global.Symbol.iterator]();
Expand All @@ -71,7 +75,7 @@ export default function(instance) {
}
context = newContext;
for (let j = context.length; i < j; i++) {
execIteration(i, i, i === context.length - 1);
execIteration(i, context[i], i, i === context.length - 1);
}
} else {
let priorKey;
Expand All @@ -81,13 +85,13 @@ export default function(instance) {
// the last iteration without have to scan the object twice and create
// an itermediate keys array.
if (priorKey !== undefined) {
execIteration(priorKey, i - 1);
execIteration(priorKey, context[priorKey], i - 1);
}
priorKey = key;
i++;
});
if (priorKey !== undefined) {
execIteration(priorKey, i - 1, true);
execIteration(priorKey, context[priorKey], i - 1, true);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/handlebars/runtime.js
Expand Up @@ -127,6 +127,9 @@ export function template(templateSpec, env) {
return obj[name];
},
lookupProperty: function(parent, propertyName) {
if (Utils.isMap(parent)) {
return parent.get(propertyName);
}
let result = parent[propertyName];
if (result == null) {
return result;
Expand Down
6 changes: 6 additions & 0 deletions lib/handlebars/utils.js
Expand Up @@ -57,6 +57,12 @@ export const isArray =
: false;
};

export const isMap = function(value) {
return value && typeof value === 'object'
? toString.call(value) === '[object Map]'
: false;
};

// Older IE versions do not directly support indexOf so we must implement our own, sadly.
export function indexOf(array, value) {
for (let i = 0, len = array.length; i < len; i++) {
Expand Down
8 changes: 8 additions & 0 deletions spec/basic.js
Expand Up @@ -394,6 +394,14 @@ describe('basic context', function() {
);
});

it('nested paths with Map', function() {
var map = new Map();
map.set('expression', 'beautiful');
expectTemplate('Goodbye {{foo/expression}} world!')
.withInput({ foo: map })
.toCompileTo('Goodbye beautiful world!');
});

it('nested paths with empty string value', function() {
shouldCompileTo(
'Goodbye {{alan/expression}} world!',
Expand Down
21 changes: 21 additions & 0 deletions spec/builtins.js
Expand Up @@ -551,6 +551,27 @@ describe('builtin helpers', function() {
);
});
}

it('each on Map and @key', function() {
var goodbyes = new Map();
goodbyes.set('first', 'goodbye');
goodbyes.set('second', 'Goodbye');
goodbyes.set('last', 'GOODBYE');
var goodbyesEmpty = new Map();
var string =
'{{#each goodbyes}}{{@key}}. {{.}}! {{/each}}cruel {{world}}!';
var hash = { goodbyes: goodbyes, world: 'world' };

expectTemplate(string)
.withInput(hash)
.toCompileTo(
'first. goodbye! second. Goodbye! last. GOODBYE! cruel world!'
);

expectTemplate(string)
.withInput({ goodbyes: goodbyesEmpty, world: 'world' })
.toCompileTo('cruel world!');
});
});

describe('#log', function() {
Expand Down