@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,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
+ // MODULES //
22
+
23
+ var isBuffer = require( '@ohos-ports/stdlib-assert/is-buffer' );
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+ var Buffer = require( './../../ctor' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Copies buffer data to a new `Buffer` instance.
32
+ *
33
+ * @param {Buffer} buffer - buffer from which to copy
34
+ * @throws {TypeError} must provide a `Buffer` instance
35
+ * @returns {Buffer} new `Buffer` instance
36
+ *
37
+ * @example
38
+ * var fromArray = require( '@ohos-ports/stdlib-buffer/from-array' );
39
+ *
40
+ * var b1 = fromArray( [ 1, 2, 3, 4 ] );
41
+ * // returns <Buffer>
42
+ *
43
+ * var b2 = fromBuffer( b1 );
44
+ * // returns <Buffer>
45
+ */
46
+ function fromBuffer( buffer ) {
47
+ if ( !isBuffer( buffer ) ) {
48
+ throw new TypeError( format( 'invalid argument. Must provide a Buffer. Value: `%s`.', buffer ) );
49
+ }
50
+ return Buffer.from( buffer );
51
+ }
52
+
53
+
54
+ // EXPORTS //
55
+
56
+ module.exports = fromBuffer;
@@ -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
+ // MODULES //
22
+
23
+ var isBuffer = require( '@ohos-ports/stdlib-assert/is-buffer' );
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+ var Buffer = require( './../../ctor' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Copies buffer data to a new `Buffer` instance.
32
+ *
33
+ * @param {Buffer} buffer - buffer from which to copy
34
+ * @throws {TypeError} must provide a `Buffer` instance
35
+ * @returns {Buffer} new `Buffer` instance
36
+ *
37
+ * @example
38
+ * var fromArray = require( '@ohos-ports/stdlib-buffer/from-array' );
39
+ *
40
+ * var b1 = fromArray( [ 1, 2, 3, 4 ] );
41
+ * // returns <Buffer>
42
+ *
43
+ * var b2 = fromBuffer( b1 );
44
+ * // returns <Buffer>
45
+ */
46
+ function fromBuffer( buffer ) {
47
+ if ( !isBuffer( buffer ) ) {
48
+ throw new TypeError( format( 'invalid argument. Must provide a Buffer. Value: `%s`.', buffer ) );
49
+ }
50
+ return new Buffer( buffer ); // eslint-disable-line no-buffer-constructor
51
+ }
52
+
53
+
54
+ // EXPORTS //
55
+
56
+ module.exports = fromBuffer;
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@stdlib/buffer/from-buffer",
3
+ "version": "0.0.0",
4
+ "description": "Copy buffer data to a new Buffer instance.",
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
+ "copy",
63
+ "duplicate",
64
+ "reallocate",
65
+ "allocate"
66
+ ]
67
+ }
@@ -0,0 +1,158 @@
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
+ # string2buffer
22
+
23
+ > Allocate a [buffer][@stdlib/buffer/ctor] containing a provided string.
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 string2buffer = require( '@stdlib/buffer/from-string' );
41
+ ```
42
+
43
+ #### string2buffer( str\[, encoding] )
44
+
45
+ Allocates a [buffer][@stdlib/buffer/ctor] containing a provided `string`.
46
+
47
+ ```javascript
48
+ var buf = string2buffer( 'beep boop' );
49
+ // returns <Buffer>
50
+ ```
51
+
52
+ The default character encoding is `utf8`. To specify an alternative encoding, provide an `encoding` argument.
53
+
54
+ ```javascript
55
+ var buf = string2buffer( '7468697320697320612074c3a97374', 'hex' );
56
+ // returns <Buffer>
57
+ ```
58
+
59
+ The following `encodings` are supported:
60
+
61
+ - `utf8` (`utf-8`)
62
+ - `hex`
63
+ - `binary`
64
+ - `ascii`
65
+ - `ucs2` (`ucs-2`)
66
+ - `base64`
67
+
68
+ </section>
69
+
70
+ <!-- /.usage -->
71
+
72
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
73
+
74
+ <section class="notes">
75
+
76
+ </section>
77
+
78
+ <!-- /.notes -->
79
+
80
+ <!-- Package usage examples. -->
81
+
82
+ <section class="examples">
83
+
84
+ ## Examples
85
+
86
+ <!-- eslint no-undef: "error" -->
87
+
88
+ ```javascript
89
+ var randu = require( '@stdlib/random/base/randu' );
90
+ var randint = require( '@stdlib/random/base/discrete-uniform' );
91
+ var string2buffer = require( '@stdlib/buffer/from-string' );
92
+
93
+ // Create a buffer from a string:
94
+ var buf = string2buffer( 'beep boop bop' );
95
+ console.log( buf.toString() );
96
+
97
+ // Generate random strings...
98
+ var i;
99
+ var j;
100
+ for ( i = 0; i < 100; i++ ) {
101
+ j = randint( 0, buf.length );
102
+ if ( randu() < 2/buf.length ) {
103
+ buf[ j ] = 32; // space
104
+ } else {
105
+ buf[ j ] = randint( 97, 122 );
106
+ }
107
+ console.log( buf.toString() );
108
+ }
109
+ ```
110
+
111
+ </section>
112
+
113
+ <!-- /.examples -->
114
+
115
+ <!-- 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. -->
116
+
117
+ <section class="references">
118
+
119
+ </section>
120
+
121
+ <!-- /.references -->
122
+
123
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124
+
125
+ <section class="related">
126
+
127
+ * * *
128
+
129
+ ## See Also
130
+
131
+ - <span class="package-name">[`@stdlib/buffer/ctor`][@stdlib/buffer/ctor]</span><span class="delimiter">: </span><span class="description">Buffer.</span>
132
+ - <span class="package-name">[`@stdlib/buffer/from-array`][@stdlib/buffer/from-array]</span><span class="delimiter">: </span><span class="description">allocate a buffer using an octet array.</span>
133
+ - <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>
134
+ - <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>
135
+
136
+ </section>
137
+
138
+ <!-- /.related -->
139
+
140
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141
+
142
+ <section class="links">
143
+
144
+ <!-- <related-links> -->
145
+
146
+ [@stdlib/buffer/ctor]: https://github.com/stdlib-js/buffer/tree/main/ctor
147
+
148
+ [@stdlib/buffer/from-array]: https://github.com/stdlib-js/buffer/tree/main/from-array
149
+
150
+ [@stdlib/buffer/from-arraybuffer]: https://github.com/stdlib-js/buffer/tree/main/from-arraybuffer
151
+
152
+ [@stdlib/buffer/from-buffer]: https://github.com/stdlib-js/buffer/tree/main/from-buffer
153
+
154
+ <!-- </related-links> -->
155
+
156
+ </section>
157
+
158
+ <!-- /.links -->
@@ -0,0 +1,28 @@
1
+
2
+ {{alias}}( str[, encoding] )
3
+ Allocates a buffer containing a provided string.
4
+
5
+ Parameters
6
+ ----------
7
+ str: string
8
+ Input string.
9
+
10
+ encoding: string (optional)
11
+ Character encoding. Default: 'utf8'.
12
+
13
+ Returns
14
+ -------
15
+ out: Buffer
16
+ Buffer instance.
17
+
18
+ Examples
19
+ --------
20
+ > var b = {{alias}}( 'beep boop' )
21
+ <Buffer>
22
+ > b = {{alias}}( '7468697320697320612074c3a97374', 'hex' );
23
+ > b.toString()
24
+ 'this is a tést'
25
+
26
+ See Also
27
+ --------
28
+
@@ -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
+
23
+ import { Buffer } from 'buffer';
24
+
25
+ /**
26
+ * Allocates a buffer containing a provided string.
27
+ *
28
+ * @param str - input string
29
+ * @param encoding - character encoding (default: 'utf8')
30
+ * @throws second argument must be a valid encoding
31
+ * @returns new `Buffer` instance
32
+ *
33
+ * @example
34
+ * var buf = fromString( 'beep boop' );
35
+ * // returns <Buffer>
36
+ */
37
+ declare function fromString( str: string, encoding?: string ): Buffer;
38
+
39
+
40
+ // EXPORTS //
41
+
42
+ export = fromString;
@@ -0,0 +1,55 @@
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 fromString = require( './index' );
20
+
21
+ // TESTS //
22
+
23
+ // The function returns a buffer...
24
+ {
25
+ fromString( 'beep boop' ); // $ExpectType Buffer
26
+ fromString( 'beep boop', 'utf-8' ); // $ExpectType Buffer
27
+ }
28
+
29
+ // The compiler throws an error if the function is provided a first argument which is not a string...
30
+ {
31
+ fromString( 123 ); // $ExpectError
32
+ fromString( false ); // $ExpectError
33
+ fromString( true ); // $ExpectError
34
+ fromString( null ); // $ExpectError
35
+ fromString( {} ); // $ExpectError
36
+ fromString( [] ); // $ExpectError
37
+ fromString( ( x: number ): number => x ); // $ExpectError
38
+ }
39
+
40
+ // The compiler throws an error if the function is provided a second argument which is not a string...
41
+ {
42
+ fromString( 'beep boop', 123 ); // $ExpectError
43
+ fromString( 'beep boop', false ); // $ExpectError
44
+ fromString( 'beep boop', true ); // $ExpectError
45
+ fromString( 'beep boop', null ); // $ExpectError
46
+ fromString( 'beep boop', {} ); // $ExpectError
47
+ fromString( 'beep boop', [] ); // $ExpectError
48
+ fromString( 'beep boop', ( x: number ): number => x ); // $ExpectError
49
+ }
50
+
51
+ // The compiler throws an error if the function is provided an invalid number of arguments...
52
+ {
53
+ fromString(); // $ExpectError
54
+ fromString( 'beep boop', 'utf-8', 3 ); // $ExpectError
55
+ }
@@ -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 containing a provided string.
23
+ *
24
+ * @module @stdlib/buffer/from-string
25
+ *
26
+ * @example
27
+ * var string2buffer = require( '@ohos-ports/stdlib-buffer/from-string' );
28
+ *
29
+ * var buf = string2buffer( 'beep boop' );
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 string2buffer;
43
+ if ( hasFrom ) {
44
+ string2buffer = main;
45
+ } else {
46
+ string2buffer = polyfill;
47
+ }
48
+
49
+
50
+ // EXPORTS //
51
+
52
+ module.exports = string2buffer;
@@ -0,0 +1,60 @@
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 isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+ var Buffer = require( './../../ctor' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Allocates a buffer containing a provided string.
32
+ *
33
+ * @param {string} str - input string
34
+ * @param {string} [encoding="utf8"] - character encoding
35
+ * @throws {TypeError} first argument must be a string
36
+ * @throws {TypeError} second argument must be a string
37
+ * @throws {TypeError} second argument must be a valid encoding
38
+ * @returns {Buffer} new `Buffer` instance
39
+ *
40
+ * @example
41
+ * var buf = fromString( 'beep boop' );
42
+ * // returns <Buffer>
43
+ */
44
+ function fromString( str, encoding ) {
45
+ if ( !isString( str ) ) {
46
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
47
+ }
48
+ if ( arguments.length > 1 ) {
49
+ if ( !isString( encoding ) ) {
50
+ throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', encoding ) );
51
+ }
52
+ return Buffer.from( str, encoding );
53
+ }
54
+ return Buffer.from( str, 'utf8' );
55
+ }
56
+
57
+
58
+ // EXPORTS //
59
+
60
+ module.exports = fromString;
@@ -0,0 +1,60 @@
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 isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+ var Buffer = require( './../../ctor' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Allocates a buffer containing a provided string.
32
+ *
33
+ * @param {string} str - input string
34
+ * @param {string} [encoding="utf8"] - character encoding
35
+ * @throws {TypeError} first argument must be a string
36
+ * @throws {TypeError} second argument must be a string
37
+ * @throws {TypeError} second argument must be a valid encoding
38
+ * @returns {Buffer} new `Buffer` instance
39
+ *
40
+ * @example
41
+ * var buf = fromString( 'beep boop' );
42
+ * // returns <Buffer>
43
+ */
44
+ function fromString( str, encoding ) {
45
+ if ( !isString( str ) ) {
46
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
47
+ }
48
+ if ( arguments.length > 1 ) {
49
+ if ( !isString( encoding ) ) {
50
+ throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', encoding ) );
51
+ }
52
+ return new Buffer( str, encoding );
53
+ }
54
+ return new Buffer( str, 'utf8' );
55
+ }
56
+
57
+
58
+ // EXPORTS //
59
+
60
+ module.exports = fromString;