@ohos-ports/stdlib-buffer 0.0.0-stage → 0.4.1-beta.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.
Files changed (75) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +205 -2
  4. package/SECURITY.md +5 -0
  5. package/alloc-unsafe/README.md +140 -0
  6. package/alloc-unsafe/docs/repl.txt +28 -0
  7. package/alloc-unsafe/docs/types/index.d.ts +47 -0
  8. package/alloc-unsafe/docs/types/test.ts +44 -0
  9. package/alloc-unsafe/lib/has_alloc_unsafe.js +34 -0
  10. package/alloc-unsafe/lib/index.js +52 -0
  11. package/alloc-unsafe/lib/main.js +56 -0
  12. package/alloc-unsafe/lib/polyfill.js +56 -0
  13. package/alloc-unsafe/package.json +67 -0
  14. package/ctor/README.md +182 -0
  15. package/ctor/docs/repl.txt +91 -0
  16. package/ctor/docs/types/index.d.ts +37 -0
  17. package/ctor/docs/types/test.ts +35 -0
  18. package/ctor/lib/index.js +52 -0
  19. package/ctor/lib/main.js +28 -0
  20. package/ctor/lib/polyfill.js +37 -0
  21. package/ctor/package.json +62 -0
  22. package/docs/types/index.d.ts +189 -0
  23. package/from-array/README.md +141 -0
  24. package/from-array/docs/repl.txt +22 -0
  25. package/from-array/docs/types/index.d.ts +42 -0
  26. package/from-array/docs/types/test.ts +42 -0
  27. package/from-array/lib/has_from.js +34 -0
  28. package/from-array/lib/index.js +52 -0
  29. package/from-array/lib/main.js +53 -0
  30. package/from-array/lib/polyfill.js +53 -0
  31. package/from-array/package.json +69 -0
  32. package/from-arraybuffer/README.md +198 -0
  33. package/from-arraybuffer/docs/repl.txt +48 -0
  34. package/from-arraybuffer/docs/types/index.d.ts +64 -0
  35. package/from-arraybuffer/docs/types/test.ts +72 -0
  36. package/from-arraybuffer/lib/has_from.js +35 -0
  37. package/from-arraybuffer/lib/index.js +54 -0
  38. package/from-arraybuffer/lib/main.js +105 -0
  39. package/from-arraybuffer/lib/node_version.js +33 -0
  40. package/from-arraybuffer/lib/polyfill.js +107 -0
  41. package/from-arraybuffer/package.json +69 -0
  42. package/from-buffer/README.md +145 -0
  43. package/from-buffer/docs/repl.txt +23 -0
  44. package/from-buffer/docs/types/index.d.ts +45 -0
  45. package/from-buffer/docs/types/test.ts +47 -0
  46. package/from-buffer/lib/has_from.js +34 -0
  47. package/from-buffer/lib/index.js +56 -0
  48. package/from-buffer/lib/main.js +56 -0
  49. package/from-buffer/lib/polyfill.js +56 -0
  50. package/from-buffer/package.json +67 -0
  51. package/from-string/README.md +158 -0
  52. package/from-string/docs/repl.txt +28 -0
  53. package/from-string/docs/types/index.d.ts +42 -0
  54. package/from-string/docs/types/test.ts +55 -0
  55. package/from-string/lib/has_from.js +34 -0
  56. package/from-string/lib/index.js +52 -0
  57. package/from-string/lib/main.js +60 -0
  58. package/from-string/lib/polyfill.js +60 -0
  59. package/from-string/package.json +66 -0
  60. package/lib/index.js +114 -0
  61. package/package.json +63 -4
  62. package/reviver/README.md +137 -0
  63. package/reviver/docs/repl.txt +32 -0
  64. package/reviver/docs/types/index.d.ts +41 -0
  65. package/reviver/docs/types/test.ts +49 -0
  66. package/reviver/lib/index.js +43 -0
  67. package/reviver/lib/main.js +58 -0
  68. package/reviver/package.json +73 -0
  69. package/to-json/README.md +142 -0
  70. package/to-json/docs/repl.txt +31 -0
  71. package/to-json/docs/types/index.d.ts +46 -0
  72. package/to-json/docs/types/test.ts +47 -0
  73. package/to-json/lib/index.js +44 -0
  74. package/to-json/lib/main.js +63 -0
  75. package/to-json/package.json +69 -0
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
24
+ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
25
+ var Buffer = require( './../../ctor' );
26
+ var format = require( '@stdlib/string/format' );
27
+ var version = require( './node_version.js' );
28
+
29
+
30
+ // MAIN //
31
+
32
+ /**
33
+ * Allocates a buffer from an `ArrayBuffer`.
34
+ *
35
+ * ## Notes
36
+ *
37
+ * The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
38
+ *
39
+ * - `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view.
40
+ * - otherwise, the function returns a view of an ArrayBuffer without copying the underlying memory.
41
+ *
42
+ * @param {ArrayBuffer} buf - ArrayBuffer instance
43
+ * @param {NonNegativeInteger} [byteOffset=0] - index specifying the location of the first buffer byte
44
+ * @param {NonNegativeInteger} [length=buf.byteLength] - number of buffer bytes
45
+ * @throws {TypeError} first argument must be an ArrayBuffer
46
+ * @throws {TypeError} second argument must be a nonnegative integer
47
+ * @throws {RangeError} second argument must not exceed number of bytes in input ArrayBuffer
48
+ * @throws {TypeError} last argument must be a nonnegative integer
49
+ * @throws {RangeError} last argument must not exceed number of bytes in input ArrayBuffer
50
+ * @returns {Buffer} new `Buffer` instance
51
+ *
52
+ * @example
53
+ * var ArrayBuffer = require( '@stdlib/array/buffer' );
54
+ *
55
+ * var ab = new ArrayBuffer( 10 );
56
+ * var buf = fromArrayBuffer( ab );
57
+ * // returns <Buffer>
58
+ *
59
+ * @example
60
+ * var ArrayBuffer = require( '@stdlib/array/buffer' );
61
+ *
62
+ * var ab = new ArrayBuffer( 10 );
63
+ * var buf = fromArrayBuffer( ab, 2, 4 );
64
+ * // returns <Buffer>
65
+ */
66
+ function fromArrayBuffer( buf, byteOffset, length ) {
67
+ var offset;
68
+ var len;
69
+ if ( !isArrayBuffer( buf ) ) {
70
+ throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', buf ) );
71
+ }
72
+ if ( arguments.length > 1 ) {
73
+ if ( !isNonNegativeInteger( byteOffset ) ) {
74
+ throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', byteOffset ) );
75
+ }
76
+ if ( byteOffset > buf.byteLength ) {
77
+ throw new RangeError( format( 'invalid argument. Second argument must not exceed the number of bytes in the input ArrayBuffer. Value: `%s`.', byteOffset ) );
78
+ }
79
+ offset = byteOffset;
80
+ if ( arguments.length > 2 ) {
81
+ if ( !isNonNegativeInteger( length ) ) {
82
+ throw new TypeError( format( 'invalid argument. Last argument must be a nonnegative integer. Value: `%s`.', length ) );
83
+ }
84
+ if ( length > buf.byteLength ) {
85
+ throw new RangeError( format( 'invalid argument. Last argument must not exceed the number of bytes in the input ArrayBuffer. Value: `%s`.', length ) );
86
+ }
87
+ len = length;
88
+ } else {
89
+ len = buf.byteLength - offset;
90
+ }
91
+ } else {
92
+ offset = 0;
93
+ len = buf.byteLength;
94
+ }
95
+ // Address Node v5.x where providing an empty ArrayBuffer throws an error:
96
+ if ( len === 0 && version < 6 ) {
97
+ return Buffer.from( [] );
98
+ }
99
+ return Buffer.from( buf, offset, len );
100
+ }
101
+
102
+
103
+ // EXPORTS //
104
+
105
+ module.exports = fromArrayBuffer;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 NODE_VERSION = require( '@stdlib/process/node-version' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ var MAJOR = ( NODE_VERSION ) ? parseInt( NODE_VERSION.split( '.' )[ 0 ], 10 ) : -1;
29
+
30
+
31
+ // EXPORTS //
32
+
33
+ module.exports = MAJOR;
@@ -0,0 +1,107 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
24
+ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
25
+ var Buffer = require( './../../ctor' );
26
+ var Uint8Array = require( '@stdlib/array/uint8' );
27
+ var fromArray = require( './../../from-array' );
28
+ var format = require( '@stdlib/string/format' );
29
+ var version = require( './node_version.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Allocates a buffer from an `ArrayBuffer`.
36
+ *
37
+ * ## Notes
38
+ *
39
+ * The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
40
+ *
41
+ * - `<3.0.0`: the function copies ArrayBuffer bytes to a new Buffer instance.
42
+ * - `>=3.0.0`: if provided a byte offset, the function copies `ArrayBuffer` bytes to a new `Buffer` instance; otherwise, the function returns a view of an `ArrayBuffer` without copying the underlying memory.
43
+ * - `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view.
44
+ *
45
+ * @param {ArrayBuffer} buf - ArrayBuffer instance
46
+ * @param {NonNegativeInteger} [byteOffset=0] - index specifying the location of the first buffer byte
47
+ * @param {NonNegativeInteger} [length=buf.byteLength] - number of buffer bytes
48
+ * @throws {TypeError} first argument must be an ArrayBuffer
49
+ * @throws {TypeError} second argument must be a nonnegative integer
50
+ * @throws {RangeError} second argument must not exceed number of bytes in input ArrayBuffer
51
+ * @throws {TypeError} last argument must be a nonnegative integer
52
+ * @throws {RangeError} last argument must not exceed number of bytes in input ArrayBuffer
53
+ * @returns {Buffer} new `Buffer` instance
54
+ *
55
+ * @example
56
+ * var ArrayBuffer = require( '@stdlib/array/buffer' );
57
+ * var ab = new ArrayBuffer( 10 );
58
+ *
59
+ * var buf = fromArrayBuffer( ab );
60
+ * // returns <Buffer>
61
+ *
62
+ * @example
63
+ * var ArrayBuffer = require( '@stdlib/array/buffer' );
64
+ * var ab = new ArrayBuffer( 10 );
65
+ *
66
+ * var buf = fromArrayBuffer( ab, 2, 4 );
67
+ * // returns <Buffer>
68
+ */
69
+ function fromArrayBuffer( buf, byteOffset, length ) {
70
+ var len;
71
+ if ( !isArrayBuffer( buf ) ) {
72
+ throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', buf ) );
73
+ }
74
+ if ( arguments.length > 1 ) {
75
+ if ( !isNonNegativeInteger( byteOffset ) ) {
76
+ throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', byteOffset ) );
77
+ }
78
+ if ( byteOffset > buf.byteLength ) {
79
+ throw new RangeError( format( 'invalid argument. Second argument must not exceed the number of bytes in the input ArrayBuffer. Value: `%s`.', byteOffset ) );
80
+ }
81
+ if ( arguments.length > 2 ) {
82
+ if ( !isNonNegativeInteger( length ) ) {
83
+ throw new TypeError( format( 'invalid argument. Last argument must be a nonnegative integer. Value: `%s`.', length ) );
84
+ }
85
+ if ( length > buf.byteLength ) {
86
+ throw new RangeError( format( 'invalid argument. Last argument must not exceed the number of bytes in the input ArrayBuffer. Value: `%s`.', length ) );
87
+ }
88
+ len = length;
89
+ } else {
90
+ len = buf.byteLength - byteOffset;
91
+ }
92
+ return fromArray( new Uint8Array( buf, byteOffset, len ) );
93
+ }
94
+ if ( version < 3 ) {
95
+ return fromArray( new Uint8Array( buf ) );
96
+ }
97
+ // Address Node <= v5.x where providing an empty ArrayBuffer throws an error:
98
+ if ( buf.byteLength === 0 ) {
99
+ return new Buffer( [] );
100
+ }
101
+ return new Buffer( buf );
102
+ }
103
+
104
+
105
+ // EXPORTS //
106
+
107
+ module.exports = fromArrayBuffer;
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@stdlib/buffer/from-arraybuffer",
3
+ "version": "0.0.0",
4
+ "description": "Allocate a buffer from an ArrayBuffer.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {},
26
+ "homepage": "https://github.com/stdlib-js/stdlib",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git://github.com/stdlib-js/stdlib.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/stdlib-js/stdlib/issues"
33
+ },
34
+ "dependencies": {},
35
+ "devDependencies": {},
36
+ "engines": {
37
+ "node": ">=0.10.0",
38
+ "npm": ">2.7.0"
39
+ },
40
+ "os": [
41
+ "aix",
42
+ "darwin",
43
+ "freebsd",
44
+ "linux",
45
+ "macos",
46
+ "openbsd",
47
+ "openharmony",
48
+ "sunos",
49
+ "win32",
50
+ "windows"
51
+ ],
52
+ "keywords": [
53
+ "stdlib",
54
+ "stdtypes",
55
+ "types",
56
+ "data",
57
+ "structure",
58
+ "buffer",
59
+ "buf",
60
+ "binary",
61
+ "octets",
62
+ "octet",
63
+ "array",
64
+ "typed",
65
+ "typed-array",
66
+ "allocate",
67
+ "arraybuffer"
68
+ ]
69
+ }
@@ -0,0 +1,145 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2018 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
+ # copyBuffer
22
+
23
+ > Copy [buffer][@stdlib/buffer/ctor] data to a new [`Buffer`][@stdlib/buffer/ctor] instance.
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 copyBuffer = require( '@stdlib/buffer/from-buffer' );
41
+ ```
42
+
43
+ #### copyBuffer( buffer )
44
+
45
+ Copies [buffer][@stdlib/buffer/ctor] data to a new [`Buffer`][@stdlib/buffer/ctor] instance.
46
+
47
+ ```javascript
48
+ var array2buffer = require( '@stdlib/buffer/from-array' );
49
+
50
+ var b1 = array2buffer( [ 1, 2, 3, 4 ] );
51
+ // returns <Buffer>[ 1, 2, 3, 4 ]
52
+
53
+ var b2 = copyBuffer( b1 );
54
+ // returns <Buffer>[ 1, 2, 3, 4 ]
55
+ ```
56
+
57
+ </section>
58
+
59
+ <!-- /.usage -->
60
+
61
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62
+
63
+ <section class="notes">
64
+
65
+ </section>
66
+
67
+ <!-- /.notes -->
68
+
69
+ <!-- Package usage examples. -->
70
+
71
+ <section class="examples">
72
+
73
+ ## Examples
74
+
75
+ <!-- eslint no-undef: "error" -->
76
+
77
+ ```javascript
78
+ var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
79
+ var copyBuffer = require( '@stdlib/buffer/from-buffer' );
80
+
81
+ var bool;
82
+ var b1;
83
+ var b2;
84
+ var i;
85
+
86
+ // Allocate a new buffer:
87
+ b1 = allocUnsafe( 10 );
88
+
89
+ // Generate a new buffer from the existing buffer:
90
+ b2 = copyBuffer( b1 );
91
+
92
+ bool = ( b2 === b1 );
93
+ // returns false
94
+
95
+ bool = ( b2.length === b1.length );
96
+ // returns true
97
+
98
+ for ( i = 0; i < b2.length; i++ ) {
99
+ console.log( b2[ i ] === b1[ i ] );
100
+ // => true
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
+ * * *
121
+
122
+ ## See Also
123
+
124
+ - <span class="package-name">[`@stdlib/buffer/alloc-unsafe`][@stdlib/buffer/alloc-unsafe]</span><span class="delimiter">: </span><span class="description">allocate a buffer having a specified number of bytes.</span>
125
+ - <span class="package-name">[`@stdlib/buffer/ctor`][@stdlib/buffer/ctor]</span><span class="delimiter">: </span><span class="description">Buffer.</span>
126
+
127
+ </section>
128
+
129
+ <!-- /.related -->
130
+
131
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132
+
133
+ <section class="links">
134
+
135
+ <!-- <related-links> -->
136
+
137
+ [@stdlib/buffer/alloc-unsafe]: https://github.com/stdlib-js/buffer/tree/main/alloc-unsafe
138
+
139
+ [@stdlib/buffer/ctor]: https://github.com/stdlib-js/buffer/tree/main/ctor
140
+
141
+ <!-- </related-links> -->
142
+
143
+ </section>
144
+
145
+ <!-- /.links -->
@@ -0,0 +1,23 @@
1
+
2
+ {{alias}}( buffer )
3
+ Copies buffer data to a new Buffer instance.
4
+
5
+ Parameters
6
+ ----------
7
+ buffer: Buffer
8
+ Buffer to copy from.
9
+
10
+ Returns
11
+ -------
12
+ out: Buffer
13
+ Buffer instance.
14
+
15
+ Examples
16
+ --------
17
+ > var b1 = {{alias:@stdlib/buffer/from-array}}( [ 1, 2, 3, 4 ] );
18
+ > var b2 = {{alias}}( b1 )
19
+ <Buffer>[ 1, 2, 3, 4 ]
20
+
21
+ See Also
22
+ --------
23
+
@@ -0,0 +1,45 @@
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: 4.1
20
+
21
+ /// <reference types="node"/>
22
+
23
+ import { Buffer } from 'buffer';
24
+
25
+ /**
26
+ * Copies buffer data to a new `Buffer` instance.
27
+ *
28
+ * @param buffer - buffer from which to copy
29
+ * @returns new `Buffer` instance
30
+ *
31
+ * @example
32
+ * var fromArray = require( '@stdlib/buffer/from-array' );
33
+ *
34
+ * var b1 = fromArray( [ 1, 2, 3, 4 ] );
35
+ * // returns <Buffer>
36
+ *
37
+ * var b2 = fromBuffer( b1 );
38
+ * // returns <Buffer>
39
+ */
40
+ declare function fromBuffer( buffer: Buffer ): Buffer;
41
+
42
+
43
+ // EXPORTS //
44
+
45
+ export = fromBuffer;
@@ -0,0 +1,47 @@
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
+ import fromArray = require( './../../../from-array' );
20
+ import fromBuffer = require( './index' );
21
+
22
+ // TESTS //
23
+
24
+ // The function returns a buffer...
25
+ {
26
+ const buf = fromArray( [ 1, 2, 3, 4 ] );
27
+ fromBuffer( buf ); // $ExpectType Buffer
28
+ }
29
+
30
+ // The compiler throws an error if the function is provided a first argument which is not a buffer...
31
+ {
32
+ fromBuffer( 'abc' ); // $ExpectError
33
+ fromBuffer( 123 ); // $ExpectError
34
+ fromBuffer( false ); // $ExpectError
35
+ fromBuffer( true ); // $ExpectError
36
+ fromBuffer( null ); // $ExpectError
37
+ fromBuffer( {} ); // $ExpectError
38
+ fromBuffer( [] ); // $ExpectError
39
+ fromBuffer( ( x: number ): number => x ); // $ExpectError
40
+ }
41
+
42
+ // The compiler throws an error if the function is provided an invalid number of arguments...
43
+ {
44
+ const buf = fromArray( [ 1, 2, 3, 4 ] );
45
+ fromBuffer(); // $ExpectError
46
+ fromBuffer( buf, 4 ); // $ExpectError
47
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 isFunction = require( '@stdlib/assert/is-function' );
24
+ var Buffer = require( './../../ctor' );
25
+
26
+
27
+ // MAIN //
28
+
29
+ var bool = isFunction( Buffer.from );
30
+
31
+
32
+ // EXPORTS //
33
+
34
+ module.exports = bool;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ * Copy buffer data to a new `Buffer` instance.
23
+ *
24
+ * @module @stdlib/buffer/from-buffer
25
+ *
26
+ * @example
27
+ * var fromArray = require( '@stdlib/buffer/from-array' );
28
+ * var copyBuffer = require( '@stdlib/buffer/from-buffer' );
29
+ *
30
+ * var b1 = fromArray( [ 1, 2, 3, 4 ] );
31
+ * // returns <Buffer>
32
+ *
33
+ * var b2 = copyBuffer( b1 );
34
+ * // returns <Buffer>
35
+ */
36
+
37
+ // MODULES //
38
+
39
+ var hasFrom = require( './has_from.js' );
40
+ var main = require( './main.js' );
41
+ var polyfill = require( './polyfill.js' );
42
+
43
+
44
+ // MAIN //
45
+
46
+ var copyBuffer;
47
+ if ( hasFrom ) {
48
+ copyBuffer = main;
49
+ } else {
50
+ copyBuffer = polyfill;
51
+ }
52
+
53
+
54
+ // EXPORTS //
55
+
56
+ module.exports = copyBuffer;