|
1
|
|
|
const {setInputType, removeQuotes} = require('./commonFuncs.js') |
|
2
|
|
|
|
|
3
|
|
|
// ===================textFrom================================= |
|
4
|
|
|
export function textFrom (inpObj, cmdArgs, inclusive) { |
|
5
|
|
|
// cmdArgs is an array of arguments |
|
6
|
|
|
// inpObj is an object containing the text object we need to snip contents from |
|
7
|
|
|
// the format of the call is eg. |
|
8
|
|
|
// '--textFrom "some text" 2 - would start from the second instance of "some text" |
|
9
|
|
|
if (setInputType(inpObj, 'plain')) { |
|
10
|
|
|
let x = findLocation(inpObj, cmdArgs, inclusive ? '--start' : '--from') |
|
11
|
|
|
if (x.found) { |
|
12
|
|
|
inpObj.plain = inpObj.plain.substr(x.loc + (inclusive === true ? 0 : x.len)) |
|
13
|
|
|
} |
|
14
|
|
|
} |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
// ===================textTo=================================== |
|
18
|
|
|
export function textTo (inpObj, cmdArgs, inclusive) { |
|
19
|
|
|
// cmdArgs is an array of arguments |
|
20
|
|
|
// inpObj is an object containing the text object we need to snip contents from |
|
21
|
|
|
// the format of the call is eg. |
|
22
|
|
|
// '--textTo "some text" 2 - would go up to the second instance of "some text" |
|
23
|
|
|
if (setInputType(inpObj, 'plain')) { |
|
24
|
|
|
let x = findLocation(inpObj, cmdArgs, inclusive ? '--finish' : '--to') |
|
25
|
|
|
if (x.found) { |
|
26
|
|
|
inpObj.plain = inpObj.plain.substring(0, x.loc + (inclusive === true ? x.len : 0)) |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
function findLocation (inpObj, cmdArgs, errString) { |
|
32
|
|
|
// find the location of the nth occurrence of the text specified in the command arguments |
|
33
|
|
|
let occ |
|
34
|
|
|
if (cmdArgs.length === 1) { |
|
35
|
|
|
occ = 1 // by default we take from the first occurrence of this text |
|
36
|
|
|
} else if (cmdArgs.length === 2) { |
|
37
|
|
|
if ((cmdArgs[1] - 0) === (cmdArgs[1] - 0)) { |
|
38
|
|
|
occ = (cmdArgs[1] - 0) |
|
39
|
|
|
if (occ < 1) { |
|
40
|
|
|
inpObj.error.push(errString + ' requires its second argument to be a numeric value of at least 1 being the instance required') |
|
41
|
|
|
return {found: false} |
|
42
|
|
|
} |
|
43
|
|
|
} else { |
|
44
|
|
|
inpObj.error.push(errString + " requires its second argument to be numeric eg. '" + errString + " sometext 2' with the optional second argument being the instance required") |
|
45
|
|
|
return {found: false} |
|
46
|
|
|
} |
|
47
|
|
|
} else { |
|
48
|
|
|
inpObj.error.push(errString + " requires 1 or 2 arguments eg. '" + errString + " sometext' with the optional second argument being the instance required.") |
|
49
|
|
|
return {found: false} |
|
50
|
|
|
} |
|
51
|
|
|
let x = -1 |
|
52
|
|
|
let arg = removeQuotes(cmdArgs[0]) |
|
53
|
|
|
for (let i = 0; i < occ; i++) { |
|
54
|
|
|
x = inpObj.plain.indexOf(arg, x + 1) |
|
55
|
|
|
} |
|
56
|
|
|
if (x === -1) { |
|
57
|
|
|
inpObj.error.push('unable to find occurrence ' + occ + ' of "' + arg + '"') |
|
58
|
|
|
} |
|
59
|
|
|
return {found: (x !== -1), loc: x, len: arg.length} |
|
60
|
|
|
} |
|
61
|
|
|
|