Skip to content

Commit 70fb13a

Browse files
committed
1 parent 88402dc commit 70fb13a

File tree

11 files changed

+84
-0
lines changed

11 files changed

+84
-0
lines changed

packages/core-js-compat/src/data.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,8 @@ export const data = {
16881688
// TODO: Remove from `core-js@4`
16891689
'esnext.string.at': {
16901690
},
1691+
'esnext.string.cooked': {
1692+
},
16911693
'esnext.string.code-points': {
16921694
},
16931695
// TODO: Remove from `core-js@4`

packages/core-js-compat/src/modules-by-versions.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@ export default {
111111
'esnext.array.from-async',
112112
'esnext.typed-array.from-async',
113113
],
114+
3.19: [
115+
'esnext.string.cooked',
116+
],
114117
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('../../modules/esnext.string.cooked');
2+
var path = require('../../internals/path');
3+
4+
module.exports = path.String.cooked;

packages/core-js/features/string/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var parent = require('../../stable/string');
2+
// TODO: remove from `core-js@4`
23
require('../../modules/esnext.string.at');
4+
require('../../modules/esnext.string.cooked');
35
// TODO: disabled by default because of the conflict with another proposal
46
// require('../../modules/esnext.string.at-alternative');
57
require('../../modules/esnext.string.code-points');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var $ = require('../internals/export');
2+
var toIndexedObject = require('../internals/to-indexed-object');
3+
var toString = require('../internals/to-string');
4+
var lengthOfArrayLike = require('../internals/length-of-array-like');
5+
6+
var ArrayPrototype = Array.prototype;
7+
var push = ArrayPrototype.push;
8+
var join = ArrayPrototype.join;
9+
10+
// `String.cooked` method
11+
// https://github.com/bathos/proposal-string-cooked
12+
$({ target: 'String', stat: true }, {
13+
cooked: function cooked(template /* , ...substitutions */) {
14+
var cookedTemplate = toIndexedObject(template);
15+
var literalSegments = lengthOfArrayLike(cookedTemplate);
16+
var argumentsLength = arguments.length;
17+
var elements = [];
18+
var i = 0;
19+
while (literalSegments > i) {
20+
var nextVal = cookedTemplate[i++];
21+
if (nextVal === undefined) throw TypeError('Incorrect template');
22+
push.call(elements, toString(nextVal));
23+
if (i === literalSegments) return join.call(elements, '');
24+
if (i < argumentsLength) push.call(elements, toString(arguments[i]));
25+
}
26+
}
27+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// https://github.com/bathos/proposal-string-cooked
2+
require('../modules/esnext.string.cooked');

packages/core-js/stage/0.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require('../proposals/efficient-64-bit-arithmetic');
22
require('../proposals/string-at');
3+
require('../proposals/string-cooked');
34
require('../proposals/url');
45
var parent = require('./1');
56

tests/commonjs.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
676676
ok(load(NS, 'set/some')(new Set([1, 2, 3]), it => typeof it == 'number'));
677677
ok(load(NS, 'set/symmetric-difference')(new Set([1, 2, 3]), [3, 4, 5]).size === 4);
678678
ok(load(NS, 'set/union')(new Set([1, 2, 3]), [3, 4, 5]).size === 5);
679+
ok(load(NS, 'string/cooked')`a${ 1 }b` === 'a1b');
679680
ok('next' in load(NS, 'string/code-points')('a'));
680681
ok('next' in load(NS, 'string/virtual/code-points').call('a'));
681682
ok(load(NS, 'symbol/async-dispose'));
@@ -770,6 +771,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
770771
load('proposals/seeded-random');
771772
load('proposals/set-methods');
772773
load('proposals/string-at');
774+
load('proposals/string-cooked');
773775
load('proposals/string-code-points');
774776
load('proposals/string-match-all');
775777
load('proposals/string-replace-all');

tests/compat/tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,9 @@ GLOBAL.tests = {
14691469
'esnext.string.code-points': function () {
14701470
return String.prototype.codePoints;
14711471
},
1472+
'esnext.string.cooked': function () {
1473+
return String.cooked;
1474+
},
14721475
'esnext.symbol.async-dispose': function () {
14731476
return Symbol.dispose;
14741477
},

tests/pure/esnext.string.cooked.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Symbol from 'core-js-pure/es/symbol';
2+
import cooked from 'core-js-pure/features/string/cooked';
3+
4+
QUnit.test('String.cooked', assert => {
5+
assert.isFunction(cooked);
6+
assert.arity(cooked, 1);
7+
assert.name(cooked, 'cooked');
8+
assert.strictEqual(cooked(['Hi\\n', '!'], 'Bob'), 'Hi\\nBob!', 'template is an array');
9+
assert.strictEqual(cooked('test', 0, 1, 2), 't0e1s2t', 'template is a string');
10+
assert.strictEqual(cooked('test', 0), 't0est', 'lacks substituting');
11+
12+
if (typeof Symbol == 'function' && !Symbol.sham) {
13+
assert.throws(() => cooked([Symbol()]), TypeError, 'throws on symbol #1');
14+
assert.throws(() => cooked('test', Symbol()), TypeError, 'throws on symbol #2');
15+
}
16+
17+
assert.throws(() => cooked([undefined]), TypeError);
18+
assert.throws(() => cooked(null), TypeError);
19+
});

tests/tests/esnext.string.cooked.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
QUnit.test('String.cooked', assert => {
2+
const { cooked } = String;
3+
assert.isFunction(cooked);
4+
assert.arity(cooked, 1);
5+
assert.name(cooked, 'cooked');
6+
assert.looksNative(cooked);
7+
assert.nonEnumerable(String, 'cooked');
8+
assert.strictEqual(cooked(['Hi\\n', '!'], 'Bob'), 'Hi\\nBob!', 'template is an array');
9+
assert.strictEqual(cooked('test', 0, 1, 2), 't0e1s2t', 'template is a string');
10+
assert.strictEqual(cooked('test', 0), 't0est', 'lacks substituting');
11+
12+
if (typeof Symbol == 'function' && !Symbol.sham) {
13+
assert.throws(() => cooked([Symbol()]), TypeError, 'throws on symbol #1');
14+
assert.throws(() => cooked('test', Symbol()), TypeError, 'throws on symbol #2');
15+
}
16+
17+
assert.throws(() => cooked([undefined]), TypeError);
18+
assert.throws(() => cooked(null), TypeError);
19+
});

0 commit comments

Comments
 (0)