Last change
on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var isExtendable = require('is-extendable');
|
---|
| 4 | var assignSymbols = require('assign-symbols');
|
---|
| 5 |
|
---|
| 6 | module.exports = Object.assign || function(obj/*, objects*/) {
|
---|
| 7 | if (obj === null || typeof obj === 'undefined') {
|
---|
| 8 | throw new TypeError('Cannot convert undefined or null to object');
|
---|
| 9 | }
|
---|
| 10 | if (!isObject(obj)) {
|
---|
| 11 | obj = {};
|
---|
| 12 | }
|
---|
| 13 | for (var i = 1; i < arguments.length; i++) {
|
---|
| 14 | var val = arguments[i];
|
---|
| 15 | if (isString(val)) {
|
---|
| 16 | val = toObject(val);
|
---|
| 17 | }
|
---|
| 18 | if (isObject(val)) {
|
---|
| 19 | assign(obj, val);
|
---|
| 20 | assignSymbols(obj, val);
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 | return obj;
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | function assign(a, b) {
|
---|
| 27 | for (var key in b) {
|
---|
| 28 | if (hasOwn(b, key)) {
|
---|
| 29 | a[key] = b[key];
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | function isString(val) {
|
---|
| 35 | return (val && typeof val === 'string');
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | function toObject(str) {
|
---|
| 39 | var obj = {};
|
---|
| 40 | for (var i in str) {
|
---|
| 41 | obj[i] = str[i];
|
---|
| 42 | }
|
---|
| 43 | return obj;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function isObject(val) {
|
---|
| 47 | return (val && typeof val === 'object') || isExtendable(val);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Returns true if the given `key` is an own property of `obj`.
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 | function hasOwn(obj, key) {
|
---|
| 55 | return Object.prototype.hasOwnProperty.call(obj, key);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | function isEnum(obj, key) {
|
---|
| 59 | return Object.prototype.propertyIsEnumerable.call(obj, key);
|
---|
| 60 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.