@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,142 @@
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
+ # buffer2json
22
+
23
+ > Return a [JSON][json] representation of a [Buffer][@stdlib/buffer/ctor].
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 buffer2json = require( '@stdlib/buffer/to-json' );
41
+ ```
42
+
43
+ #### buffer2json( buffer )
44
+
45
+ Returns a [JSON][json] representation of a [`Buffer`][@stdlib/buffer/ctor].
46
+
47
+ ```javascript
48
+ var array2buffer = require( '@stdlib/buffer/from-array' );
49
+
50
+ var buf = array2buffer( [ 1, 2 ] );
51
+
52
+ var json = buffer2json( buf );
53
+ /* returns
54
+ {
55
+ 'type': 'Buffer',
56
+ 'data': [ 1, 2 ]
57
+ }
58
+ */
59
+ ```
60
+
61
+ For guidance on reviving a JSON-serialized [`Buffer`][@stdlib/buffer/ctor], see [`reviver()`][@stdlib/buffer/reviver].
62
+
63
+ </section>
64
+
65
+ <!-- /.usage -->
66
+
67
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68
+
69
+ <section class="notes">
70
+
71
+ </section>
72
+
73
+ <!-- /.notes -->
74
+
75
+ <!-- Package usage examples. -->
76
+
77
+ <section class="examples">
78
+
79
+ ## Examples
80
+
81
+ <!-- eslint no-undef: "error" -->
82
+
83
+ ```javascript
84
+ var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
85
+ var randi = require( '@stdlib/random/base/discrete-uniform' );
86
+ var buffer2json = require( '@stdlib/buffer/to-json' );
87
+
88
+ var buf = allocUnsafe( 100 );
89
+ var i;
90
+ for ( i = 0; i < buf.length; i++ ) {
91
+ buf[ i ] = randi( 0, 255 );
92
+ }
93
+
94
+ console.log( buffer2json( buf ) );
95
+ ```
96
+
97
+ </section>
98
+
99
+ <!-- /.examples -->
100
+
101
+ <!-- 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. -->
102
+
103
+ <section class="references">
104
+
105
+ </section>
106
+
107
+ <!-- /.references -->
108
+
109
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110
+
111
+ <section class="related">
112
+
113
+ * * *
114
+
115
+ ## See Also
116
+
117
+ - <span class="package-name">[`@stdlib/array/to-json`][@stdlib/array/to-json]</span><span class="delimiter">: </span><span class="description">return a JSON representation of a typed array.</span>
118
+ - <span class="package-name">[`@stdlib/buffer/reviver`][@stdlib/buffer/reviver]</span><span class="delimiter">: </span><span class="description">revive a JSON-serialized Buffer.</span>
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
+ [json]: http://www.json.org/
129
+
130
+ [@stdlib/buffer/ctor]: https://github.com/stdlib-js/buffer/tree/main/ctor
131
+
132
+ <!-- <related-links> -->
133
+
134
+ [@stdlib/array/to-json]: https://www.npmjs.com/package/@stdlib/array-to-json
135
+
136
+ [@stdlib/buffer/reviver]: https://github.com/stdlib-js/buffer/tree/main/reviver
137
+
138
+ <!-- </related-links> -->
139
+
140
+ </section>
141
+
142
+ <!-- /.links -->
@@ -0,0 +1,31 @@
1
+
2
+ {{alias}}( buffer )
3
+ Returns a JSON representation of a buffer.
4
+
5
+ Parameters
6
+ ----------
7
+ buffer: Buffer
8
+ Buffer to serialize.
9
+
10
+ Returns
11
+ -------
12
+ out: Object
13
+ JSON representation.
14
+
15
+ out.type: string
16
+ Value type. The assigned value is always "Buffer".
17
+
18
+ out.data: Array
19
+ Buffer data.
20
+
21
+ Examples
22
+ --------
23
+ > var buf = new {{alias:@stdlib/buffer/alloc-unsafe}}( 2 );
24
+ > buf[ 0 ] = 1;
25
+ > buf[ 1 ] = 2;
26
+ > var json = {{alias}}( buf )
27
+ { 'type': 'Buffer', 'data': [ 1, 2 ] }
28
+
29
+ See Also
30
+ --------
31
+
@@ -0,0 +1,46 @@
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
+ * Returns a JSON representation of a `Buffer`.
27
+ *
28
+ * @param buffer - buffer to serialize
29
+ * @throws first argument must be a `Buffer`
30
+ * @returns JSON representation
31
+ *
32
+ * @example
33
+ * var array2buffer = require( '@stdlib/buffer/from-array' );
34
+ *
35
+ * var buf = array2buffer( [ 1, 2 ] );
36
+ * // returns <Buffer>
37
+ *
38
+ * var json = buffer2json( buf );
39
+ * // returns { 'type': 'Buffer', 'data': [ 1, 2 ] }
40
+ */
41
+ declare function buffer2json( buffer: Buffer ): any;
42
+
43
+
44
+ // EXPORTS //
45
+
46
+ export = buffer2json;
@@ -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 array2buffer = require( './../../../from-array' );
20
+ import buffer2json = require( './index' );
21
+
22
+
23
+ // TESTS //
24
+
25
+ // The function returns an object...
26
+ {
27
+ const buf = array2buffer( [ 1, 2 ] );
28
+ buffer2json( buf ); // $ExpectType any
29
+ }
30
+
31
+ // The compiler throws an error if the function is provided a value other than a buffer...
32
+ {
33
+ buffer2json( 'abc' ); // $ExpectError
34
+ buffer2json( true ); // $ExpectError
35
+ buffer2json( false ); // $ExpectError
36
+ buffer2json( null ); // $ExpectError
37
+ buffer2json( undefined ); // $ExpectError
38
+ buffer2json( 5 ); // $ExpectError
39
+ buffer2json( [] ); // $ExpectError
40
+ buffer2json( {} ); // $ExpectError
41
+ buffer2json( ( x: number ): number => x ); // $ExpectError
42
+ }
43
+
44
+ // The compiler throws an error if the function is provided insufficient arguments...
45
+ {
46
+ buffer2json(); // $ExpectError
47
+ }
@@ -0,0 +1,44 @@
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
+ * Return a JSON representation of a `Buffer`.
23
+ *
24
+ * @module @stdlib/buffer/to-json
25
+ *
26
+ * @example
27
+ * var array2buffer = require( '@ohos-ports/stdlib-buffer/from-array' );
28
+ * var buffer2json = require( '@ohos-ports/stdlib-buffer/to-json' );
29
+ *
30
+ * var buf = array2buffer( [ 1, 2 ] );
31
+ * // returns <Buffer>
32
+ *
33
+ * var json = buffer2json( buf );
34
+ * // returns { 'type': 'Buffer', 'data': [ 1, 2 ] }
35
+ */
36
+
37
+ // MODULES //
38
+
39
+ var buffer2json = require( './main.js' );
40
+
41
+
42
+ // EXPORTS //
43
+
44
+ module.exports = buffer2json;
@@ -0,0 +1,63 @@
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 isBuffer = require( '@ohos-ports/stdlib-assert/is-buffer' );
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+
26
+
27
+ // MAIN //
28
+
29
+ /**
30
+ * Returns a JSON representation of a `Buffer`.
31
+ *
32
+ * @param {Buffer} buffer - buffer to serialize
33
+ * @throws {TypeError} first argument must be a `Buffer`
34
+ * @returns {Object} JSON representation
35
+ *
36
+ * @example
37
+ * var array2buffer = require( '@ohos-ports/stdlib-buffer/from-array' );
38
+ *
39
+ * var buf = array2buffer( [ 1, 2 ] );
40
+ * // returns <Buffer>
41
+ *
42
+ * var json = buffer2json( buf );
43
+ * // returns { 'type': 'Buffer', 'data': [ 1, 2 ] }
44
+ */
45
+ function buffer2json( buffer ) {
46
+ var out;
47
+ var i;
48
+ if ( !isBuffer( buffer ) ) {
49
+ throw new TypeError( format( 'invalid argument. Must provide a Buffer. Value: `%s`.', buffer ) );
50
+ }
51
+ out = {};
52
+ out.type = 'Buffer';
53
+ out.data = [];
54
+ for ( i = 0; i < buffer.length; i++ ) {
55
+ out.data.push( buffer[ i ] );
56
+ }
57
+ return out;
58
+ }
59
+
60
+
61
+ // EXPORTS //
62
+
63
+ module.exports = buffer2json;
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@stdlib/buffer/to-json",
3
+ "version": "0.0.0",
4
+ "description": "Return a JSON representation of a 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
+ "utils",
56
+ "util",
57
+ "utilities",
58
+ "utility",
59
+ "buffer",
60
+ "buf",
61
+ "binary",
62
+ "serialize",
63
+ "marshal",
64
+ "tojson",
65
+ "json",
66
+ "to",
67
+ "convert"
68
+ ]
69
+ }