rantjs 1.0.9 → 2.0.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 +15 -0
- package/README.md +103 -117
- package/dist/cli.js +711 -0
- package/dist/index.cjs +762 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +754 -0
- package/dist/rant.js +679 -0
- package/dist/rant.min.js +6 -0
- package/package.json +56 -73
- package/.idea/compiler.xml +0 -22
- package/.idea/copyright/profiles_settings.xml +0 -3
- package/.idea/misc.xml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/rantnpm.iml +0 -9
- package/.idea/workspace.xml +0 -441
- package/.npmignore +0 -1
- package/index.js +0 -83
- package/index.min.js +0 -945
- package/source/arrayMethods.js +0 -162
- package/source/core/braceParser.js +0 -60
- package/source/core/capitalize.js +0 -19
- package/source/core/en_US.js +0 -317
- package/source/core/getCase.js +0 -20
- package/source/core/lexer.js +0 -24
- package/source/core/randomString.js +0 -11
- package/source/core/replaceToken.js +0 -70
- package/source/core/sample_dic.js +0 -11
- package/source/dic/custom.js +0 -21
- package/source/dic/en_US.js +0 -322
- package/source/dic/tokens.js +0 -3
- package/source/extendFunction.js +0 -188
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2026 Sven Anders Robbestad
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,158 +1,144 @@
|
|
|
1
1
|
# Rantjs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Procedural text for JavaScript. Write a pattern, get a sentence.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Inspired by [Rant](https://github.com/TheBerkin/rant3). This is the Rantjs dialect (`<noun>`, `[rep:3]{...}`), not a Rant 4 VM.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```js
|
|
8
|
+
import { rant } from "rantjs";
|
|
8
9
|
|
|
10
|
+
const sentence = rant(
|
|
11
|
+
"<firstname male> likes to <verb-transitive> <noun.plural> with <pron poss male> pet <noun-animal> on <timenoun dayofweek plural>.",
|
|
12
|
+
);
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Click [here][4] for a writeup on my blog
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
## Usage
|
|
18
|
-
|
|
19
|
-
# When using the npm module
|
|
20
|
-
|
|
21
|
-
var rant = require("rantjs");
|
|
22
|
-
var sentence=rant('<firstname male> likes to <verb-transitive> <noun.plural> with <pron poss male> pet <noun-animal> on <timenoun dayofweek plural>.');
|
|
23
|
-
|
|
24
|
-
console.log(sentence); // 'Sean likes to chop parrots with his pet cat on Saturdays.'
|
|
25
|
-
|
|
26
|
-
For implementation details, please visit the [npmjs page][3]
|
|
27
|
-
|
|
28
|
-
# When using the CDN version at https://cdnjs.com/libraries/rantjs
|
|
29
|
-
|
|
30
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/rantjs/1.0.6/rant.min.js">
|
|
31
|
-
<script type="text/javascript">
|
|
32
|
-
var sentence=rant('<firstname male> likes to <verb-transitive> <noun.plural> with <pron poss male> pet <noun-animal> on <timenoun dayofweek plural>.');
|
|
33
|
-
console.log(sentence); // 'Sean likes to chop parrots with his pet cat on Saturdays.'
|
|
34
|
-
document.querySelector('#app').textContent=sentence;
|
|
35
|
-
</script>
|
|
36
|
-
|
|
37
|
-
## Install
|
|
14
|
+
console.log(sentence);
|
|
15
|
+
// 'Sean likes to chop parrots with his pet cat on Saturdays.'
|
|
16
|
+
```
|
|
38
17
|
|
|
39
18
|
```bash
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
gulp serve
|
|
19
|
+
npm install rantjs
|
|
20
|
+
npx rantjs --seed 42 '<firstname male> found [a] <noun-animal>.'
|
|
43
21
|
```
|
|
44
|
-
Then open http://localhost:8000
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
## Development Plan
|
|
48
|
-
|
|
49
|
-
Easy way to choose alternative dictionaries
|
|
50
|
-
|
|
51
|
-
Indefinite article (a/an) automation
|
|
52
|
-
|
|
53
|
-
Overwriting (targets)
|
|
54
22
|
|
|
55
|
-
|
|
23
|
+
## Patterns
|
|
56
24
|
|
|
57
|
-
|
|
25
|
+
**Queries** pull a random dictionary entry. Filters and inflections can be separated with a space, dash, or dot:
|
|
58
26
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
27
|
+
```
|
|
28
|
+
<firstname male>
|
|
29
|
+
<noun-animal plural>
|
|
30
|
+
<verb.ed>
|
|
31
|
+
<pron poss male>
|
|
32
|
+
<yn yes>
|
|
33
|
+
```
|
|
65
34
|
|
|
66
|
-
|
|
35
|
+
**Blocks** choose an alternative. A block with no repeater runs once. `|` splits options. Nested braces work.
|
|
67
36
|
|
|
68
|
-
|
|
37
|
+
```
|
|
38
|
+
{heads|tails}
|
|
39
|
+
{Example text}
|
|
40
|
+
[rep:3][sep:\s]{click|clack}
|
|
41
|
+
```
|
|
69
42
|
|
|
70
|
-
|
|
43
|
+
**Tags**
|
|
71
44
|
|
|
72
|
-
|
|
45
|
+
| Tag | Effect |
|
|
46
|
+
| --- | --- |
|
|
47
|
+
| `[case:none\|default\|first\|word\|title\|upper\|lower\|sentence]` | Casing for the finished string |
|
|
48
|
+
| `[rep:n]` | Repeat the next block `n` times |
|
|
49
|
+
| `[sep:\s\|\n\|literal]` | Join those repetitions |
|
|
50
|
+
| `[a]` | Insert *a* or *an* before the next word |
|
|
51
|
+
| `[if:name]{then}{else}` | Branch on whether carrier `name` is set |
|
|
73
52
|
|
|
74
|
-
|
|
53
|
+
**Carriers** remember a result so a character stays the same person:
|
|
75
54
|
|
|
76
|
-
|
|
55
|
+
```
|
|
56
|
+
<firstname male :: hero> saw <::hero> in the <place>.
|
|
57
|
+
```
|
|
77
58
|
|
|
78
|
-
|
|
79
|
-
Branches : 67.65% ( 46/68 )
|
|
80
|
-
Functions : 100% ( 18/18 )
|
|
81
|
-
Lines : 97.24% ( 458/471 )
|
|
59
|
+
**Escapes:** `\C` is a random A–Z letter.
|
|
82
60
|
|
|
83
|
-
|
|
61
|
+
NSFW entries are omitted unless the query asks (`<noun nsfw>`) or you pass `{ nsfw: true }`.
|
|
84
62
|
|
|
85
|
-
##
|
|
63
|
+
## API
|
|
86
64
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
var yourCustomDic = {}
|
|
90
|
-
...
|
|
91
|
-
rant('your text', yourCustomDic);
|
|
92
|
-
|
|
93
|
-
A sample dictionary file can be found in the ./src folder. Also take a look at the built-in english dicionary in the same folder for reference.
|
|
65
|
+
```ts
|
|
66
|
+
import { rant, createRant, enUS } from "rantjs";
|
|
94
67
|
|
|
95
|
-
|
|
68
|
+
rant(pattern);
|
|
69
|
+
rant(pattern, { seed: 42 });
|
|
70
|
+
rant(pattern, { seed: "chapter-1", nsfw: false, dictionary: enUS });
|
|
71
|
+
rant(pattern, customDictionary); // 1.x-compatible second argument
|
|
96
72
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
0.9.4 - Added Death (<verb death>)
|
|
102
|
-
|
|
103
|
-
0.9.3 - Added climb (<verb climb up|down>)
|
|
73
|
+
const r = createRant({ seed: 42 });
|
|
74
|
+
r.run(pattern);
|
|
75
|
+
r.run(pattern, { seed: 99 });
|
|
76
|
+
```
|
|
104
77
|
|
|
105
|
-
|
|
78
|
+
Custom dictionary shape:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const pets = {
|
|
82
|
+
tables: {
|
|
83
|
+
pet: {
|
|
84
|
+
name: "pet",
|
|
85
|
+
subs: ["default", "plural"],
|
|
86
|
+
entries: [{ forms: ["capybara", "capybaras"], classes: ["animal"] }],
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
```
|
|
106
91
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
Removed several gulp tasks (concat, minify)
|
|
110
|
-
|
|
111
|
-
Refactored the tests
|
|
112
|
-
|
|
113
|
-
Renamed the app internally
|
|
114
|
-
|
|
115
|
-
New usage syntax (not compatible with 0.8.x)
|
|
116
|
-
|
|
117
|
-
You can now call Rantjs directly from require:
|
|
118
|
-
|
|
119
|
-
var sentence=require("rantjs")("<firstname male> likes to <verb-transitive>
|
|
120
|
-
<noun.plural> with <pron poss male> pet <noun-animal> on <timenoun dayofweek plural>.");
|
|
121
|
-
|
|
122
|
-
Added randomization:
|
|
123
|
-
|
|
124
|
-
require("rantjs")("A random string: [rep:8][sep:\N]{\C}");
|
|
125
|
-
//A random string: XUACJGOGN
|
|
92
|
+
## Browser
|
|
126
93
|
|
|
127
|
-
|
|
94
|
+
ESM:
|
|
128
95
|
|
|
129
|
-
|
|
96
|
+
```html
|
|
97
|
+
<script type="module">
|
|
98
|
+
import { rant } from "https://cdn.jsdelivr.net/npm/rantjs@2.0.0/+esm";
|
|
99
|
+
document.body.textContent = rant("<greet> <firstname>.");
|
|
100
|
+
</script>
|
|
101
|
+
```
|
|
130
102
|
|
|
131
|
-
|
|
103
|
+
Script tag (cdnjs / jsDelivr):
|
|
132
104
|
|
|
133
|
-
|
|
105
|
+
```html
|
|
106
|
+
<script src="https://cdn.jsdelivr.net/npm/rantjs@2.0.0/dist/rant.min.js"></script>
|
|
107
|
+
<script>
|
|
108
|
+
document.body.textContent = rant("<greet> <firstname>.");
|
|
109
|
+
</script>
|
|
110
|
+
```
|
|
134
111
|
|
|
135
|
-
|
|
136
|
-
// I Like Bulls but not Horses
|
|
137
|
-
// I Like Poodles but not Owls
|
|
112
|
+
The IIFE build assigns `rant` on `globalThis`. The Node/ESM build does not touch `window`.
|
|
138
113
|
|
|
139
|
-
|
|
114
|
+
## CLI
|
|
140
115
|
|
|
141
|
-
|
|
116
|
+
```bash
|
|
117
|
+
rantjs '<pattern>'
|
|
118
|
+
rantjs --seed 7 -f story.rant
|
|
119
|
+
rantjs --nsfw '<adj nsfw> <noun>'
|
|
120
|
+
```
|
|
142
121
|
|
|
143
|
-
|
|
122
|
+
## Development
|
|
144
123
|
|
|
145
|
-
|
|
124
|
+
```bash
|
|
125
|
+
npm install
|
|
126
|
+
npm test
|
|
127
|
+
npm run demo # playground at http://localhost:5173
|
|
128
|
+
```
|
|
146
129
|
|
|
147
|
-
|
|
130
|
+
Dictionary sources live in `vocab/` (Rantionary plus a few custom tables). `npm run build:dict` compiles them into `src/dictionaries/en-US.ts`.
|
|
148
131
|
|
|
149
|
-
|
|
132
|
+
The playground is `npm run demo`. After GitHub Pages is enabled it will live at `https://robbestad.github.io/Rantjs/`.
|
|
150
133
|
|
|
151
|
-
|
|
134
|
+
## Migrating from 1.x
|
|
152
135
|
|
|
136
|
+
- Node 18+.
|
|
137
|
+
- `import { rant } from "rantjs"` (or `require("rantjs").rant`).
|
|
138
|
+
- `window.rant` is only set by the IIFE browser file, not by `require("rantjs")`.
|
|
139
|
+
- `String.prototype` is no longer patched.
|
|
140
|
+
- Some dictionary words changed; the query syntax did not.
|
|
153
141
|
|
|
142
|
+
## License
|
|
154
143
|
|
|
155
|
-
|
|
156
|
-
[3]: https://www.npmjs.com/package/rantjs
|
|
157
|
-
[4]: http://www.robbestad.com/blog/procedurally-generated-text-with-rantjs
|
|
158
|
-
[5]: http://rantjs.surge.sh/
|
|
144
|
+
ISC
|