forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes.object.assign.js
66 lines (64 loc) · 2.25 KB
/
es.object.assign.js
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
import { DESCRIPTORS } from '../helpers/constants';
import Symbol from 'core-js-pure/es/symbol';
import defineProperty from 'core-js-pure/es/object/define-property';
import keys from 'core-js-pure/es/object/keys';
import assign from 'core-js-pure/es/object/assign';
QUnit.test('Object.assign', assert => {
assert.isFunction(assign);
let object = { q: 1 };
assert.same(object, assign(object, { bar: 2 }), 'assign return target');
assert.same(object.bar, 2, 'assign define properties');
assert.deepEqual(assign({}, { q: 1 }, { w: 2 }), { q: 1, w: 2 });
assert.deepEqual(assign({}, 'qwe'), { 0: 'q', 1: 'w', 2: 'e' });
assert.throws(() => assign(null, { q: 1 }), TypeError);
assert.throws(() => assign(undefined, { q: 1 }), TypeError);
let string = assign('qwe', { q: 1 });
assert.same(typeof string, 'object');
assert.same(String(string), 'qwe');
assert.same(string.q, 1);
assert.same(assign({}, { valueOf: 42 }).valueOf, 42, 'IE enum keys bug');
if (DESCRIPTORS) {
object = { baz: 1 };
assign(object, defineProperty({}, 'bar', {
get() {
return this.baz + 1;
},
}));
assert.same(object.bar, undefined, "assign don't copy descriptors");
object = { a: 'a' };
const c = Symbol('c');
const d = Symbol('d');
object[c] = 'c';
defineProperty(object, 'b', { value: 'b' });
defineProperty(object, d, { value: 'd' });
const object2 = assign({}, object);
assert.same(object2.a, 'a', 'a');
assert.same(object2.b, undefined, 'b');
assert.same(object2[c], 'c', 'c');
assert.same(object2[d], undefined, 'd');
try {
assert.same(Function('assign', `
return assign({ b: 1 }, { get a() {
delete this.b;
}, b: 2 });
`)(assign).b, 1);
} catch { /* empty */ }
try {
assert.same(Function('assign', `
return assign({ b: 1 }, { get a() {
Object.defineProperty(this, "b", {
value: 4,
enumerable: false
});
}, b: 2 });
`)(assign).b, 1);
} catch { /* empty */ }
}
string = 'abcdefghijklmnopqrst';
const result = {};
for (let i = 0, { length } = string; i < length; ++i) {
const chr = string.charAt(i);
result[chr] = chr;
}
assert.same(keys(assign({}, result)).join(''), string);
});