@theenix/token-zap 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 +21 -0
- package/README.md +93 -0
- package/index.js +18 -0
- package/package.json +39 -0
- package/utils/removeArticles.js +15 -0
- package/utils/trimExtraSpaces.js +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Theekshana Nirmal
|
|
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,93 @@
|
|
|
1
|
+
# TokenZap
|
|
2
|
+
<div>
|
|
3
|
+
<img src="assets/logo.webp" alt="TokenZap Logo" width="800">
|
|
4
|
+
</div>
|
|
5
|
+
|
|
6
|
+
TokenZap is a lightweight utility designed to optimize text payloads before they are sent to Large Language Models (LLMs). By systematically identifying and removing redundant spacing, unnecessary characters, and structural filler, the tool helps developers reduce API token consumption and lower operational costs.
|
|
7
|
+
|
|
8
|
+
Because LLM tokenizers process text differently than humans, hidden characters like consecutive spaces, trailing lines, and specific structural markers inflate your token count without adding any semantic value. TokenZap strips away this hidden overhead, enabling you to fit more actual content into the model's context window.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
You can install TokenZap directly from the npm registry using the following command:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @thee-nix/token-zap
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
### Basic Guide
|
|
20
|
+
|
|
21
|
+
TokenZap exports a single function called `tokenZap` that accepts a text string and an options object. Here is a simple example:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { tokenZap } from "@thee-nix/token-zap";
|
|
25
|
+
|
|
26
|
+
const text = "This is a sample text with extra spaces.";
|
|
27
|
+
const cleaned = tokenZap(text);
|
|
28
|
+
|
|
29
|
+
console.log(cleaned);
|
|
30
|
+
// Output: "This is a sample text with extra spaces."
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Options
|
|
34
|
+
|
|
35
|
+
The `tokenZap` function accepts the following options:
|
|
36
|
+
|
|
37
|
+
- **removeArticles** (boolean, default: `false`) - When set to `true`, removes articles like "a", "an", and "the" from the text. This can further reduce token count but may affect readability.
|
|
38
|
+
|
|
39
|
+
- **trimExtraSpaces** (boolean, default: `true`) - When set to `true`, removes extra spaces and collapses multiple spaces into single spaces. This option is enabled by default.
|
|
40
|
+
|
|
41
|
+
### Examples
|
|
42
|
+
|
|
43
|
+
#### Remove Extra Spaces Only
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import { tokenZap } from "@thee-nix/token-zap";
|
|
47
|
+
|
|
48
|
+
const text = "This is a line with extra spaces.";
|
|
49
|
+
const result = tokenZap(text);
|
|
50
|
+
|
|
51
|
+
console.log(result);
|
|
52
|
+
// Output: "This is a line with extra spaces."
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Remove Articles and Extra Spaces
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { tokenZap } from "@thee-nix/token-zap";
|
|
59
|
+
|
|
60
|
+
const text = "The quick brown fox jumps over the lazy dog.";
|
|
61
|
+
const result = tokenZap(text, { removeArticles: true });
|
|
62
|
+
|
|
63
|
+
console.log(result);
|
|
64
|
+
// Output: "quick brown fox jumps over lazy dog."
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### Disable Trimming Extra Spaces
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
import { tokenZap } from "@thee-nix/token-zap";
|
|
71
|
+
|
|
72
|
+
const text = "This is a sample.";
|
|
73
|
+
const result = tokenZap(text, { trimExtraSpaces: false, removeArticles: true });
|
|
74
|
+
|
|
75
|
+
console.log(result);
|
|
76
|
+
// Output: "This is sample."
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Future Plans
|
|
80
|
+
|
|
81
|
+
TokenZap is actively being developed. Here are some features planned for future releases:
|
|
82
|
+
|
|
83
|
+
- Support for removing common filler words and stop words to further reduce token count.
|
|
84
|
+
- Language-specific optimization for different languages beyond English.
|
|
85
|
+
- Statistics and reporting features to show how many tokens were saved.
|
|
86
|
+
- Customizable word removal lists for domain-specific text optimization.
|
|
87
|
+
- Performance improvements for processing large text documents.
|
|
88
|
+
- Integration with popular LLM libraries for seamless prompt optimization.
|
|
89
|
+
|
|
90
|
+
## Repository Link
|
|
91
|
+
|
|
92
|
+
The source code is available on GitHub:
|
|
93
|
+
[https://github.com/theekshana-nirmal/token-zap](https://www.google.com/search?q=https://github.com/theekshana-nirmal/token-zap)
|
package/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { removeArticles as rmArticles } from "./utils/removeArticles.js";
|
|
2
|
+
import { trimExtraSpaces as trimSpaces } from "./utils/trimExtraSpaces.js";
|
|
3
|
+
|
|
4
|
+
export function tokenZap(prompt, options = {}) {
|
|
5
|
+
const { removeArticles = false, trimExtraSpaces = true } = options;
|
|
6
|
+
|
|
7
|
+
// Remove articles if the option is enabled
|
|
8
|
+
if (removeArticles) {
|
|
9
|
+
prompt = rmArticles(prompt);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Trim extra spaces by default
|
|
13
|
+
if (trimExtraSpaces) {
|
|
14
|
+
prompt = trimSpaces(prompt);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return prompt;
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theenix/token-zap",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shrink your LLM prompts instantly. TokenZap removes wasteful spacing and unnecessary characters to keep your API payloads lean and token-efficient.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test/test.js"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "github",
|
|
12
|
+
"url": "git+https://github.com/theekshana-nirmal/token-zap.git#main"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/theekshana-nirmal/token-zap#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/theekshana-nirmal/token-zap/issues"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./index.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"index.js",
|
|
23
|
+
"README.md",
|
|
24
|
+
"utils/",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"keywords": [
|
|
28
|
+
"llm",
|
|
29
|
+
"prompt-engineering",
|
|
30
|
+
"token-reduction",
|
|
31
|
+
"text-compression",
|
|
32
|
+
"openai",
|
|
33
|
+
"minify",
|
|
34
|
+
"prompt-optimization",
|
|
35
|
+
"tokenizer"
|
|
36
|
+
],
|
|
37
|
+
"author": "Theekshana Nirmal <131228387+theekshana-nirmal@users.noreply.github.com>",
|
|
38
|
+
"license": "MIT"
|
|
39
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Remove articles from the prompt to reduce token count.
|
|
3
|
+
This function uses a regular expression to identify and remove the articles "a", "an", and "the" from the input string.
|
|
4
|
+
The regex is case-insensitive and ensures that only whole words are removed, preventing partial matches.
|
|
5
|
+
|
|
6
|
+
@param {string} prompt - The input string from which to remove articles.
|
|
7
|
+
@returns {string} - The cleaned string with articles removed.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export function removeArticles(prompt) {
|
|
11
|
+
const regex = new RegExp("\\b(a|an|the)\\b", "gi");
|
|
12
|
+
let cleanText = prompt.replace(regex, "");
|
|
13
|
+
|
|
14
|
+
return cleanText;
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trims extra spaces and new line characters from the input string, and also removes leading and trailing spaces.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} prompt - The input string from which to trim extra spaces and new line characters.
|
|
5
|
+
* @returns {string} - The cleaned string with extra spaces, new line characters removed, and leading/trailing spaces trimmed.
|
|
6
|
+
*/
|
|
7
|
+
export function trimExtraSpaces(prompt) {
|
|
8
|
+
const cleanPrompt = prompt.replace(/\s+/g, " ").trim();
|
|
9
|
+
return cleanPrompt;
|
|
10
|
+
}
|