项目作者: Radioreve

项目描述 :
Extract substrings between two delimiters characters
高级语言: JavaScript
项目地址: git://github.com/Radioreve/unbracket.git
创建时间: 2018-04-16T15:42:01Z
项目社区:https://github.com/Radioreve/unbracket

开源协议:Other

下载


Motivation

Sometimes, strings contain data that’s structured in such a way, that the relevant piece of information is
between two - usually symmetrical - delimiters characters (parenthesis, brackets…)

When trying to extract it, you’re thinking… cool, let’s use a regex

After 30 minutes of failed attempts, you decide to look it up on stackoverflow, where someone - several people actually - casually suggest to use something like (?:(?<=().+?(?=))|(?<=[).+?(?=])), like a random monday morning. sigh

Have you ever done this? Well, I have. So I decided to roll up my own non-regexp function to just do that, and never have to think about it again.

Unbracket

Default usage

Meet the unbracket package. A simple “unbracketter” without regexp that just works

  1. const unbracket = require("unbracket")
  2. const string = "I don't (really) think that you should do this (crazy) thing"
  3. const thoughts = unbracket.withParenthesis(string) // ["really", "crazy"]

API

The API contains all the common delimiters along with custom one

  1. unbracket.withBrackets(<String>)
  2. unbracket.withCurlyBraces(<String>)
  3. unbracket.withRafters(<String>)
  4. unbracket.withCustom(<String>, OPENING_DELIMITER, CLOSING_DELIMITER)

Limitations

Unbracket has zero opinion on how to deal with nested delimiters:

  1. const unbracket = require("unbracket")
  2. const string = "GroupA(Jean, Jack(Gold, Man)), GroupB(Ben)"
  3. const thoughts = unbracket.withParenthesis(string) // ["Jean, Jack(Gold, Man)", "Ben"]

Installing

Install via npm is straightforward:

  1. npm install unbracket

Unit tests

To run the tests in case you wanna contribute

  1. cd /path/to/unbracket
  2. npm install
  3. npm run test

Contributing

Pull requests are always welcome ! Please make sure to heavily unit test your feature before.

Authors

Léo Jacquemin (https://ljaya.me)

License

This project is licensed under the MIT License - see the LICENSE.md file for details