@ohos-ports/stdlib-buffer 0.0.0-stage → 0.4.1-beta.1

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,37 @@
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
+ // TODO: write (browser) polyfill
22
+
23
+ // MAIN //
24
+
25
+ /**
26
+ * Buffer constructor.
27
+ *
28
+ * @throws {Error} not implemented
29
+ */
30
+ function polyfill() {
31
+ throw new Error( 'not implemented' );
32
+ }
33
+
34
+
35
+ // EXPORTS //
36
+
37
+ module.exports = polyfill;
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@stdlib/buffer/ctor",
3
+ "version": "0.0.0",
4
+ "description": "Buffer.",
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
+ "binary",
60
+ "octets"
61
+ ]
62
+ }
@@ -0,0 +1,189 @@
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
+ /* eslint-disable max-lines */
22
+
23
+ import allocUnsafe = require( './../../alloc-unsafe' );
24
+ import Buffer = require( './../../ctor' );
25
+ import array2buffer = require( './../../from-array' );
26
+ import arraybuffer2buffer = require( './../../from-arraybuffer' );
27
+ import copyBuffer = require( './../../from-buffer' );
28
+ import string2buffer = require( './../../from-string' );
29
+ import reviveBuffer = require( './../../reviver' );
30
+ import buffer2json = require( './../../to-json' );
31
+
32
+ /**
33
+ * Interface describing the `buffer` namespace.
34
+ */
35
+ interface Namespace {
36
+ /**
37
+ * Allocates a buffer having a specified number of bytes.
38
+ *
39
+ * ## Notes
40
+ *
41
+ * - The underlying memory of returned `Buffer` instances is not initialized. Memory contents are unknown and may contain sensitive data.
42
+ * - When the size is less than half the pool size (specified on the `Buffer` constructor), memory is allocated from the `Buffer` pool for faster allocation of new `Buffer` instances.
43
+ *
44
+ *
45
+ * @param size - number of bytes to allocate
46
+ * @throws must provide a nonnegative integer
47
+ * @returns new `Buffer` instance
48
+ *
49
+ * @example
50
+ * var buf = ns.allocUnsafe( 10 );
51
+ * // returns <Buffer>
52
+ */
53
+ allocUnsafe: typeof allocUnsafe;
54
+
55
+ /**
56
+ * Buffer constructor.
57
+ *
58
+ * @example
59
+ * var ctor = require( './../../ctor' );
60
+ *
61
+ * var b = new ctor( [ 1, 2, 3, 4 ] );
62
+ * // returns <Buffer>
63
+ */
64
+ Buffer: typeof Buffer;
65
+
66
+ /**
67
+ * Allocates a buffer using an octet array.
68
+ *
69
+ * @param arr - octet array
70
+ * @returns new `Buffer` instance
71
+ *
72
+ * @example
73
+ * var buf = ns.array2buffer( [ 1, 2, 3, 4 ] );
74
+ * // returns <Buffer>
75
+ */
76
+ array2buffer: typeof array2buffer;
77
+
78
+ /**
79
+ * Allocates a buffer from an `ArrayBuffer`.
80
+ *
81
+ * ## Notes
82
+ *
83
+ * The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
84
+ *
85
+ * - `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view.
86
+ * - otherwise, the function returns a view of an ArrayBuffer without copying the underlying memory.
87
+ *
88
+ *
89
+ * @param buf - ArrayBuffer instance
90
+ * @param byteOffset - index specifying the location of the first buffer byte (default: 0)
91
+ * @param length - number of buffer bytes (default: buf.byteLength)
92
+ * @throws second argument must be a nonnegative integer
93
+ * @throws second argument must not exceed number of bytes in input ArrayBuffer
94
+ * @throws last argument must be a nonnegative integer
95
+ * @throws last argument must not exceed number of bytes in input ArrayBuffer
96
+ * @returns new `Buffer` instance
97
+ *
98
+ * @example
99
+ * var ArrayBuffer = require( '@stdlib/array/buffer' );
100
+ * var ab = new ArrayBuffer( 10 );
101
+ *
102
+ * var buf = ns.arraybuffer2buffer( ab );
103
+ * // returns <Buffer>
104
+ *
105
+ * @example
106
+ * var ArrayBuffer = require( '@stdlib/array/buffer' );
107
+ * var ab = new ArrayBuffer( 10 );
108
+ *
109
+ * var buf = ns.arraybuffer2buffer( ab, 2, 4 );
110
+ * // returns <Buffer>
111
+ */
112
+ arraybuffer2buffer: typeof arraybuffer2buffer;
113
+
114
+ /**
115
+ * Copies buffer data to a new `Buffer` instance.
116
+ *
117
+ * @param buffer - buffer from which to copy
118
+ * @returns new `Buffer` instance
119
+ *
120
+ * @example
121
+ * var fromArray = require( './../../from-array' );
122
+ *
123
+ * var b1 = fromArray( [ 1, 2, 3, 4 ] );
124
+ * // returns <Buffer>
125
+ *
126
+ * var b2 = ns.copyBuffer( b1 );
127
+ * // returns <Buffer>
128
+ */
129
+ copyBuffer: typeof copyBuffer;
130
+
131
+ /**
132
+ * Allocates a buffer containing a provided string.
133
+ *
134
+ * @param str - input string
135
+ * @param encoding - character encoding (default: 'utf8')
136
+ * @throws second argument must be a valid encoding
137
+ * @returns new `Buffer` instance
138
+ *
139
+ * @example
140
+ * var buf = ns.string2buffer( 'beep boop' );
141
+ * // returns <Buffer>
142
+ */
143
+ string2buffer: typeof string2buffer;
144
+
145
+ /**
146
+ * Revives a JSON-serialized `Buffer`.
147
+ *
148
+ * @param key - key
149
+ * @param value - value
150
+ * @returns value or Buffer
151
+ *
152
+ * @example
153
+ * var parseJSON = require( '@stdlib/utils/parse-json' );
154
+ *
155
+ * var str = '{"type":"Buffer","data":[5,3]}';
156
+ *
157
+ * var buf = parseJSON( str, ns.reviveBuffer );
158
+ * // returns <Buffer>[ 5, 3 ]
159
+ */
160
+ reviveBuffer: typeof reviveBuffer;
161
+
162
+ /**
163
+ * Returns a JSON representation of a `Buffer`.
164
+ *
165
+ * @param buffer - buffer to serialize
166
+ * @throws first argument must be a `Buffer`
167
+ * @returns JSON representation
168
+ *
169
+ * @example
170
+ * var array2buffer = require( './../../from-array' );
171
+ *
172
+ * var buf = array2buffer( [ 1, 2 ] );
173
+ * // returns <Buffer>
174
+ *
175
+ * var json = ns.buffer2json( buf );
176
+ * // returns { 'type': 'Buffer', 'data': [ 1, 2 ] }
177
+ */
178
+ buffer2json: typeof buffer2json;
179
+ }
180
+
181
+ /**
182
+ * Buffer.
183
+ */
184
+ declare var ns: Namespace;
185
+
186
+
187
+ // EXPORTS //
188
+
189
+ export = ns;
@@ -0,0 +1,141 @@
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
+ # array2buffer
22
+
23
+ > Allocate a [buffer][@stdlib/buffer/ctor] using an octet array.
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 array2buffer = require( '@stdlib/buffer/from-array' );
41
+ ```
42
+
43
+ #### array2buffer( arr )
44
+
45
+ Allocates a [buffer][@stdlib/buffer/ctor] using an `array` (or array-like `object`) of octets.
46
+
47
+ ```javascript
48
+ var buf = array2buffer( [ 1, 2, 3, 4 ] );
49
+ // returns <Buffer>[ 1, 2, 3, 4 ]
50
+ ```
51
+
52
+ </section>
53
+
54
+ <!-- /.usage -->
55
+
56
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57
+
58
+ <section class="notes">
59
+
60
+ </section>
61
+
62
+ <!-- /.notes -->
63
+
64
+ <!-- Package usage examples. -->
65
+
66
+ <section class="examples">
67
+
68
+ ## Examples
69
+
70
+ <!-- eslint no-undef: "error" -->
71
+
72
+ ```javascript
73
+ var array2buffer = require( '@stdlib/buffer/from-array' );
74
+
75
+ var octets;
76
+ var buf;
77
+ var str;
78
+ var i;
79
+
80
+ // Define a string we want to convert to a buffer:
81
+ str = 'this is a string.';
82
+
83
+ // Manually convert the string to an array of octets...
84
+ octets = new Array( str.length );
85
+ for ( i = 0; i < str.length; i++ ) {
86
+ octets[ i ] = str.charCodeAt( i ) % 256;
87
+ }
88
+
89
+ // Create a buffer from the octet array:
90
+ buf = array2buffer( octets );
91
+ console.log( buf.toString() );
92
+ ```
93
+
94
+ </section>
95
+
96
+ <!-- /.examples -->
97
+
98
+ <!-- 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. -->
99
+
100
+ <section class="references">
101
+
102
+ </section>
103
+
104
+ <!-- /.references -->
105
+
106
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
107
+
108
+ <section class="related">
109
+
110
+ * * *
111
+
112
+ ## See Also
113
+
114
+ - <span class="package-name">[`@stdlib/buffer/ctor`][@stdlib/buffer/ctor]</span><span class="delimiter">: </span><span class="description">Buffer.</span>
115
+ - <span class="package-name">[`@stdlib/buffer/from-arraybuffer`][@stdlib/buffer/from-arraybuffer]</span><span class="delimiter">: </span><span class="description">allocate a buffer from an ArrayBuffer.</span>
116
+ - <span class="package-name">[`@stdlib/buffer/from-buffer`][@stdlib/buffer/from-buffer]</span><span class="delimiter">: </span><span class="description">copy buffer data to a new Buffer instance.</span>
117
+ - <span class="package-name">[`@stdlib/buffer/from-string`][@stdlib/buffer/from-string]</span><span class="delimiter">: </span><span class="description">allocate a buffer containing a provided string.</span>
118
+
119
+ </section>
120
+
121
+ <!-- /.related -->
122
+
123
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124
+
125
+ <section class="links">
126
+
127
+ <!-- <related-links> -->
128
+
129
+ [@stdlib/buffer/ctor]: https://github.com/stdlib-js/buffer/tree/main/ctor
130
+
131
+ [@stdlib/buffer/from-arraybuffer]: https://github.com/stdlib-js/buffer/tree/main/from-arraybuffer
132
+
133
+ [@stdlib/buffer/from-buffer]: https://github.com/stdlib-js/buffer/tree/main/from-buffer
134
+
135
+ [@stdlib/buffer/from-string]: https://github.com/stdlib-js/buffer/tree/main/from-string
136
+
137
+ <!-- </related-links> -->
138
+
139
+ </section>
140
+
141
+ <!-- /.links -->
@@ -0,0 +1,22 @@
1
+
2
+ {{alias}}( arr )
3
+ Allocates a buffer using an octet array.
4
+
5
+ Parameters
6
+ ----------
7
+ arr: Array<integer>
8
+ Array (or array-like object) of octets from which to copy.
9
+
10
+ Returns
11
+ -------
12
+ out: Buffer
13
+ Buffer instance.
14
+
15
+ Examples
16
+ --------
17
+ > var buf = {{alias}}( [ 1, 2, 3, 4 ] )
18
+ <Buffer>[ 1, 2, 3, 4 ]
19
+
20
+ See Also
21
+ --------
22
+
@@ -0,0 +1,42 @@
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
+ /// <reference types="@stdlib/types"/>
23
+
24
+ import { Buffer } from 'buffer';
25
+ import { Collection } from '@stdlib/types/array';
26
+
27
+ /**
28
+ * Allocates a buffer using an octet array.
29
+ *
30
+ * @param arr - octet array
31
+ * @returns new `Buffer` instance
32
+ *
33
+ * @example
34
+ * var buf = fromArray( [ 1, 2, 3, 4 ] );
35
+ * // returns <Buffer>
36
+ */
37
+ declare function fromArray( arr: Collection<number> ): Buffer;
38
+
39
+
40
+ // EXPORTS //
41
+
42
+ export = fromArray;
@@ -0,0 +1,42 @@
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( './index' );
20
+
21
+
22
+ // TESTS //
23
+
24
+ // The function returns a buffer...
25
+ {
26
+ fromArray( [ 1, 2, 3, 4 ] ); // $ExpectType Buffer
27
+ }
28
+
29
+ // The compiler throws an error if the function is provided a first argument which is not a collection...
30
+ {
31
+ fromArray( 123 ); // $ExpectError
32
+ fromArray( false ); // $ExpectError
33
+ fromArray( true ); // $ExpectError
34
+ fromArray( null ); // $ExpectError
35
+ fromArray( {} ); // $ExpectError
36
+ }
37
+
38
+ // The compiler throws an error if the function is provided an invalid number of arguments...
39
+ {
40
+ fromArray(); // $ExpectError
41
+ fromArray( [ 1, 2, 3, 4 ], 4 ); // $ExpectError
42
+ }
@@ -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( '@ohos-ports/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,52 @@
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
+ * Allocate a buffer using an octet array.
23
+ *
24
+ * @module @stdlib/buffer/from-array
25
+ *
26
+ * @example
27
+ * var array2buffer = require( '@ohos-ports/stdlib-buffer/from-array' );
28
+ *
29
+ * var buf = array2buffer( [ 1, 2, 3, 4 ] );
30
+ * // returns <Buffer>
31
+ */
32
+
33
+ // MODULES //
34
+
35
+ var hasFrom = require( './has_from.js' );
36
+ var main = require( './main.js' );
37
+ var polyfill = require( './polyfill.js' );
38
+
39
+
40
+ // MAIN //
41
+
42
+ var array2buffer;
43
+ if ( hasFrom ) {
44
+ array2buffer = main;
45
+ } else {
46
+ array2buffer = polyfill;
47
+ }
48
+
49
+
50
+ // EXPORTS //
51
+
52
+ module.exports = array2buffer;
@@ -0,0 +1,53 @@
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 isCollection = require( '@ohos-ports/stdlib-assert/is-collection' );
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+ var Buffer = require( './../../ctor' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Allocates a buffer using an octet array.
32
+ *
33
+ * @param {Collection} arr - octet array
34
+ * @throws {TypeError} must provide an array-like object
35
+ * @returns {Buffer} new `Buffer` instance
36
+ *
37
+ * @example
38
+ * var fromArray = require( '@ohos-ports/stdlib-buffer/from-array' );
39
+ *
40
+ * var buf = fromArray( [ 1, 2, 3, 4 ] );
41
+ * // returns <Buffer>
42
+ */
43
+ function fromArray( arr ) {
44
+ if ( !isCollection( arr ) ) {
45
+ throw new TypeError( format( 'invalid argument. Must provide an array-like object. Value: `%s`.', arr ) );
46
+ }
47
+ return Buffer.from( arr );
48
+ }
49
+
50
+
51
+ // EXPORTS //
52
+
53
+ module.exports = fromArray;