Skip to content

Instantly share code, notes, and snippets.

@underwindfall
Created March 1, 2017 09:50
Show Gist options
  • Save underwindfall/130d65bda22ac3ad92aefd3f5f550646 to your computer and use it in GitHub Desktop.
Save underwindfall/130d65bda22ac3ad92aefd3f5f550646 to your computer and use it in GitHub Desktop.
Answers of Es6katas [http://es6katas.org/]
// 3: template strings - tagged
// To do: make all tests pass, leave the asserts unchanged!
describe('tagged template strings, are an advanced form of template strings', function() {
it('syntax: prefix the template string with a function to call (without "()" around it)', function() {
function tagFunction(s) {
return s.toString();
}
var evaluated = tagFunction `template string`;
assert.equal(evaluated, 'template string');
});
describe('the function can access each part of the template', function() {
describe('the 1st parameter - receives only the pure strings of the template string', function() {
function tagFunction(strings) {
return strings;
}
it('the strings are an array', function() {
var result = ['template string'];
assert.deepEqual(tagFunction`template string`, result);
});
it('expressions are NOT passed to it', function() {
var tagged = tagFunction`one${23}two`;
assert.deepEqual(tagged, ['one', 'two']);
});
});
describe('the 2nd and following parameters - contain the values of the processed substitution', function() {
var one = 1;
var two = 2;
var three = 3;
it('the 2nd parameter contains the first expression`s value', function() {
function firstValueOnly(strings, firstValue) {
return firstValue;
}
assert.equal(firstValueOnly`uno ${one}, dos ${two}`, 1);
});
it('the 3rd parameter contains the second expression`s value', function() {
function firstValueOnly(strings, firstValue, secondValue) {
return secondValue;
}
assert.equal(firstValueOnly`uno ${one}, dos ${two}`, 2);
});
it('using ES6 rest syntax, all values can be accessed via one variable', function() {
function valuesOnly(stringsArray, ...allValues) { // using the new ES6 rest syntax
return allValues;
}
assert.deepEqual(valuesOnly`uno=${one}, dos=${two}, tres=${three}`, [1, 2, 3]);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment