Skip to content

Instantly share code, notes, and snippets.

@egdelwonk
Created April 9, 2014 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egdelwonk/c2ce8ed5780224d7b845 to your computer and use it in GitHub Desktop.
Save egdelwonk/c2ce8ed5780224d7b845 to your computer and use it in GitHub Desktop.
// Array.from copies an array.
load(libdir + "asserts.js");
var testArray = [1, 2, 3];
//test that Array.from creates a new array
assertDeepEq(Array.from(testArray), testArray);
assertEq(Array.from(testArray) === testArray, false);
assertEq(Array.from(testArray) === Array.from(testArray), false);
// If an object has both .length and [@@iterator] properties, [@@iterator] is used.
load(libdir + "iteration.js");
load(libdir + "asserts.js");
var a = ['a', 'e', 'i', 'o', 'u'];
a[std_iterator] = function* () {
for (var i = this.length; i--; )
yield this[i];
};
var log = '';
function f(x) {
log += x;
return x + x;
}
var b = Array.from(a, f);
assertDeepEq(b, ['uu', 'oo', 'ii', 'ee', 'aa']);
assertEq(log, 'uoiea');
// Array.from string
load(libdir + "asserts.js");
var testString = "test string";
assertDeepEq(Array.from(testString), ['t', 'e', 's', 't', ' ', 's', 't', 'r', 'i', 'n', 'g']);
// Array.from string iterator maintains surrogate pairs.
load(libdir + "asserts.js");
var pairString = "\uD834\uDF06";
assertDeepEq(Array.from(pairString), [pairString]);
// Array.from: Assert typeerror if arrayLike is undefined or null
load(libdir + "asserts.js");
assertThrowsInstanceOf(() => Array.from(undefined), TypeError);
assertThrowsInstanceOf(() => Array.from(null), TypeError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment