to-assigned 1.0.1 → 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 +22 -14
- package/dist/index.d.ts +15 -13
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
# to-assigned
|
|
2
2
|
|
|
3
|
-
Create a new object, array, or
|
|
3
|
+
Create a new object, array, function, or special object by copying enumerable properties from one or more source objects. The prototype and type of the result are determined by the first non-primitive source (see below). More flexible than `Object.assign`, supporting arrays, functions, and special objects as the first source.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- The returned value's prototype will be the same as the first source (if it's an object), or `null` if the first source is a primitive.
|
|
8
|
-
- If the first source is:
|
|
9
|
-
- **Array**: result is an array, sharing the same prototype.
|
|
10
|
-
- **Function**: result is a function with the same `name`, `length`, and prototype. (But `caller`, `callee`, and `arguments` are not copied.)
|
|
11
|
-
- **Map/Set/WeakMap/WeakSet/Date**: result is a new instance of the same type, initialized from the first source. (But only own enumerable properties are merged, not collection contents.)
|
|
12
|
-
- **Other objects**: result is a plain object with the same prototype.
|
|
13
|
-
- **Non-object**: result is a plain object with `null` prototype.
|
|
14
|
-
- Only enumerable properties are copied (including symbol keys).
|
|
15
7
|
- Does not modify the sources.
|
|
16
8
|
- Ignores all non-object arguments.
|
|
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 `Object.prototype` as its prototype.
|
|
11
|
+
- If the first non-primitive source is:
|
|
12
|
+
- **Array**: result is an array, prototype same as source.
|
|
13
|
+
- **Function**: result is a function with same `name`, `length`, and prototype. (`caller`, `callee`, `arguments` not copied)
|
|
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 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
|
|
@@ -65,7 +73,7 @@ const result5 = toAssigned(123, { a: 1 });
|
|
|
65
73
|
### `toAssigned(...sources: any[]): any`
|
|
66
74
|
|
|
67
75
|
- **sources**: Any number of source objects. Non-object arguments are ignored.
|
|
68
|
-
- **Returns**: A new object, array, function, or special object (Map/Set/...) with all enumerable properties from the sources, and prototype/type inherited from the first source.
|
|
76
|
+
- **Returns**: A new object, array, function, or special object (Map/Set/...) with all enumerable properties from the sources, and prototype/type inherited from the first non-primitive source (see above for rules).
|
|
69
77
|
|
|
70
78
|
## License
|
|
71
79
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Creates a new object with properties from the provided sources
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* -
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
2
|
+
* Creates a new object, array, function, or special object with properties from the provided sources.
|
|
3
|
+
* Prototype and type are determined by the first non-primitive source (see below):
|
|
4
|
+
*
|
|
5
|
+
* - The sources are never modified.
|
|
6
|
+
* - Non-object arguments are ignored.
|
|
7
|
+
* - If all sources are primitive, returns `{}`.
|
|
8
|
+
* - If the first non-primitive source is:
|
|
9
|
+
* - **Array**: result is an array, prototype same as source.
|
|
10
|
+
* - **Function**: result is a function with same `name`, `length`, and prototype. (`caller`, `callee`, `arguments` not copied)
|
|
11
|
+
* - **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).
|
|
12
|
+
* - **Other object**: result is a plain object with the same prototype as the source.
|
|
13
|
+
*
|
|
14
|
+
* - Only enumerable properties (including symbol keys) are copied from all sources.
|
|
15
|
+
*
|
|
16
|
+
* @returns A new object, array, function, or special object with merged properties and inherited prototype/type.
|
|
15
17
|
*/
|
|
16
18
|
declare function toAssigned(...sources: any[]): any;
|
|
17
19
|
|
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
|
|
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};
|