Skip to content

Commit

Permalink
feat: rewrite in TS, start using named exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: previously: "import expander from ..." - now: "import { expander } from ..."
  • Loading branch information
revelt committed Dec 29, 2020
1 parent a127aa9 commit ea4ccc3
Show file tree
Hide file tree
Showing 17 changed files with 634 additions and 550 deletions.
3 changes: 2 additions & 1 deletion packages/string-range-expander/.npmignore
@@ -1,4 +1,4 @@
# .... generated using www.npmjs.com/package/lect ....
# generated using codsen.com/os/lect
#
#
# __ ______ ______ ______
Expand All @@ -23,3 +23,4 @@ test
.prettierignore
rollup.config.js
testStats.md
tsconfig.json
2 changes: 1 addition & 1 deletion packages/string-range-expander/README.md
Expand Up @@ -34,7 +34,7 @@ npm i string-range-expander

```js
import { strict as assert } from "assert";
import expander from "string-range-expander";
import { expander } from "string-range-expander";

// let's say we have picked the "zzzz" index range - [16, 20]
// "something>\n\t zzzz <here"
Expand Down
@@ -1 +1 @@
{"total":{"lines":{"total":68,"covered":55,"skipped":0,"pct":80.88},"statements":{"total":71,"covered":58,"skipped":0,"pct":81.69},"functions":{"total":4,"covered":3,"skipped":0,"pct":75},"branches":{"total":127,"covered":118,"skipped":0,"pct":92.91}}}
{"total":{"lines":{"total":69,"covered":56,"skipped":0,"pct":81.16},"statements":{"total":72,"covered":59,"skipped":0,"pct":81.94},"functions":{"total":4,"covered":3,"skipped":0,"pct":75},"branches":{"total":127,"covered":118,"skipped":0,"pct":92.91}}}
174 changes: 81 additions & 93 deletions packages/string-range-expander/dist/string-range-expander.cjs.js
Expand Up @@ -9,148 +9,124 @@

'use strict';

function _typeof(obj) {
"@babel/helpers - typeof";

if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
Object.defineProperty(exports, '__esModule', { value: true });

return _typeof(obj);
}
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

return obj;
}

function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);

if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);

return keys;
}

function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};

if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
var version = "1.12.0";

return target;
}
var defaults = {
str: "",
from: 0,
to: 0,
ifLeftSideIncludesThisThenCropTightly: "",
ifLeftSideIncludesThisCropItToo: "",
ifRightSideIncludesThisThenCropTightly: "",
ifRightSideIncludesThisCropItToo: "",
extendToOneSide: false,
wipeAllWhitespaceOnLeft: false,
wipeAllWhitespaceOnRight: false,
addSingleSpaceToPreventAccidentalConcatenation: false
};

function expander(originalOpts) {
var letterOrDigit = /^[0-9a-zA-Z]+$/;
var letterOrDigit = /^[0-9a-zA-Z]+$/; // Internal functions
// ---------------------------------------------------------------------------

function isWhitespace(char) {
if (!char || typeof char !== "string") {
return false;
}

return !char.trim();
}

function isStr(something) {
return typeof something === "string";
}
if (!originalOpts || _typeof(originalOpts) !== "object" || Array.isArray(originalOpts)) {
} // Sanitise the inputs
// ---------------------------------------------------------------------------


if (!originalOpts || typeof originalOpts !== "object" || Array.isArray(originalOpts)) {
var supplementalString;

if (originalOpts === undefined) {
supplementalString = "but it is missing completely.";
} else if (originalOpts === null) {
supplementalString = "but it was given as null.";
} else {
supplementalString = "but it was given as ".concat(_typeof(originalOpts), ", equal to:\n").concat(JSON.stringify(originalOpts, null, 4), ".");
supplementalString = "but it was given as " + typeof originalOpts + ", equal to:\n" + JSON.stringify(originalOpts, null, 4) + ".";
}
throw new Error("string-range-expander: [THROW_ID_01] Input must be a plain object ".concat(supplementalString));
} else if (_typeof(originalOpts) === "object" && originalOpts !== null && !Array.isArray(originalOpts) && !Object.keys(originalOpts).length) {

throw new Error("string-range-expander: [THROW_ID_01] Input must be a plain object " + supplementalString);
} else if (typeof originalOpts === "object" && originalOpts !== null && !Array.isArray(originalOpts) && !Object.keys(originalOpts).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 originalOpts.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 ".concat(_typeof(originalOpts.from), ", equal to ").concat(JSON.stringify(originalOpts.from, null, 0)));
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));
}

if (typeof originalOpts.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 ".concat(_typeof(originalOpts.to), ", equal to ").concat(JSON.stringify(originalOpts.to, null, 0)));
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));
}

if (!originalOpts.str[originalOpts.from] && originalOpts.from !== originalOpts.to) {
throw new Error("string-range-expander: [THROW_ID_05] The given input string opts.str (\"".concat(originalOpts.str, "\") must contain the character at index \"from\" (\"").concat(originalOpts.from, "\")"));
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 + "\")");
}

if (!originalOpts.str[originalOpts.to - 1]) {
throw new Error("string-range-expander: [THROW_ID_06] The given input string, opts.str (\"".concat(originalOpts.str, "\") must contain the character at index before \"to\" (\"").concat(originalOpts.to - 1, "\")"));
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) + "\")");
}

if (originalOpts.from > originalOpts.to) {
throw new Error("string-range-expander: [THROW_ID_07] The given \"from\" index, \"".concat(originalOpts.from, "\" is greater than \"to\" index, \"").concat(originalOpts.to, "\". That's wrong!"));
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!");
}

if (isStr(originalOpts.extendToOneSide) && originalOpts.extendToOneSide !== "left" && originalOpts.extendToOneSide !== "right" || !isStr(originalOpts.extendToOneSide) && originalOpts.extendToOneSide !== undefined && originalOpts.extendToOneSide !== false) {
throw new Error("string-range-expander: [THROW_ID_08] The opts.extendToOneSide value is not recogniseable! It's set to: \"".concat(originalOpts.extendToOneSide, "\" (").concat(_typeof(originalOpts.extendToOneSide), "). It has to be either Boolean \"false\" or strings \"left\" or \"right\""));
}
var defaults = {
str: "",
from: 0,
to: 0,
ifLeftSideIncludesThisThenCropTightly: "",
ifLeftSideIncludesThisCropItToo: "",
ifRightSideIncludesThisThenCropTightly: "",
ifRightSideIncludesThisCropItToo: "",
extendToOneSide: false,
wipeAllWhitespaceOnLeft: false,
wipeAllWhitespaceOnRight: false,
addSingleSpaceToPreventAccidentalConcatenation: false
};
var opts = _objectSpread2(_objectSpread2({}, defaults), originalOpts);
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\"");
} // Prepare the opts
// ---------------------------------------------------------------------------


var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults), originalOpts);

if (Array.isArray(opts.ifLeftSideIncludesThisThenCropTightly)) {
var culpritsIndex;
var culpritsValue;

if (opts.ifLeftSideIncludesThisThenCropTightly.every(function (val, i) {
if (!isStr(val)) {
culpritsIndex = i;
culpritsValue = val;
return false;
}

return true;
})) {
opts.ifLeftSideIncludesThisThenCropTightly = opts.ifLeftSideIncludesThisThenCropTightly.join("");
} else {
throw new Error("string-range-expander: [THROW_ID_09] The opts.ifLeftSideIncludesThisThenCropTightly was set to an array:\n".concat(JSON.stringify(opts.ifLeftSideIncludesThisThenCropTightly, null, 4), ". Now, that array contains not only string elements. For example, an element at index ").concat(culpritsIndex, " is of a type ").concat(_typeof(culpritsValue), " (equal to ").concat(JSON.stringify(culpritsValue, null, 0), ")."));
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) + ").");
}
}
var str = opts.str;
} // Action
// ---------------------------------------------------------------------------


var str = opts.str; // convenience

var from = opts.from;
var to = opts.to;
var to = opts.to; // 1. expand the given range outwards and leave a single space or
// {single-of-whatever-there-was} (like line break, tab etc) on each side

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]))) {
// loop backwards

for (var i = from; i--;) {

if (!opts.ifLeftSideIncludesThisCropItToo.includes(str[i])) {
if (str[i].trim()) {
if (opts.wipeAllWhitespaceOnLeft || opts.ifLeftSideIncludesThisCropItToo.includes(str[i + 1])) {
Expand All @@ -169,9 +145,14 @@ function expander(originalOpts) {
}
}
}
}
} // 2. expand forward


if (opts.extendToOneSide !== "left" && (isWhitespace(str[to]) && (opts.wipeAllWhitespaceOnRight || isWhitespace(str[to + 1])) || opts.ifRightSideIncludesThisCropItToo.includes(str[to]))) {
// loop forward

for (var _i = to, len = str.length; _i < len; _i++) {

if (!opts.ifRightSideIncludesThisCropItToo.includes(str[_i]) && (str[_i] && str[_i].trim() || str[_i] === undefined)) {
if (opts.wipeAllWhitespaceOnRight || opts.ifRightSideIncludesThisCropItToo.includes(str[_i - 1])) {
to = _i;
Expand All @@ -181,19 +162,26 @@ function expander(originalOpts) {
break;
}
}
}
} // 3. tight crop adjustments


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]))) {

if (opts.extendToOneSide !== "right" && isWhitespace(str[from - 1]) && !opts.wipeAllWhitespaceOnLeft) {
from -= 1;
}

if (opts.extendToOneSide !== "left" && isWhitespace(str[to]) && !opts.wipeAllWhitespaceOnRight) {
to += 1;
}
}

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]))) {
return [from, to, " "];
}
return [from, to];
}

module.exports = expander;
exports.defaults = defaults;
exports.expander = expander;
exports.version = version;

0 comments on commit ea4ccc3

Please sign in to comment.