string-split
Advanced tools
Comparing version
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
## 0.3.0 - 2015-05-16 | ||
### Added | ||
- add support for splitting by function. | ||
- add documentation for `splitBy` function. | ||
### Changed | ||
- use `curry2` lib. | ||
## 0.2.5 - 2015-04-29 | ||
@@ -5,0 +13,0 @@ ### Changed |
57
index.js
'use strict' | ||
/** | ||
/*! | ||
* exports. | ||
*/ | ||
module.exports = split | ||
var curry2 = require('curry2') | ||
/*! | ||
* exports. | ||
*/ | ||
module.exports = curry2(split) | ||
/** | ||
* Curried `String.prototype.split`. | ||
* A curried `String.prototype.split` with support | ||
* for splitting by String, RegExp, or Function. | ||
* | ||
* @param {String|RegExp} pattern | ||
* Pattern used to split string. | ||
* @param {String|RegExp|Function} splitBy | ||
* String, RegExp, or Function to split by. | ||
* | ||
@@ -22,10 +29,38 @@ * @param {String} string | ||
function split (pattern) { | ||
return arguments.length > 1 | ||
? splitter(pattern, arguments[1]) | ||
: splitter.bind(null, pattern) | ||
function split (splitBy, string) { | ||
return (typeof splitBy === 'function') | ||
? predicate(splitBy, string) | ||
: string.split(splitBy) | ||
} | ||
function splitter (pattern, string) { | ||
return string.split(pattern) | ||
/** | ||
* Split via predicate function. | ||
* | ||
* @param {Function} fn | ||
* Predicate function. | ||
* | ||
* @param {String} string | ||
* String to split. | ||
* | ||
* @return {Array} | ||
* List of split string parts. | ||
*/ | ||
function predicate (fn, string) { | ||
var idx = -1 | ||
var end = string.length | ||
var out = [] | ||
var buf = '' | ||
while (++idx < end) { | ||
if (fn(string[idx], idx) === true) { | ||
if (buf) out.push(buf) | ||
buf = '' | ||
} else { | ||
buf += string[idx] | ||
} | ||
} | ||
if (buf) out.push(buf) | ||
return out | ||
} |
{ | ||
"name": "string-split", | ||
"description": "String.prototype.split as a curried function.", | ||
"version": "0.2.5", | ||
"description": "A curried `String.prototype.split` with support for splitting by String, RegExp, or Function.", | ||
"version": "0.3.0", | ||
"author": "Wil Moore III <wil.moore@wilmoore.com>", | ||
@@ -9,3 +9,5 @@ "bugs": { | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"curry2": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
@@ -43,2 +45,5 @@ "nodemon": "^1.3.7", | ||
"dev": "nodemon -x 'npm run test --silent' -e 'js json'", | ||
"release-major": "npm version major && git push --follow-tags && npm publish", | ||
"release-minor": "npm version minor && git push --follow-tags && npm publish", | ||
"release-patch": "npm version patch && git push --follow-tags && npm publish", | ||
"standard": "standard", | ||
@@ -45,0 +50,0 @@ "test": "npm run standard --silent && node test.js | tap-spec" |
# string-split | ||
> String.prototype.split as a curried function. | ||
> A curried `String.prototype.split` with support for splitting by String, RegExp, or Function. | ||
@@ -14,5 +14,5 @@ [](https://travis-ci.org/wilmoore/string-split.js) [](https://codeclimate.com/github/wilmoore/string-split.js) [](https://github.com/feross/standard) | ||
## API Examples & Inspiration | ||
## Examples | ||
###### Require | ||
###### require | ||
@@ -23,3 +23,3 @@ ```js | ||
###### Equivalent to "example.com".split(".") | ||
###### full application | ||
@@ -31,5 +31,13 @@ ```js | ||
###### Transform a list with a custom split pattern | ||
###### partial application | ||
```js | ||
var undot = split('.') | ||
undot('example.com'); | ||
//=> ["example", "com"] | ||
``` | ||
###### iteratee | ||
```js | ||
var transform = split("::"); | ||
@@ -42,4 +50,51 @@ var foodtypes = ["Entree::Seafood", "Entree::Chicken"]; | ||
###### predicate | ||
```js | ||
function isNumber (chr, _) { | ||
return !!Number(chr) | ||
} | ||
split(isNumber, 'Hello1World2') | ||
//=> ['Hello', 'World'] | ||
``` | ||
###### predicate using index | ||
```js | ||
function odd (chr, idx) { | ||
return idx % 2 !== 0 | ||
} | ||
split(odd, 'AaBbCcDd') | ||
//=> ['A', 'B', 'C', 'D'] | ||
``` | ||
## Features | ||
- Supports splitting by String, RegExp, or Function. | ||
- Curried. | ||
## API | ||
### `split(splitBy, string)` | ||
###### arguments | ||
- `splitBy: (String|RegExp|Function)` String, RegExp, or Function to split by. | ||
- `string: (String)` String to split. | ||
###### returns | ||
- `(Array)` List of split string parts. | ||
###### splitBy function signature | ||
> Return `true` to split by current `chr` or `idx`. | ||
- `chr: (String)` current character. | ||
- `idx: (Number)` current character index. | ||
## License | ||
[](https://github.com/wilmoore/string-split.js/blob/master/license) |
6549
40.72%55
120%97
130.95%1
Infinity%+ Added
+ Added