Skip to content

Commit c60558f

Browse files
committed
Add BigInt factory function
1 parent 17f60de commit c60558f

File tree

10 files changed

+516
-0
lines changed

10 files changed

+516
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# BigInt
22+
23+
> [BigInt][mdn-bigint] factory.
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 BigInt = require( '@stdlib/bigint/ctor' );
41+
```
42+
43+
#### BigInt( value )
44+
45+
Returns a [`BigInt`][mdn-bigint] primitive.
46+
47+
<!-- run-disable -->
48+
49+
```javascript
50+
var v = BigInt( '1' );
51+
// returns <bigint>
52+
```
53+
54+
TODO: document properties/methods
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+
- Unlike conventional constructors, the function does **not** support the `new` keyword.
67+
- The function is only supported in environments which support [`BigInt`][mdn-bigint]. In non-supporting environments, the value is `undefined`.
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<!-- Package usage examples. -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
83+
var BigInt = require( '@stdlib/bigint/ctor' );
84+
85+
var v;
86+
87+
if ( hasBigIntSupport() ) {
88+
v = BigInt( '1' );
89+
90+
// Print the value type:
91+
console.log( typeof v );
92+
// => 'bigint'
93+
94+
// Serialize the BigInt as a string:
95+
console.log( v.toString() );
96+
// => '1'
97+
} else {
98+
console.log( 'Environment does not support BigInts.' );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- 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. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
113+
114+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
115+
116+
<section class="links">
117+
118+
[mdn-bigint]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
119+
120+
</section>
121+
122+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
25+
var isBigInt = require( '@stdlib/assert/is-bigint' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var BigInt = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var opts = {
33+
'skip': !hasBigIntSupport()
34+
};
35+
36+
37+
// MAIN //
38+
39+
bench( pkg, opts, function benchmark( b ) {
40+
var v;
41+
var i;
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
v = BigInt( i );
46+
if ( typeof v !== 'bigint' ) {
47+
b.fail( 'should return a BigInt' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isBigInt( v ) ) {
52+
b.fail( 'should return a BigInt' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( value )
3+
Returns a BigInt.
4+
5+
Unlike conventional constructors, this function does **not** support the
6+
`new` keyword.
7+
8+
This function is only supported in environments which support BigInts.
9+
10+
Parameters
11+
----------
12+
value: integer|string
13+
Value of the BigInt.
14+
15+
Returns
16+
-------
17+
out: BigInt
18+
BigInt.
19+
20+
Examples
21+
--------
22+
> var v = ( {{alias}} ) ? {{alias}}( '1' ) : null
23+
24+
25+
TODO: document properties/methods
26+
27+
See Also
28+
--------
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 a BigInt.
25+
*
26+
* ## Notes
27+
*
28+
* - Unlike conventional constructors, this function does **not** support the `new` keyword.
29+
* - This function is only supported in environments which support `BigInt`s.
30+
*
31+
* @param value - value of the BigInt
32+
* @returns BigInt
33+
*/
34+
export = BigInt; // tslint:disable-line
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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-unused-expression, no-unsafe-any
20+
21+
import BigInt = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns a BigInt...
27+
{
28+
BigInt( '1' ); // $ExpectType any
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
22+
var BigInt = require( './../lib' );
23+
24+
var v;
25+
26+
if ( hasBigIntSupport() ) {
27+
v = BigInt( '1' );
28+
29+
// Print the value type:
30+
console.log( typeof v );
31+
// => 'bigint'
32+
33+
// Serialize the BigInt as a string:
34+
console.log( v.toString() );
35+
// => '1'
36+
} else {
37+
console.log( 'Environment does not support BigInts.' );
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
/**
22+
* BigInt factory.
23+
*
24+
* @module @stdlib/bigint/ctor
25+
*
26+
* @example
27+
* var BigInt = require( '@stdlib/bigint/ctor' );
28+
*
29+
* var v = BigInt( '1' );
30+
* // returns <bigint>
31+
*/
32+
33+
// MODULES //
34+
35+
var BigInt = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = BigInt;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
/* global BigInt */
20+
21+
'use strict';
22+
23+
// MAIN //
24+
25+
var BigInteger = ( typeof BigInt === 'function' ) ? BigInt : void 0; // eslint-disable-line stdlib/require-globals
26+
27+
28+
// EXPORTS //
29+
30+
module.exports = BigInteger;

0 commit comments

Comments
 (0)