Skip to content

Commit 9662b62

Browse files
committed
feat: add object/assign-in
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent a729aff commit 9662b62

File tree

10 files changed

+937
-0
lines changed

10 files changed

+937
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# assignIn
22+
23+
> Copy enumerable own and inherited properties from one or more source objects to a target object.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var assignIn = require( '@stdlib/object/assign-in' );
41+
```
42+
43+
#### assignIn( target, source1\[, source2\[,...,sourceN]] )
44+
45+
Copies enumerable own and inherited properties from one or more source objects to a target object.
46+
47+
```javascript
48+
function Foo() {
49+
this.a = 'beep';
50+
return this;
51+
}
52+
53+
Foo.prototype.b = 'boop';
54+
55+
var x = {};
56+
var y = new Foo();
57+
58+
var z = assignIn( x, y );
59+
// returns { 'a': 'beep', 'b': 'boop' }
60+
61+
var bool = ( z === x );
62+
// returns true
63+
```
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- If a property key is present in multiple sources, the property from the last source that defines the key prevails.
76+
- The target object is mutated.
77+
- Both string and symbol properties are copied.
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<!-- Package usage examples. -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var assign = require( '@stdlib/object/assign-in' );
93+
94+
function Foo() {
95+
this.a = 1;
96+
return this;
97+
}
98+
99+
Foo.prototype.b = 2;
100+
101+
function Bar() {
102+
this.c = 3;
103+
return this;
104+
}
105+
106+
Bar.prototype.d = 4;
107+
108+
var obj1 = new Foo();
109+
var obj2 = new Bar();
110+
111+
var result = assignIn( {}, obj1, obj2 );
112+
// returns { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="references">
122+
123+
</section>
124+
125+
<!-- /.references -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
<!-- /.related -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
<!-- <related-links> -->
138+
139+
<!-- </related-links> -->
140+
141+
</section>
142+
143+
<!-- /.links -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isObject = require( '@stdlib/assert/is-object' );
25+
var pkg = require( './../package.json' ).name;
26+
var assignIn = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var source1;
33+
var source2;
34+
var target;
35+
var out;
36+
var i;
37+
38+
source1 = {
39+
'a': 'beep'
40+
};
41+
source2 = {
42+
'b': 'boop'
43+
};
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
target = {
48+
'a': i
49+
};
50+
out = assignIn( target, source1, source2 );
51+
if ( !isObject( out ) ) {
52+
b.fail( 'should return an object' );
53+
}
54+
}
55+
b.toc();
56+
if ( out.a !== 'beep' || out.b !== 'boop' ) {
57+
b.fail( 'should assign properties' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( target, ...source )
3+
Copies own and inherited enumerable properties from one or more source
4+
objects to a target object.
5+
6+
If a property key is present in multiple sources, the property from the last
7+
source that defines the key prevails.
8+
9+
Both string and symbol properties are copied.
10+
11+
The target object is mutated.
12+
13+
Parameters
14+
----------
15+
target: Object
16+
Target object.
17+
18+
source: ...Object
19+
Source object(s).
20+
21+
Returns
22+
-------
23+
out: Object
24+
Target object.
25+
26+
Examples
27+
--------
28+
> var obj1 = { 'name': 'John', 'age': 30 };
29+
> var obj2 = { 'country': 'US', 'city': 'San Francisco' };
30+
> var result = {{alias}}( obj1, obj2 )
31+
{ 'name': 'John', 'age': 30, 'country': 'US', 'city': 'San Francisco' }
32+
33+
See Also
34+
--------

0 commit comments

Comments
 (0)