Chainable string processing for people who are tired of juggling intermediate variables, half-broken regex code, and one-off parsing helpers.
string-saw lets you match, split, filter, reshape, and convert text through a fluent pipeline that stays readable even when the parsing logic gets real.
npm install string-saw- Chains string operations into one clean pipeline
- Works with raw strings, arrays of strings, regex match results, and numbers
- Handles extraction, cleanup, reshaping, and conversion in the same flow
- Makes regex-heavy parsing less annoying
- Returns a new
Sawat each step, so you can reuse earlier pipelines
import saw from 'string-saw';
const value = saw('one two three four five six hello 323423 hello')
.remove(/\d+/g, 'hello')
.split(' ')
.slice(3, 4)
.toString();
// "four"- Regex when you need it, plain strings when you do not
- Array-style operations after
split()ormatch() - Easy conversions to
string,number,boolean,array, andobject - Named capture groups can become objects instantly
- Non-matching operations degrade safely instead of blowing up your pipeline
import saw from 'string-saw';
const users = saw('joe:56, bob:57')
.matchAll(/(?<name>\S+):(?<age>\d+)/g);
// [
// { name: 'joe', age: '56' },
// { name: 'bob', age: '57' }
// ]const port = saw('PORT=8080')
.match(/PORT=(\d+)/)
.first()
.toNumber();
// 8080const headline = saw(' hello from string-saw ')
.split(/\s+/)
.filter()
.capitalize()
.join(' ')
.toString();
// "Hello From String-saw"const version = saw('release-2026-03-27')
.split('-')
.itemFromRight(0)
.toNumber();
// 27const person = saw('John. Smith.')
.match(/\S+/g)
.remove('.')
.toObject('first', 'last');
// { first: 'John', last: 'Smith' }const words = saw('one two three').split(' ');
const reversed = words
.map(word => word.split('').reverse().join(''))
.join(' ')
.toString();
const uppercased = words
.upperCase()
.join(' ')
.toString();
// reversed => "eno owt eerht"
// uppercased => "ONE TWO THREE"Most pipelines look like this:
- Start with
saw(input) - Narrow or reshape with
match(),split(),filter(),slice(), ormap() - Clean or format with
trim(),remove(),replace(),capitalize(),upperCase(),lowerCase(),join() - Finish with
toString(),toArray(),toNumber(),toObject(), or another terminal check
match(string | RegExp | function | Array<matcher>)matchAll(RegExp)has(string | RegExp | function)find(string | RegExp | function)filter(string | RegExp | function)filterNot(string | RegExp | function)indexOf(string | RegExp | function)indexesOf(string | RegExp | function)startsWith(string)endsWith(string)
split(string | RegExp)slice(start?, end?)item(index)itemFromRight(offset)first()last()map(fn)each(fn)reduce(fn)transform(fn)sort(compareFn?)reverse()uniq()length()
replace(match, replacement)remove(...matches)trim()join(separator | separatorFn)prepend(string)append(string)capitalize()upperCase()lowerCase()
toString()toArray()toBoolean()toNumber()toInt()toFloat()toObject(...keys)
saw() accepts:
stringnumberstring[]
That means you can start from raw text, parsed tokens, or numeric input and keep using the same fluent API.
match()accepts plain strings, regular expressions, matcher functions, or an array/list of matchersmatchAll()returns arrays for unnamed groups and objects for named groupstoNumber()returns0when conversion failstoInt()andtoFloat()can returnNaN- CommonJS and TypeScript consumers are supported by the published package exports
MIT