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 findMalformed from ..." - now "import { findMalformed } from
..."
  • Loading branch information
revelt committed Dec 28, 2020
1 parent 7cf002e commit ea15d44
Show file tree
Hide file tree
Showing 17 changed files with 2,562 additions and 2,323 deletions.
3 changes: 2 additions & 1 deletion packages/string-find-malformed/.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
4 changes: 2 additions & 2 deletions packages/string-find-malformed/README.md
Expand Up @@ -34,11 +34,11 @@ npm i string-find-malformed

```js
import { strict as assert } from "assert";
import strFindMalformed from "string-find-malformed";
import { findMalformed } from "string-find-malformed";

// Below, we look for dodgy cases of `<!--`
const gathered = [];
strFindMalformed(
findMalformed(
"<div><!-something--></div>",
"<!--",
// your callback function:
Expand Down
@@ -1 +1 @@
{"total":{"lines":{"total":59,"covered":58,"skipped":0,"pct":98.31},"statements":{"total":63,"covered":62,"skipped":0,"pct":98.41},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"branches":{"total":53,"covered":52,"skipped":0,"pct":98.11}}}
{"total":{"lines":{"total":56,"covered":55,"skipped":0,"pct":98.21},"statements":{"total":64,"covered":63,"skipped":0,"pct":98.44},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"branches":{"total":61,"covered":58,"skipped":0,"pct":95.08}}}
243 changes: 118 additions & 125 deletions packages/string-find-malformed/dist/string-find-malformed.cjs.js
Expand Up @@ -9,163 +9,134 @@

'use strict';

var stringLeftRight = require('string-left-right');

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;
};
}

return _typeof(obj);
}

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;
}

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);
}

return keys;
}
Object.defineProperty(exports, '__esModule', { value: true });

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 _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var stringLeftRight = require('string-left-right');

return target;
}
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);

function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
var version = "1.2.1";

function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
function isObj(something) {
return something && typeof something === "object" && !Array.isArray(something);
}

function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
function isStr(something) {
return typeof something === "string";
}

function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
var defaults = {
stringOffset: 0,
maxDistance: 1,
ignoreWhitespace: true
};

for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];

return arr2;
}

function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function findMalformed(str, refStr, cb, originalOpts) { //
// insurance
// ---------

function isObj(something) {
return something && _typeof(something) === "object" && !Array.isArray(something);
}
function isStr(something) {
return typeof something === "string";
}
function strFindMalformed(str, refStr, cb, originalOpts) {
if (!isStr(str)) {
throw new TypeError("string-find-malformed: [THROW_ID_01] the first input argument, string where to look for, must be a string! Currently it's equal to: ".concat(str, " (type: ").concat(_typeof(str), ")"));
throw new TypeError("string-find-malformed: [THROW_ID_01] the first input argument, string where to look for, must be a string! Currently it's equal to: " + str + " (type: " + typeof str + ")");
} else if (!str.length) {
// empty string - quick ending
return;
}

if (!isStr(refStr)) {
throw new TypeError("string-find-malformed: [THROW_ID_02] the second input argument, string we should find, must be a string! Currently it's equal to: ".concat(refStr, " (type: ").concat(_typeof(refStr), ")"));
throw new TypeError("string-find-malformed: [THROW_ID_02] the second input argument, string we should find, must be a string! Currently it's equal to: " + refStr + " (type: " + typeof refStr + ")");
} else if (!refStr.length) {
// empty string to look for - quick ending
return;
}

if (typeof cb !== "function") {
throw new TypeError("string-find-malformed: [THROW_ID_03] the third input argument, a callback function, must be a function! Currently it's equal to: ".concat(cb, " (type: ").concat(_typeof(cb), ")"));
throw new TypeError("string-find-malformed: [THROW_ID_03] the third input argument, a callback function, must be a function! Currently it's equal to: " + cb + " (type: " + typeof cb + ")");
}

if (originalOpts && !isObj(originalOpts)) {
throw new TypeError("string-find-malformed: [THROW_ID_04] the fourth input argument, an Optional Options Object, must be a plain object! Currently it's equal to: ".concat(originalOpts, " (type: ").concat(_typeof(originalOpts), ")"));
throw new TypeError("string-find-malformed: [THROW_ID_04] the fourth input argument, an Optional Options Object, must be a plain object! Currently it's equal to: " + originalOpts + " (type: " + typeof originalOpts + ")");
}
var defaults = {
stringOffset: 0,
maxDistance: 1,
ignoreWhitespace: true
};
var opts = _objectSpread2(_objectSpread2({}, defaults), originalOpts);

var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults), originalOpts); // we perform the validation upon Object-assigned "opts" instead
// of incoming "originalOpts" because we don't want to mutate the
// "originalOpts" and making note of fixed values, Object-assigning
// "opts" and then putting those noted fixed values on top is more
// tedious than letting Object-assign to do the job, then validating
// it, then trying to salvage the value (if possible).


if (typeof opts.stringOffset === "string" && /^\d*$/.test(opts.stringOffset)) {
opts.stringOffset = Number(opts.stringOffset);
} else if (!Number.isInteger(opts.stringOffset) || opts.stringOffset < 0) {
throw new TypeError("".concat(opts.source, " [THROW_ID_05] opts.stringOffset must be a natural number or zero! Currently it's: ").concat(opts.fromIndex));
}
var len = str.length;
var len2 = Math.min(refStr.length, opts.maxDistance + 1);
var pendingMatchesArr = [];
var patience = opts.maxDistance;
throw new TypeError("[THROW_ID_05] opts.stringOffset must be a natural number or zero! Currently it's: " + opts.stringOffset);
} //
// action
// ------

var len = str.length; // "current" character (str[i]) is matched against first character
// of "refStr", then, if opts.maxDistance allows and refStr has
// enough length, current character (str[i]) is matched against the
// second character of "refStr" - this time, "patience" is subtracted
// by amount of skipped characters, in this case, by 1... and so on...
// That matching chain is a "for" loop and that loop's length is below:

var len2 = Math.min(refStr.length, (opts.maxDistance || 0) + 1);
var pendingMatchesArr = []; // when it attempts to dip below zero, match is failed

var patience = opts.maxDistance || 1;
var wasThisLetterMatched;

for (var i = 0; i < len; i++) {
//
//
//
//
// THE TOP
// ███████
//
//
//
//
// Logging:
// -------------------------------------------------------------------------

if (opts.ignoreWhitespace && !str[i].trim()) {
continue;
}
} //
//
//
//
// THE MIDDLE
// ██████████
//
//
//
//

for (var z = 0, len3 = pendingMatchesArr.length; z < len3; z++) {
wasThisLetterMatched = false;

if (Array.isArray(pendingMatchesArr[z].pendingToCheck) && pendingMatchesArr[z].pendingToCheck.length && str[i] === pendingMatchesArr[z].pendingToCheck[0]) {
wasThisLetterMatched = true;
wasThisLetterMatched = true; // if matched, shift() it

pendingMatchesArr[z].pendingToCheck.shift();
} else if (Array.isArray(pendingMatchesArr[z].pendingToCheck) && pendingMatchesArr[z].pendingToCheck.length && str[i] === pendingMatchesArr[z].pendingToCheck[1]) {
wasThisLetterMatched = true;
wasThisLetterMatched = true; // if matched, shift() it

pendingMatchesArr[z].pendingToCheck.shift();
pendingMatchesArr[z].pendingToCheck.shift();
pendingMatchesArr[z].patienceLeft -= 1;
pendingMatchesArr[z].patienceLeft -= 1; //
} else {
pendingMatchesArr[z].patienceLeft -= 1;
pendingMatchesArr[z].patienceLeft -= 1; // we look up the next character, if it matches, we don't pop it

if (str[stringLeftRight.right(str, i)] !== pendingMatchesArr[z].pendingToCheck[0]) {
pendingMatchesArr[z].pendingToCheck.shift();
pendingMatchesArr[z].pendingToCheck.shift(); // after popping, match the current character at str[i] is it
// equal to the first element of recently-shifted
// pendingMatchesArr[z].pendingToCheck:

if (str[i] === pendingMatchesArr[z].pendingToCheck[0]) {
pendingMatchesArr[z].pendingToCheck.shift();
}
Expand All @@ -174,26 +145,36 @@ function strFindMalformed(str, refStr, cb, originalOpts) {
}
pendingMatchesArr = pendingMatchesArr.filter(function (obj) {
return obj.patienceLeft >= 0;
});
}); // out of all objects which deplete pendingToCheck[] to zero length,
// we pick the one with the smallest "startsAt" value - that's filtering
// the overlapping values

var tempArr = pendingMatchesArr.filter(function (obj) {
return obj.pendingToCheck.length === 0;
}).map(function (obj) {
return obj.startsAt;
});

if (Array.isArray(tempArr) && tempArr.length) {
var idxFrom = Math.min.apply(Math, _toConsumableArray(tempArr));
var idxFrom = Math.min.apply(Math, tempArr);
var idxTo = i + (wasThisLetterMatched ? 1 : 0);

if (str.slice(idxFrom, idxTo) !== refStr) {
// only ping malformed values, don't ping those exactly matching "refStr"
cb({
idxFrom: idxFrom + opts.stringOffset,
idxTo: idxTo + opts.stringOffset
idxFrom: idxFrom + (opts.stringOffset || 0),
idxTo: idxTo + (opts.stringOffset || 0)
});
}
} // remove pendingMatchesArr[] entries with no characters to check


pendingMatchesArr = pendingMatchesArr.filter(function (obj) {
return obj.pendingToCheck.length;
});
}

for (var y = 0; y < len2; y++) {

if (str[i] === refStr[y]) {
var whatToPush = {
startsAt: i,
Expand All @@ -203,8 +184,20 @@ function strFindMalformed(str, refStr, cb, originalOpts) {
pendingMatchesArr.push(whatToPush);
break;
}
}
} //
//
//
//
// THE BOTTOM
// ██████████
//
//
//
//
// Logging
}
}

module.exports = strFindMalformed;
exports.defaults = defaults;
exports.findMalformed = findMalformed;
exports.version = version;

0 comments on commit ea15d44

Please sign in to comment.