summaryrefslogtreecommitdiff
path: root/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSArrayOps.scala
blob: 71f705d2b23bf406fa823c04acb5946714ade95d (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js API               **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-lang.org/     **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


/**
 * All doc-comments marked as "MDN" are by Mozilla Contributors,
 * distributed under the Creative Commons Attribution-ShareAlike license from
 * https://developer.mozilla.org/en-US/docs/Web/Reference/API
 */
package scala.scalajs.js

import scala.language.implicitConversions

import scala.scalajs.js.annotation._

/** Discouraged native JavaScript Array methods.
 *
 *  In general, you should prefer the Scala collection methods available
 *  implicitly through [[ArrayOps]], because they are inlineable, and hence
 *  faster.
 *
 *  To enable the use of these functions on js.[[Array]]s, import the implicit
 *  conversion [[JSArrayOps.jsArrayOps]].
 */
trait JSArrayOps[A] extends Object {

  /**
   * The indexOf() method returns the first index at which a given element can
   * be found in the array, or -1 if it is not present.
   *
   * MDN
   */
  @JSName("indexOf")
  def jsIndexOf(searchElement: A, fromIndex: Int): Int = native
  @JSName("indexOf")
  def jsIndexOf(searchElement: A): Int = native

  /**
   * The lastIndexOf() method returns the last index at which a given element
   * can be found in the array, or -1 if it is not present. The array is
   * searched backwards, starting at fromIndex.
   *
   * MDN
   */
  @JSName("lastIndexOf")
  def jsLastIndexOf(searchElement: A, fromIndex: Int): Int = native
  @JSName("lastIndexOf")
  def jsLastIndexOf(searchElement: A): Int = native

  /**
   * The every method executes the provided callback function once for each
   * element present in the array until it finds one where callback returns
   * a falsy value (a value that becomes false when converted to a Boolean).
   * If such an element is found, the every method immediately returns false.
   * Otherwise, if callback returned a true value for all elements, every
   * will return true. callback is invoked only for indexes of the array
   * which have assigned values; it is not invoked for indexes which have been
   * deleted or which have never been assigned values.
   *
   * callback is invoked with three arguments:
   *
   * - the value of the element
   * - the index of the element
   * - and the Array object being traversed.
   *
   * If a thisObject parameter is provided to every, it will be used as the
   * this for each invocation of the callback. If it is not provided, or is
   * null, the global object associated with callback is used instead.
   *
   * every does not mutate the array on which it is called.
   *
   * every acts like the "for all" quantifier in mathematics. In particular, for
   * an empty array, it returns true. (It is vacuously true that all elements of
   * the empty set satisfy any given condition.)
   *
   * MDN
   */
  @JSName("every")
  def jsEvery[T](callbackfn: ThisFunction3[T, A, Int, Array[A], Boolean],
      thisArg: T): Boolean = native
  @JSName("every")
  def jsEvery(callbackfn: Function3[A, Int, Array[A], Boolean]): Boolean = native

  /**
   * some executes the callback function once for each element present in the
   * array until it finds one where callback returns a true value. If such an
   * element is found, some immediately returns true. Otherwise, some returns
   * false. callback is invoked only for indexes of the array which have assigned
   * values; it is not invoked for indexes which have been deleted or which
   * have never been assigned values.
   *
   * callback is invoked with three arguments: the value of the element, the index
   * of the element, and the Array object being traversed.
   *
   * If a thisObject parameter is provided to some, it will be used as the this
   * for each invocation of the callback. If it is not provided, or is null,
   * the global object associated with callback is used instead.
   *
   * some does not mutate the array on which it is called.
   *
   * MDN
   */
  @JSName("some")
  def jsSome[T](callbackfn: ThisFunction3[T, A, Int, Array[A], Boolean],
      thisArg: T): Boolean = native
  @JSName("some")
  def jsSome(callbackfn: Function3[A, Int, Array[A], Boolean]): Boolean = native
  @JSName("some")
  def jsSome(callbackfn: Function2[A, Int, Boolean]): Boolean = native
  @JSName("some")
  def jsSome(callbackfn: Function1[A, Boolean]): Boolean = native

  /**
   * forEach executes the provided callback once for each element of the array
   * with an assigned value. It is not invoked for indexes which have been deleted
   * or which have been initialized to undefined.
   *
   * callback is invoked with three arguments:
   *
   * - the element value
   * - the element index
   * - the array being traversed
   *
   * If a thisArg parameter is provided to forEach, it will be used as the
   * this value for each callback invocation as if callback.call(thisArg,
   * element, index, array) was called. If thisArg is undefined or null,
   * the this value within the function depends on whether the function
   * is in strict mode or not (passed value if in strict mode, global object
   * if in non-strict mode).
   *
   * MDN
   */
  @JSName("forEach")
  def jsForEach[T](callbackfn: ThisFunction3[T, A, Int, Array[A], _],
      thisArg: T): Unit = native
  @JSName("forEach")
  def jsForEach(callbackfn: Function3[A, Int, Array[A], _]): Unit = native
  @JSName("forEach")
  def jsForEach(callbackfn: Function2[A, Int, _]): Unit = native
  @JSName("forEach")
  def jsForEach(callbackfn: Function1[A, _]): Unit = native

  /**
   * map calls a provided callback function once for each element in an array,
   * in order, and constructs a new array from the results. callback is
   * invoked only for indexes of the array which have assigned values; it is
   * not invoked for indexes which have been deleted or which have never been
   * assigned values.
   *
   * callback is invoked with three arguments: the value of the element, the
   * index of the element, and the Array object being traversed.
   *
   * If a thisArg parameter is provided to map, it will be used as the this for
   * each invocation of the callback. If it is not provided, or is null, the
   * global object associated with callback is used instead.
   *
   * map does not mutate the array on which it is called.
   *
   * MDN
   */
  @JSName("map")
  def jsMap[B, T](callbackfn: ThisFunction3[T, A, Int, Array[A], B],
      thisArg: T): Array[B] = native
  @JSName("map")
  def jsMap[B](callbackfn: Function3[A, Int, Array[A], B]): Array[B] = native
  @JSName("map")
  def jsMap[B](callbackfn: Function2[A, Int, B]): Array[B] = native
  @JSName("map")
  def jsMap[B](callbackfn: Function1[A, B]): Array[B] = native

  /**
   * filter calls a provided callback function once for each element in an array,
   * and constructs a new array of all the values for which callback returns a true
   * value. callback is invoked only for indexes of the array which have assigned
   * values; it is not invoked for indexes which have been deleted or which have
   * never been assigned values. Array elements which do not pass the callback
   * test are simply skipped, and are not included in the new array.
   *
   * callback is invoked with three arguments:
   *
   * - the value of the element
   * - the index of the element
   * - the Array object being traversed
   *
   * If a thisObject parameter is provided to filter, it will be used as the this
   * for each invocation of the callback. If it is not provided, or is null, the
   * global object associated with callback is used instead.
   *
   * filter does not mutate the array on which it is called.
   *
   * MDN
   */
  @JSName("filter")
  def jsFilter[T](callbackfn: ThisFunction3[T, A, Int, Array[A], Boolean],
      thisArg: T): Array[A] = native
  @JSName("filter")
  def jsFilter(callbackfn: Function3[A, Int, Array[A], Boolean]): Array[A] = native
  @JSName("filter")
  def jsFilter(callbackfn: Function2[A, Int, Boolean]): Array[A] = native
  @JSName("filter")
  def jsFilter(callbackfn: Function1[A, Boolean]): Array[A] = native

  /**
   * reduce executes the callback function once for each element present in
   * the array, excluding holes in the array, receiving four arguments: the
   * initial value (or value from the previous callback call), the value of
   * the current element, the current index, and the array over which
   * iteration is occurring.
   *
   * The first time the callback is called, previousValue and currentValue can
   * be one of two values. If initialValue is provided in the call to reduce,
   * then previousValue will be equal to initialValue and currentValue will be
   * equal to the first value in the array. If no initialValue was provided,
   * then previousValue will be equal to the first value in the array and
   * currentValue will be equal to the second.
   *
   * MDN
   */
  @JSName("reduce")
  def jsReduce[B](callbackfn: Function4[B, A, Int, Array[A], B], initialValue: B): B = native
  @JSName("reduce")
  def jsReduce[B](callbackfn: Function3[B, A, Int, B], initialValue: B): B = native
  @JSName("reduce")
  def jsReduce[B](callbackfn: Function2[B, A, B], initialValue: B): B = native
  @JSName("reduce")
  def jsReduce[B](callbackfn: Function4[B, A, Int, Array[A], B]): B = native
  @JSName("reduce")
  def jsReduce[B](callbackfn: Function3[B, A, Int, B]): B = native
  @JSName("reduce")
  def jsReduce[B](callbackfn: Function2[B, A, B]): B = native

  /**
   * reduceRight executes the callback function once for each element present
   * in the array, excluding holes in the array, receiving four arguments:
   * the initial value (or value from the previous callback call), the value
   * of the current element, the current index, and the array over which
   * iteration is occurring.
   *
   * MDN
   */
  @JSName("reduceRight")
  def jsReduceRight[B](callbackfn: Function4[B, A, Int, Array[A], B], initialValue: B): B = native
  @JSName("reduceRight")
  def jsReduceRight[B](callbackfn: Function3[B, A, Int, B], initialValue: B): B = native
  @JSName("reduceRight")
  def jsReduceRight[B](callbackfn: Function2[B, A, B], initialValue: B): B = native
  @JSName("reduceRight")
  def jsReduceRight[B](callbackfn: Function4[B, A, Int, Array[A], B]): B = native
  @JSName("reduceRight")
  def jsReduceRight[B](callbackfn: Function3[B, A, Int, B]): B = native
  @JSName("reduceRight")
  def jsReduceRight[B](callbackfn: Function2[B, A, B]): B = native

}

object JSArrayOps {
  @inline implicit def jsArrayOps[A](array: Array[A]): JSArrayOps[A] =
    array.asInstanceOf[JSArrayOps[A]]
}