dirac-stdlib 0.1.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.
- package/LICENSE +21 -0
- package/README.md +143 -0
- package/lib/index.di +8 -0
- package/lib/math.di +25 -0
- package/lib/string.di +113 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zhi Wang
|
|
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,143 @@
|
|
|
1
|
+
# dirac-stdlib
|
|
2
|
+
|
|
3
|
+
Standard library for DIRAC - provides core utilities for math and string operations.
|
|
4
|
+
|
|
5
|
+
**Status**: ✅ Production ready - All 24 unit tests passing
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- ✅ **Math Operations**: ADD, SQUARE, FACTORIAL with Number() type coercion
|
|
10
|
+
- ✅ **String Operations**: SUBSTRING, REPLACE, SPLIT, JOIN, TRIM, UPPERCASE, LOWERCASE, INDEXOF, INCLUDES, LENGTH, CONCAT
|
|
11
|
+
- ✅ **Unit Tests**: Comprehensive test coverage (24 tests)
|
|
12
|
+
- ✅ **Type Safety**: Automatic type coercion for robust handling
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install dirac-stdlib
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Math Operations
|
|
23
|
+
|
|
24
|
+
```xml
|
|
25
|
+
<dirac>
|
|
26
|
+
<import src="dirac-stdlib/lib/math"/>
|
|
27
|
+
|
|
28
|
+
<!-- Add two numbers -->
|
|
29
|
+
<call name="ADD" a="5" b="3" output="result"/>
|
|
30
|
+
<output><variable name="result"/></output> <!-- 8 -->
|
|
31
|
+
|
|
32
|
+
<!-- Square a number -->
|
|
33
|
+
<call name="SQUARE" x="4" output="squared"/>
|
|
34
|
+
<output><variable name="squared"/></output> <!-- 16 -->
|
|
35
|
+
|
|
36
|
+
<!-- Calculate factorial -->
|
|
37
|
+
<call name="FACTORIAL" num="5" output="fact"/>
|
|
38
|
+
<output><variable name="fact"/></output> <!-- 120 -->
|
|
39
|
+
</dirac>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### String Operations
|
|
43
|
+
|
|
44
|
+
```xml
|
|
45
|
+
<dirac>
|
|
46
|
+
<import src="dirac-stdlib/lib/string.di"/>
|
|
47
|
+
|
|
48
|
+
<!-- Extract substring -->
|
|
49
|
+
<call name="SUBSTRING" str="Hello World" start="0" length="5"/>
|
|
50
|
+
<!-- Output: Hello -->
|
|
51
|
+
|
|
52
|
+
<!-- Replace text -->
|
|
53
|
+
<call name="REPLACE" str="Hello World" search="World" replace="DIRAC"/>
|
|
54
|
+
<!-- Output: Hello DIRAC -->
|
|
55
|
+
|
|
56
|
+
<!-- Split string -->
|
|
57
|
+
<call name="SPLIT" str="apple,banana,cherry" delimiter=","/>
|
|
58
|
+
<!-- Output:
|
|
59
|
+
apple
|
|
60
|
+
banana
|
|
61
|
+
cherry
|
|
62
|
+
-->
|
|
63
|
+
|
|
64
|
+
<!-- Join array -->
|
|
65
|
+
<call name="JOIN" items="apple
|
|
66
|
+
banana
|
|
67
|
+
cherry" delimiter=", "/>
|
|
68
|
+
<!-- Output: apple, banana, cherry -->
|
|
69
|
+
|
|
70
|
+
<!-- Trim whitespace -->
|
|
71
|
+
<call name="TRIM" str=" hello "/>
|
|
72
|
+
<!-- Output: hello -->
|
|
73
|
+
|
|
74
|
+
<!-- Case conversion -->
|
|
75
|
+
<call name="UPPERCASE" str="hello"/>
|
|
76
|
+
<!-- Output: HELLO -->
|
|
77
|
+
|
|
78
|
+
<call name="LOWERCASE" str="WORLD"/>
|
|
79
|
+
<!-- Output: world -->
|
|
80
|
+
|
|
81
|
+
<!-- Search operations -->
|
|
82
|
+
<call name="INDEXOF" str="hello world" search="world"/>
|
|
83
|
+
<!-- Output: 6 -->
|
|
84
|
+
|
|
85
|
+
<call name="INCLUDES" str="hello world" search="world"/>
|
|
86
|
+
<!-- Output: true -->
|
|
87
|
+
|
|
88
|
+
<!-- String length -->
|
|
89
|
+
<call name="LENGTH" str="hello"/>
|
|
90
|
+
<!-- Output: 5 -->
|
|
91
|
+
|
|
92
|
+
<!-- Concatenate -->
|
|
93
|
+
<call name="CONCAT" str1="hello" str2=" world"/>
|
|
94
|
+
<!-- Output: hello world -->
|
|
95
|
+
</dirac>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## API Reference
|
|
99
|
+
|
|
100
|
+
### Math Operations
|
|
101
|
+
|
|
102
|
+
- **ADD(a, b)**: Add two numbers
|
|
103
|
+
- **SQUARE(x)**: Square a number
|
|
104
|
+
- **FACTORIAL(num)**: Calculate factorial
|
|
105
|
+
|
|
106
|
+
### String Operations
|
|
107
|
+
|
|
108
|
+
- **SUBSTRING(str, start, [length])**: Extract portion of string
|
|
109
|
+
- **REPLACE(str, search, replace, [regex])**: Replace text (supports regex with regex="true")
|
|
110
|
+
- **SPLIT(str, delimiter)**: Split string by delimiter (returns newline-separated values)
|
|
111
|
+
- **JOIN(items, [delimiter])**: Join newline-separated items with delimiter
|
|
112
|
+
- **TRIM(str)**: Remove leading and trailing whitespace
|
|
113
|
+
- **UPPERCASE(str)**: Convert to uppercase
|
|
114
|
+
- **LOWERCASE(str)**: Convert to lowercase
|
|
115
|
+
- **INDEXOF(str, search, [start])**: Find position of substring (-1 if not found)
|
|
116
|
+
- **INCLUDES(str, search)**: Check if string contains substring (returns true/false)
|
|
117
|
+
- **LENGTH(str)**: Get string length
|
|
118
|
+
- **CONCAT(str1, str2)**: Concatenate two strings
|
|
119
|
+
|
|
120
|
+
See [EXAMPLES.md](./EXAMPLES.md) for more usage examples.
|
|
121
|
+
|
|
122
|
+
## Testing
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
npm test
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
All 24 unit tests passing:
|
|
129
|
+
- 8 math operation tests
|
|
130
|
+
- 16 string operation tests
|
|
131
|
+
|
|
132
|
+
## Development
|
|
133
|
+
|
|
134
|
+
This library requires `dirac-lang` as a devDependency for testing:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm install
|
|
138
|
+
npm test
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
MIT
|
package/lib/index.di
ADDED
package/lib/math.di
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<!-- Math utility library -->
|
|
2
|
+
<dirac>
|
|
3
|
+
<!-- Square a number -->
|
|
4
|
+
<subroutine name="SQUARE" param-x="number">
|
|
5
|
+
<eval name="result">return Number(x) * Number(x);</eval>
|
|
6
|
+
<output><variable name="result" /></output>
|
|
7
|
+
</subroutine>
|
|
8
|
+
|
|
9
|
+
<!-- Add two numbers -->
|
|
10
|
+
<subroutine name="ADD" param-a="number" param-b="number">
|
|
11
|
+
<eval name="result">return Number(a) + Number(b);</eval>
|
|
12
|
+
<output><variable name="result" /></output>
|
|
13
|
+
</subroutine>
|
|
14
|
+
|
|
15
|
+
<!-- Calculate factorial -->
|
|
16
|
+
<subroutine name="FACTORIAL" param-num="number">
|
|
17
|
+
<eval name="result">
|
|
18
|
+
const n = parseInt(num, 10);
|
|
19
|
+
let fact = 1;
|
|
20
|
+
for (let i = 2; i <= n; i++) fact *= i;
|
|
21
|
+
return fact;
|
|
22
|
+
</eval>
|
|
23
|
+
<output><variable name="result" /></output>
|
|
24
|
+
</subroutine>
|
|
25
|
+
</dirac>
|
package/lib/string.di
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
<!-- String Operations Library for DIRAC Standard Library -->
|
|
2
|
+
<dirac>
|
|
3
|
+
|
|
4
|
+
<!-- SUBSTRING: Extract portion of string -->
|
|
5
|
+
<subroutine name="SUBSTRING" param-str="string" param-start="number" param-length="number (optional)">
|
|
6
|
+
<eval name="result">
|
|
7
|
+
const s = String(str);
|
|
8
|
+
const startPos = Number(start);
|
|
9
|
+
if (typeof length !== 'undefined' && length !== null && length !== '') {
|
|
10
|
+
return s.substring(startPos, startPos + Number(length));
|
|
11
|
+
} else {
|
|
12
|
+
return s.substring(startPos);
|
|
13
|
+
}
|
|
14
|
+
</eval>
|
|
15
|
+
<output><variable name="result" /></output>
|
|
16
|
+
</subroutine>
|
|
17
|
+
|
|
18
|
+
<!-- REPLACE: Replace text in string -->
|
|
19
|
+
<subroutine name="REPLACE" param-str="string" param-search="string" param-replace="string" param-regex="boolean (optional)">
|
|
20
|
+
<eval name="result">
|
|
21
|
+
const s = String(str);
|
|
22
|
+
const searchStr = String(search);
|
|
23
|
+
const replaceStr = String(replace);
|
|
24
|
+
if (regex === 'true' || regex === true) {
|
|
25
|
+
return s.replace(new RegExp(searchStr, 'g'), replaceStr);
|
|
26
|
+
} else {
|
|
27
|
+
return s.split(searchStr).join(replaceStr);
|
|
28
|
+
}
|
|
29
|
+
</eval>
|
|
30
|
+
<output><variable name="result" /></output>
|
|
31
|
+
</subroutine>
|
|
32
|
+
|
|
33
|
+
<!-- SPLIT: Split string by delimiter -->
|
|
34
|
+
<subroutine name="SPLIT" param-str="string" param-delimiter="string">
|
|
35
|
+
<eval name="result">
|
|
36
|
+
return String(str).split(String(delimiter)).join('\n');
|
|
37
|
+
</eval>
|
|
38
|
+
<output><variable name="result" /></output>
|
|
39
|
+
</subroutine>
|
|
40
|
+
|
|
41
|
+
<!-- JOIN: Join array elements (newline-separated input) -->
|
|
42
|
+
<subroutine name="JOIN" param-items="string" param-delimiter="string (optional)">
|
|
43
|
+
<eval name="result">
|
|
44
|
+
const itemStr = String(items);
|
|
45
|
+
const delim = (typeof delimiter !== 'undefined' && delimiter !== null && delimiter !== '') ? String(delimiter) : '';
|
|
46
|
+
return itemStr.split('\n').join(delim);
|
|
47
|
+
</eval>
|
|
48
|
+
<output><variable name="result" /></output>
|
|
49
|
+
</subroutine>
|
|
50
|
+
|
|
51
|
+
<!-- TRIM: Remove whitespace from both ends -->
|
|
52
|
+
<subroutine name="TRIM" param-str="string">
|
|
53
|
+
<eval name="result">
|
|
54
|
+
return String(str).trim();
|
|
55
|
+
</eval>
|
|
56
|
+
<output><variable name="result" /></output>
|
|
57
|
+
</subroutine>
|
|
58
|
+
|
|
59
|
+
<!-- UPPERCASE: Convert to uppercase -->
|
|
60
|
+
<subroutine name="UPPERCASE" param-str="string">
|
|
61
|
+
<eval name="result">
|
|
62
|
+
return String(str).toUpperCase();
|
|
63
|
+
</eval>
|
|
64
|
+
<output><variable name="result" /></output>
|
|
65
|
+
</subroutine>
|
|
66
|
+
|
|
67
|
+
<!-- LOWERCASE: Convert to lowercase -->
|
|
68
|
+
<subroutine name="LOWERCASE" param-str="string">
|
|
69
|
+
<eval name="result">
|
|
70
|
+
return String(str).toLowerCase();
|
|
71
|
+
</eval>
|
|
72
|
+
<output><variable name="result" /></output>
|
|
73
|
+
</subroutine>
|
|
74
|
+
|
|
75
|
+
<!-- INDEXOF: Find position of substring (returns -1 if not found) -->
|
|
76
|
+
<subroutine name="INDEXOF" param-str="string" param-search="string" param-start="number (optional)">
|
|
77
|
+
<eval name="result">
|
|
78
|
+
const s = String(str);
|
|
79
|
+
const searchStr = String(search);
|
|
80
|
+
if (typeof start !== 'undefined' && start !== null && start !== '') {
|
|
81
|
+
return s.indexOf(searchStr, Number(start));
|
|
82
|
+
} else {
|
|
83
|
+
return s.indexOf(searchStr);
|
|
84
|
+
}
|
|
85
|
+
</eval>
|
|
86
|
+
<output><variable name="result" /></output>
|
|
87
|
+
</subroutine>
|
|
88
|
+
|
|
89
|
+
<!-- INCLUDES: Check if string contains substring (returns true/false) -->
|
|
90
|
+
<subroutine name="INCLUDES" param-str="string" param-search="string">
|
|
91
|
+
<eval name="result">
|
|
92
|
+
return String(str).includes(String(search));
|
|
93
|
+
</eval>
|
|
94
|
+
<output><variable name="result" /></output>
|
|
95
|
+
</subroutine>
|
|
96
|
+
|
|
97
|
+
<!-- LENGTH: Get string length -->
|
|
98
|
+
<subroutine name="LENGTH" param-str="string">
|
|
99
|
+
<eval name="result">
|
|
100
|
+
return String(str).length;
|
|
101
|
+
</eval>
|
|
102
|
+
<output><variable name="result" /></output>
|
|
103
|
+
</subroutine>
|
|
104
|
+
|
|
105
|
+
<!-- CONCAT: Concatenate strings -->
|
|
106
|
+
<subroutine name="CONCAT" param-str1="string" param-str2="string">
|
|
107
|
+
<eval name="result">
|
|
108
|
+
return String(str1) + String(str2);
|
|
109
|
+
</eval>
|
|
110
|
+
<output><variable name="result" /></output>
|
|
111
|
+
</subroutine>
|
|
112
|
+
|
|
113
|
+
</dirac>
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dirac-stdlib",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Standard library for DIRAC - Math and String operations",
|
|
5
|
+
"main": "lib/index.di",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node scripts/test.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"lib/"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"dirac",
|
|
15
|
+
"stdlib",
|
|
16
|
+
"math",
|
|
17
|
+
"string"
|
|
18
|
+
],
|
|
19
|
+
"author": "Zhi Wang",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/wangzhi63/dirac-stdlib.git"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/wangzhi63/dirac-stdlib/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/wangzhi63/dirac-stdlib#readme",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"dirac-lang": "^0.1.26"
|
|
31
|
+
}
|
|
32
|
+
}
|