Skip to content

Commit

Permalink
string.raw changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dekajp committed May 19, 2013
1 parent a5bf08e commit 998fa49
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
40 changes: 40 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,46 @@ var main = function() {
}
});

defineProperties(String, {
raw: function() {
var callSite = arguments[0];
var substitutions = Array.prototype.slice.call(arguments, 1);
/*var cooked = Object(callSite);

This comment has been minimized.

Copy link
@ljharb

ljharb May 20, 2013

Collaborator

also, why is this whole section commented out?

This comment has been minimized.

Copy link
@dekajp

dekajp May 20, 2013

Author Contributor

I will make callSite an Object and uncomment those lines - then this will be inline to the spec http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.5.3.4

var rawValue = cooked.raw;
var raw = Object(rawValue);
var len = raw.length;
var literalsegments = ToUint32(len);*/

This comment has been minimized.

Copy link
@paulmillr

paulmillr May 19, 2013

Owner

we have ES.ToUint32 for that purpose

This comment has been minimized.

Copy link
@dekajp

dekajp May 20, 2013

Author Contributor

sure , i will use ES


var literalsegments = callSite.length;

if (literalsegments === 0){return '';}

var stringElements = [];
var nextIndex = 0;

var nextKey , next, nextSeg, nextSub ;
while (nextIndex < literalsegments) {
nextKey = nextIndex.toString();
next = callSite[nextKey];
nextSeg = next.toString();

stringElements.push(nextSeg);

if (nextIndex + 1 >= literalsegments || nextIndex >= substitutions.length){
break;
}
next = substitutions[nextKey];
nextSub = next.toString();
stringElements.push(nextSub);

nextIndex++;

}

return stringElements.join('');
}
});

defineProperties(String.prototype, {
// Fast repeat, uses the `Exponentiation by squaring` algorithm.
repeat: function(times) {
Expand Down
13 changes: 13 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,17 @@ describe('String', function() {
expect(str.codePointAt(str.length)).to.be.undefined;
});
});

describe('#raw()', function() {
it('test 1', function() {
var str = 'The total is 10 ($11 with tax)';
var str2 = 'The total is {total} (${total * 1.01} with tax)';
expect(String.raw([ "The total is ", " ($", " with tax)" ],10,11)).to.eql(str);
expect(String.raw([ "The total is ", " ($", " with tax)" ],'{total}','{total * 1.01}')).to.eql(str2);

});

});


});

1 comment on commit 998fa49

@dekajp
Copy link
Contributor Author

@dekajp dekajp commented on 998fa49 May 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulmillr @ljharb

Here is the new code - d32a97e . thanks for your assistance and help :). I will submit a pull request

Please sign in to comment.