mohitkhare
v1.0.0
Published
Developer utilities for text processing, token estimation, and data transformation by Mohit Khare
Maintainers
Readme
mohitkhare
Developer utilities for text processing, token estimation, and data transformation by Mohit Khare.
Zero-dependency collection of helpers for everyday JavaScript and Node.js development. Includes text processing, LLM token estimation, async control flow, and object manipulation.
Installation
npm install mohitkhareUsage
Text Processing
const mk = require("mohitkhare");
mk.slugify("Hello World! This is a Test");
// => "hello-world-this-is-a-test"
mk.unslugify("hello-world");
// => "Hello World"
mk.truncate("A very long paragraph that needs trimming...", 30);
// => "A very long paragraph that..."
mk.stripHtml("<p>Hello <b>world</b></p>");
// => "Hello world"
mk.wordCount("The quick brown fox jumps"); // 5
mk.readingTime(longArticle); // 7 (minutes)Token Estimation (LLM)
mk.estimateTokens("Hello, how are you doing today?");
// => 8 (character-based estimate)
mk.estimateTokens("Hello, how are you doing today?", "words");
// => 9 (word-based estimate)
mk.estimateCost(1000, 500);
// => 0.0105 (USD at default pricing)
mk.estimateCost(1000, 500, { input: 0.15, output: 0.60 });
// => 0.00045 (USD at budget pricing)Async Utilities
// Retry with exponential backoff
const data = await mk.retry(
() => fetch(url).then(r => r.json()),
{ retries: 3, delay: 1000, factor: 2 }
);
// Sleep
await mk.sleep(2000);
// Batch process with concurrency control
const results = await mk.batchProcess(
urls,
url => fetch(url).then(r => r.json()),
5 // max 5 concurrent requests
);Data Transformation
// Flatten nested objects
mk.flatten({ a: { b: 1, c: { d: 2 } } });
// => { "a.b": 1, "a.c.d": 2 }
// Unflatten back
mk.unflatten({ "a.b": 1, "a.c.d": 2 });
// => { a: { b: 1, c: { d: 2 } } }
// Pick / omit keys
mk.pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // { a: 1, c: 3 }
mk.omit({ a: 1, b: 2, c: 3 }, ["b"]); // { a: 1, c: 3 }
// Group by key
mk.groupBy(
[{ type: "a", v: 1 }, { type: "b", v: 2 }, { type: "a", v: 3 }],
"type"
);
// => { a: [{...}, {...}], b: [{...}] }
// De-duplicate
mk.unique([1, 2, 2, 3]); // [1, 2, 3]
mk.unique(items, item => item.id); // by key function
// Deep clone
const copy = mk.deepClone(complexObject);API Reference
Text
slugify(text)- Convert to URL-safe slug.unslugify(slug)- Convert slug back to title case.truncate(text, maxLength?, suffix?)- Truncate at word boundary.stripHtml(html)- Remove HTML tags.wordCount(text)- Count words.readingTime(text, wpm?)- Estimate reading time in minutes.
Token Estimation
estimateTokens(text, method?)- Estimate LLM token count.estimateCost(inputTokens, outputTokens, pricing?)- Estimate API call cost in USD.
Async
retry(fn, options?)- Retry with exponential backoff.sleep(ms)- Wait for N milliseconds.batchProcess(items, fn, batchSize?)- Process array items with concurrency control.
Data
flatten(obj)- Flatten nested object to dot-notation keys.unflatten(obj)- Restore nested structure from dot-notation.deepClone(obj)- Deep copy any value.pick(obj, keys)- Keep only specified keys.omit(obj, keys)- Remove specified keys.groupBy(arr, key)- Group array items by a key.unique(arr, keyFn?)- Remove duplicates.
TypeScript
Type definitions are included.
import { slugify, retry, RetryOptions } from "mohitkhare";Links
License
MIT. See LICENSE for details.
