From b1a7c178562e861ab62aa08e73140af2c0ebaf26 Mon Sep 17 00:00:00 2001 From: Josh Crowther Date: Mon, 5 Mar 2018 09:45:07 -0800 Subject: [PATCH 1/3] Refactor to organize shims by Object --- packages/polyfill/index.ts | 4 +- .../src/shims/{findIndex.ts => Array.ts} | 51 ++++++++++++++- packages/polyfill/src/shims/String.ts | 9 +++ packages/polyfill/src/shims/find.ts | 64 ------------------- 4 files changed, 61 insertions(+), 67 deletions(-) rename packages/polyfill/src/shims/{findIndex.ts => Array.ts} (57%) create mode 100644 packages/polyfill/src/shims/String.ts delete mode 100644 packages/polyfill/src/shims/find.ts diff --git a/packages/polyfill/index.ts b/packages/polyfill/index.ts index 21b16b16ee6..f3714ae33fa 100644 --- a/packages/polyfill/index.ts +++ b/packages/polyfill/index.ts @@ -15,5 +15,5 @@ */ import './src/polyfills/promise'; -import './src/shims/find'; -import './src/shims/findIndex'; +import './src/shims/Array'; +import './src/shims/String'; diff --git a/packages/polyfill/src/shims/findIndex.ts b/packages/polyfill/src/shims/Array.ts similarity index 57% rename from packages/polyfill/src/shims/findIndex.ts rename to packages/polyfill/src/shims/Array.ts index 325c7f0d59d..79a1bd872c4 100644 --- a/packages/polyfill/src/shims/findIndex.ts +++ b/packages/polyfill/src/shims/Array.ts @@ -14,6 +14,55 @@ * limitations under the License. */ +/** + * This is the Array.prototype.find polyfill from MDN + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find + * https://tc39.github.io/ecma262/#sec-array.prototype.find + */ +if (!Array.prototype.find) { + Object.defineProperty(Array.prototype, 'find', { + value: function (predicate) { + // 1. Let O be ? ToObject(this value). + if (this == null) { + throw new TypeError('"this" is null or not defined'); + } + + var o = Object(this); + + // 2. Let len be ? ToLength(? Get(O, "length")). + var len = o.length >>> 0; + + // 3. If IsCallable(predicate) is false, throw a TypeError exception. + if (typeof predicate !== 'function') { + throw new TypeError('predicate must be a function'); + } + + // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + var thisArg = arguments[1]; + + // 5. Let k be 0. + var k = 0; + + // 6. Repeat, while k < len + while (k < len) { + // a. Let Pk be ! ToString(k). + // b. Let kValue be ? Get(O, Pk). + // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + // d. If testResult is true, return kValue. + var kValue = o[k]; + if (predicate.call(thisArg, kValue, k, o)) { + return kValue; + } + // e. Increase k by 1. + k++; + } + + // 7. Return undefined. + return undefined; + } + }); +} + /** * This is the Array.prototype.findIndex polyfill from MDN * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex @@ -21,7 +70,7 @@ */ if (!Array.prototype.findIndex) { Object.defineProperty(Array.prototype, 'findIndex', { - value: function(predicate) { + value: function (predicate) { // 1. Let O be ? ToObject(this value). if (this == null) { throw new TypeError('"this" is null or not defined'); diff --git a/packages/polyfill/src/shims/String.ts b/packages/polyfill/src/shims/String.ts new file mode 100644 index 00000000000..fa1418c80af --- /dev/null +++ b/packages/polyfill/src/shims/String.ts @@ -0,0 +1,9 @@ +/** + * This is the String.prototype.startsWith polyfill from MDN + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith + */ +if (!String.prototype.startsWith) { + String.prototype.startsWith = function (search, pos) { + return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; + }; +} diff --git a/packages/polyfill/src/shims/find.ts b/packages/polyfill/src/shims/find.ts deleted file mode 100644 index 587e23ddb67..00000000000 --- a/packages/polyfill/src/shims/find.ts +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * This is the Array.prototype.find polyfill from MDN - * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find - * https://tc39.github.io/ecma262/#sec-array.prototype.find - */ -if (!Array.prototype.find) { - Object.defineProperty(Array.prototype, 'find', { - value: function(predicate) { - // 1. Let O be ? ToObject(this value). - if (this == null) { - throw new TypeError('"this" is null or not defined'); - } - - var o = Object(this); - - // 2. Let len be ? ToLength(? Get(O, "length")). - var len = o.length >>> 0; - - // 3. If IsCallable(predicate) is false, throw a TypeError exception. - if (typeof predicate !== 'function') { - throw new TypeError('predicate must be a function'); - } - - // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. - var thisArg = arguments[1]; - - // 5. Let k be 0. - var k = 0; - - // 6. Repeat, while k < len - while (k < len) { - // a. Let Pk be ! ToString(k). - // b. Let kValue be ? Get(O, Pk). - // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). - // d. If testResult is true, return kValue. - var kValue = o[k]; - if (predicate.call(thisArg, kValue, k, o)) { - return kValue; - } - // e. Increase k by 1. - k++; - } - - // 7. Return undefined. - return undefined; - } - }); -} From 4f6d656e5114416959388a16a698f70dbf656802 Mon Sep 17 00:00:00 2001 From: Josh Crowther Date: Mon, 5 Mar 2018 09:45:32 -0800 Subject: [PATCH 2/3] [AUTOMATED]: Prettier Code Styling --- packages/polyfill/src/shims/Array.ts | 4 ++-- packages/polyfill/src/shims/String.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/polyfill/src/shims/Array.ts b/packages/polyfill/src/shims/Array.ts index 79a1bd872c4..798a4f3a343 100644 --- a/packages/polyfill/src/shims/Array.ts +++ b/packages/polyfill/src/shims/Array.ts @@ -21,7 +21,7 @@ */ if (!Array.prototype.find) { Object.defineProperty(Array.prototype, 'find', { - value: function (predicate) { + value: function(predicate) { // 1. Let O be ? ToObject(this value). if (this == null) { throw new TypeError('"this" is null or not defined'); @@ -70,7 +70,7 @@ if (!Array.prototype.find) { */ if (!Array.prototype.findIndex) { Object.defineProperty(Array.prototype, 'findIndex', { - value: function (predicate) { + value: function(predicate) { // 1. Let O be ? ToObject(this value). if (this == null) { throw new TypeError('"this" is null or not defined'); diff --git a/packages/polyfill/src/shims/String.ts b/packages/polyfill/src/shims/String.ts index fa1418c80af..a7ba0f779ca 100644 --- a/packages/polyfill/src/shims/String.ts +++ b/packages/polyfill/src/shims/String.ts @@ -3,7 +3,7 @@ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith */ if (!String.prototype.startsWith) { - String.prototype.startsWith = function (search, pos) { + String.prototype.startsWith = function(search, pos) { return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; }; } From 70cead7521d02765bff55af59e382b2ad2707cfa Mon Sep 17 00:00:00 2001 From: Josh Crowther Date: Mon, 5 Mar 2018 09:45:32 -0800 Subject: [PATCH 3/3] [AUTOMATED]: License Headers --- packages/polyfill/src/shims/String.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/polyfill/src/shims/String.ts b/packages/polyfill/src/shims/String.ts index a7ba0f779ca..c8c006f7c15 100644 --- a/packages/polyfill/src/shims/String.ts +++ b/packages/polyfill/src/shims/String.ts @@ -1,3 +1,19 @@ +/** + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /** * This is the String.prototype.startsWith polyfill from MDN * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith