to-assigned 1.0.0
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/LICENSE +21 -0
- package/README.md +72 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.mjs +1 -0
- package/dist/native.d.ts +18 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kasukabe tsumugi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# to-assigned
|
|
2
|
+
|
|
3
|
+
Create a new object, array, or function by copying enumerable properties from one or more source objects, with prototype and type inheritance from the first source. More flexible than `Object.assign`, supporting arrays, functions, and special objects as the first source.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
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
|
+
- Does not modify the sources.
|
|
16
|
+
- Ignores all non-object arguments.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add to-assigned
|
|
22
|
+
# or
|
|
23
|
+
npm install to-assigned
|
|
24
|
+
# or
|
|
25
|
+
yarn add to-assigned
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { toAssigned } from 'to-assigned';
|
|
32
|
+
|
|
33
|
+
// Object prototype inheritance
|
|
34
|
+
const proto = { foo: 1 };
|
|
35
|
+
const obj = Object.create(proto);
|
|
36
|
+
obj.bar = 2;
|
|
37
|
+
const result1 = toAssigned(obj, { baz: 3 });
|
|
38
|
+
// result1: { bar: 2, baz: 3 }, result1.__proto__ === proto
|
|
39
|
+
|
|
40
|
+
// Array as first source
|
|
41
|
+
const arr = [1, 2];
|
|
42
|
+
const result2 = toAssigned(arr, { 2: 3, length: 3 });
|
|
43
|
+
// result2: [1, 2, 3]
|
|
44
|
+
|
|
45
|
+
// Function as first source
|
|
46
|
+
function fn(a, b) {
|
|
47
|
+
return a + b;
|
|
48
|
+
}
|
|
49
|
+
fn.extra = 42;
|
|
50
|
+
const result3 = toAssigned(fn, { extra: 99 });
|
|
51
|
+
// typeof result3 === 'function', result3(1,2) === 3, result3.extra === 99
|
|
52
|
+
|
|
53
|
+
// Map/Set/WeakMap/WeakSet/Date as first source
|
|
54
|
+
const map = new Map([[1, 'a']]);
|
|
55
|
+
const result4 = toAssigned(map, { foo: 'bar' });
|
|
56
|
+
// result4 instanceof Map, result4.get(1) === 'a', result4.foo === 'bar'
|
|
57
|
+
|
|
58
|
+
// Non-object as first source
|
|
59
|
+
const result5 = toAssigned(123, { a: 1 });
|
|
60
|
+
// result5: { a: 1 }, Object.getPrototypeOf(result5) === null
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
### `toAssigned(...sources: any[]): any`
|
|
66
|
+
|
|
67
|
+
- **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.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object with properties from the provided sources
|
|
3
|
+
* - Basically the same as Object.assign({}, ...sources.filter(isObjectOrFunction))
|
|
4
|
+
* - returned prototype will be **the same as the first source**
|
|
5
|
+
* - if the first source is:
|
|
6
|
+
* - *non-object*: `result.prototype` will be `null`
|
|
7
|
+
* - *array*: result will be an array and shares the same prototype
|
|
8
|
+
* - *function*: result will be a function with the same `name`, `length` and shares the same prototype. But `caller`, `callee`, and `arguments` will not be copied
|
|
9
|
+
* - *Map/Set/WeakMap/WeakSet/Date*: create a new one with the old
|
|
10
|
+
* - *other objects*: starts from an empty object
|
|
11
|
+
* - only enumerable properties are copied
|
|
12
|
+
* - will not modify the sources
|
|
13
|
+
* - ignores all non-object arguments
|
|
14
|
+
* @returns a new object with properties from the sources
|
|
15
|
+
*/
|
|
16
|
+
declare function toAssigned(...sources: any[]): any;
|
|
17
|
+
|
|
18
|
+
export { toAssigned };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Reflect.ownKeys,n=Reflect.setPrototypeOf,t=Reflect.getPrototypeOf,r=Object.prototype.propertyIsEnumerable,o=Array.isArray,f=Reflect.defineProperty;function a(e){var r,a,c=null===(r=e)||"object"!=typeof r&&"function"!=typeof r?null:t(e);if(o(e))a=[];else if("function"==typeof e){var u=void 0;!function(e){try{return new new Proxy(e,{construct:function(){return{}}}),!1}catch(e){return!0}}(e)?(u=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return e.apply(void 0,n)},u.bind=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return e.bind.apply(e,n)}):u=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return e.apply(void 0,n)},f(u,"name",{value:e.name,configurable:!0}),f(u,"length",{value:e.length,configurable:!0}),a=u}else a=e instanceof Map?new Map(e.entries()):e instanceof Set?new Set(e.values()):e instanceof WeakMap?new WeakMap:e instanceof WeakSet?new WeakSet:e instanceof Date?new Date(e.getTime()):{};return n(a,c),a}function c(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];if(0===n.length)return{};for(var o=a(n[0]),f=0;f<n.length;f++){var c=n[f];if(c&&"object"==typeof c)for(var u=0,i=e(c);u<i.length;u++){var l=i[u];r.call(c,l)&&(o[l]=c[l])}}return o}Reflect.set(c,Symbol("version"),"1.0.0");export{c as toAssigned};
|
package/dist/native.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const ownKeys: typeof Reflect.ownKeys;
|
|
2
|
+
export declare const setPrototypeOf: typeof Reflect.setPrototypeOf;
|
|
3
|
+
export declare const getPrototypeOf: typeof Reflect.getPrototypeOf;
|
|
4
|
+
export declare const isEnumerable: (v: PropertyKey) => boolean;
|
|
5
|
+
export declare const isArray: (arg: any) => arg is any[];
|
|
6
|
+
export declare const create: {
|
|
7
|
+
(o: object | null): any;
|
|
8
|
+
(o: object | null, properties: PropertyDescriptorMap & ThisType<any>): any;
|
|
9
|
+
};
|
|
10
|
+
export declare const defineProperty: typeof Reflect.defineProperty;
|
|
11
|
+
export declare const notPrimitive: (v: unknown) => v is object;
|
|
12
|
+
/**
|
|
13
|
+
* ! Only use when `fn` is known to be a function
|
|
14
|
+
*
|
|
15
|
+
* This is the simplified version
|
|
16
|
+
* @see https://github.com/baendlorel/get-function-features
|
|
17
|
+
*/
|
|
18
|
+
export declare const isArrowFunction: (fn: new () => any) => boolean;
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "to-assigned",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "KasukabeTsumugi",
|
|
6
|
+
"email": "futami16237@gmail.com"
|
|
7
|
+
},
|
|
8
|
+
"description": "A simple utility to concatenate multiple arrays end-to-end.",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rimraf dist && rollup -c",
|
|
19
|
+
"test": "clear && vitest"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/baendlorel/to-assigned.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"array",
|
|
27
|
+
"concat",
|
|
28
|
+
"utility",
|
|
29
|
+
"javascript",
|
|
30
|
+
"npm"
|
|
31
|
+
],
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/baendlorel/to-assigned/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/baendlorel/to-assigned#readme",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@babel/plugin-proposal-decorators": "^7.28.0",
|
|
39
|
+
"@babel/preset-env": "^7.28.0",
|
|
40
|
+
"@eslint/js": "^9.30.0",
|
|
41
|
+
"@rollup/plugin-alias": "^5.1.1",
|
|
42
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
43
|
+
"@rollup/plugin-commonjs": "^28.0.6",
|
|
44
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
45
|
+
"@rollup/plugin-replace": "^6.0.2",
|
|
46
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
47
|
+
"@rollup/plugin-typescript": "^12.1.3",
|
|
48
|
+
"@types/node": "^24.0.4",
|
|
49
|
+
"eslint": "^9.30.0",
|
|
50
|
+
"rimraf": "^6.0.1",
|
|
51
|
+
"rollup": "^4.44.1",
|
|
52
|
+
"rollup-plugin-dts": "^6.2.1",
|
|
53
|
+
"tslib": "^2.8.1",
|
|
54
|
+
"typescript": "^5.8.3",
|
|
55
|
+
"typescript-eslint": "^8.38.0",
|
|
56
|
+
"vitest": "^3.2.4"
|
|
57
|
+
}
|
|
58
|
+
}
|