From 886f20be37dfc7ab561587964f2d06367abf29f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20i?= =?UTF-8?q?nvntrm=40?= Date: Wed, 8 Feb 2017 13:24:07 +0300 Subject: [PATCH] Make shim-array.js less breaking world around --- shim-array.js | 61 ++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/shim-array.js b/shim-array.js index a6d74c7..4d77ed4 100644 --- a/shim-array.js +++ b/shim-array.js @@ -17,40 +17,47 @@ module.exports = Array; var array_splice = Array.prototype.splice; var array_slice = Array.prototype.slice; -Array.empty = []; +if (!Array.empty) { + Array.empty = []; -if (Object.freeze) { - Object.freeze(Array.empty); + if (Object.freeze) { + Object.freeze(Array.empty); + } } -Array.from = function (values) { - var array = []; - array.addEach(values); - return array; -}; +if (!Array.from) { + // Does not compatible with iterators + Array.from = function (values) { + var array = []; + array.addEach(values); + return array; + }; +} -Array.unzip = function (table) { - var transpose = []; - var length = Infinity; - // compute shortest row - for (var i = 0; i < table.length; i++) { - var row = table[i]; - table[i] = row.toArray(); - if (row.length < length) { - length = row.length; +if (!Array.unzip) { + Array.unzip = function (table) { + var transpose = []; + var length = Infinity; + // compute shortest row + for (var i = 0; i < table.length; i++) { + var row = table[i]; + table[i] = row.toArray(); + if (row.length < length) { + length = row.length; + } } - } - for (var i = 0; i < table.length; i++) { - var row = table[i]; - for (var j = 0; j < row.length; j++) { - if (j < length && j in row) { - transpose[j] = transpose[j] || []; - transpose[j][i] = row[j]; + for (var i = 0; i < table.length; i++) { + var row = table[i]; + for (var j = 0; j < row.length; j++) { + if (j < length && j in row) { + transpose[j] = transpose[j] || []; + transpose[j][i] = row[j]; + } } } - } - return transpose; -}; + return transpose; + }; +} function define(key, value) { Object.defineProperty(Array.prototype, key, {