{"id":18396717,"url":"https://github.com/igorskyflyer/npm-magic-string","last_synced_at":"2026-01-06T15:09:38.627Z","repository":{"id":251218106,"uuid":"835960624","full_name":"igorskyflyer/npm-magic-string","owner":"igorskyflyer","description":"🧵 An expressive and chainable library for advanced string manipulations. Supports appending, prepending, trimming, quoting, and path formatting with customizable whitespace handling. Makes advanced String manipulations a piece of cake. 🦥","archived":false,"fork":false,"pushed_at":"2024-08-04T19:59:46.000Z","size":72,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-07T13:40:40.024Z","etag":null,"topics":["accumulator","append","back-end","igorskyflyer","javascript","js","magic","magic-string","manipulation","prepend","string","trim","ts","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@igor.dvlpr/magic-string","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/igorskyflyer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"ko_fi":"igorskyflyer"}},"created_at":"2024-07-30T21:55:47.000Z","updated_at":"2024-08-04T19:59:48.000Z","dependencies_parsed_at":"2024-08-01T15:34:16.422Z","dependency_job_id":"7d5668c5-dba9-494c-b2f5-5520efa2aa29","html_url":"https://github.com/igorskyflyer/npm-magic-string","commit_stats":null,"previous_names":["igorskyflyer/npm-magic-string"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorskyflyer%2Fnpm-magic-string","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorskyflyer%2Fnpm-magic-string/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorskyflyer%2Fnpm-magic-string/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorskyflyer%2Fnpm-magic-string/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/igorskyflyer","download_url":"https://codeload.github.com/igorskyflyer/npm-magic-string/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245756353,"owners_count":20667144,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["accumulator","append","back-end","igorskyflyer","javascript","js","magic","magic-string","manipulation","prepend","string","trim","ts","typescript"],"created_at":"2024-11-06T02:14:38.777Z","updated_at":"2026-01-06T15:09:38.622Z","avatar_url":"https://github.com/igorskyflyer.png","language":"TypeScript","funding_links":["https://ko-fi.com/igorskyflyer"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/igorskyflyer/npm-magic-string/main/media/magic-string.png\" alt=\"Icon of MagicString\" width=\"256\" height=\"256\"\u003e\n  \u003ch1\u003eMagicString\u003c/h1\u003e\n\u003c/div\u003e\n\n\u003cbr\u003e\n\n\u003ch4 align=\"center\"\u003e\n  🧵 An expressive and chainable library for advanced string manipulations. Supports appending, prepending, trimming, quoting, and path formatting with customizable whitespace handling. Makes advanced String manipulations a piece of cake. 🦥\n\u003c/h4\u003e\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 📃 Table of Contents\n\n- [Features](#-features)\n- [Usage](#-usage)\n- [API](#-api)\n  - [StringValue](#type-stringvalue--string--string)\n  - [constructor()](#constructorinitialvalue-stringvalue)\n  - [value](#value-string)\n  - [length](#length-number)\n  - [isEmpty()](#isempty-boolean)\n  - [clear()](#clear)\n  - [append()](#appendvalue-stringvalue)\n  - [appendIf()](#appendifvalue-stringvalue-rest-stringvalue)\n  - [appendSingleQuoted()](#appendsinglequotedvalue-stringvalue)\n  - [appendQuoted()](#appendquotedvalue-stringvalue)\n  - [trimAll()](#trimall)\n  - [trim()](#trim)\n  - [trimOn()](#trimon)\n  - [trimOff()](#trimoff)\n  - [trimStart()](#trimstart)\n  - [trimEnd()](#trimend)\n  - [path()](#pathvalue-stringvalue)\n  - [prepend()](#prependvalue-stringvalue)\n  - [toLowerCase()](#tolowercase)\n  - [toUpperCase()](#touppercase)\n  - [replace()](#replacesearchvalue-string-replacevalue-string)\n  - [insert()](#insertvalue-stringvalue-index-number)\n  - [substring()](#substringstart-number-end-number)\n  - [scramble()](#scramble)\n- [Examples](#️-examples)\n- [Changelog](#-changelog)\n- [Support](#-support)\n- [License](#-license)\n- [Related](#-related)\n- [Author](#-author)\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 🤖 Features\n\n- 🪄 Chainable API - build complex string transformations step‑by‑step\n- ➕ Append \u0026 prepend - add text anywhere with ease\n- 🧹 Whitespace control - trim, normalize, or toggle trimming on/off\n- 🗨 Smart quoting - wrap values in single or double quotes automatically\n- 📂 Path‑safe formatting - auto‑quote paths with spaces\n- 🔄 Case transforms - switch to upper or lower case instantly\n- 🪢 Conditional append - add only when extra values exist\n- ✂️ Substring \u0026 insert - extract or inject text at any position\n- 🧩 Replace - swap substrings with precision\n- 🎲 Scramble - randomize characters for fun or obfuscation (powered by [ScRaMbLe](https://www.npmjs.com/package/@igorskyflyer/scramble))\n- 🧼 Array support - join string arrays into clean, single string\n- ⚡ Lightweight \u0026 dependency‑free - minimal footprint, maximum utility\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 🕵🏼 Usage\n\nInstall it by executing any of the following, depending on your preferred package manager:\n\n```bash\npnpm add @igorskyflyer/magic-string\n```\n\n```bash\nyarn add @igorskyflyer/magic-string\n```\n\n```bash\nnpm i @igorskyflyer/magic-string\n```\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 🤹🏼 API\n\nAll string-related methods use a common string type that can either be a string or a string array. `StringValue` defined as:\n\n### `type StringValue = string | string[]`\n\n---\n\n### `constructor(initialValue?: StringValue)` \n\n*Creates a new instance of MagicString.*\n\n`initialValue` - Optional, the initial value to assign to the accumulator.\n\n\u003cbr\u003e\n\n\u003e ℹ️ **NOTE**\n\u003e\n\u003e The value is assigned to the accumulator as-is, no processing is performed.\n\u003e\n\n---\n\n### `value: string`\n\n*Gets the accumulator value.*\n\n\u003cbr\u003e\n\nReturns the accumulator as a string.\n\n---\n\n### `length: number`\n\n*Gets the length of the accumulator.*\n\n\u003cbr\u003e\n\nReturns the length of the accumulator.\n\n---\n\n### `isEmpty(): boolean`\n\n*Checks if the accumulator is empty.*  \n\nReturns true if the accumulator is empty, false otherwise.\n\n---\n\n### `clear()`\n\n*Clears the accumulator.*\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `append(value: StringValue)`\n\n*Appends a value to the accumulator.*\n\n`value` - The value to append.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `appendIf(value: StringValue, ...rest: StringValue[])`\n\n*Conditionally appends values to the accumulator only if the primary value is followed by other non-empty values.*  \n\n*This method checks if the provided rest values are non-empty before appending them.*  \n\n*Supports passing multiple string values as rest parameters or a single array of strings.*  \n\n`value` - The primary value to append.  \n\n`rest` - Additional values to check and append.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `appendSingleQuoted(value: StringValue)`\n\n*Appends a single-quoted value to the accumulator.*\n\n`value` - The value to append.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `appendQuoted(value: StringValue)`\n\n*Appends a double-quoted value to the accumulator.*\n\n`value` - The value to append.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `trimAll()`\n\n*Trims all whitespace in the accumulator.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `trim()`\n\n*Trims only leading and trailing whitespace from the accumulator.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `trimOn()`\n\n*Enables trimming of values before adding them to the accumulator.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `trimOff()`\n\n*Disables trimming of values before adding them to the accumulator.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `trimStart()`\n\n*Trims only leading whitespace from the accumulator.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `trimEnd()`\n\n*Trims only trailing whitespace from the accumulator.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `path(value: StringValue)`\n\n*Appends a value representing a path to the accumulator, wrapping it in double quotes if necessary.*\n\n`value` - The path value to append.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `prepend(value: StringValue)`\n\n*Prepends a value to the accumulator.*\n\n`value` - The value to prepend.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `toLowerCase()`\n\n*Converts the accumulator to lower case.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `toUpperCase()`\n\n*Converts the accumulator to upper case.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `replace(searchValue: string, replaceValue: string)`\n\n*Replaces occurrences of a substring in the accumulator with a new substring.*  \n\n`searchValue` - The substring to search for.  \n\n`replaceValue` - The substring to replace with.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `insert(value: StringValue, index: number)`\n\n*Inserts a value at a specified index in the accumulator.*  \n\n*If the `index` is negative or zero, the value is prepended.*  \n\n*If the `index` is greater than or equal to the length of the current accumulator, the value is appended.*\n\n\u003cbr\u003e\n\n`value` - The value to insert.  \n\n`index` - The position at which to insert the value.\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `substring(start: number, end?: number)`\n\n*Extracts a substring from the accumulator between specified indices.*  \n\n`start` - The starting index of the substring.  \n\n`end` - The ending index of the substring (optional).\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n---\n\n### `scramble()`\n\n*Scrambles (rearranges characters randomly) of the current accumulator.*  \n   \n*Requires that the current accumulator value has a `length` \u003e 3.*\n\n\u003cbr\u003e\n\nReturns the current instance of `MagicString`.\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 🗒️ Examples\n\n`example.ts`\n```ts\nimport { MagicString } from '@igorskyflyer/magic-string'\n\n\nconsole.log(\n  new MagicString()\n    .append('Hello')\n    .appendSingleQuoted('world')\n    .appendQuoted('this is     a test')\n    .prepend('Start:    ')\n    .trimAll()\n    .path('/usr/local/bin')\n    .toUpperCase()\n    .path('/usr/local/my file.txt\"') // intentional stray quote\n    .toLowerCase()\n    .toUpperCase()\n    .trimOn()\n    .append('   Trimmed   ')\n    .trimOff()\n    .append('  Not trimmed')\n    .trim().value\n)\n\n// prints 'START: HELLO \\'WORLD\\' \"THIS IS A TEST\" /USR/LOCAL/BIN \"/USR/LOCAL/MY FILE.TXT\" Trimmed   Not trimmed'\n```\n\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 📝 Changelog\n\n📑 The changelog is available here, [CHANGELOG.md](https://github.com/igorskyflyer/npm-magic-string/blob/main/CHANGELOG.md).\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 🪪 License\n\nLicensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-magic-string/blob/main/LICENSE).\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 💖 Support\n\n\u003cdiv align=\"center\"\u003e\n  I work hard for every project, including this one and your support means a lot to me!\n  \u003cbr\u003e\n  Consider buying me a coffee. ☕\n  \u003cbr\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://ko-fi.com/igorskyflyer\" target=\"_blank\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/igorskyflyer/igorskyflyer/main/assets/ko-fi.png\" alt=\"Donate to igorskyflyer\" width=\"180\" height=\"46\"\u003e\u003c/a\u003e\n  \u003cbr\u003e\n  \u003cbr\u003e\n  \u003cem\u003eThank you for supporting my efforts!\u003c/em\u003e 🙏😊\n\u003c/div\u003e\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 🧬 Related\n\n[@igorskyflyer/extendable-string](https://www.npmjs.com/package/@igorskyflyer/extendable-string)\n\n\u003e _🦀 ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings. 🪀_\n\n\u003cbr\u003e\n\n[@igorskyflyer/str-is-in](https://www.npmjs.com/package/@igorskyflyer/str-is-in)\n\n\u003e _🧵 Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. 🔍_\n\n\u003cbr\u003e\n\n[@igorskyflyer/normalized-string](https://www.npmjs.com/package/@igorskyflyer/normalized-string)\n\n\u003e _💊 NormalizedString provides you with a String type with consistent line-endings, guaranteed. 📮_\n\n\u003cbr\u003e\n\n[@igorskyflyer/astro-escaped-component](https://www.npmjs.com/package/@igorskyflyer/astro-escaped-component)\n\n\u003e _🏃🏻‍♂️‍➡️ An Astro component that holds only HTML-encoded content. 📜_\n\n\u003cbr\u003e\n\n[@igorskyflyer/strip-html](https://www.npmjs.com/package/@igorskyflyer/strip-html)\n\n\u003e _🥞 Removes HTML code from the given string. Can even extract text-only from the given an HTML string. ✨_\n\n\u003cbr\u003e\n\u003cbr\u003e\n\u003cbr\u003e\n\n## 👨🏻‍💻 Author\nCreated by **Igor Dimitrijević** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figorskyflyer%2Fnpm-magic-string","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figorskyflyer%2Fnpm-magic-string","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figorskyflyer%2Fnpm-magic-string/lists"}