bare-script 2.0.2

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Craig A. Hobbs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,132 @@
1
+ # bare-script
2
+
3
+ [![npm](https://img.shields.io/npm/v/bare-script)](https://www.npmjs.com/package/bare-script)
4
+ [![GitHub](https://img.shields.io/github/license/craigahobbs/bare-script)](https://github.com/craigahobbs/bare-script/blob/main/LICENSE)
5
+
6
+ The bare-script package is the JavaScript implementation of the
7
+ [BareScript language](https://craigahobbs.github.io/bare-script/language/).
8
+
9
+
10
+ ## Links
11
+
12
+ - [The BareScript Language](https://craigahobbs.github.io/bare-script/language/)
13
+ - [The BareScript Library](https://craigahobbs.github.io/bare-script/library/)
14
+ - [The BareScript Expression Library](https://craigahobbs.github.io/bare-script/library/expression.html)
15
+ - [API Documentation](https://craigahobbs.github.io/bare-script/)
16
+ - [Source code](https://github.com/craigahobbs/bare-script)
17
+
18
+
19
+ ## Executing BareScript Scripts
20
+
21
+ To execute a BareScript script, parse the script using the
22
+ [parseScript](https://craigahobbs.github.io/bare-script/module-lib_parser.html#.parseScript)
23
+ function. Then execute the script using the
24
+ [executeScript](https://craigahobbs.github.io/bare-script/module-lib_runtime.html#.executeScript)
25
+ function or the
26
+ [executeScriptAsync](https://craigahobbs.github.io/bare-script/module-lib_runtimeAsync.html#.executeScriptAsync)
27
+ function. For example:
28
+
29
+ ~~~ javascript
30
+ import {executeScript} from 'bare-script/lib/runtime.js';
31
+ import {parseScript} from 'bare-script/lib/parser.js';
32
+
33
+ // Parse the script
34
+ const script = parseScript(`\
35
+ # Double a number
36
+ function double(n)
37
+ return n * 2
38
+ endfunction
39
+
40
+ return N + ' times 2 is ' + double(N)
41
+ `);
42
+
43
+ // Execute the script
44
+ const globals = {'N': 10};
45
+ console.log(executeScript(script, {'globals': globals}));
46
+ ~~~
47
+
48
+ This outputs:
49
+
50
+ ~~~
51
+ 10 times 2 is 20
52
+ ~~~
53
+
54
+
55
+ ### The BareScript Library
56
+
57
+ [The BareScript Library](https://craigahobbs.github.io/bare-script/library/) includes a set of
58
+ built-in functions for mathematical operations, object manipulation, array manipulation, regular
59
+ expressions, HTTP fetch and more. The following example demonstrates the use of the
60
+ [httpFetch](https://craigahobbs.github.io/bare-script/library/#var.vName='httpFetch'),
61
+ [objectGet](https://craigahobbs.github.io/bare-script/library/#var.vName='objectGet'), and
62
+ [arrayLength](https://craigahobbs.github.io/bare-script/library/#var.vName='arrayLength') functions.
63
+
64
+ ~~~ javascript
65
+ import {executeScriptAsync} from 'bare-script/lib/runtimeAsync.js';
66
+ import {parseScript} from 'bare-script/lib/parser.js';
67
+
68
+ // Parse the script
69
+ const script = parseScript(`\
70
+ # Fetch the BareScript library documentation JSON
71
+ libraryDocs = httpFetch('https://craigahobbs.github.io/bare-script/library/library.json')
72
+
73
+ # Return the number of library functions
74
+ return 'The BareScript Library has ' + arrayLength(objectGet(libraryDocs, 'functions')) + ' functions'
75
+ `);
76
+
77
+ // Execute the script
78
+ console.log(await executeScriptAsync(script, {'fetchFn': fetch}));
79
+ ~~~
80
+
81
+ This outputs:
82
+
83
+ ~~~
84
+ The BareScript Library has 86 functions
85
+ ~~~
86
+
87
+
88
+ ## Evaluating BareScript Expressions
89
+
90
+ To evaluate a
91
+ [BareScript expression](https://craigahobbs.github.io/bare-script/language/#expressions),
92
+ parse the expression using the
93
+ [parseExpression](https://craigahobbs.github.io/bare-script/module-lib_parser.html#.parseExpression)
94
+ function. Then evaluate the expression using the
95
+ [evaluateExpression](https://craigahobbs.github.io/bare-script/module-lib_runtime.html#.evaluateExpression)
96
+ function or the
97
+ [evaluateExpressionAsync](https://craigahobbs.github.io/bare-script/module-lib_runtimeAsync.html#.evaluateExpressionAsync)
98
+ function.
99
+
100
+ Expression evaluation includes the
101
+ [BareScript Expression Library](https://craigahobbs.github.io/bare-script/library/expression.html),
102
+ a set of built-in, spreadsheet-like functions.
103
+
104
+ For example:
105
+
106
+ ~~~ javascript
107
+ import {evaluateExpression} from 'bare-script/lib/runtime.js';
108
+ import {parseExpression} from 'bare-script/lib/parser.js';
109
+
110
+ // Parse the expression
111
+ const expr = parseExpression('2 * max(a, b, c)');
112
+
113
+ // Evaluate the expression
114
+ const variables = {'a': 1, 'b': 2, 'c': 3};
115
+ console.log(evaluateExpression(expr, null, variables))
116
+ ~~~
117
+
118
+ This outputs:
119
+
120
+ ~~~
121
+ 6
122
+ ~~~
123
+
124
+
125
+ ## Development
126
+
127
+ This package is developed using [javascript-build](https://github.com/craigahobbs/javascript-build#readme).
128
+ It was started using [javascript-template](https://github.com/craigahobbs/javascript-template#readme) as follows:
129
+
130
+ ~~~
131
+ template-specialize javascript-template/template/ bare-script/ -k package bare-script -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs' -k noapp 1
132
+ ~~~
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ // Licensed under the MIT License
3
+ // https://github.com/craigahobbs/bare-script/blob/main/LICENSE
4
+
5
+ import {argv, exit, stderr, stdout} from 'node:process';
6
+ import {parseLibraryDoc} from '../lib/libraryDoc.js';
7
+ import {readFileSync} from 'node:fs';
8
+
9
+ try {
10
+ stdout.write(JSON.stringify(parseLibraryDoc(argv.slice(2).map((file) => [file, readFileSync(file, 'utf-8')])), null, 4));
11
+ } catch (error) {
12
+ stderr.write(error.message);
13
+ stderr.write('\n');
14
+ exit(1);
15
+ }