string-replace-async
Advanced tools
Comparing version 1.0.0 to 1.1.0
70
index.js
'use strict'; | ||
const escapeStr = require('escape-string-regexp'); | ||
var escapeStringRegexp = require('escape-string-regexp'); | ||
var objectAssign = require('object-assign'); | ||
function matchAll(str, re) { | ||
const matches = []; | ||
let res = re.exec(str); | ||
var matches = []; | ||
var res = re.exec(str); | ||
@@ -22,20 +23,40 @@ while (res) { | ||
function replaceAll(str, matches) { | ||
return matches | ||
.reverse() | ||
.reduce((res, match) => { | ||
const prefix = res.slice(0, match.index); | ||
const postfix = res.slice(match.index + match[0].length); | ||
return matches.reverse().reduce(function (res, match) { | ||
var prefix = res.slice(0, match.index); | ||
var postfix = res.slice(match.index + match[0].length); | ||
return prefix + match.replacement + postfix; | ||
}, str); | ||
return prefix + match.replacement + postfix; | ||
}, str); | ||
} | ||
function assignReplacement(match, replacer) { | ||
const args = match.concat([match.index, match.input]); | ||
var args = match.concat([match.index, match.input]); | ||
return replacer.apply(null, args) | ||
.then(replacement => Object.assign({}, match, {replacement})); | ||
return replacer.apply(null, args).then(function (res) { | ||
return objectAssign({}, match, {replacement: res}); | ||
}); | ||
} | ||
module.exports = function (str, re, replacer) { | ||
function sequence(matches, replacer) { | ||
var initialResult = Promise.resolve([]); | ||
return matches.reduce(function (prev, match) { | ||
return prev.then(function (ret) { | ||
return assignReplacement(match, replacer).then(function (match) { | ||
ret.push(match); | ||
return ret; | ||
}); | ||
}); | ||
}, initialResult); | ||
} | ||
function concurrency(matches, replacer) { | ||
var promises = matches.map(function (match) { | ||
return assignReplacement(match, replacer); | ||
}); | ||
return Promise.all(promises); | ||
} | ||
function processString(str, re, replacer, seq) { | ||
if (typeof replacer === 'string') { | ||
@@ -46,10 +67,21 @@ return Promise.resolve(str.replace(re, replacer)); | ||
if (typeof re === 'string') { | ||
re = new RegExp(escapeStr(re)); | ||
re = new RegExp(escapeStringRegexp(re)); | ||
} | ||
const matches = matchAll(str, re); | ||
const promises = matches.map(match => assignReplacement(match, replacer)); | ||
var matches = matchAll(str, re); | ||
var processor = seq ? sequence : concurrency; | ||
return Promise.all(promises) | ||
.then(matches => replaceAll(str, matches)); | ||
return processor(matches, replacer).then(function (matches) { | ||
return replaceAll(str, matches); | ||
}); | ||
} | ||
function stringReplaceAsync(str, re, replacer) { | ||
return processString(str, re, replacer, false); | ||
} | ||
stringReplaceAsync.seq = function (str, re, replacer) { | ||
return processString(str, re, replacer, true); | ||
}; | ||
module.exports = stringReplaceAsync; |
{ | ||
"name": "string-replace-async", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Asynchronous String.prototype.replace()", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=0.12" | ||
}, | ||
@@ -29,6 +29,9 @@ "scripts": { | ||
"asynchronous", | ||
"promise" | ||
"promise", | ||
"concurrent", | ||
"sequence" | ||
], | ||
"dependencies": { | ||
"escape-string-regexp": "^1.0.4" | ||
"escape-string-regexp": "^1.0.4", | ||
"object-assign": "^4.0.1" | ||
}, | ||
@@ -35,0 +38,0 @@ "devDependencies": { |
@@ -35,2 +35,8 @@ # string-replace-async [![Build Status](https://travis-ci.org/dsblv/string-replace-async.svg?branch=master)](https://travis-ci.org/dsblv/string-replace-async) | ||
Invokes `replacers` *concurrently*, returns a `promise` that resolves to processed string. | ||
### stringReplaceAsync.seq(string, expression, replacer) | ||
Invokes `replacers` *sequentially*, returns a `promise` that resolves to processed string. | ||
#### string | ||
@@ -52,3 +58,3 @@ | ||
Type: `fuction`, `string` | ||
Type: `function`, `string` | ||
*Required* | ||
@@ -55,0 +61,0 @@ |
5417
65
66
2
+ Addedobject-assign@^4.0.1
+ Addedobject-assign@4.1.1(transitive)