-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (45 loc) · 1.89 KB
/
index.js
File metadata and controls
55 lines (45 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var replaceWithCover = require('./lib/replaceWithCover');
var replaceWithoutCover = require('./lib/replaceWithoutCover');
var toString = Object.prototype.toString;
function isSubArray(arr, subArr) {
return subArr.every(s => arr.includes(s));
}
/**
* @param {String} input A string to be processed.
* @param {Object} matcherObj An object that represents a string replacement mapping.
* @param {Array|Function} sequencer A `function` that takes the keys of `matcherObj`, and return an suquence array.
*/
function multiReplace(input, matcherObj, sequencer) {
var argsLength = arguments.length;
if (argsLength !== 2 && argsLength !== 3) {
throw new TypeError('The number of parameters is incorrect.');
}
if (typeof input !== 'string') {
throw new TypeError('Expected input to be a string, got ' + typeof input);
}
if (typeof matcherObj !== 'object') {
throw new TypeError('Expected matcherObj to be a object, got ' + typeof matcherObj);
} else {
if (Object.keys(matcherObj).length === 0) {
return 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(!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);
}
}
module.exports = multiReplace;