GithubHelp home page GithubHelp logo

sindresorhus / split-on-first Goto Github PK

View Code? Open in Web Editor NEW
73.0 3.0 14.0 9 KB

Split a string on the first occurrence of a given separator

License: MIT License

JavaScript 88.64% TypeScript 11.36%
split string-manipulation string-split string npm-package

split-on-first's Introduction

split-on-first

Split a string on the first occurrence of a given separator

This is similar to String#split(), but that one splits on all the occurrences, not just the first one.

Install

$ npm install split-on-first

Usage

import splitOnFirst from 'split-on-first';

splitOnFirst('a-b-c', '-');
//=> ['a', 'b-c']

splitOnFirst('key:value:value2', ':');
//=> ['key', 'value:value2']

splitOnFirst('a---b---c', '---');
//=> ['a', 'b---c']

splitOnFirst('a-b-c', '+');
//=> []

splitOnFirst('abc', '');
//=> []

API

splitOnFirst(string, separator)

string

Type: string

The string to split.

separator

Type: string

The separator to split on.

Related

  • split-at - Split a string at one or more indices

split-on-first's People

Contributors

bendingbender avatar faizanu94 avatar fregante avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

split-on-first's Issues

Non spec compliance

Using 'abc'.split(''), this value is given:

["a", "b", "c"]

But when using splitOnFirst('abc', ''), this expected result was not given:

["a", "bc"]

Instead, the value was:

["abc"]

IE 11 throws error due to use of reserved keyword (string) as function parameter

My app recently stopped working in IE 11, I tracked this down to a piece of your code that we are pulling in due to our use of the query-string module which uses split-on-first.

The issue is you're using a reserved keyword (string) as a function param here:

module.exports = (string, separator) => {
if (!(typeof string === 'string' && typeof separator === 'string')) {
throw new TypeError('Expected the arguments to be of type string');
}

While most modern browsers seem to be able to understand what the code really meant, not IE 11.
A simple fix would be changing string to str in this particular function

Update from CommonJS to ESM

Hi!

I would like to use query-string in a environment that doesn't support the old CommonJS,
it uses ESM since recently, but it still depends on split-on-first which does not.

Any chance of updating it?
A similar change was done recently for another dependency, which can be used as example: SamVerschueren/decode-uri-component#11

Thanks!

Regex support

Thoughts on regex support? indexOf would have to be replaced with search

const splitOnFirst = require('split-on-first');

splitOnFirst('a,b,c', /[.,]/);
//=> ['a', 'b,c']

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.