@ohos-ports/stdlib-function 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.
@@ -0,0 +1,43 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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 function2string = require( './index' );
20
+
21
+
22
+ // TESTS //
23
+
24
+ // The function returns a string...
25
+ {
26
+ function2string( function add( x: number, y: number ): number { return x + y; } ); // $ExpectType string
27
+ }
28
+
29
+ // The compiler throws an error if the function is not provided a function...
30
+ {
31
+ function2string( 'abc' ); // $ExpectError
32
+ function2string( 5 ); // $ExpectError
33
+ function2string( true ); // $ExpectError
34
+ function2string( false ); // $ExpectError
35
+ function2string( [] ); // $ExpectError
36
+ function2string( {} ); // $ExpectError
37
+ }
38
+
39
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
40
+ {
41
+ function2string(); // $ExpectError
42
+ function2string( function add( x: number, y: number ): number { return x + y; }, {} ); // $ExpectError
43
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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 string representing the source code of a provided function.
23
+ *
24
+ * @module @stdlib/function/to-string
25
+ *
26
+ * @example
27
+ * var function2string = require( '@ohos-ports/stdlib-function/to-string' );
28
+ *
29
+ * function add( x, y ) {
30
+ * return x + y;
31
+ * }
32
+ *
33
+ * var str = function2string( add );
34
+ * // e.g., returns 'function add( x, y ) {\n return x + y;\n}'
35
+ */
36
+
37
+ // MODULES //
38
+
39
+ var main = require( './main.js' );
40
+
41
+
42
+ // EXPORTS //
43
+
44
+ module.exports = main;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2022 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
+ // VARIABLES //
22
+
23
+ // Using `Function#toString` bypasses a value's own `toString` method to provide an extra, but not security proof, precaution to prevent a provided function from impersonating a native function:
24
+ var fcn2str = Function.prototype.toString;
25
+
26
+
27
+ // MAIN //
28
+
29
+ /**
30
+ * Returns a string representing the source code of a provided function.
31
+ *
32
+ * ## Notes
33
+ *
34
+ * - If called on built-in functions, functions created by `Function.prototype.bind()`, or other non-JavaScript functions, the function returns a "native" function string similar to the following:
35
+ *
36
+ * ```text
37
+ * "function foo() { [native code] }"
38
+ * ```
39
+ *
40
+ * For intrinsic object methods and functions, `foo` is the initial name of the function.
41
+ *
42
+ * - If called on a function created by the `Function` constructor, the function returns the source code of a synthesized function declaration having the name "anonymous" and using the provided parameters and function body.
43
+ *
44
+ * - Starting in ES2018, the ECMAScript specification requires that the returned string contain the exact same source code as it was declared, including any whitespace and/or comments. If the host is unable to access the source code, the specification requires that the returned string be the native function string.
45
+ *
46
+ * @param {Function} fcn - input function
47
+ * @throws {TypeError} must provide a function
48
+ * @returns {string} string representing function source code
49
+ *
50
+ * @example
51
+ * function add( x, y ) {
52
+ * return x + y;
53
+ * }
54
+ *
55
+ * var str = function2string( add );
56
+ * // e.g., returns 'function add( x, y ) {\n return x + y;\n}'
57
+ */
58
+ function function2string( fcn ) {
59
+ return fcn2str.call( fcn );
60
+ }
61
+
62
+
63
+ // EXPORTS //
64
+
65
+ module.exports = function2string;
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@stdlib/function/to-string",
3
+ "version": "0.0.0",
4
+ "description": "Return a string representing the source code of a provided function.",
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
+ "sunos",
48
+ "win32",
49
+ "windows"
50
+ ],
51
+ "keywords": [
52
+ "stdlib",
53
+ "function",
54
+ "serialization",
55
+ "serialize",
56
+ "source",
57
+ "string",
58
+ "code"
59
+ ],
60
+ "__stdlib__": {}
61
+ }