to-assigned 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -7
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,13 +7,13 @@ Create a new object, array, function, or special object by copying enumerable pr
|
|
|
7
7
|
- Does not modify the sources.
|
|
8
8
|
- Ignores all non-object arguments.
|
|
9
9
|
- The result's prototype and type are determined by the first non-primitive source:
|
|
10
|
-
- If all sources are primitive, returns a plain object with
|
|
10
|
+
- If all sources are primitive, returns a plain object with `Object.prototype` as its prototype.
|
|
11
11
|
- If the first non-primitive source is:
|
|
12
12
|
- **Array**: result is an array, prototype same as source.
|
|
13
13
|
- **Function**: result is a function with same `name`, `length`, and prototype. (`caller`, `callee`, `arguments` not copied)
|
|
14
14
|
- **Map/Set/WeakMap/WeakSet/Date**: result is a new instance of the same type, initialized from the first source (only own enumerable properties are merged, not collection contents).
|
|
15
|
-
- **Other object**: result is
|
|
16
|
-
- Only enumerable properties
|
|
15
|
+
- **Other object**: result is an object with the same prototype as the source.
|
|
16
|
+
- Only enumerable properties (including symbol keys) are copied from all sources. Later sources overwrite earlier ones with the same key.
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
19
19
|
|
|
@@ -35,12 +35,14 @@ const proto = { foo: 1 };
|
|
|
35
35
|
const obj = Object.create(proto);
|
|
36
36
|
obj.bar = 2;
|
|
37
37
|
const result1 = toAssigned(obj, { baz: 3 });
|
|
38
|
-
// result1: { bar: 2, baz: 3 }
|
|
38
|
+
// result1: { bar: 2, baz: 3 }
|
|
39
|
+
// Object.getPrototypeOf(result1) === proto
|
|
39
40
|
|
|
40
41
|
// Array as first source
|
|
41
42
|
const arr = [1, 2];
|
|
42
43
|
const result2 = toAssigned(arr, { 2: 3, length: 3 });
|
|
43
44
|
// result2: [1, 2, 3]
|
|
45
|
+
// Object.getPrototypeOf(result2) === Object.getPrototypeOf(arr)
|
|
44
46
|
|
|
45
47
|
// Function as first source
|
|
46
48
|
function fn(a, b) {
|
|
@@ -48,16 +50,22 @@ function fn(a, b) {
|
|
|
48
50
|
}
|
|
49
51
|
fn.extra = 42;
|
|
50
52
|
const result3 = toAssigned(fn, { extra: 99 });
|
|
51
|
-
// typeof result3 === 'function'
|
|
53
|
+
// typeof result3 === 'function'
|
|
54
|
+
// result3(1,2) === 3
|
|
55
|
+
// result3.extra === 99
|
|
56
|
+
// Object.getPrototypeOf(result3) === Object.getPrototypeOf(fn)
|
|
52
57
|
|
|
53
58
|
// Map/Set/WeakMap/WeakSet/Date as first source
|
|
54
59
|
const map = new Map([[1, 'a']]);
|
|
55
60
|
const result4 = toAssigned(map, { foo: 'bar' });
|
|
56
|
-
// result4 instanceof Map
|
|
61
|
+
// result4 instanceof Map
|
|
62
|
+
// result4.get(1) === 'a'
|
|
63
|
+
// result4.foo === 'bar'
|
|
57
64
|
|
|
58
65
|
// Non-object as first source
|
|
59
66
|
const result5 = toAssigned(123, { a: 1 });
|
|
60
|
-
// result5: { a: 1 }
|
|
67
|
+
// result5: { a: 1 }
|
|
68
|
+
// Object.getPrototypeOf(result5) === Object.prototype
|
|
61
69
|
```
|
|
62
70
|
|
|
63
71
|
## API
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=Reflect.ownKeys,t=Reflect.setPrototypeOf,n=Reflect.getPrototypeOf,o=Object.prototype.propertyIsEnumerable,c=Array.isArray,f=Reflect.defineProperty,r=e=>null!==e&&("object"==typeof e||"function"==typeof e);function a(e){const o=e.find(r);if(!o)return{};const a=n(o);let i;if(c(o))i=[];else if("function"==typeof o){let e;(e=>{try{return new new Proxy(e,{construct:()=>({})}),!1}catch{return!0}})(o)?e=(...e)=>o(...e):(e=function(...e){return o(...e)},e.bind=(e,...t)=>o.bind(e,...t)),f(e,"name",{value:o.name,configurable:!0}),f(e,"length",{value:o.length,configurable:!0}),i=e}else i=o instanceof Map?new Map(o.entries()):o instanceof Set?new Set(o.values()):o instanceof WeakMap?new WeakMap:o instanceof WeakSet?new WeakSet:o instanceof Date?new Date(o.getTime()):{};return t(i,a),i}function i(...t){if(0===t.length)return{};const n=a(t);for(let c=0;c<t.length;c++){const f=t[c];if(f&&"object"==typeof f)for(const t of e(f))o.call(f,t)&&(n[t]=f[t])}return n}Reflect.set(i,Symbol("version"),"1.1.
|
|
1
|
+
const e=Reflect.ownKeys,t=Reflect.setPrototypeOf,n=Reflect.getPrototypeOf,o=Object.prototype.propertyIsEnumerable,c=Array.isArray,f=Reflect.defineProperty,r=e=>null!==e&&("object"==typeof e||"function"==typeof e);function a(e){const o=e.find(r);if(!o)return{};const a=n(o);let i;if(c(o))i=[];else if("function"==typeof o){let e;(e=>{try{return new new Proxy(e,{construct:()=>({})}),!1}catch{return!0}})(o)?e=(...e)=>o(...e):(e=function(...e){return o(...e)},e.bind=(e,...t)=>o.bind(e,...t)),f(e,"name",{value:o.name,configurable:!0}),f(e,"length",{value:o.length,configurable:!0}),i=e}else i=o instanceof Map?new Map(o.entries()):o instanceof Set?new Set(o.values()):o instanceof WeakMap?new WeakMap:o instanceof WeakSet?new WeakSet:o instanceof Date?new Date(o.getTime()):{};return t(i,a),i}function i(...t){if(0===t.length)return{};const n=a(t);for(let c=0;c<t.length;c++){const f=t[c];if(f&&"object"==typeof f)for(const t of e(f))o.call(f,t)&&(n[t]=f[t])}return n}Reflect.set(i,Symbol("version"),"1.1.1");export{i as toAssigned};
|