Supports appending, prepending, trimming, quoting, and
path formatting with customizable whitespace handling. 🦥
Makes advanced String manipulations a piece of cake.
I work hard for every project, including this one
and your support means a lot to me!
Consider buying me a coffee. ☕
Thank you for supporting my efforts! 🙏😊
@igorskyflyer
- 🗣️ expressive and chainable
- 📃 with many string manipulation methods
- 👽 increases productiveness
Install it by executing:
npm i '@igor.dvlpr/magic-string'
All string-related methods use a common string type that can either be a string or a string array. StringValue
defined as:
Creates a new instance of MagicString.
initialValue
- Optional, the initial value to assign to the accumulator.
Note
The value is assigned to the accumulator as-is, no processing is performed.
Gets the accumulator value.
Returns the accumulator as a string.
Gets the length of the accumulator.
Returns the length of the accumulator.
Checks if the accumulator is empty.
Returns true if the accumulator is empty, false otherwise.
Clears the accumulator.
Returns the current instance of MagicString
.
Appends a value to the accumulator.
value
- The value to append.
Returns the current instance of MagicString
.
Conditionally appends values to the accumulator only if the primary value is followed by other non-empty values.
This method checks if the provided rest values are non-empty before appending them.
Supports passing multiple string values as rest parameters or a single array of strings.
value
- The primary value to append.
rest
- Additional values to check and append.
Returns the current instance of MagicString
.
Appends a single-quoted value to the accumulator.
value
- The value to append.
Returns the current instance of MagicString
.
Appends a double-quoted value to the accumulator.
value
- The value to append.
Returns the current instance of MagicString
.
Trims all whitespace in the accumulator.
Returns the current instance of MagicString
.
Trims only leading and trailing whitespace from the accumulator.
Returns the current instance of MagicString
.
Enables trimming of values before adding them to the accumulator.
Returns the current instance of MagicString
.
Disables trimming of values before adding them to the accumulator.
Returns the current instance of MagicString
.
Trims only leading whitespace from the accumulator.
Returns the current instance of MagicString
.
Trims only trailing whitespace from the accumulator.
Returns the current instance of MagicString
.
Appends a value representing a path to the accumulator, wrapping it in double quotes if necessary.
value
- The path value to append.
Returns the current instance of MagicString
.
Prepends a value to the accumulator.
value
- The value to prepend.
Returns the current instance of MagicString
.
Converts the accumulator to lower case.
Returns the current instance of MagicString
.
Converts the accumulator to upper case.
Returns the current instance of MagicString
.
Replaces occurrences of a substring in the accumulator with a new substring.
searchValue
- The substring to search for.
replaceValue
- The substring to replace with.
Returns the current instance of MagicString
.
Inserts a value at a specified index in the accumulator.
If the index
is negative or zero, the value is prepended.
If the index
is greater than or equal to the length of the current accumulator, the value is appended.
value
- The value to insert.
index
- The position at which to insert the value.
Returns the current instance of MagicString
.
Extracts a substring from the accumulator between specified indices.
start
- The starting index of the substring.
end
- The ending index of the substring (optional).
Returns the current instance of MagicString
.
Scrambles (rearranges characters randomly) of the current accumulator.
Requires that the current accumulator value has a length
> 3.
Returns the current instance of MagicString
.
example.mts
import { MagicString } from '@igor.dvlpr/magic-string'
console.log(
new MagicString()
.append('Hello')
.appendSingleQuoted('world')
.appendQuoted('this is a test')
.prepend('Start: ')
.trimAll()
.path('/usr/local/bin')
.toUpperCase()
.path('/usr/local/my file.txt"') // intentional stray quote
.toLowerCase()
.toUpperCase()
.trimOn()
.append(' Trimmed ')
.trimOff()
.append(' Not trimmed')
.trim().value
)
// prints 'START: HELLO \'WORLD\' "THIS IS A TEST" /USR/LOCAL/BIN "/USR/LOCAL/MY FILE.TXT" Trimmed Not trimmed'
📑 The changelog is available here: CHANGELOG.md.
Licensed under the MIT license which is available here, MIT license.
🦀 ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings. 🪀
🧵 Provides ways of checking whether a String is present in an Array of Strings using custom Comparators. 🔍
💊 NormalizedString provides you with a String type with consistent line-endings, guaranteed. 📮
@igor.dvlpr/astro-escaped-component
🏃🏻♂️➡️ An Astro component that holds only HTML-encoded content. 📜
🥞 Removes HTML code from the given string. Can even extract text-only from the given an HTML string. ✨
Created by Igor Dimitrijević (@igorskyflyer).