File: esnext.iterator.range.js

package info (click to toggle)
node-core-js 3.33.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,828 kB
  • sloc: javascript: 87,204; makefile: 13
file content (126 lines) | stat: -rw-r--r-- 4,700 bytes parent folder | download
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
/* eslint-disable es/no-bigint -- safe */
QUnit.test('Iterator.range', assert => {
  const { range } = Iterator;
  const { from } = Array;
  assert.isFunction(range);
  assert.name(range, 'range');
  assert.arity(range, 3);
  assert.looksNative(range);
  assert.nonEnumerable(Iterator, 'range');

  let iterator = range(1, 2);

  assert.isIterator(iterator);
  assert.isIterable(iterator);
  assert.deepEqual(iterator.next(), {
    value: 1,
    done: false,
  });
  assert.deepEqual(iterator.next(), {
    value: undefined,
    done: true,
  });

  assert.deepEqual(from(range(-1, 5)), [-1, 0, 1, 2, 3, 4]);
  assert.deepEqual(from(range(-5, 1)), [-5, -4, -3, -2, -1, 0]);
  assert.deepEqual(
    from(range(0, 1, 0.1)),
    [0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9],
  );
  assert.deepEqual(
    from(range(2 ** 53 - 1, 2 ** 53, { inclusive: true })),
    [9007199254740991, 9007199254740992],
  );
  assert.deepEqual(from(range(0, 0)), []);
  assert.deepEqual(from(range(0, -5, 1)), []);

  assert.deepEqual(from(range(NaN, 0)), []);
  assert.deepEqual(from(range(0, NaN)), []);
  assert.deepEqual(from(range(NaN, NaN)), []);
  assert.deepEqual(from(range(0, 0, { step: NaN })), []);
  assert.deepEqual(from(range(0, 5, NaN)), []);

  iterator = range(1, 3);
  assert.deepEqual(iterator.start, 1);
  assert.deepEqual(iterator.end, 3);
  assert.deepEqual(iterator.step, 1);
  assert.false(iterator.inclusive);

  iterator = range(-1, -3, { inclusive: true });
  assert.deepEqual(iterator.start, -1);
  assert.deepEqual(iterator.end, -3);
  assert.same(iterator.step, -1);
  assert.true(iterator.inclusive);

  iterator = range(-1, -3, { step: 4, inclusive() { /* empty */ } });
  assert.same(iterator.start, -1);
  assert.same(iterator.end, -3);
  assert.same(iterator.step, 4);
  assert.true(iterator.inclusive);

  iterator = range(0, 5);
  assert.throws(() => Object.getOwnPropertyDescriptor(iterator, 'start').get.call({}), TypeError);

  assert.throws(() => range(Infinity, 10, 0), RangeError);
  assert.throws(() => range(-Infinity, 10, 0), RangeError);
  assert.throws(() => range(0, 10, Infinity), RangeError);
  assert.throws(() => range(0, 10, { step: Infinity }), RangeError);

  assert.throws(() => range({}, 1), TypeError);
  assert.throws(() => range(1, {}), TypeError);
  assert.throws(() => range('1', 2), TypeError);
  assert.throws(() => range({ valueOf() { return 1; } }, 2), TypeError);

  if (typeof BigInt == 'function') {
    iterator = range(BigInt(1), BigInt(2));

    assert.isIterator(iterator);
    assert.isIterable(iterator);
    assert.deepEqual(iterator.next(), {
      value: BigInt(1),
      done: false,
    });
    assert.deepEqual(iterator.next(), {
      value: undefined,
      done: true,
    });

    assert.deepEqual(from(range(BigInt(-1), BigInt(5))), [BigInt(-1), BigInt(0), BigInt(1), BigInt(2), BigInt(3), BigInt(4)]);
    assert.deepEqual(from(range(BigInt(-5), BigInt(1))), [BigInt(-5), BigInt(-4), BigInt(-3), BigInt(-2), BigInt(-1), BigInt(0)]);
    assert.deepEqual(
      from(range(BigInt('9007199254740991'), BigInt('9007199254740992'), { inclusive: true })),
      [BigInt('9007199254740991'), BigInt('9007199254740992')],
    );
    assert.deepEqual(from(range(BigInt(0), BigInt(0))), []);
    assert.deepEqual(from(range(BigInt(0), BigInt(-5), BigInt(1))), []);

    iterator = range(BigInt(1), BigInt(3));
    assert.deepEqual(iterator.start, BigInt(1));
    assert.deepEqual(iterator.end, BigInt(3));
    assert.deepEqual(iterator.step, BigInt(1));
    assert.false(iterator.inclusive);

    iterator = range(BigInt(-1), BigInt(-3), { inclusive: true });
    assert.deepEqual(iterator.start, BigInt(-1));
    assert.deepEqual(iterator.end, BigInt(-3));
    assert.same(iterator.step, BigInt(-1));
    assert.true(iterator.inclusive);

    iterator = range(BigInt(-1), BigInt(-3), { step: BigInt(4), inclusive() { /* empty */ } });
    assert.same(iterator.start, BigInt(-1));
    assert.same(iterator.end, BigInt(-3));
    assert.same(iterator.step, BigInt(4));
    assert.true(iterator.inclusive);

    iterator = range(BigInt(0), BigInt(5));
    assert.throws(() => Object.getOwnPropertyDescriptor(iterator, 'start').get.call({}), TypeError);

    assert.throws(() => range(Infinity, BigInt(10), BigInt(0)), TypeError);
    assert.throws(() => range(-Infinity, BigInt(10), BigInt(0)), TypeError);
    assert.throws(() => range(BigInt(0), BigInt(10), Infinity), TypeError);
    assert.throws(() => range(BigInt(0), BigInt(10), { step: Infinity }), TypeError);

    assert.throws(() => range({}, BigInt(1)), TypeError);
    assert.throws(() => range(BigInt(1), {}), TypeError);
  }
});