string-range-expander 3.0.5 → 3.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +4 -5
- package/dist/string-range-expander.esm.js +4 -129
- package/dist/string-range-expander.umd.js +4 -2
- package/examples/_quickTake.js +1 -0
- package/package.json +23 -80
- package/types/index.d.ts +14 -13
- package/examples/api.json +0 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2010-
|
|
3
|
+
Copyright (c) 2010-2022 Roy Revelt and other contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
6
|
a copy of this software and associated documentation files (the
|
package/README.md
CHANGED
|
@@ -26,18 +26,17 @@
|
|
|
26
26
|
|
|
27
27
|
## Install
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
The latest version is **ESM only**: Node 12+ is needed to use it and it must be `import`ed instead of `require`d. If your project is not on ESM yet and you want to use `require`, use an older version of this program, `2.1.0`.
|
|
30
30
|
|
|
31
31
|
```bash
|
|
32
32
|
npm i string-range-expander
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
If you need a legacy version which works with `require`, use version 2.1.0
|
|
36
|
-
|
|
37
35
|
## Quick Take
|
|
38
36
|
|
|
39
37
|
```js
|
|
40
38
|
import { strict as assert } from "assert";
|
|
39
|
+
|
|
41
40
|
import { expander } from "string-range-expander";
|
|
42
41
|
|
|
43
42
|
// let's say we have picked the "zzzz" index range - [16, 20]
|
|
@@ -60,7 +59,7 @@ assert.deepEqual(
|
|
|
60
59
|
|
|
61
60
|
## Documentation
|
|
62
61
|
|
|
63
|
-
Please [visit codsen.com](https://codsen.com/os/string-range-expander/) for a full description of the API
|
|
62
|
+
Please [visit codsen.com](https://codsen.com/os/string-range-expander/) for a full description of the API.
|
|
64
63
|
|
|
65
64
|
## Contributing
|
|
66
65
|
|
|
@@ -70,6 +69,6 @@ To report bugs or request features or assistance, [raise an issue](https://githu
|
|
|
70
69
|
|
|
71
70
|
MIT License
|
|
72
71
|
|
|
73
|
-
Copyright (c) 2010-
|
|
72
|
+
Copyright (c) 2010-2022 Roy Revelt and other contributors
|
|
74
73
|
|
|
75
74
|
<img src="https://codsen.com/images/png-codsen-ok.png" width="98" alt="ok" align="center"> <img src="https://codsen.com/images/png-codsen-1.png" width="148" alt="codsen" align="center"> <img src="https://codsen.com/images/png-codsen-star-small.png" width="32" alt="star" align="center">
|
|
@@ -1,137 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name string-range-expander
|
|
3
3
|
* @fileoverview Expands string index ranges within whitespace boundaries until letters are met
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.11
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/string-range-expander/}
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const defaults = {
|
|
14
|
-
str: "",
|
|
15
|
-
from: 0,
|
|
16
|
-
to: 0,
|
|
17
|
-
ifLeftSideIncludesThisThenCropTightly: "",
|
|
18
|
-
ifLeftSideIncludesThisCropItToo: "",
|
|
19
|
-
ifRightSideIncludesThisThenCropTightly: "",
|
|
20
|
-
ifRightSideIncludesThisCropItToo: "",
|
|
21
|
-
extendToOneSide: false,
|
|
22
|
-
wipeAllWhitespaceOnLeft: false,
|
|
23
|
-
wipeAllWhitespaceOnRight: false,
|
|
24
|
-
addSingleSpaceToPreventAccidentalConcatenation: false
|
|
25
|
-
};
|
|
26
|
-
function expander(originalOpts) {
|
|
27
|
-
const letterOrDigit = /^[0-9a-zA-Z]+$/;
|
|
28
|
-
function isWhitespace(char) {
|
|
29
|
-
if (!char || typeof char !== "string") {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
return !char.trim();
|
|
33
|
-
}
|
|
34
|
-
function isStr(something) {
|
|
35
|
-
return typeof something === "string";
|
|
36
|
-
}
|
|
37
|
-
if (!originalOpts || typeof originalOpts !== "object" || Array.isArray(originalOpts)) {
|
|
38
|
-
let supplementalString;
|
|
39
|
-
if (originalOpts === undefined) {
|
|
40
|
-
supplementalString = "but it is missing completely.";
|
|
41
|
-
} else if (originalOpts === null) {
|
|
42
|
-
supplementalString = "but it was given as null.";
|
|
43
|
-
} else {
|
|
44
|
-
supplementalString = `but it was given as ${typeof originalOpts}, equal to:\n${JSON.stringify(originalOpts, null, 4)}.`;
|
|
45
|
-
}
|
|
46
|
-
throw new Error(`string-range-expander: [THROW_ID_01] Input must be a plain object ${supplementalString}`);
|
|
47
|
-
} else if (typeof originalOpts === "object" && originalOpts !== null && !Array.isArray(originalOpts) && !Object.keys(originalOpts).length) {
|
|
48
|
-
throw new Error(`string-range-expander: [THROW_ID_02] Input must be a plain object but it was given as a plain object without any keys.`);
|
|
49
|
-
}
|
|
50
|
-
if (typeof originalOpts.from !== "number") {
|
|
51
|
-
throw new Error(`string-range-expander: [THROW_ID_03] The input's "from" value opts.from, is not a number! Currently it's given as ${typeof originalOpts.from}, equal to ${JSON.stringify(originalOpts.from, null, 0)}`);
|
|
52
|
-
}
|
|
53
|
-
if (typeof originalOpts.to !== "number") {
|
|
54
|
-
throw new Error(`string-range-expander: [THROW_ID_04] The input's "to" value opts.to, is not a number! Currently it's given as ${typeof originalOpts.to}, equal to ${JSON.stringify(originalOpts.to, null, 0)}`);
|
|
55
|
-
}
|
|
56
|
-
if (originalOpts && originalOpts.str && !originalOpts.str[originalOpts.from] && originalOpts.from !== originalOpts.to) {
|
|
57
|
-
throw new Error(`string-range-expander: [THROW_ID_05] The given input string opts.str ("${originalOpts.str}") must contain the character at index "from" ("${originalOpts.from}")`);
|
|
58
|
-
}
|
|
59
|
-
if (originalOpts && originalOpts.str && !originalOpts.str[originalOpts.to - 1]) {
|
|
60
|
-
throw new Error(`string-range-expander: [THROW_ID_06] The given input string, opts.str ("${originalOpts.str}") must contain the character at index before "to" ("${originalOpts.to - 1}")`);
|
|
61
|
-
}
|
|
62
|
-
if (originalOpts.from > originalOpts.to) {
|
|
63
|
-
throw new Error(`string-range-expander: [THROW_ID_07] The given "from" index, "${originalOpts.from}" is greater than "to" index, "${originalOpts.to}". That's wrong!`);
|
|
64
|
-
}
|
|
65
|
-
if (isStr(originalOpts.extendToOneSide) && originalOpts.extendToOneSide !== "left" && originalOpts.extendToOneSide !== "right" || !isStr(originalOpts.extendToOneSide) && originalOpts.extendToOneSide !== undefined && originalOpts.extendToOneSide !== false) {
|
|
66
|
-
throw new Error(`string-range-expander: [THROW_ID_08] The opts.extendToOneSide value is not recogniseable! It's set to: "${originalOpts.extendToOneSide}" (${typeof originalOpts.extendToOneSide}). It has to be either Boolean "false" or strings "left" or "right"`);
|
|
67
|
-
}
|
|
68
|
-
const opts = { ...defaults,
|
|
69
|
-
...originalOpts
|
|
70
|
-
};
|
|
71
|
-
if (Array.isArray(opts.ifLeftSideIncludesThisThenCropTightly)) {
|
|
72
|
-
let culpritsIndex;
|
|
73
|
-
let culpritsValue;
|
|
74
|
-
if (opts.ifLeftSideIncludesThisThenCropTightly.every((val, i) => {
|
|
75
|
-
if (!isStr(val)) {
|
|
76
|
-
culpritsIndex = i;
|
|
77
|
-
culpritsValue = val;
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
return true;
|
|
81
|
-
})) {
|
|
82
|
-
opts.ifLeftSideIncludesThisThenCropTightly = opts.ifLeftSideIncludesThisThenCropTightly.join("");
|
|
83
|
-
} else {
|
|
84
|
-
throw new Error(`string-range-expander: [THROW_ID_09] The opts.ifLeftSideIncludesThisThenCropTightly was set to an array:\n${JSON.stringify(opts.ifLeftSideIncludesThisThenCropTightly, null, 4)}. Now, that array contains not only string elements. For example, an element at index ${culpritsIndex} is of a type ${typeof culpritsValue} (equal to ${JSON.stringify(culpritsValue, null, 0)}).`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const str = opts.str;
|
|
88
|
-
let from = opts.from;
|
|
89
|
-
let to = opts.to;
|
|
90
|
-
if (opts.extendToOneSide !== "right" && (isWhitespace(str[from - 1]) && (isWhitespace(str[from - 2]) || opts.ifLeftSideIncludesThisCropItToo.includes(str[from - 2])) || str[from - 1] && opts.ifLeftSideIncludesThisCropItToo.includes(str[from - 1]) || opts.wipeAllWhitespaceOnLeft && isWhitespace(str[from - 1]))) {
|
|
91
|
-
for (let i = from; i--;) {
|
|
92
|
-
if (!opts.ifLeftSideIncludesThisCropItToo.includes(str[i])) {
|
|
93
|
-
if (str[i].trim()) {
|
|
94
|
-
if (opts.wipeAllWhitespaceOnLeft || opts.ifLeftSideIncludesThisCropItToo.includes(str[i + 1])) {
|
|
95
|
-
from = i + 1;
|
|
96
|
-
} else {
|
|
97
|
-
from = i + 2;
|
|
98
|
-
}
|
|
99
|
-
break;
|
|
100
|
-
} else if (i === 0) {
|
|
101
|
-
if (opts.wipeAllWhitespaceOnLeft) {
|
|
102
|
-
from = 0;
|
|
103
|
-
} else {
|
|
104
|
-
from = 1;
|
|
105
|
-
}
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
if (opts.extendToOneSide !== "left" && (isWhitespace(str[to]) && (opts.wipeAllWhitespaceOnRight || isWhitespace(str[to + 1])) || opts.ifRightSideIncludesThisCropItToo.includes(str[to]))) {
|
|
112
|
-
for (let i = to, len = str.length; i < len; i++) {
|
|
113
|
-
if (!opts.ifRightSideIncludesThisCropItToo.includes(str[i]) && (str[i] && str[i].trim() || str[i] === undefined)) {
|
|
114
|
-
if (opts.wipeAllWhitespaceOnRight || opts.ifRightSideIncludesThisCropItToo.includes(str[i - 1])) {
|
|
115
|
-
to = i;
|
|
116
|
-
} else {
|
|
117
|
-
to = i - 1;
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (opts.extendToOneSide !== "right" && isStr(opts.ifLeftSideIncludesThisThenCropTightly) && opts.ifLeftSideIncludesThisThenCropTightly && (str[from - 2] && opts.ifLeftSideIncludesThisThenCropTightly.includes(str[from - 2]) || str[from - 1] && opts.ifLeftSideIncludesThisThenCropTightly.includes(str[from - 1])) || opts.extendToOneSide !== "left" && isStr(opts.ifRightSideIncludesThisThenCropTightly) && opts.ifRightSideIncludesThisThenCropTightly && (str[to + 1] && opts.ifRightSideIncludesThisThenCropTightly.includes(str[to + 1]) || str[to] && opts.ifRightSideIncludesThisThenCropTightly.includes(str[to]))) {
|
|
124
|
-
if (opts.extendToOneSide !== "right" && isWhitespace(str[from - 1]) && !opts.wipeAllWhitespaceOnLeft) {
|
|
125
|
-
from -= 1;
|
|
126
|
-
}
|
|
127
|
-
if (opts.extendToOneSide !== "left" && isWhitespace(str[to]) && !opts.wipeAllWhitespaceOnRight) {
|
|
128
|
-
to += 1;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
if (opts.addSingleSpaceToPreventAccidentalConcatenation && str[from - 1] && str[from - 1].trim() && str[to] && str[to].trim() && (!opts.ifLeftSideIncludesThisThenCropTightly && !opts.ifRightSideIncludesThisThenCropTightly || !((!opts.ifLeftSideIncludesThisThenCropTightly || opts.ifLeftSideIncludesThisThenCropTightly.includes(str[from - 1])) && (!opts.ifRightSideIncludesThisThenCropTightly || str[to] && opts.ifRightSideIncludesThisThenCropTightly.includes(str[to])))) && (letterOrDigit.test(str[from - 1]) || letterOrDigit.test(str[to]))) {
|
|
132
|
-
return [from, to, " "];
|
|
133
|
-
}
|
|
134
|
-
return [from, to];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export { defaults, expander, version };
|
|
10
|
+
var u="3.0.11";var m=u,a={str:"",from:0,to:0,ifLeftSideIncludesThisThenCropTightly:"",ifLeftSideIncludesThisCropItToo:"",ifRightSideIncludesThisThenCropTightly:"",ifRightSideIncludesThisCropItToo:"",extendToOneSide:!1,wipeAllWhitespaceOnLeft:!1,wipeAllWhitespaceOnRight:!1,addSingleSpaceToPreventAccidentalConcatenation:!1};function y(t){let f=/^[0-9a-zA-Z]+$/;function s(i){return!i||typeof i!="string"?!1:!i.trim()}function l(i){return typeof i=="string"}if(!t||typeof t!="object"||Array.isArray(t)){let i;throw t===void 0?i="but it is missing completely.":t===null?i="but it was given as null.":i=`but it was given as ${typeof t}, equal to:
|
|
11
|
+
${JSON.stringify(t,null,4)}.`,new Error(`string-range-expander: [THROW_ID_01] Input must be a plain object ${i}`)}else if(typeof t=="object"&&t!==null&&!Array.isArray(t)&&!Object.keys(t).length)throw new Error("string-range-expander: [THROW_ID_02] Input must be a plain object but it was given as a plain object without any keys.");if(typeof t.from!="number")throw new Error(`string-range-expander: [THROW_ID_03] The input's "from" value opts.from, is not a number! Currently it's given as ${typeof t.from}, equal to ${JSON.stringify(t.from,null,0)}`);if(typeof t.to!="number")throw new Error(`string-range-expander: [THROW_ID_04] The input's "to" value opts.to, is not a number! Currently it's given as ${typeof t.to}, equal to ${JSON.stringify(t.to,null,0)}`);if(t?.str&&!t.str[t.from]&&t.from!==t.to)throw new Error(`string-range-expander: [THROW_ID_05] The given input string opts.str ("${t.str}") must contain the character at index "from" ("${t.from}")`);if(t?.str&&!t.str[t.to-1])throw new Error(`string-range-expander: [THROW_ID_06] The given input string, opts.str ("${t.str}") must contain the character at index before "to" ("${t.to-1}")`);if(t.from>t.to)throw new Error(`string-range-expander: [THROW_ID_07] The given "from" index, "${t.from}" is greater than "to" index, "${t.to}". That's wrong!`);if(l(t.extendToOneSide)&&t.extendToOneSide!=="left"&&t.extendToOneSide!=="right"||!l(t.extendToOneSide)&&t.extendToOneSide!==void 0&&t.extendToOneSide!==!1)throw new Error(`string-range-expander: [THROW_ID_08] The opts.extendToOneSide value is not recogniseable! It's set to: "${t.extendToOneSide}" (${typeof t.extendToOneSide}). It has to be either Boolean "false" or strings "left" or "right"`);let e={...a,...t};if(Array.isArray(e.ifLeftSideIncludesThisThenCropTightly)){let i,d;if(e.ifLeftSideIncludesThisThenCropTightly.every((h,c)=>l(h)?!0:(i=c,d=h,!1)))e.ifLeftSideIncludesThisThenCropTightly=e.ifLeftSideIncludesThisThenCropTightly.join("");else throw new Error(`string-range-expander: [THROW_ID_09] The opts.ifLeftSideIncludesThisThenCropTightly was set to an array:
|
|
12
|
+
${JSON.stringify(e.ifLeftSideIncludesThisThenCropTightly,null,4)}. Now, that array contains not only string elements. For example, an element at index ${i} is of a type ${typeof d} (equal to ${JSON.stringify(d,null,0)}).`)}let n=e.str,r=e.from,o=e.to;if(e.extendToOneSide!=="right"&&(s(n[r-1])&&(s(n[r-2])||e.ifLeftSideIncludesThisCropItToo.includes(n[r-2]))||n[r-1]&&e.ifLeftSideIncludesThisCropItToo.includes(n[r-1])||e.wipeAllWhitespaceOnLeft&&s(n[r-1]))){for(let i=r;i--;)if(!e.ifLeftSideIncludesThisCropItToo.includes(n[i])){if(n[i].trim()){e.wipeAllWhitespaceOnLeft||e.ifLeftSideIncludesThisCropItToo.includes(n[i+1])?r=i+1:r=i+2;break}else if(i===0){e.wipeAllWhitespaceOnLeft?r=0:r=1;break}}}if(e.extendToOneSide!=="left"&&(s(n[o])&&(e.wipeAllWhitespaceOnRight||s(n[o+1]))||e.ifRightSideIncludesThisCropItToo.includes(n[o]))){for(let i=o,d=n.length;i<d;i++)if(!e.ifRightSideIncludesThisCropItToo.includes(n[i])&&n[i]?.trim()){e.wipeAllWhitespaceOnRight||e.ifRightSideIncludesThisCropItToo.includes(n[i-1])?o=i:o=i-1;break}}return(e.extendToOneSide!=="right"&&l(e.ifLeftSideIncludesThisThenCropTightly)&&e.ifLeftSideIncludesThisThenCropTightly&&(n[r-2]&&e.ifLeftSideIncludesThisThenCropTightly.includes(n[r-2])||n[r-1]&&e.ifLeftSideIncludesThisThenCropTightly.includes(n[r-1]))||e.extendToOneSide!=="left"&&l(e.ifRightSideIncludesThisThenCropTightly)&&e.ifRightSideIncludesThisThenCropTightly&&(n[o+1]&&e.ifRightSideIncludesThisThenCropTightly.includes(n[o+1])||n[o]&&e.ifRightSideIncludesThisThenCropTightly.includes(n[o])))&&(e.extendToOneSide!=="right"&&s(n[r-1])&&!e.wipeAllWhitespaceOnLeft&&(r-=1),e.extendToOneSide!=="left"&&s(n[o])&&!e.wipeAllWhitespaceOnRight&&(o+=1)),e.addSingleSpaceToPreventAccidentalConcatenation&&n[r-1]&&n[r-1].trim()&&n[o]&&n[o].trim()&&(!e.ifLeftSideIncludesThisThenCropTightly&&!e.ifRightSideIncludesThisThenCropTightly||!((!e.ifLeftSideIncludesThisThenCropTightly||e.ifLeftSideIncludesThisThenCropTightly.includes(n[r-1]))&&(!e.ifRightSideIncludesThisThenCropTightly||n[o]&&e.ifRightSideIncludesThisThenCropTightly.includes(n[o]))))&&(f.test(n[r-1])||f.test(n[o]))?[r,o," "]:[r,o]}export{a as defaults,y as expander,m as version};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name string-range-expander
|
|
3
3
|
* @fileoverview Expands string index ranges within whitespace boundaries until letters are met
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.11
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/string-range-expander/}
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
var stringRangeExpander=(()=>{var h=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames,a=Object.getOwnPropertySymbols;var p=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var m=(e,r,o)=>r in e?h(e,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[r]=o,u=(e,r)=>{for(var o in r||(r={}))p.call(r,o)&&m(e,o,r[o]);if(a)for(var o of a(r))b.call(r,o)&&m(e,o,r[o]);return e};var x=e=>h(e,"__esModule",{value:!0});var w=(e,r)=>{for(var o in r)h(e,o,{get:r[o],enumerable:!0})},R=(e,r,o,l)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of I(r))!p.call(e,t)&&(o||t!=="default")&&h(e,t,{get:()=>r[t],enumerable:!(l=S(r,t))||l.enumerable});return e};var C=(e=>(r,o)=>e&&e.get(r)||(o=R(x({}),r,1),e&&e.set(r,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var D={};w(D,{defaults:()=>y,expander:()=>A,version:()=>L});var g="3.0.11";var L=g,y={str:"",from:0,to:0,ifLeftSideIncludesThisThenCropTightly:"",ifLeftSideIncludesThisCropItToo:"",ifRightSideIncludesThisThenCropTightly:"",ifRightSideIncludesThisCropItToo:"",extendToOneSide:!1,wipeAllWhitespaceOnLeft:!1,wipeAllWhitespaceOnRight:!1,addSingleSpaceToPreventAccidentalConcatenation:!1};function A(e){var c;let r=/^[0-9a-zA-Z]+$/;function o(i){return!i||typeof i!="string"?!1:!i.trim()}function l(i){return typeof i=="string"}if(!e||typeof e!="object"||Array.isArray(e)){let i;throw e===void 0?i="but it is missing completely.":e===null?i="but it was given as null.":i=`but it was given as ${typeof e}, equal to:
|
|
11
|
+
${JSON.stringify(e,null,4)}.`,new Error(`string-range-expander: [THROW_ID_01] Input must be a plain object ${i}`)}else if(typeof e=="object"&&e!==null&&!Array.isArray(e)&&!Object.keys(e).length)throw new Error("string-range-expander: [THROW_ID_02] Input must be a plain object but it was given as a plain object without any keys.");if(typeof e.from!="number")throw new Error(`string-range-expander: [THROW_ID_03] The input's "from" value opts.from, is not a number! Currently it's given as ${typeof e.from}, equal to ${JSON.stringify(e.from,null,0)}`);if(typeof e.to!="number")throw new Error(`string-range-expander: [THROW_ID_04] The input's "to" value opts.to, is not a number! Currently it's given as ${typeof e.to}, equal to ${JSON.stringify(e.to,null,0)}`);if((e==null?void 0:e.str)&&!e.str[e.from]&&e.from!==e.to)throw new Error(`string-range-expander: [THROW_ID_05] The given input string opts.str ("${e.str}") must contain the character at index "from" ("${e.from}")`);if((e==null?void 0:e.str)&&!e.str[e.to-1])throw new Error(`string-range-expander: [THROW_ID_06] The given input string, opts.str ("${e.str}") must contain the character at index before "to" ("${e.to-1}")`);if(e.from>e.to)throw new Error(`string-range-expander: [THROW_ID_07] The given "from" index, "${e.from}" is greater than "to" index, "${e.to}". That's wrong!`);if(l(e.extendToOneSide)&&e.extendToOneSide!=="left"&&e.extendToOneSide!=="right"||!l(e.extendToOneSide)&&e.extendToOneSide!==void 0&&e.extendToOneSide!==!1)throw new Error(`string-range-expander: [THROW_ID_08] The opts.extendToOneSide value is not recogniseable! It's set to: "${e.extendToOneSide}" (${typeof e.extendToOneSide}). It has to be either Boolean "false" or strings "left" or "right"`);let t=u(u({},y),e);if(Array.isArray(t.ifLeftSideIncludesThisThenCropTightly)){let i,f;if(t.ifLeftSideIncludesThisThenCropTightly.every((T,$)=>l(T)?!0:(i=$,f=T,!1)))t.ifLeftSideIncludesThisThenCropTightly=t.ifLeftSideIncludesThisThenCropTightly.join("");else throw new Error(`string-range-expander: [THROW_ID_09] The opts.ifLeftSideIncludesThisThenCropTightly was set to an array:
|
|
12
|
+
${JSON.stringify(t.ifLeftSideIncludesThisThenCropTightly,null,4)}. Now, that array contains not only string elements. For example, an element at index ${i} is of a type ${typeof f} (equal to ${JSON.stringify(f,null,0)}).`)}let n=t.str,s=t.from,d=t.to;if(t.extendToOneSide!=="right"&&(o(n[s-1])&&(o(n[s-2])||t.ifLeftSideIncludesThisCropItToo.includes(n[s-2]))||n[s-1]&&t.ifLeftSideIncludesThisCropItToo.includes(n[s-1])||t.wipeAllWhitespaceOnLeft&&o(n[s-1]))){for(let i=s;i--;)if(!t.ifLeftSideIncludesThisCropItToo.includes(n[i])){if(n[i].trim()){t.wipeAllWhitespaceOnLeft||t.ifLeftSideIncludesThisCropItToo.includes(n[i+1])?s=i+1:s=i+2;break}else if(i===0){t.wipeAllWhitespaceOnLeft?s=0:s=1;break}}}if(t.extendToOneSide!=="left"&&(o(n[d])&&(t.wipeAllWhitespaceOnRight||o(n[d+1]))||t.ifRightSideIncludesThisCropItToo.includes(n[d]))){for(let i=d,f=n.length;i<f;i++)if(!t.ifRightSideIncludesThisCropItToo.includes(n[i])&&((c=n[i])==null?void 0:c.trim())){t.wipeAllWhitespaceOnRight||t.ifRightSideIncludesThisCropItToo.includes(n[i-1])?d=i:d=i-1;break}}return(t.extendToOneSide!=="right"&&l(t.ifLeftSideIncludesThisThenCropTightly)&&t.ifLeftSideIncludesThisThenCropTightly&&(n[s-2]&&t.ifLeftSideIncludesThisThenCropTightly.includes(n[s-2])||n[s-1]&&t.ifLeftSideIncludesThisThenCropTightly.includes(n[s-1]))||t.extendToOneSide!=="left"&&l(t.ifRightSideIncludesThisThenCropTightly)&&t.ifRightSideIncludesThisThenCropTightly&&(n[d+1]&&t.ifRightSideIncludesThisThenCropTightly.includes(n[d+1])||n[d]&&t.ifRightSideIncludesThisThenCropTightly.includes(n[d])))&&(t.extendToOneSide!=="right"&&o(n[s-1])&&!t.wipeAllWhitespaceOnLeft&&(s-=1),t.extendToOneSide!=="left"&&o(n[d])&&!t.wipeAllWhitespaceOnRight&&(d+=1)),t.addSingleSpaceToPreventAccidentalConcatenation&&n[s-1]&&n[s-1].trim()&&n[d]&&n[d].trim()&&(!t.ifLeftSideIncludesThisThenCropTightly&&!t.ifRightSideIncludesThisThenCropTightly||!((!t.ifLeftSideIncludesThisThenCropTightly||t.ifLeftSideIncludesThisThenCropTightly.includes(n[s-1]))&&(!t.ifRightSideIncludesThisThenCropTightly||n[d]&&t.ifRightSideIncludesThisThenCropTightly.includes(n[d]))))&&(r.test(n[s-1])||r.test(n[d]))?[s,d," "]:[s,d]}return C(D);})();
|
package/examples/_quickTake.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "string-range-expander",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.11",
|
|
4
4
|
"description": "Expands string index ranges within whitespace boundaries until letters are met",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"expand",
|
|
@@ -32,93 +32,36 @@
|
|
|
32
32
|
},
|
|
33
33
|
"types": "types/index.d.ts",
|
|
34
34
|
"scripts": {
|
|
35
|
-
"build": "
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"tap": "tap",
|
|
50
|
-
"pretest": "npm run build",
|
|
51
|
-
"test": "npm run lint && npm run unittest && npm run test:examples && npm run clean_cov && npm run format",
|
|
52
|
-
"test:examples": "../../scripts/test-examples.js && npm run lect && npm run prettier",
|
|
53
|
-
"tsc": "tsc",
|
|
54
|
-
"unittest": "tap --no-only --output-file=testStats.md --reporter=terse && tsc -p tsconfig.json --noEmit && npm run clean_cov && npm run perf"
|
|
35
|
+
"build": "node '../../ops/scripts/esbuild.js' && yarn run dts",
|
|
36
|
+
"dev": "DEV=true node '../../ops/scripts/esbuild.js' && yarn run dts",
|
|
37
|
+
"dts": "rollup -c && yarn run prettier 'types/index.d.ts' --write",
|
|
38
|
+
"examples": "node '../../ops/scripts/run-examples.js'",
|
|
39
|
+
"lect": "node '../../ops/lect/lect.js'",
|
|
40
|
+
"letspublish": "yarn publish || :",
|
|
41
|
+
"lint": "eslint . --fix",
|
|
42
|
+
"perf": "node perf/check.js",
|
|
43
|
+
"prepare": "echo 'ready'",
|
|
44
|
+
"prettier": "prettier",
|
|
45
|
+
"prettier:format": "prettier --write '**/*.{ts,tsx,md}' --no-error-on-unmatched-pattern",
|
|
46
|
+
"pretest": "yarn run lect && yarn run build",
|
|
47
|
+
"test": "c8 yarn run unit && yarn run examples && yarn run lint",
|
|
48
|
+
"unit": "uvu test"
|
|
55
49
|
},
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"--no-warnings",
|
|
64
|
-
"--experimental-loader",
|
|
65
|
-
"@istanbuljs/esm-loader-hook"
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
52
|
+
},
|
|
53
|
+
"c8": {
|
|
54
|
+
"check-coverage": true,
|
|
55
|
+
"exclude": [
|
|
56
|
+
"**/test/**/*.*"
|
|
66
57
|
],
|
|
67
|
-
"
|
|
58
|
+
"lines": 100
|
|
68
59
|
},
|
|
69
60
|
"lect": {
|
|
70
61
|
"licence": {
|
|
71
62
|
"extras": [
|
|
72
63
|
""
|
|
73
64
|
]
|
|
74
|
-
},
|
|
75
|
-
"req": "{ expander }",
|
|
76
|
-
"various": {
|
|
77
|
-
"devDependencies": []
|
|
78
65
|
}
|
|
79
|
-
},
|
|
80
|
-
"dependencies": {
|
|
81
|
-
"@babel/runtime": "^7.16.0"
|
|
82
|
-
},
|
|
83
|
-
"devDependencies": {
|
|
84
|
-
"@babel/cli": "^7.16.0",
|
|
85
|
-
"@babel/core": "^7.16.0",
|
|
86
|
-
"@babel/node": "^7.16.0",
|
|
87
|
-
"@babel/plugin-external-helpers": "^7.16.0",
|
|
88
|
-
"@babel/plugin-proposal-class-properties": "^7.16.0",
|
|
89
|
-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0",
|
|
90
|
-
"@babel/plugin-proposal-object-rest-spread": "^7.16.0",
|
|
91
|
-
"@babel/plugin-proposal-optional-chaining": "^7.16.0",
|
|
92
|
-
"@babel/plugin-transform-runtime": "^7.16.0",
|
|
93
|
-
"@babel/preset-env": "^7.16.0",
|
|
94
|
-
"@babel/preset-typescript": "^7.16.0",
|
|
95
|
-
"@babel/register": "^7.16.0",
|
|
96
|
-
"@istanbuljs/esm-loader-hook": "^0.1.2",
|
|
97
|
-
"@rollup/plugin-babel": "^5.3.0",
|
|
98
|
-
"@rollup/plugin-commonjs": "^21.0.1",
|
|
99
|
-
"@rollup/plugin-json": "^4.1.0",
|
|
100
|
-
"@rollup/plugin-node-resolve": "^13.0.6",
|
|
101
|
-
"@rollup/plugin-strip": "^2.1.0",
|
|
102
|
-
"@rollup/plugin-typescript": "^8.3.0",
|
|
103
|
-
"@types/node": "^16.11.6",
|
|
104
|
-
"@types/tap": "^15.0.5",
|
|
105
|
-
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
|
106
|
-
"@typescript-eslint/parser": "^5.3.0",
|
|
107
|
-
"core-js": "^3.19.1",
|
|
108
|
-
"cross-env": "^7.0.3",
|
|
109
|
-
"eslint": "^8.2.0",
|
|
110
|
-
"lect": "^0.18.5",
|
|
111
|
-
"rollup": "^2.59.0",
|
|
112
|
-
"rollup-plugin-ascii": "^0.0.3",
|
|
113
|
-
"rollup-plugin-banner": "^0.2.1",
|
|
114
|
-
"rollup-plugin-cleanup": "^3.2.1",
|
|
115
|
-
"rollup-plugin-dts": "^4.0.1",
|
|
116
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
117
|
-
"tap": "^15.0.10",
|
|
118
|
-
"tslib": "^2.3.1",
|
|
119
|
-
"typescript": "^4.4.4"
|
|
120
|
-
},
|
|
121
|
-
"engines": {
|
|
122
|
-
"node": ">=12"
|
|
123
66
|
}
|
|
124
67
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
declare type Range =
|
|
1
|
+
declare type Range =
|
|
2
|
+
| [from: number, to: number]
|
|
3
|
+
| [from: number, to: number, whatToInsert: string | null | undefined];
|
|
2
4
|
|
|
3
5
|
declare const version: string;
|
|
4
|
-
|
|
5
6
|
interface Opts {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
str: string;
|
|
8
|
+
from: number;
|
|
9
|
+
to: number;
|
|
10
|
+
ifLeftSideIncludesThisThenCropTightly: string;
|
|
11
|
+
ifLeftSideIncludesThisCropItToo: string;
|
|
12
|
+
ifRightSideIncludesThisThenCropTightly: string;
|
|
13
|
+
ifRightSideIncludesThisCropItToo: string;
|
|
14
|
+
extendToOneSide: false | "left" | "right";
|
|
15
|
+
wipeAllWhitespaceOnLeft: boolean;
|
|
16
|
+
wipeAllWhitespaceOnRight: boolean;
|
|
17
|
+
addSingleSpaceToPreventAccidentalConcatenation: boolean;
|
|
17
18
|
}
|
|
18
19
|
declare const defaults: Opts;
|
|
19
20
|
declare function expander(originalOpts: Partial<Opts>): Range;
|
package/examples/api.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"_quickTake.js":{"title":"Quick Take","content":"import { strict as assert } from \"assert\";\nimport { expander } from \"string-range-expander\";\n\n// let's say we have picked the \"zzzz\" index range - [16, 20]\n// \"something>\\n\\t zzzz <here\"\n// | |\n// from to\n//\n// PS. \"\\n\" and \"\\t\" take up a single character's length\n\nassert.deepEqual(\n expander({\n str: \"something>\\n\\t zzzz <here\",\n from: 16,\n to: 20,\n ifRightSideIncludesThisThenCropTightly: \"<\",\n }),\n [10, 21]\n);"}}
|