Socket
Socket
Sign inDemoInstall

string-multiple-replace

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.6 to 1.0.0

57

index.js
'use strict';
/**
* V1.0.0 Version upgrade instructions:
* Discarded:
* 1. @param needCover is no longer necessary.
* The existence state of the sequencer determines whether to replace the previous replacement operation in execution.
*/

@@ -6,2 +12,4 @@ var replaceWithCover = require('./lib/replaceWithCover');

var toString = Object.prototype.toString;
function isSubArray(arr, subArr) {

@@ -19,27 +27,13 @@ arr = arr.slice();

function fn(input, matcherObj, needCover, sequence) {
if (needCover) {
return replaceWithCover(input, matcherObj, sequence);
} else {
return replaceWithoutCover(input, matcherObj, sequence);
}
}
/**
* @param {String} input A string to be processed.
* @param {Object} matcherObj An object that represents a string replacement mapping.
* @param {Boolean} needCover A boolean determines whether to replace the last replacement data in the execution.
* @param {array} sequencer A `function` that takes the keys of `mapperObj`, and return an suquence array.
*/
function multiReplace(input, matcherObj, needCover, sequencer) {
function multiReplace(input, matcherObj, sequencer) {
var argsLength = arguments.length;
if (argsLength !== 3 && argsLength !== 4) {
if (argsLength !== 2 && argsLength !== 3) {
throw new TypeError('The number of parameters is incorrect.');
}
var input = arguments[0];
var matcherObj = arguments[1];
var needCover = argsLength === 4 ? arguments[2] : false;
var sequencer = argsLength === 4 ? arguments[3] : arguments[2];
if (typeof input !== 'string') {

@@ -56,19 +50,20 @@ throw new TypeError('Expected input to be a string, got ' + typeof input);

}
if(sequencer) {
var sequence = [];
if (typeof sequencer === 'function') {
sequence = sequence.concat(sequencer(Object.keys(matcherObj)));
} else if (toString.call(sequencer) === '[object Array]') {
sequence = sequence.concat(sequencer);
} else {
throw new TypeError('Expected sequencer to be a callback or array, got ' + toString.call(sequencer));
}
if (typeof needCover !== 'boolean') {
throw new TypeError('Expected needCover to be a boolean, got ' + typeof needCover);
if(!isSubArray(Object.keys(matcherObj), sequence)) {
throw new TypeError('Expected sequence is the subset of Object.keys(matcherObj), got: ' + sequence);
}
return replaceWithCover(input, matcherObj, sequence);
} else {
return replaceWithoutCover(input, matcherObj);
}
var sequence = [];
if (typeof sequencer === 'function') {
sequence = sequencer(Object.keys(matcherObj));
} else if (toString.call(sequencer) === '[object Array]') {
sequence = sequencer;
}
if(!isSubArray(Object.keys(matcherObj), sequence)) {
throw new TypeError('Expected sequence is the subset of Object.keys(matcherObj), got: ' + sequence);
}
return fn(input, matcherObj, needCover, sequence);
}

@@ -75,0 +70,0 @@

@@ -51,6 +51,7 @@ 'use strict';

function generateItems(input, matcherObj, sequence) {
function generateItems(input, matcherObj) {
var items = [];
for(var i=0, len = sequence.length; i<len;i++) {
var item = generateItemByTarget(input, sequence[i], matcherObj[sequence[i]]);
var keys = Object.keys(matcherObj);
for(var i=0, len = keys.length; i<len;i++) {
var item = generateItemByTarget(input, keys[i], matcherObj[keys[i]]);
items.push(item);

@@ -62,5 +63,5 @@ }

// this replacement operation will not be affected next time.
module.exports = function(input, matcherObj, sequence) {
var items = generateItems(input, matcherObj, sequence);
module.exports = function(input, matcherObj) {
var items = generateItems(input, matcherObj);
return replaceRange(input, items);
}
{
"_from": "string-multiple-replace@0.1.5",
"_id": "string-multiple-replace@0.1.5",
"_from": "string-multiple-replace",
"_id": "string-multiple-replace@0.1.6",
"_inBundle": false,
"_integrity": "sha512-BZ1DnRb7tm2W7/TiN0nEW53HwW6R41rigb0rhhpKRWEXPuS21rR5H7g2QTDYMr7J3p0taPJjIYaPmtxln4En+Q==",
"_integrity": "sha512-7QU7AcTk3w2E0KYnf3zE40iyzGdarIff7B20jcmxzZ5ZEmN5IYmp2ZodvgzI+2iT+Gl0/Hw0r4JoyASkQpocEw==",
"_location": "/string-multiple-replace",
"_phantomChildren": {},
"_requested": {
"type": "version",
"type": "tag",
"registry": true,
"raw": "string-multiple-replace@0.1.5",
"raw": "string-multiple-replace",
"name": "string-multiple-replace",
"escapedName": "string-multiple-replace",
"rawSpec": "0.1.5",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "0.1.5"
"fetchSpec": "latest"
},

@@ -22,5 +22,5 @@ "_requiredBy": [

],
"_resolved": "https://registry.npmjs.org/string-multiple-replace/-/string-multiple-replace-0.1.5.tgz",
"_shasum": "80d48ee474ee455636c66cbdcffa66c3e8ac114c",
"_spec": "string-multiple-replace@0.1.5",
"_resolved": "https://registry.npmjs.org/string-multiple-replace/-/string-multiple-replace-0.1.6.tgz",
"_shasum": "031e337229f2249c7c0a555e4668555eb7462ec4",
"_spec": "string-multiple-replace",
"_where": "C:\\Users\\Administrator\\Desktop\\stringReplacer",

@@ -33,2 +33,5 @@ "author": {

"description": "Replace multiple substrings in a string in turn",
"devDependencies": {
"chai": "^4.2.0"
},
"keywords": [

@@ -49,3 +52,3 @@ "replace",

},
"version": "0.1.6"
"version": "1.0.0"
}

@@ -15,2 +15,18 @@

## Usage
### v1.x.x +
```js
const multiReplace = require('string-multiple-replace');
const input = "abcde";
const matcherObj = {
"a": "b",
"b": "c",
"c": "d",
"d": "e"
}
multiReplace(input, matcherObj, ["a", "b", "c", "d"]); // eeeee
multiReplace(input, matcherObj); // bcdee
```
### v0.x.x
- Example-1

@@ -21,3 +37,2 @@ ```js

const input = "I'm only brave when I have to be. Being brave doesn't mean you go looking for trouble.";
const matcherObj = {

@@ -27,5 +42,3 @@ "brave": "cowardly",

}
const sequencer = ["brave", "trouble"];
multiReplace(input, matcherObj, true, sequencer);

@@ -41,3 +54,2 @@ //I'm only cowardly when I have to be. Being cowardly doesn't mean you go looking for escape.

const input = "I'm only brave when I have to be. Being brave doesn't mean you go looking for trouble.";
const matcherObj = {

@@ -48,5 +60,3 @@ "brave": "cowardly",

multiReplace(input, matcherObj, true, function(keys) {
return keys; //or keys.sort(callback)
});
multiReplace(input, matcherObj, true, keys => keys);
//I'm only cowardly when I have to be. Being cowardly doesn't mean you go looking for escape.

@@ -61,3 +71,2 @@

const input = "abcd, abcd";
const matcherObj = {

@@ -74,5 +83,11 @@ "a": "b",

## API
### v1.x.x
> multiReplace(input, matcherObj[,sequencer])
### multiReplace(input, matcherObj, [needCover,] sequencer)
The original string is replaced in turn according to the `matcherObj`, where `sequencer` determines the replacement order, and the existence state of `sequencer` determines whether the last operation overwrites the previous operation.
### v0.x.x
> multiReplace(input, matcherObj, [needCover,] sequencer)
The original string is replaced in turn according to the `matcherObj`, where `sequencer` determines the replacement order, and `needCover` determines whether to replace the last replacement data in the execution.

@@ -85,3 +100,3 @@

A string to be processed.
> A string to be processed.

@@ -92,5 +107,5 @@ #### matcherObj

An object that represents a string replacement mapping.
> An object that represents a string replacement mapping.
#### needCover
#### needCover: `discarded`
Type: `boolean`

@@ -101,8 +116,10 @@ *Default: false*

> Discarded Instruction: Remove redundant parameters,The existence state of `sequencer`determines the replacement method.
#### sequencer
Type: `function`, `array`
*Required*
*Required*
*Default: u*
A `function` that takes the keys of `matcherObj`, and return an suquence array.
>Upgrade Instruction: the existence state of `sequencer` determines whether the last operation overwrites the previous operation.

@@ -7,3 +7,3 @@ var should = require('chai').should();

describe('needCover is true: The replacement operation will be overwritten', function() {
describe("need cover previous replacement", function() {
it('Replace brave & trouble from a text with cowardly & escape, and sequencer is a array that decide the order of replacement.', function(){

@@ -17,3 +17,3 @@ var input = "I'm only brave when I have to be. Being brave doesn't mean you go looking for trouble.";

var resultStr = multiReplace(input, matcherObj, true, sequencer);;
var resultStr = multiReplace(input, matcherObj, sequencer);;
resultStr.should.equal("I'm only cowardly when I have to be. Being cowardly doesn't mean you go looking for escape.");

@@ -29,3 +29,3 @@ });

var resultStr = multiReplace(input, matcherObj, true, function(keys) {
var resultStr = multiReplace(input, matcherObj, function(keys) {
return keys; //or keys.sort(callback)

@@ -45,3 +45,3 @@ });

var resultStr = multiReplace(input, matcherObj, true, sequencer);;
var resultStr = multiReplace(input, matcherObj, sequencer);;
resultStr.should.equal("My friend have a dog. My friend want a dog too!");

@@ -51,3 +51,3 @@ });

describe("needCover is false: The replacement operation will not be overwritten", function() {
describe("needn't cover previous replacement", function() {
it("Replace 'My firend' with 'I', and then 'I' will not be overwritten with 'My friend'", function(){

@@ -61,3 +61,3 @@ var input = "My friend has a dog. I want a dog too!";

var resultStr = multiReplace(input, matcherObj, false, Object.keys(matcherObj));;
var resultStr = multiReplace(input, matcherObj);;
resultStr.should.equal("I have a dog. My friend want a dog too!");

@@ -73,3 +73,3 @@ });

var resultStr = multiReplace(input, matcherObj, false, Object.keys(matcherObj));
var resultStr = multiReplace(input, matcherObj);
resultStr.should.equal("bacd, bacd, b");

@@ -87,5 +87,4 @@ });

}
var sequencer = ["a", "b", "c"];
return multiReplace(input, matcherObj, false, sequencer);
return multiReplace(input, matcherObj, sequencer);
}

@@ -92,0 +91,0 @@ expect(badFun).to.throw("Expected sequence is the subset of Object.keys(matcherObj), got: a,b,c");

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc