Skip to content

Commit 5b19780

Browse files
committed
Add package exporting the Object constructor
1 parent c082e83 commit 5b19780

File tree

10 files changed

+662
-0
lines changed

10 files changed

+662
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# Object
22+
23+
> [Object][mdn-object] constructor.
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+
<!-- eslint-disable stdlib/no-redeclare -->
40+
41+
```javascript
42+
var Object = require( '@stdlib/object/ctor' );
43+
```
44+
45+
#### Object( value )
46+
47+
Returns a new [object][mdn-object].
48+
49+
<!-- eslint-disable stdlib/no-redeclare -->
50+
51+
```javascript
52+
var o = new Object( null );
53+
// returns {}
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- If provided `null` or `undefined`, the function returns an empty object.
67+
- If provided an existing object, the function returns the input value unchanged.
68+
- Otherwise, if provided any other value (e.g., a number, string, etc), the function will return an object of the corresponding type.
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
<!-- eslint-disable stdlib/no-redeclare -->
83+
84+
```javascript
85+
var Object = require( '@stdlib/object/ctor' );
86+
87+
var values = [
88+
'5',
89+
5,
90+
true,
91+
false,
92+
null,
93+
void 0,
94+
[],
95+
{}
96+
];
97+
98+
var i;
99+
for ( i = 0; i < values.length; i++ ) {
100+
console.log( new Object( values[ i ] ) );
101+
}
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- 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. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
[mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
129+
130+
</section>
131+
132+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 pkg = require( './../package.json' ).name;
25+
var Obj = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var values;
32+
var o;
33+
var i;
34+
35+
values = [
36+
null,
37+
void 0,
38+
5,
39+
'5',
40+
{},
41+
[]
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
o = new Obj( values[ i%values.length ] );
47+
if ( typeof o !== 'object' ) {
48+
b.fail( 'should return an object' );
49+
}
50+
}
51+
b.toc();
52+
if ( typeof o !== 'object' ) {
53+
b.fail( 'should return an object' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( value )
3+
Returns an object.
4+
5+
If provided `null` or `undefined`, the function returns an empty object.
6+
7+
If provided an existing object, the function returns the input value
8+
unchanged.
9+
10+
Otherwise, if provided any other value (e.g., a number, string, etc), the
11+
function will return an object of the corresponding type.
12+
13+
Parameters
14+
----------
15+
value: any
16+
Input value.
17+
18+
Returns
19+
-------
20+
out: Object
21+
Output object.
22+
23+
Examples
24+
--------
25+
> var o = new {{alias}}( null )
26+
{}
27+
> o = new {{alias}}( 5.0 )
28+
<Number>
29+
> o = new {{alias}}( 'beep' )
30+
<String>
31+
32+
See Also
33+
--------
34+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
// TypeScript Version: 2.0
20+
21+
// EXPORTS //
22+
23+
/**
24+
* Returns an object.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `null` or `undefined`, the function returns an empty object.
29+
* - If provided an existing object, the function returns the input value unchanged.
30+
* - Otherwise, if provided any other value (e.g., a number, string, etc), the function will return an object of the corresponding type.
31+
*
32+
* @param value - input value
33+
* @returns object
34+
*
35+
* @example
36+
* var o = new Object( null );
37+
* // returns {}
38+
*
39+
* @example
40+
* var o = new Object( 5.0 );
41+
* // returns <Number>
42+
*
43+
* @example
44+
* var o = new Object( 'beep' );
45+
* // returns <String>
46+
*
47+
* @example
48+
* var o1 = {};
49+
*
50+
* var o2 = new Object( o1 );
51+
* // returns {}
52+
*
53+
* var bool = ( o1 === o2 );
54+
* // returns true
55+
*/
56+
export = Object;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
// tslint:disable: no-construct
20+
// tslint:disable: no-unused-expression
21+
22+
import Obj = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an object...
28+
{
29+
new Obj( null ); // $ExpectType Object
30+
new Obj( undefined ); // $ExpectType Object
31+
new Obj( {} ); // $ExpectType Object
32+
new Obj( 5 ); // $ExpectType Object
33+
new Obj( 'beep' ); // $ExpectType Object
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
var Obj = require( './../lib' );
22+
23+
var values = [
24+
'5',
25+
5,
26+
true,
27+
false,
28+
null,
29+
void 0,
30+
[],
31+
{}
32+
];
33+
34+
var i;
35+
for ( i = 0; i < values.length; i++ ) {
36+
console.log( new Obj( values[ i ] ) );
37+
}

0 commit comments

Comments
 (0)