@spintax/core 0.1.0 → 0.1.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.
- package/CHANGELOG.md +47 -35
- package/LICENSE +21 -21
- package/README.md +144 -122
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +73 -66
package/CHANGELOG.md
CHANGED
|
@@ -1,35 +1,47 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to `@spintax/core` are documented here. This project adheres to
|
|
4
|
-
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
5
|
-
|
|
6
|
-
## 0.1.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
-
|
|
34
|
-
|
|
35
|
-
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@spintax/core` are documented here. This project adheres to
|
|
4
|
+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
5
|
+
|
|
6
|
+
## 0.1.1 — 2026-07-08
|
|
7
|
+
|
|
8
|
+
Docs + metadata. First release published from CI with **provenance** (npm Trusted Publishing).
|
|
9
|
+
No engine or API changes.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- npm `keywords` + `description` reworked for discoverability — spintax / text-spinning /
|
|
14
|
+
LLM-authoring workflow; dropped the obscure `gtw` tag.
|
|
15
|
+
- README: badges, npm + [301.st](https://301.st) links, and LLM-pairing positioning (draft a
|
|
16
|
+
template with a model once, generate unlimited deterministic variations on-device).
|
|
17
|
+
|
|
18
|
+
## 0.1.0 — 2026-07-07
|
|
19
|
+
|
|
20
|
+
First public release. Feature-complete engine; the TS suite passes the full **deterministic**
|
|
21
|
+
golden corpus that encodes the Spintax WordPress plugin's behavior contract (validation verdicts,
|
|
22
|
+
plural buckets, conditional truthiness, `#set` collapse, post-process output, enum/perm selection).
|
|
23
|
+
Cross-engine execution of that corpus by the PHP plugin is the remaining verification gate (see Notes).
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
|
|
27
|
+
- `parse(input)` — opaque, versioned AST for reuse.
|
|
28
|
+
- `render(input, opts)` — seeded, lenient rendering: enumerations, permutations (config +
|
|
29
|
+
per-element separators), variables (recursive), `#set` (collapse-once), conditionals, plurals
|
|
30
|
+
(locale buckets), `#include` (host-injected resolver, scope isolation, circular guard), and the
|
|
31
|
+
12-step cosmetic post-process (URL/email/domain/abbreviation shielding, spacing, capitalization).
|
|
32
|
+
Post-process defaults on; `postProcess: false` yields the raw pick.
|
|
33
|
+
- `validate(input, opts)` — diagnostics with a parity-gated verdict (valid ⇔ no `error`);
|
|
34
|
+
`knownIncludes` / `knownVariables` options.
|
|
35
|
+
- `extract(input)` — `{ refs, sets, includes }`.
|
|
36
|
+
- `analyze(input, opts)` — extract + validate + a best-effort construct census.
|
|
37
|
+
- `neutralize(value)` — text-safe shielding of data-derived input, with a mandatory safety-restore.
|
|
38
|
+
|
|
39
|
+
### Notes
|
|
40
|
+
|
|
41
|
+
- `render()` is lenient on depth: a circular / too-deep `#include` resolves to `''` (there is
|
|
42
|
+
**no** `MaxDepthExceededError`), matching the plugin. It throws only on a resolver that itself
|
|
43
|
+
throws (`IncludeResolverError`) or a foreign `Ast` (`AstVersionError`) — revised after the
|
|
44
|
+
reference-Worker dogfood surfaced the phantom error export.
|
|
45
|
+
- Zero runtime dependencies; ESM-first with dual CJS. Node 18+, Cloudflare Workers, browser.
|
|
46
|
+
- Cross-engine RNG-sequence parity with the PHP plugin is a non-goal; only deterministic behavior
|
|
47
|
+
is parity-gated. The PHP-side corpus runner is the remaining cross-engine verification gate.
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 301st (https://301.st)
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 301st (https://301.st)
|
|
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
CHANGED
|
@@ -1,122 +1,144 @@
|
|
|
1
|
-
# @spintax/core
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
### `
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
1
|
+
# @spintax/core
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@spintax/core)
|
|
4
|
+
[](https://www.npmjs.com/package/@spintax/core)
|
|
5
|
+
[](https://github.com/investblog/spintax-js/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/@spintax/core)
|
|
7
|
+
[](https://github.com/investblog/spintax-js/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
Framework-agnostic **[spintax](https://spintax.net) engine** for JavaScript / TypeScript —
|
|
10
|
+
parse, render, validate, extract, analyze, and neutralize spintax templates.
|
|
11
|
+
|
|
12
|
+
> **Pairs naturally with LLMs.** Have a model draft a spintax template once, then generate
|
|
13
|
+
> unlimited variations on-device — deterministic, free, and offline, with no per-generation
|
|
14
|
+
> API calls. The LLM handles creativity; the engine handles scale.
|
|
15
|
+
|
|
16
|
+
- **Zero runtime dependencies.** Runs unchanged on Cloudflare Workers, Node 18+, and in the browser.
|
|
17
|
+
- **ESM-first, dual CJS.** Ships `.d.ts` types for both.
|
|
18
|
+
- **Built to the [Spintax WordPress plugin](https://wordpress.org/plugins/spintax/)'s behavior
|
|
19
|
+
contract** via a shared golden corpus — an *independent* TypeScript implementation, not a
|
|
20
|
+
line-by-line port. The TS suite passes the full deterministic corpus; cross-engine execution
|
|
21
|
+
of that corpus by the PHP plugin is pending.
|
|
22
|
+
- **MIT** licensed.
|
|
23
|
+
|
|
24
|
+
> **Status: `0.1.0`** — first public release. Feature-complete; passes the full deterministic
|
|
25
|
+
> golden corpus (encoding the plugin's contract, TS side); the §9.2 API is proven by a reference
|
|
26
|
+
> Cloudflare Worker (`examples/worker`).
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @spintax/core
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick start
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { render, validate, extract } from '@spintax/core';
|
|
38
|
+
|
|
39
|
+
render('{Hello|Hi|Hey} %name%!', { context: { name: 'Ada' }, seed: 42 });
|
|
40
|
+
// → "Hi Ada!" (deterministic for a given seed; post-processed by default)
|
|
41
|
+
|
|
42
|
+
validate('{a|b'); // → [{ severity: 'error', code: 'bracket.unclosed', … }]
|
|
43
|
+
extract('%title% {?promo?Sale}'); // → { refs: ['title', 'promo'], sets: [], includes: [] }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Spintax syntax
|
|
47
|
+
|
|
48
|
+
| Construct | Example | Meaning |
|
|
49
|
+
| --- | --- | --- |
|
|
50
|
+
| Enumeration | `{a\|b\|c}` | pick one (nestable: `{a\|{b\|c}}`) |
|
|
51
|
+
| Permutation | `[a\|b\|c]` | pick N, shuffle, join — `[<minsize=1;maxsize=2;sep=", ">a\|b\|c]` |
|
|
52
|
+
| Variable | `%var%` | substitute a context value |
|
|
53
|
+
| Local set | `#set %v% = value` | define a variable (one line) |
|
|
54
|
+
| Conditional | `{?VAR?then\|else}` | `then` if `VAR` is truthy, else `else` |
|
|
55
|
+
| Plural | `{plural %n%: one\|few\|many}` | grammatical agreement by locale |
|
|
56
|
+
| Include | `#include "slug-or-id"` | embed another template (host-resolved) |
|
|
57
|
+
| Comment | `/# … #/` | stripped before rendering |
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
All functions accept a `string` **or** a parsed `Ast` (from `parse`) as their first argument.
|
|
62
|
+
|
|
63
|
+
### `render(input, options?): string`
|
|
64
|
+
|
|
65
|
+
Renders a template to a single string. **Lenient** — never throws on malformed markup (a bad
|
|
66
|
+
block is emitted verbatim with fullwidth braces `{…}`). Cosmetic post-processing (spacing,
|
|
67
|
+
capitalization, URL/email shielding) is **on by default**.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
render(input, {
|
|
71
|
+
context?: Record<string, string>, // variable map
|
|
72
|
+
seed?: number | string, // deterministic RNG; omit ⇒ random
|
|
73
|
+
locale?: string, // plural buckets, e.g. 'ru' (3-form)
|
|
74
|
+
includeResolver?: (ref: string) => string | null, // host-injected, synchronous
|
|
75
|
+
postProcess?: boolean, // default true; false ⇒ raw pick
|
|
76
|
+
maxDepth?: number, // #include / nesting guard (default 20)
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`#include` is resolved only when you pass an `includeResolver`; child templates inherit the
|
|
81
|
+
runtime context but **not** the parent's `#set` locals. Circular / too-deep includes resolve to
|
|
82
|
+
`''` (lenient).
|
|
83
|
+
|
|
84
|
+
### `validate(input, options?): Diagnostic[]`
|
|
85
|
+
|
|
86
|
+
Returns diagnostics. A template is **valid** ⇔ no diagnostic has `severity: 'error'`. An
|
|
87
|
+
unresolved `%var%` is a `warning`, not an error.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
validate(input, {
|
|
91
|
+
locale?: string, // locale-aware plural-arity verdicts
|
|
92
|
+
knownIncludes?: readonly string[], // enables "unknown #include target" errors
|
|
93
|
+
knownVariables?: readonly string[], // suppresses undefined-variable warnings
|
|
94
|
+
});
|
|
95
|
+
// Diagnostic: { severity, code, message, line, column, endLine?, endColumn?, data? }
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### `extract(input): { refs, sets, includes }`
|
|
99
|
+
|
|
100
|
+
Variable references (`%var%`, `{?…}`, plural counts), `#set` names, and `#include` targets.
|
|
101
|
+
|
|
102
|
+
### `analyze(input, options?): Analysis`
|
|
103
|
+
|
|
104
|
+
`extract` + `validate` + a best-effort `constructs` census (`{ enumeration, permutation,
|
|
105
|
+
variable, conditional, plural, set, include }`). The census counts author-visible constructs;
|
|
106
|
+
it is **not** a variant-cardinality promise.
|
|
107
|
+
|
|
108
|
+
### `parse(input): Ast`
|
|
109
|
+
|
|
110
|
+
Parses once for reuse. `Ast` is **opaque and versioned** — pass it back to the other functions,
|
|
111
|
+
do not introspect or persist it across engine versions.
|
|
112
|
+
|
|
113
|
+
### `neutralize(value): string`
|
|
114
|
+
|
|
115
|
+
Shields data-derived (untrusted) text so it can't be re-interpreted as spintax markup — use it
|
|
116
|
+
on any value you inject via `context` that isn't author-controlled. It is **text-safe**
|
|
117
|
+
(round-trips to literal glyphs in any sink), not HTML/XSS escaping.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
render('%bio%', { context: { bio: neutralize('Save {50|60}% today') }, postProcess: false });
|
|
121
|
+
// → "Save {50|60}% today" (the braces stay literal, not a random pick)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Determinism & RNG
|
|
125
|
+
|
|
126
|
+
With a `seed`, `render` is reproducible within this engine. Cross-engine RNG-sequence parity
|
|
127
|
+
with the PHP plugin is a **non-goal** — only the *deterministic* behavior (validation verdicts,
|
|
128
|
+
plural buckets, conditional truthiness, `#set` collapse, post-process output) is parity-gated.
|
|
129
|
+
|
|
130
|
+
## Links
|
|
131
|
+
|
|
132
|
+
- 📦 npm — [`@spintax/core`](https://www.npmjs.com/package/@spintax/core)
|
|
133
|
+
- 🧩 Source — [investblog/spintax-js](https://github.com/investblog/spintax-js)
|
|
134
|
+
- 🌐 Product — [spintax.net](https://spintax.net)
|
|
135
|
+
- 🏠 Maintainer — [301.st](https://301.st)
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
[MIT](https://github.com/investblog/spintax-js/blob/main/LICENSE). The Spintax WordPress plugin
|
|
140
|
+
remains GPL; MIT/Expat is GPL-compatible.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
Part of the [301.st](https://301.st) toolset. Product home: [spintax.net](https://spintax.net).
|