How to use the core-js-pure/features/array/from function in core-js-pure

To help you get started, we’ve selected a few core-js-pure examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zloirock / core-js / tests / pure / esnext.map.delete-all.js View on Github external
QUnit.test('Map#deleteAll', assert => {
  const { deleteAll } = Map.prototype;

  assert.isFunction(deleteAll);
  assert.arity(deleteAll, 0);
  assert.nonEnumerable(Map.prototype, 'deleteAll');

  let set = new Map([[1, 2], [2, 3], [3, 4]]);
  assert.same(set.deleteAll(1, 2), true);
  assert.deepEqual(from(set), [[3, 4]]);

  set = new Map([[1, 2], [2, 3], [3, 4]]);
  assert.same(set.deleteAll(3, 4), false);
  assert.deepEqual(from(set), [[1, 2], [2, 3]]);

  set = new Map([[1, 2], [2, 3], [3, 4]]);
  assert.same(set.deleteAll(4, 5), false);
  assert.deepEqual(from(set), [[1, 2], [2, 3], [3, 4]]);

  set = new Map([[1, 2], [2, 3], [3, 4]]);
  assert.same(set.deleteAll(), true);
  assert.deepEqual(from(set), [[1, 2], [2, 3], [3, 4]]);

  assert.notThrows(() => !deleteAll.call({ delete() { /* empty */ } }, 1, 2, 3));
  assert.throws(() => deleteAll.call({}, 1, 2, 3), TypeError);
  assert.throws(() => deleteAll.call(undefined, 1, 2, 3), TypeError);
github zloirock / core-js / tests / pure / esnext.map.merge.js View on Github external
QUnit.test('Map#merge', assert => {
  const { merge } = Map.prototype;

  assert.isFunction(merge);
  assert.arity(merge, 1);
  assert.name(merge, 'merge');
  assert.nonEnumerable(Map.prototype, 'merge');

  const map = new Map([[1, 2]]);
  const result = map.merge([[3, 4]]);
  assert.ok(result === map);
  assert.ok(result instanceof Map);

  assert.deepEqual(from(new Map([[1, 2], [3, 4]]).merge([[5, 6]])), [[1, 2], [3, 4], [5, 6]]);
  assert.deepEqual(from(new Map([[1, 2], [3, 4]]).merge([[3, 5], [5, 6]])), [[1, 2], [3, 5], [5, 6]]);
  assert.deepEqual(from(new Map([[1, 2], [3, 4]]).merge([])), [[1, 2], [3, 4]]);

  assert.deepEqual(from(new Map([[1, 2], [3, 4]]).merge([[3, 5]], [[5, 6]])), [[1, 2], [3, 5], [5, 6]]);

  assert.throws(() => merge.call({}, [[1, 2]]), TypeError);
  assert.throws(() => merge.call(undefined, [[1, 2]]), TypeError);
  assert.throws(() => merge.call(null, [[1, 2]]), TypeError);
});
github zloirock / core-js / tests / pure / esnext.set.of.js View on Github external
QUnit.test('Set.of', assert => {
  const { of } = Set;
  assert.isFunction(of);
  assert.arity(of, 0);
  assert.ok(Set.of() instanceof Set);
  assert.deepEqual(toArray(Set.of(1)), [1]);
  assert.deepEqual(toArray(Set.of(1, 2, 3, 2, 1)), [1, 2, 3]);
  assert.throws(() => of(1));
  let arg = null;
  function F(it) {
    return arg = it;
  }
  of.call(F, 1, 2, 3);
  assert.deepEqual(arg, [1, 2, 3]);
});
github zloirock / core-js / tests / pure / esnext.map.group-by.js View on Github external
QUnit.test('Map.groupBy', assert => {
  const { groupBy } = Map;

  assert.isFunction(groupBy);
  assert.arity(groupBy, 2);
  assert.name(groupBy, 'groupBy');

  assert.ok(Map.groupBy([], it => it) instanceof Map);

  assert.deepEqual(toArray(Map.groupBy([], it => it)), []);
  assert.deepEqual(toArray(Map.groupBy([1, 2], it => it ** 2)), [[1, [1]], [4, [2]]]);
  assert.deepEqual(toArray(Map.groupBy([1, 2, 1], it => it ** 2)), [[1, [1, 1]], [4, [2]]]);
  assert.deepEqual(toArray(Map.groupBy(createIterable([1, 2]), it => it ** 2)), [[1, [1]], [4, [2]]]);

  const element = {};
  Map.groupBy([element], it => assert.same(it, element));

  assert.throws(() => groupBy([1, 2], it => it));
});
github zloirock / core-js / tests / pure / es.array.from.js View on Github external
types = {
    'array-like': {
      length: 1,
      0: 1,
    },
    arguments: function () {
      return arguments;
    }(1),
    array: [1],
    iterable: createIterable([1]),
    string: '1',
  };
  for (const type in types) {
    const data = types[type];
    const context = {};
    assert.arrayEqual(from(data, function (value, key) {
      assert.same(this, context, `Works with ${ type }, correct callback context`);
      assert.same(value, type === 'string' ? '1' : 1, `Works with ${ type }, correct callback key`);
      assert.same(key, 0, `Works with ${ type }, correct callback value`);
      assert.same(arguments.length, 2, `Works with ${ type }, correct callback arguments number`);
      return 42;
    }, context), [42], `Works with ${ type }, correct result`);
  }
  const primitives = [false, true, 0];
  for (const primitive of primitives) {
    assert.arrayEqual(from(primitive), [], `Works with ${ primitive }`);
  }
  assert.throws(() => from(null), TypeError, 'Throws on null');
  assert.throws(() => from(undefined), TypeError, 'Throws on undefined');
  assert.arrayEqual(from('ð ®·ð ®·ð ®·'), ['ð ®·', 'ð ®·', 'ð ®·'], 'Uses correct string iterator');
  let done = true;
  from(createIterable([1, 2, 3], {
github zloirock / core-js / tests / pure / es.array.from.js View on Github external
const context = {};
    assert.arrayEqual(from(data, function (value, key) {
      assert.same(this, context, `Works with ${ type }, correct callback context`);
      assert.same(value, type === 'string' ? '1' : 1, `Works with ${ type }, correct callback key`);
      assert.same(key, 0, `Works with ${ type }, correct callback value`);
      assert.same(arguments.length, 2, `Works with ${ type }, correct callback arguments number`);
      return 42;
    }, context), [42], `Works with ${ type }, correct result`);
  }
  const primitives = [false, true, 0];
  for (const primitive of primitives) {
    assert.arrayEqual(from(primitive), [], `Works with ${ primitive }`);
  }
  assert.throws(() => from(null), TypeError, 'Throws on null');
  assert.throws(() => from(undefined), TypeError, 'Throws on undefined');
  assert.arrayEqual(from('ð ®·ð ®·ð ®·'), ['ð ®·', 'ð ®·', 'ð ®·'], 'Uses correct string iterator');
  let done = true;
  from(createIterable([1, 2, 3], {
    return() {
      return done = false;
    },
  }), () => false);
  assert.ok(done, '.return #default');
  done = false;
  try {
    from(createIterable([1, 2, 3], {
      return() {
        return done = true;
      },
    }), () => {
      throw new Error();
    });
github zloirock / core-js / tests / pure / esnext.map.map-values.js View on Github external
assert.name(mapValues, 'mapValues');
  assert.nonEnumerable(Map.prototype, 'mapValues');

  const map = new Map([[1, 2]]);
  const context = {};
  map.mapValues(function (value, key, that) {
    assert.same(arguments.length, 3, 'correct number of callback arguments');
    assert.same(value, 2, 'correct value in callback');
    assert.same(key, 1, 'correct key in callback');
    assert.same(that, map, 'correct link to map in callback');
    assert.same(this, context, 'correct callback context');
  }, context);

  assert.ok(new Map().mapValues(it => it) instanceof Map);

  assert.deepEqual(from(new Map([
    ['a', 1],
    [1, 2],
    ['b', 3],
    [2, 'q'],
    ['c', {}],
    [3, 4],
    ['d', true],
    [4, 5],
  ]).mapValues((value, key) => `${ key }${ value }`)), [
    ['a', 'a1'],
    [1, '12'],
    ['b', 'b3'],
    [2, '2q'],
    ['c', 'c[object Object]'],
    [3, '34'],
    ['d', 'dtrue'],
github zloirock / core-js / tests / pure / es.array.from.js View on Github external
assert.arrayEqual(instance, [1, 2], 'generic, iterable case, elements');
  instance = from.call(C, {
    0: 1,
    1: 2,
    length: 2,
  });
  assert.ok(instance instanceof C, 'generic, array-like case, instanceof');
  assert.arrayEqual(instance, [1, 2], 'generic, array-like case, elements');
  let array = [1, 2, 3];
  done = false;
  array['@@iterator'] = undefined;
  array[Symbol.iterator] = function () {
    done = true;
    return getIteratorMethod([]).call(this);
  };
  assert.arrayEqual(from(array), [1, 2, 3], 'Array with custom iterator, elements');
  assert.ok(done, 'call @@iterator in Array with custom iterator');
  array = [1, 2, 3];
  delete array[1];
  assert.arrayEqual(from(array, String), ['1', 'undefined', '3'], 'Ignores holes');
  assert.notThrows(() => from({
    length: -1,
    0: 1,
  }, () => {
    throw new Error();
  }), 'Uses ToLength');
  assert.arrayEqual(from([], undefined), [], 'Works with undefined as asecond argument');
  assert.throws(() => from([], null), TypeError, 'Throws with null as second argument');
  assert.throws(() => from([], 0), TypeError, 'Throws with 0 as second argument');
  assert.throws(() => from([], ''), TypeError, 'Throws with "" as second argument');
  assert.throws(() => from([], false), TypeError, 'Throws with false as second argument');
  assert.throws(() => from([], {}), TypeError, 'Throws with {} as second argument');
github zloirock / core-js / tests / pure / esnext.map.filter.js View on Github external
assert.isFunction(filter);
  assert.arity(filter, 1);
  assert.name(filter, 'filter');
  assert.nonEnumerable(Map.prototype, 'filter');

  const map = new Map([[1, 2]]);
  const context = {};
  map.filter(function (value, key, that) {
    assert.same(arguments.length, 3, 'correct number of callback arguments');
    assert.same(value, 2, 'correct value in callback');
    assert.same(key, 1, 'correct key in callback');
    assert.same(that, map, 'correct link to map in callback');
    assert.same(this, context, 'correct callback context');
  }, context);

  assert.deepEqual(from(new Map([
    ['a', 1],
    [1, 2],
    ['b', 3],
    [2, 'q'],
    ['c', {}],
    [3, 4],
    ['d', true],
    [4, 5],
  ]).filter(it => typeof it === 'number')), [
    ['a', 1],
    [1, 2],
    ['b', 3],
    [3, 4],
    [4, 5],
  ]);
github zloirock / core-js / tests / pure / es.array.from.js View on Github external
length: 2,
  });
  assert.ok(instance instanceof C, 'generic, array-like case, instanceof');
  assert.arrayEqual(instance, [1, 2], 'generic, array-like case, elements');
  let array = [1, 2, 3];
  done = false;
  array['@@iterator'] = undefined;
  array[Symbol.iterator] = function () {
    done = true;
    return getIteratorMethod([]).call(this);
  };
  assert.arrayEqual(from(array), [1, 2, 3], 'Array with custom iterator, elements');
  assert.ok(done, 'call @@iterator in Array with custom iterator');
  array = [1, 2, 3];
  delete array[1];
  assert.arrayEqual(from(array, String), ['1', 'undefined', '3'], 'Ignores holes');
  assert.notThrows(() => from({
    length: -1,
    0: 1,
  }, () => {
    throw new Error();
  }), 'Uses ToLength');
  assert.arrayEqual(from([], undefined), [], 'Works with undefined as asecond argument');
  assert.throws(() => from([], null), TypeError, 'Throws with null as second argument');
  assert.throws(() => from([], 0), TypeError, 'Throws with 0 as second argument');
  assert.throws(() => from([], ''), TypeError, 'Throws with "" as second argument');
  assert.throws(() => from([], false), TypeError, 'Throws with false as second argument');
  assert.throws(() => from([], {}), TypeError, 'Throws with {} as second argument');
  if (DESCRIPTORS) {
    let called = false;
    defineProperty(C.prototype, 0, {
      set() {