
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
power-split
Advanced tools
Because sometimes String.split()
it's not enough!
This is a small Typescript library that I've wrote out of frustration building a command line parser from scratch. It provides a few utility methods that solves some use case that a bare call to String.split()
won't solve.
PowerSplit.split()
PowerSplit.splitWithIndexes()
PowerSplit.substring()
PowerSplit.cutAt()
That's all (currently). Maybe it's not much but solves some pretty tedious use cases. See the Example.
The library is written with typescript and provides it's own typings out-of-the-box. To start using it you just need to install it using:
npm install --save power-split
Now import the PowerSplit
class and you're ready!
import { PowerSplit } from 'power-split';
The code is documented using tsdoc so any compatible IDE should display it. Additionally you can find the documentation on the repository-connected github pages at: https://monesidn.github.io/power-split/
All the following example are available under the examples folder and can be run using ts-node
.
examples/get-start-end-indexes.ts
import { PowerSplit } from '../src';
const input = `Lorem ipsum dolor \tsit amet`;
// Here we want to split the string by spaces but also we need the start of each
// word. Using split whould successfully parse words into an array loosing index
// information.
console.log(JSON.stringify(input.split(/\s+/gu))); // ["Lorem", "ipsum", "dolor", "sit", "amet"]
// PowerParser to the rescue
console.log(JSON.stringify(PowerSplit.splitWithIndexes(input, /\s+/g), null, ' '));
// [
// {
// start: 0,
// end: 5,
// token: 'Lorem',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 6,
// end: 11,
// token: 'ipsum',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 12,
// end: 17,
// token: 'dolor',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 19,
// end: 22,
// token: 'sit',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 26,
// end: 30,
// token: 'amet',
// originalString: 'Lorem ipsum dolor \tsit amet'
// }
// ]
examples/parse-getting-remainder.ts
import { LimitMode, PowerSplit } from '../src';
const input = `Lorem ipsum dolor \tsit amet`;
// Here we want to split the string into two chunks but also we want to get what is
// left of it. With split it won't work as people from other languages expect.
console.log(JSON.stringify(input.split(/\s+/, 2))); // ["Lorem", "ipsum"]
// With power parser we can use two approches:
// LimitMode.REMAINDER_AS_LAST
console.log(JSON.stringify(PowerSplit.splitWithIndexes(input, /\s+/g, 3, LimitMode.REMAINDER_AS_LAST), null, ' '));
// [
// {
// start: 0,
// end: 5,
// token: 'Lorem',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 6,
// end: 11,
// token: 'ipsum',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 12,
// end: 30,
// token: 'dolor \tsit amet',
// originalString: 'Lorem ipsum dolor \tsit amet'
// }
// ]
// or in a more "manual" approach we can use the last index.
const powerSplit2 = PowerSplit.splitWithIndexes(input, /\s+/g, 2);
console.log(JSON.stringify(powerSplit2, null, ' '));
console.log(JSON.stringify(input.substring(powerSplit2[1].end)));
// [
// {
// start: 0,
// end: 5,
// token: 'Lorem',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// {
// start: 6,
// end: 11,
// token: 'ipsum',
// originalString: 'Lorem ipsum dolor \tsit amet'
// },
// ]
// "dolor \tsit amet"
FAQs
Sometimes string.split() is not enough!
The npm package power-split receives a total of 0 weekly downloads. As such, power-split popularity was classified as not popular.
We found that power-split demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.