Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Advance ECMAScript 2017 additions to es2017.js script
Browse files Browse the repository at this point in the history
  • Loading branch information
inexorabletash committed Nov 5, 2017
1 parent 459c53c commit 84cb3b3
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 219 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Since I generally use several in my hobby projects, bundled/minified versions ar

* [web.js](web.js) (minified: [web.min.js](web.min.js)) includes the most common Web polyfills - it assumes ES2015 support
* Includes: [html.js](html.js) [dom.js](dom.js) [xhr.js](xhr.js) [cssom.js](cssom.js) [url.js](url.js) [fetch.js](fetch.js)
* [polyfill.js](polyfill.js) (minified: [polyfill.min.js](polyfill.min.js)) has everything in [web.js](web.js) plus [es5.js](es5.js) and [es6.js](es6.js) and [es2016.js](es2016.js)
* [polyfill.js](polyfill.js) (minified: [polyfill.min.js](polyfill.min.js)) has everything in [web.js](web.js) plus [es5.js](es5.js) and [es6.js](es6.js) and [es2016.js](es2016.js) and [es2017.js](es2017.js)

Minification is done via https://github.com/mishoo/UglifyJS2

> Some of the files use `console.assert()` calls to catch bugs during development. These are
> automatically removed from the included minified versions. If you use your own minifying
> automatically removed from the included minified versions. If you use your own minifying
> processor it may cause to assertions to appear when unnecessary function names are stripped.
> You can safely remove these lines as part of a build step (e.g. using `grep -V`), or use a
> minifier that does this automatically. For [UglifyJS2](https://github.com/mishoo/UglifyJS2)
Expand All @@ -52,7 +52,9 @@ ECMAScript / JavaScript Polyfills

[ECMAScript 2015](es6.md) - Previous standard, supported by browsers circa 2016.

[ECMAScript 2016](es2016.md) - Most recent standard. Not fully supported by modern browsers yet.
[ECMAScript 2016](es2016.md) - Previous standard, supported by browsers circa 2017.

[ECMAScript 2017](es2017.md) - Most recent standard. Implementation in progress or complete in latest browsers.

[ECMAScript proposed](experimental/es-proposed.md) - Proposals for future editions of the standard. Here there be dragons.

Expand Down
208 changes: 208 additions & 0 deletions es2017.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
//----------------------------------------------------------------------
//
// ECMAScript 2017 Polyfills
//
//----------------------------------------------------------------------

(function (global) {
'use strict';
var undefined = (void 0); // Paranoia

// Helpers

function isSymbol(s) {
return (typeof s === 'symbol') || ('Symbol' in global && s instanceof global.Symbol);
}

function define(o, p, v, override) {
if (p in o && !override)
return;

if (typeof v === 'function') {
// Sanity check that functions are appropriately named (where possible)
console.assert(isSymbol(p) || !('name' in v) || v.name === p || v.name === p + '_', 'Expected function name "' + p.toString() + '", was "' + v.name + '"');
Object.defineProperty(o, p, {
value: v,
configurable: true,
enumerable: false,
writable: true
});
} else {
Object.defineProperty(o, p, {
value: v,
configurable: false,
enumerable: false,
writable: false
});
}
}

// Snapshot intrinsic functions
var $isNaN = global.isNaN;

var abs = Math.abs,
floor = Math.floor,
min = Math.min;

//----------------------------------------
// 7 Abstract Operations
//----------------------------------------

// 7.1.4
function ToInteger(n) {
n = Number(n);
if ($isNaN(n)) return 0;
if (n === 0 || n === Infinity || n === -Infinity) return n;
return ((n < 0) ? -1 : 1) * floor(abs(n));
}

// 7.1.13 ToObject
function ToObject(v) {
if (v === null || v === undefined) throw TypeError();
return Object(v);
}

// 7.1.15 ToLength ( argument )
function ToLength(v) {
var len = ToInteger(v);
if (len <= 0) {
return 0;
}
return min(len, 0x20000000000000 - 1); // 2^53-1
}

//----------------------------------------
// 7.3 Operations on Objects
//----------------------------------------

// 7.3.4
function CreateDataProperty(O, P, V) {
Object.defineProperty(O, P, {
value: V,
writable: true,
enumerable: true,
configurable: true
});
}

// 7.3.21
function EnumerableOwnProperties(o, kind) {
var ownKeys = Object.keys(o);
var properties = [];
ownKeys.forEach(function(key) {
var desc = Object.getOwnPropertyDescriptor(o, key);
if (desc && desc.enumerable) {
if (kind === 'key') properties.push(key);
else {
var value = o[key];
if (kind === 'value') properties.push(value);
else properties.push([key, value]);
}
}
});
return properties;
}


//----------------------------------------------------------------------
// 19 Fundamental Objects
//----------------------------------------------------------------------

// 19.1 Object Objects
// 19.1.2 Properties of the Object Constructor

// 19.1.2.5 Object.entries
define(
Object, 'entries',
function entries(o) {
var obj = ToObject(o);
return EnumerableOwnProperties(obj, 'key+value');
});

// 19.1.2.8 Object.getOwnPropertyDescriptors ( O )
define(
Object, 'getOwnPropertyDescriptors',
function getOwnPropertyDescriptors(o) {
var obj = ToObject(o);
// ReturnIfAbrupt(obj)
var keys = Object.getOwnPropertyNames(obj);
// ReturnIfAbrupt(keys)
var descriptors = {};
for (var i = 0; i < keys.length; ++i) {
var nextKey = keys[i];
var descriptor = Object.getOwnPropertyDescriptor(obj, nextKey);
// ReturnIfAbrupt(desc)
// ReturnIfAbrupt(descriptor)
CreateDataProperty(descriptors, nextKey, descriptor);
}
return descriptors;
});

// 19.1.2.21 Object.values
define(
Object, 'values',
function values(o) {
var obj = ToObject(o);
return EnumerableOwnProperties(obj, 'value');
});



//----------------------------------------------------------------------
// 21 Text Processing
//----------------------------------------------------------------------

// 21.1 String Objects
// 21.1.3 Properties of the String Prototype Object

// 21.1.3.13 String.prototype.padEnd( maxLength [ , fillString ] )
define(
String.prototype, 'padEnd',
function padEnd(maxLength) {
var fillString = arguments[1];

var o = this;
// ReturnIfAbrupt(o)
var s = String(this);
// ReturnIfAbrupt(s)
var stringLength = s.length;
if (fillString === undefined) var fillStr = '';
else fillStr = String(fillString);
// ReturnIfAbrupt(fillStr)
if (fillStr === '') fillStr = ' ';
var intMaxLength = ToLength(maxLength);
// ReturnIfAbrupt(intMaxLength)
if (intMaxLength <= stringLength) return s;
var fillLen = intMaxLength - stringLength;
var stringFiller = '';
while (stringFiller.length < fillLen)
stringFiller = stringFiller + fillStr;
return s + stringFiller.substring(0, fillLen);
});

// 21.1.3.14 String.prototype.padStart( maxLength [ , fillString ] )
define(
String.prototype, 'padStart',
function padStart(maxLength) {
var fillString = arguments[1];

var o = this;
// ReturnIfAbrupt(o)
var s = String(this);
// ReturnIfAbrupt(s)
var stringLength = s.length;
if (fillString === undefined) var fillStr = '';
else fillStr = String(fillString);
// ReturnIfAbrupt(fillStr)
if (fillStr === '') fillStr = ' ';
var intMaxLength = ToLength(maxLength);
// ReturnIfAbrupt(intMaxLength)
if (intMaxLength <= stringLength) return s;
var fillLen = intMaxLength - stringLength;
var stringFiller = '';
while (stringFiller.length < fillLen)
stringFiller = stringFiller + fillStr;
return stringFiller.substring(0, fillLen) + s;
});

}(this));
14 changes: 14 additions & 0 deletions es2017.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ECMAScript 2017 Polyfill

[script](es2017.js) -
[unit tests](https://inexorabletash.github.io/polyfill/tests/es2017.html)

This assumes ES2016, so use [es5.js](es5.js), [es6.js](es6.js) and [es2016.js](es2016.js) for older browsers (IE9-).

[ECMAScript Standard](http://www.ecma-international.org/ecma-262/)

* `Object.entries()` [ref](https://tc39.github.io/ecma262/#sec-object.entries)
* `Object.values()` [ref](https://tc39.github.io/ecma262/#sec-object.values)
* `Object.getOwnPropertyDescriptors` [ref](https://tc39.github.io/ecma262/#sec-object.getownpropertydescriptors)
* `String.prototype.padEnd()` [ref](https://tc39.github.io/ecma262/#sec-string.prototype.padend)
* `String.prototype.padStart()` [ref](https://tc39.github.io/ecma262/#sec-string.prototype.padstart)
Loading

0 comments on commit 84cb3b3

Please sign in to comment.