Skip to content

Commit d5311ca

Browse files
andrew-colemanmattbaileyuk
authored andcommitted
remove Array.from() polyfill and recode references to it
Signed-off-by: andrew-coleman <[email protected]>
1 parent 63b4ffc commit d5311ca

File tree

4 files changed

+28
-86
lines changed

4 files changed

+28
-86
lines changed

polyfill.js

-80
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,6 @@ Number.isInteger = Number.isInteger || function(value) {
1313
Math.floor(value) === value;
1414
};
1515

16-
// Production steps of ECMA-262, Edition 6, 22.1.2.1
17-
if (!Array.from) {
18-
Array.from = (function () {
19-
'use strict';
20-
var toStr = Object.prototype.toString;
21-
var isCallable = function (fn) {
22-
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
23-
};
24-
var toInteger = function (value) {
25-
var number = Number(value);
26-
if (isNaN(number)) { return 0; }
27-
if (number === 0 || !isFinite(number)) { return number; }
28-
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
29-
};
30-
31-
var toLength = function (value) {
32-
var len = toInteger(value);
33-
return len >>> 0;
34-
};
35-
36-
// The length property of the from method is 1.
37-
return function from(arrayLike/*, mapFn, thisArg */) {
38-
// 1. Let C be the this value.
39-
var C = this;
40-
41-
// 2. Let items be ToObject(arrayLike).
42-
var items = Object(arrayLike);
43-
44-
// 3. ReturnIfAbrupt(items).
45-
if (arrayLike === null) {
46-
throw new TypeError('Array.from requires an array-like object - not null or undefined');
47-
}
48-
49-
// 4. If mapfn is undefined, then let mapping be false.
50-
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
51-
var T;
52-
if (typeof mapFn !== 'undefined') {
53-
// 5. else
54-
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
55-
if (!isCallable(mapFn)) {
56-
throw new TypeError('Array.from: when provided, the second argument must be a function');
57-
}
58-
59-
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
60-
if (arguments.length > 2) {
61-
T = arguments[2];
62-
}
63-
}
64-
65-
// 10. Let lenValue be Get(items, "length").
66-
// 11. Let len be ToLength(lenValue).
67-
var len = toLength(items.length);
68-
69-
// 13. If IsConstructor(C) is true, then
70-
// 13. a. Let A be the result of calling the [[Construct]] internal method
71-
// of C with an argument list containing the single item len.
72-
// 14. a. Else, Let A be ArrayCreate(len).
73-
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
74-
75-
// 16. Let k be 0.
76-
var k = 0;
77-
// 17. Repeat, while k < len… (also steps a - h)
78-
var kValue;
79-
while (k < len) {
80-
kValue = items[k];
81-
if (mapFn) {
82-
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
83-
} else {
84-
A[k] = kValue;
85-
}
86-
k += 1;
87-
}
88-
// 18. Let putStatus be Put(A, "length", len, true).
89-
A.length = len;
90-
// 20. Return A.
91-
return A;
92-
};
93-
}());
94-
}
95-
9616
if (!String.fromCodePoint) (function(stringFromCharCode) {
9717
var fromCodePoint = function(_) {
9818
var codeUnits = [], codeLen = 0, result = "";

src/datetime.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* This project is licensed under the MIT License, see LICENSE
55
*/
66

7+
const utils = require('./utils');
8+
79
/**
810
* DateTime formatting and parsing functions
911
* Implements the xpath-functions format-date-time specification
@@ -12,6 +14,8 @@
1214
const dateTime = (function () {
1315
'use strict';
1416

17+
const stringToArray = utils.stringToArray;
18+
1519
const few = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
1620
'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
1721
const ordinals = ['Zeroth', 'First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth',
@@ -270,7 +274,7 @@ const dateTime = (function () {
270274
formattedInteger = padding + formattedInteger;
271275
}
272276
if (format.zeroCode !== 0x30) {
273-
formattedInteger = Array.from(formattedInteger).map(code => {
277+
formattedInteger = stringToArray(formattedInteger).map(code => {
274278
return String.fromCodePoint(code.codePointAt(0) + format.zeroCode - 0x30);
275279
}).join('');
276280
}
@@ -371,7 +375,7 @@ const dateTime = (function () {
371375
let optionalDigits = 0;
372376
let groupingSeparators = [];
373377
let separatorPosition = 0;
374-
const formatCodepoints = Array.from(primaryFormat, c => c.codePointAt(0)).reverse(); // reverse the array to determine positions of grouping-separator-signs
378+
const formatCodepoints = stringToArray(primaryFormat).map(c => c.codePointAt(0)).reverse(); // reverse the array to determine positions of grouping-separator-signs
375379
formatCodepoints.forEach((codePoint) => {
376380
// step though each char in the picture to determine the digit group
377381
let digit = false;
@@ -813,7 +817,7 @@ const dateTime = (function () {
813817
return componentValue;
814818
};
815819

816-
const iso8601Spec = analyseDateTimePicture('[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01].[f001][Z01:01t]');
820+
let iso8601Spec = null;
817821

818822
/**
819823
* formats the date/time as specified by the XPath fn:format-dateTime function
@@ -907,6 +911,9 @@ const dateTime = (function () {
907911
let formatSpec;
908912
if(typeof picture === 'undefined') {
909913
// default to ISO 8601 format
914+
if (iso8601Spec === null) {
915+
iso8601Spec = analyseDateTimePicture('[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01].[f001][Z01:01t]');
916+
}
910917
formatSpec = iso8601Spec;
911918
} else {
912919
formatSpec = analyseDateTimePicture(picture);

src/functions.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const functions = (() => {
1919
var isIterable = utils.isIterable;
2020
var getFunctionArity = utils.getFunctionArity;
2121
var deepEquals = utils.isDeepEqual;
22+
var stringToArray = utils.stringToArray;
2223

2324
/**
2425
* Sum function
@@ -150,7 +151,7 @@ const functions = (() => {
150151
return undefined;
151152
}
152153

153-
var strArray = Array.from(str);
154+
var strArray = stringToArray(str);
154155
var strLength = strArray.length;
155156

156157
if (strLength + start < 0) {
@@ -247,7 +248,7 @@ const functions = (() => {
247248
return undefined;
248249
}
249250

250-
return Array.from(str).length;
251+
return stringToArray(str).length;
251252
}
252253

253254
/**

src/utils.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ const utils = (() => {
176176
return false;
177177
}
178178

179+
/**
180+
* converts a string to an array of characters
181+
* @param {string} str - the input string
182+
* @returns {Array} - the array of characters
183+
*/
184+
function stringToArray(str) {
185+
var arr = [];
186+
for (let char of str) {
187+
arr.push(char);
188+
}
189+
return arr;
190+
}
191+
179192
return {
180193
isNumeric,
181194
isArrayOfStrings,
@@ -186,7 +199,8 @@ const utils = (() => {
186199
isLambda,
187200
isIterable,
188201
getFunctionArity,
189-
isDeepEqual
202+
isDeepEqual,
203+
stringToArray
190204
};
191205
})();
192206

0 commit comments

Comments
 (0)