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

Commit

Permalink
Add ECMAScript 2016 file, include in polyfill.js
Browse files Browse the repository at this point in the history
  • Loading branch information
inexorabletash committed Oct 3, 2016
1 parent 69ab81c commit 891d4f1
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 63 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ 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) [timing.js](timing.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)
* [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)

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


ECMAScript / JavaScript Polyfills
---------------------------------

[ECMAScript 5](es5.md) - Previous standard, supported by all modern browsers. Frozen.
[ECMAScript 5](es5.md) - Previous standard, supported by browsers circa 2012..

[ECMAScript 2015](es6.md) - Most recent standard. Not fully supported by modern browsers yet.
[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 proposed](experimental/es-proposed.md) - Proposals for future editions of the standard. Here there be dragons.

Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-polyfills",
"version": "0.1.26",
"version": "0.1.27",
"homepage": "https://github.com/inexorabletash/polyfill",
"authors": [
"Joshua Bell <[email protected]>"
Expand All @@ -10,6 +10,7 @@
"main": [
"es5.js",
"es6.js",
"es2016.js",
"html.js",
"dom.js",
"xhr.js",
Expand Down
4 changes: 1 addition & 3 deletions demos/keyboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
<title>Keyboard Event Polyfill Demo</title>
<meta charset="UTF-8">

<script type="text/javascript" src="../es5.js"></script>
<script type="text/javascript" src="../es6.js"></script>
<script type="text/javascript" src="../web.js"></script>
<script type="text/javascript" src="../polyfill.js"></script>
<script type="text/javascript" src="../keyboard.js"></script>

<style>
Expand Down
128 changes: 128 additions & 0 deletions es2016.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//----------------------------------------------------------------------
//
// ECMAScript 2016 Polyfills
//
//----------------------------------------------------------------------

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

// Helpers

function assert(c, m) {
if (!c) throw Error("Internal assertion failure" + (m ? ': ' + m : ''));
}

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)
assert((global.Symbol && p instanceof global.Symbol) || !('name' in v) || v.name === p || v.name === p + '_', 'Expected function name "' + p + '", 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,
max = Math.max,
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.2 Testing and Comparison Operations
//----------------------------------------

// 7.2.10 SameValueZero(x, y)
function SameValueZero(x, y) {
if (typeof x !== typeof y) return false;
switch (typeof x) {
case 'undefined':
return true;
case 'number':
if (x !== x && y !== y) return true;
return x === y;
case 'boolean':
case 'string':
case 'object':
default:
return x === y;
}
}

//----------------------------------------------------------------------
//
// ECMAScript 2016
//
//----------------------------------------------------------------------

// https://github.com/tc39/Array.prototype.includes/
define(
Array.prototype, 'includes',
function includes(target) {
var fromIndex = arguments[1];

var o = ToObject(this);
var len = ToLength(o["length"]);
if (len === 0) return false;
var n = ToInteger(fromIndex);
if (n >= 0) {
var k = n;
} else {
k = len + n;
if (k < 0) k = 0;
}
while (k < len) {
var elementK = o[k];
if (SameValueZero(o[k], target))
return true;
k += 1;
}
return false;
});

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

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

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

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

* `Array.prototype.includes()` [ref](http://www.ecma-international.org/ecma-262/7.0/index.html#sec-array.prototype.includes)
28 changes: 0 additions & 28 deletions experimental/es-proposed.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,34 +248,6 @@
return;
});

// https://github.com/tc39/Array.prototype.includes/
define(
Array.prototype, 'includes',
function includes(target) {
var fromIndex = arguments[1];

var o = ToObject(this);
var lenValue = o["length"];
var len = ToLength(lenValue);
if (len === 0) return false;
var n = (fromIndex !== undefined) ? ToInteger(fromIndex) : 0;
if (n >= len) return false;
if (n >= 0) {
var k = n;
} else {
k = len - abs(n);
if (k < 0) k = 0;
}
while (k < len) {
// eval('0 in [undefined]') == false in IE8-
if (/*i in t &&*/ SameValue(o[k], target)) {
return true;
}
k += 1;
}
return false;
});

// https://github.com/ljharb/proposal-object-values-entries
define(
Object, 'values',
Expand Down
4 changes: 0 additions & 4 deletions experimental/es-proposed.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ Per https://github.com/tc39/ecma262
[script](es-proposed.js) -
[unit tests](https://inexorabletash.github.io/polyfill/experimental/tests/es-proposed.html)

#### In ECMAScript 2016

* `Array.prototype.includes()` [ref](https://github.com/domenic/Array.prototype.contains/)

#### Stage 4 (In drafts for ECMAScript 2017)

* Object iterators: `Object.values()`, `Object.entries()` [ref](https://github.com/ljharb/proposal-object-values-entries)
Expand Down
1 change: 1 addition & 0 deletions experimental/tests/es-proposed.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ <h2 id="qunit-userAgent"></h2>
<script src="../../es5.js"></script>
<script src="../../typedarray.js"></script>
<script src="../../es6.js"></script>
<script src="../../es2016.js"></script>
<script src="../es-proposed.js"></script>

<script src="../../tests/testhelpers.js"></script>
Expand Down
20 changes: 0 additions & 20 deletions experimental/tests/es-proposed_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ test("Proposed Array extras", function () {
a = [1,2]; a.pushAll([]); deepEqual(a, [1,2]);
a = []; a.pushAll([1,2]); deepEqual(a, [1,2]);
a = [1,2]; a.pushAll([1,2]); deepEqual(a, [1,2,1,2]);

assertTrue("'includes' in Array.prototype");
assertEqual("typeof Array.prototype.includes", 'function');
assertTrue("[1,2,3].includes(1)");
assertFalse("[1,2,3].includes(0)");
assertTrue("[1,NaN,3].includes(NaN)");
assertFalse("[1,2,3].includes(NaN)");
assertTrue("[1,-0,3].includes(-0)");
assertFalse("[1,-0,3].includes(0)");
assertFalse("[1,[],3].includes([])");
assertFalse("[1,{},3].includes({})");
assertFalse("[1,2,3].includes(Math)");
assertTrue("[1,Math,3].includes(Math)");
assertFalse("[1,2,3].includes(undefined)");
assertTrue("[1,undefined,3].includes(undefined)");
assertFalse("[1,2,3].includes(null)");
assertTrue("[1,null,3].includes(null)");

assertTrue("[1,2,3].includes(3, 2)");
assertFalse("[1,2,3].includes(2, 2)");
});

test("Proposed Object Extras", function() {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-polyfills",
"author": "Joshua Bell <[email protected]>",
"version": "0.1.26",
"version": "0.1.27",
"description": "Collection of Web polyfills.",
"license": "Unlicense",
"main": "polyfill.js",
Expand All @@ -16,6 +16,8 @@
"es5.js",
"es6.md",
"es6.js",
"es2016.md",
"es2016.js",
"html.js",
"dom.js",
"xhr.js",
Expand Down
Loading

0 comments on commit 891d4f1

Please sign in to comment.