forked from jerryscript-project/jerryscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-prototype-map.js
More file actions
111 lines (92 loc) · 2.95 KB
/
array-prototype-map.js
File metadata and controls
111 lines (92 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright JS Foundation and other contributors, http://js.foundation
//
// 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.
// helper function - simple implementation
Array.prototype.equals = function (array) {
if (this.length != array.length)
return false;
for (var i = 0; i < this.length; i++) {
if (this[i] instanceof Array && array[i] instanceof Array) {
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
return false;
}
}
return true;
}
// check function type
try {
[0].map(new Object());
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
// various checks
assert ([1, 4, 9].map(Math.sqrt).equals([1, 2, 3]));
assert (isNaN([1, 4, "X"].map(Number)[2]));
var func = function(val, idx) {
return val + idx;
};
assert ([1, 4, 9].map(func).equals([1,5,11]));
assert ([1, "X", 10].map(func).equals([1, "X1", 12]));
var arr = [1,2,3];
arr.length = 5;
assert(arr.map(func).length === arr.length);
var long_array = [0, 1];
assert (long_array.map(func).equals([0,2]));
long_array[100] = 1;
assert (long_array.map(func).equals([0,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101]));
var arr = [1,2];
Array.prototype[0] = 3;
var newArr = arr.map(function(value) { return value; });
delete Array.prototype[0];
assert(newArr.hasOwnProperty("0"));
assert(newArr[0] === 1);
// check behavior when unable to get length
var obj = {};
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
obj.map = Array.prototype.map;
try {
obj.map(func);
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
// check behavior when unable to get element
var obj = {}
obj.length = 1;
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
obj.map = Array.prototype.map
try {
obj.map(func);
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
// check thisArg
var thisArg = {add: 10};
var func2 = function(value) {
return this.add + value;
}
assert([1,2].map(func2, thisArg).equals([11, 12]));
// check passed Object
var array_example = [1,2];
Object.defineProperty(array_example, 'const', { 'get' : function () {return "CT";} });
var func3 = function(value, idx, thisobj) {
return value * idx + thisobj.const;
}
assert(array_example.map(func3).equals(["0CT", "2CT"]));