tulisix 1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 rezzvy
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,244 @@
1
+ # Tulisix
2
+
3
+ Transform text nodes into any format imaginable.
4
+
5
+ ## Overview
6
+
7
+ Tulisix lets you transform text nodes into specific formats based on rules you define.
8
+
9
+ - Rule-based transformation
10
+ - Supports dynamic variables
11
+ - Function-based output for advanced logic
12
+ - Automatic HTML escaping (XSS-safe by default)
13
+
14
+ ## Installation & Usage
15
+
16
+ ### Installation
17
+
18
+ #### Browser
19
+
20
+ Include via CDN:
21
+
22
+ ```html
23
+ <script src="https://cdn.jsdelivr.net/gh/rezzvy/tulisix@465b13b/dist/tulisix.min.js"></script>
24
+ ```
25
+
26
+ ```javascript
27
+ const tulisix = new Tulisix();
28
+ ```
29
+
30
+ #### Node
31
+
32
+ Install via npm:
33
+
34
+ ```bash
35
+ npm install tulisix
36
+ ```
37
+
38
+ ```javascript
39
+ import Tulisix from "tulisix";
40
+ const tulisix = new Tulisix();
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ ```html
46
+ <div id="app">Hello Reza</div>
47
+ ```
48
+
49
+ ```javascript
50
+ tulisix.addRule({
51
+ from: "Hello {name}",
52
+ to: "<strong>Hello {name}</strong>",
53
+ });
54
+
55
+ tulisix.replace("#app");
56
+ ```
57
+
58
+ ## Examples
59
+
60
+ ### Multiple Patterns
61
+
62
+ ```html
63
+ <p class="text">Hi Reza</p>
64
+ ```
65
+
66
+ ```javascript
67
+ const tulisix = new Tulisix();
68
+
69
+ tulisix.addRule({
70
+ from: ["Hi {name}", "Hello {name}"],
71
+ to: "<b>{name}</b>",
72
+ });
73
+
74
+ tulisix.replace(".text");
75
+ ```
76
+
77
+ ### Dynamic Output (Function)
78
+
79
+ ```html
80
+ <p class="price">Price: 100</p>
81
+ ```
82
+
83
+ ```javascript
84
+ const tulisix = new Tulisix();
85
+
86
+ tulisix.addRule({
87
+ from: "Price: {value}",
88
+ to: (safe, match, raw) => {
89
+ return `<span>$${Number(raw.value).toFixed(2)}</span>`;
90
+ },
91
+ });
92
+
93
+ tulisix.replace(".price");
94
+ ```
95
+
96
+ ### Case Insensitive
97
+
98
+ ```html
99
+ <p class="text">hello Reza</p>
100
+ ```
101
+
102
+ ```javascript
103
+ const tulisix = new Tulisix();
104
+
105
+ tulisix.addRule({
106
+ from: "hello {name}",
107
+ to: "<i>{name}</i>",
108
+ caseSensitive: false,
109
+ });
110
+
111
+ tulisix.replace(".text");
112
+ ```
113
+
114
+ ### Escaping Curly Braces
115
+
116
+ ```html
117
+ <p class="text">{not a variable}</p>
118
+ ```
119
+
120
+ ```javascript
121
+ const tulisix = new Tulisix();
122
+
123
+ tulisix.addRule({
124
+ from: "\\{not a variable\\}",
125
+ to: "<span>literal</span>",
126
+ });
127
+
128
+ tulisix.replace(".text");
129
+ ```
130
+
131
+ ### Multiple Rules
132
+
133
+ ```html
134
+ <p class="text">**bold** and *italic*</p>
135
+ ```
136
+
137
+ ```javascript
138
+ const tulisix = new Tulisix();
139
+
140
+ tulisix.addRule([
141
+ {
142
+ from: "**{text}**",
143
+ to: "<strong>{text}</strong>",
144
+ },
145
+ {
146
+ from: "*{text}*",
147
+ to: "<em>{text}</em>",
148
+ },
149
+ ]);
150
+
151
+ tulisix.replace(".text");
152
+ ```
153
+
154
+ ## Documentation
155
+
156
+ ### API Reference
157
+
158
+ #### `addRule(param)`
159
+
160
+ Register one or more transformation rules.
161
+
162
+ | Parameter | Type | Description |
163
+ | :-------- | :-------------------------- | :------------------------------------ |
164
+ | `param` | `Object` \| `Array<Object>` | Rule object or array of rule objects. |
165
+
166
+ ##### Rule Object Structure
167
+
168
+ | Property | Type | Required | Default | Description |
169
+ | :-------------- | :-------------------------- | :------- | :------ | :-------------------------------------- |
170
+ | `from` | `String` \| `Array<String>` | Yes | - | Pattern(s) with optional `{variables}`. |
171
+ | `to` | `String` \| `Function` | Yes | - | Replacement string or function. |
172
+ | `caseSensitive` | `Boolean` | No | `true` | Enable/disable case-sensitive matching. |
173
+
174
+ ##### `to` Function Arguments
175
+
176
+ Used when `to` is a function:
177
+
178
+ | Argument | Type | Description |
179
+ | :--------- | :------- | :---------------------------------------- |
180
+ | `safeVars` | `Object` | Escaped variables (safe for HTML output). |
181
+ | `match` | `Array` | Result from `RegExp.exec()`. |
182
+ | `rawVars` | `Object` | Original (unescaped) variable values. |
183
+
184
+ #### `replace(target)`
185
+
186
+ Apply all registered rules to the target.
187
+
188
+ | Parameter | Type | Description |
189
+ | :-------- | :------------------------------------------------------ | :------------------------------------------------- |
190
+ | `target` | `String` \| `Element` \| `NodeList` \| `Array<Element>` | Selector or collection of DOM elements to process. |
191
+
192
+ ### Limitations
193
+
194
+ #### Text nodes only
195
+
196
+ Tulisix only processes text nodes. It does not parse or transform HTML attributes or element structures.
197
+ Example: `<div title="Hello Reza">` will NOT be transformed.
198
+
199
+ #### No cross-node matching
200
+
201
+ Patterns must exist within a single text node.
202
+ Text split across multiple elements will not match.
203
+
204
+ Example:
205
+
206
+ ```html
207
+ <span>Hello</span> <span>Reza</span>
208
+ ```
209
+
210
+ Will NOT match `Hello {name}`.
211
+
212
+ #### HTML output is not sanitized
213
+
214
+ Only variables are automatically escaped.
215
+ If you return raw HTML in `to`, you are responsible for ensuring it is safe.
216
+
217
+ #### Greedy / ambiguous patterns
218
+
219
+ Patterns using `{variables}` may behave unexpectedly if the structure is ambiguous.
220
+
221
+ Example:
222
+
223
+ ```
224
+ "{a} {b}"
225
+ ```
226
+
227
+ may produce unintended matches depending on content.
228
+
229
+ #### Order matters
230
+
231
+ Rules are applied sequentially.
232
+ Earlier rules can affect later matches.
233
+
234
+ #### Not a full template engine
235
+
236
+ Tulisix is designed for lightweight text transformation, not full HTML templating or parsing.
237
+
238
+ ## Contributing
239
+
240
+ There's always room for improvement. Feel free to contribute!
241
+
242
+ ## Licensing
243
+
244
+ The project is licensed under the MIT License. Check the license file for more details.
@@ -0,0 +1 @@
1
+ var g=class{#e=[];#t;constructor(){this.#t=document.createElement("template")}addRule(e){if(!e)return;let r=Array.isArray(e)?e:[e];for(let t of r){if(typeof t!="object"||t===null)throw new TypeError("Each rule must be a valid object.");if(!t.from)throw new Error("Rule is missing the required 'from' property.");if(typeof t.from!="string"&&!Array.isArray(t.from))throw new TypeError("Rule 'from' property must be a string or an array of strings.");if(typeof t.to!="string"&&typeof t.to!="function")throw new TypeError("Rule 'to' property must be a string or a function.");let o=Array.isArray(t.from)?t.from:[t.from],i=t.caseSensitive!==!1;for(let s of o){if(typeof s!="string")throw new TypeError(`Invalid pattern in 'from': ${s}. Must be a string.`);let n=this.#n(s,i);this.#e.push({regex:n.regex,varNames:n.varNames,to:t.to})}}}#n(e,r){let t=[];e=e.replace(/\\\{/g,"__ESCAPED_OPEN__").replace(/\\\}/g,"__ESCAPED_CLOSE__");let o=[],i=0,s=/\{([a-zA-Z0-9_]+)\}/g,n;for(;n=s.exec(e);){let[c,m]=n,f=n.index,p=e.slice(i,f);o.push(p.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&")),t.push(m);let h=f+c.length===e.length;o.push(h?"([^\\s<]+)":"(.*?)"),i=f+c.length}let a=e.slice(i);o.push(a.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"));let l=o.join("");return l=l.replace(/__ESCAPED_OPEN__/g,"\\{").replace(/__ESCAPED_CLOSE__/g,"\\}"),{regex:new RegExp(l,r?"g":"gi"),varNames:t}}replace(e){if(!this.#e.length)return;let r;if(typeof e=="string")r=document.querySelectorAll(e);else if(e instanceof Element)r=[e];else if(e instanceof NodeList||Array.isArray(e))r=e;else throw new TypeError("replace() requires a valid CSS selector string, a DOM Element, or a NodeList.");if(!(!r||r.length===0))for(let t of r){let o=this.#s(t);for(let i of o){let s=[i];for(let n of this.#e){let a=[];for(let l of s)l.parentNode&&this.#r(l,n,a);s=a}}}}#s(e){let r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null,!1),t=[],o;for(;o=r.nextNode();)o.nodeValue.trim()&&t.push(o);return t}#r(e,r,t){let{regex:o,varNames:i,to:s}=r;o.lastIndex=0;let n=o.exec(e.nodeValue);if(!n||n[0].length===0){e.nodeValue.length>0&&t.push(e);return}let a=n.index,l=n[0].length,c=e.splitText(a),m=c.splitText(l);e.nodeValue.length>0&&t.push(e);let f={},p={};for(let u=0;u<i.length;u++){let d=n[u+1]??"";p[i[u]]=d,f[i[u]]=this.#o(d)}let h;typeof s=="function"?h=s(f,n,p):h=this.#i(s,p),this.#t.innerHTML=h;let y=this.#t.content.cloneNode(!0);c.parentNode.replaceChild(y,c),this.#r(m,r,t)}#i(e,r){return e.replace(/\{([a-zA-Z0-9_]+)\}/g,(t,o)=>this.#o(r[o]??""))}#o(e){return String(e).replace(/[&<>"']/g,r=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[r])}};export{g as default};
@@ -0,0 +1 @@
1
+ (()=>{var m=class{#e=[];#t;constructor(){this.#t=document.createElement("template")}addRule(e){if(!e)return;let r=Array.isArray(e)?e:[e];for(let t of r){if(typeof t!="object"||t===null)throw new TypeError("Each rule must be a valid object.");if(!t.from)throw new Error("Rule is missing the required 'from' property.");if(typeof t.from!="string"&&!Array.isArray(t.from))throw new TypeError("Rule 'from' property must be a string or an array of strings.");if(typeof t.to!="string"&&typeof t.to!="function")throw new TypeError("Rule 'to' property must be a string or a function.");let o=Array.isArray(t.from)?t.from:[t.from],i=t.caseSensitive!==!1;for(let s of o){if(typeof s!="string")throw new TypeError(`Invalid pattern in 'from': ${s}. Must be a string.`);let n=this.#n(s,i);this.#e.push({regex:n.regex,varNames:n.varNames,to:t.to})}}}#n(e,r){let t=[];e=e.replace(/\\\{/g,"__ESCAPED_OPEN__").replace(/\\\}/g,"__ESCAPED_CLOSE__");let o=[],i=0,s=/\{([a-zA-Z0-9_]+)\}/g,n;for(;n=s.exec(e);){let[c,d]=n,f=n.index,p=e.slice(i,f);o.push(p.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&")),t.push(d);let u=f+c.length===e.length;o.push(u?"([^\\s<]+)":"(.*?)"),i=f+c.length}let a=e.slice(i);o.push(a.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"));let l=o.join("");return l=l.replace(/__ESCAPED_OPEN__/g,"\\{").replace(/__ESCAPED_CLOSE__/g,"\\}"),{regex:new RegExp(l,r?"g":"gi"),varNames:t}}replace(e){if(!this.#e.length)return;let r;if(typeof e=="string")r=document.querySelectorAll(e);else if(e instanceof Element)r=[e];else if(e instanceof NodeList||Array.isArray(e))r=e;else throw new TypeError("replace() requires a valid CSS selector string, a DOM Element, or a NodeList.");if(!(!r||r.length===0))for(let t of r){let o=this.#s(t);for(let i of o){let s=[i];for(let n of this.#e){let a=[];for(let l of s)l.parentNode&&this.#r(l,n,a);s=a}}}}#s(e){let r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null,!1),t=[],o;for(;o=r.nextNode();)o.nodeValue.trim()&&t.push(o);return t}#r(e,r,t){let{regex:o,varNames:i,to:s}=r;o.lastIndex=0;let n=o.exec(e.nodeValue);if(!n||n[0].length===0){e.nodeValue.length>0&&t.push(e);return}let a=n.index,l=n[0].length,c=e.splitText(a),d=c.splitText(l);e.nodeValue.length>0&&t.push(e);let f={},p={};for(let h=0;h<i.length;h++){let g=n[h+1]??"";p[i[h]]=g,f[i[h]]=this.#o(g)}let u;typeof s=="function"?u=s(f,n,p):u=this.#i(s,p),this.#t.innerHTML=u;let y=this.#t.content.cloneNode(!0);c.parentNode.replaceChild(y,c),this.#r(d,r,t)}#i(e,r){return e.replace(/\{([a-zA-Z0-9_]+)\}/g,(t,o)=>this.#o(r[o]??""))}#o(e){return String(e).replace(/[&<>"']/g,r=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[r])}};typeof window<"u"&&(window.Tulisix=m);})();
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "tulisix",
3
+ "version": "1.0.0",
4
+ "description": "Transform text node into any format imaginable.",
5
+ "type": "module",
6
+ "main": "dist/tulisix.esm.js",
7
+ "exports": {
8
+ ".": "./dist/tulisix.esm.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/rezzvy/tulisix.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/rezzvy/tulisix/issues"
16
+ },
17
+ "homepage": "https://github.com/rezzvy/tulisix#readme",
18
+ "files": [
19
+ "dist/"
20
+ ],
21
+ "scripts": {
22
+ "build:browser": "esbuild ./src/index.browser.js --bundle --minify --platform=browser --format=iife --outfile=dist/tulisix.min.js",
23
+ "build:esm": "esbuild ./src/tulisix.js --bundle --minify --format=esm --outfile=dist/tulisix.esm.js",
24
+ "build": "npm run build:browser && npm run build:esm"
25
+ },
26
+ "keywords": [
27
+ "text-transformation",
28
+ "pattern-matcher",
29
+ "safe-html-converter",
30
+ "regex-transformer",
31
+ "content-formatter"
32
+ ],
33
+ "author": "rezzvy",
34
+ "license": "MIT",
35
+ "devDependencies": {
36
+ "esbuild": "^0.27.4"
37
+ }
38
+ }