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 rCrop from ..." - now "import { rCrop } from ..."
  • Loading branch information
revelt committed Dec 26, 2020
1 parent 202b763 commit 1db2eff
Show file tree
Hide file tree
Showing 17 changed files with 651 additions and 603 deletions.
3 changes: 2 additions & 1 deletion packages/ranges-crop/.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/ranges-crop/README.md
Expand Up @@ -34,10 +34,10 @@ npm i ranges-crop

```js
import { strict as assert } from "assert";
import crop from "ranges-crop";
import { rCrop } from "ranges-crop";

assert.deepEqual(
crop(
rCrop(
[
[2, 3],
[9, 10, "bad grey wolf"],
Expand Down
2 changes: 1 addition & 1 deletion packages/ranges-crop/coverage/coverage-summary.json
@@ -1 +1 @@
{"total":{"lines":{"total":31,"covered":31,"skipped":0,"pct":100},"statements":{"total":33,"covered":32,"skipped":0,"pct":96.97},"functions":{"total":10,"covered":9,"skipped":0,"pct":90},"branches":{"total":30,"covered":30,"skipped":0,"pct":100}}}
{"total":{"lines":{"total":31,"covered":31,"skipped":0,"pct":100},"statements":{"total":36,"covered":35,"skipped":0,"pct":97.22},"functions":{"total":9,"covered":8,"skipped":0,"pct":88.89},"branches":{"total":36,"covered":34,"skipped":0,"pct":94.44}}}
70 changes: 34 additions & 36 deletions packages/ranges-crop/dist/ranges-crop.cjs.js
Expand Up @@ -9,83 +9,81 @@

'use strict';

var mergeRanges = require('ranges-merge');
Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var rangesMerge = require('ranges-merge');

var mergeRanges__default = /*#__PURE__*/_interopDefaultLegacy(mergeRanges);
var version = "3.0.2";

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;
};
function rCrop(arrOfRanges, strLen) {
if (arrOfRanges === null) {
return null;
}

return _typeof(obj);
}

function isStr(something) {
return typeof something === "string";
}
function rangesCrop(arrOfRanges, strLen) {
if (!Array.isArray(arrOfRanges)) {
throw new TypeError("ranges-crop: [THROW_ID_01] The first input's argument must be an array, consisting of range arrays! Currently its type is: ".concat(_typeof(arrOfRanges), ", equal to: ").concat(JSON.stringify(arrOfRanges, null, 4)));
}
throw new TypeError("ranges-crop: [THROW_ID_01] The first input's argument must be an array, consisting of range arrays! Currently its type is: " + typeof arrOfRanges + ", equal to: " + JSON.stringify(arrOfRanges, null, 4));
} // strLen validation


if (!Number.isInteger(strLen)) {
throw new TypeError("ranges-crop: [THROW_ID_02] The second input's argument must be a natural number or zero (coming from String.length)! Currently its type is: ".concat(_typeof(strLen), ", equal to: ").concat(JSON.stringify(strLen, null, 4)));
throw new TypeError("ranges-crop: [THROW_ID_02] The second input's argument must be a natural number or zero (coming from String.length)! Currently its type is: " + typeof strLen + ", equal to: " + JSON.stringify(strLen, null, 4));
}

if (!arrOfRanges.filter(function (range) {
return range;
}).length) {
return arrOfRanges.filter(function (range) {
return range;
});
}
var culpritsIndex;

var culpritsIndex = 0; // validate are range indexes natural numbers:

if (!arrOfRanges.filter(function (range) {
return range;
}).every(function (rangeArr, indx) {
if (!Number.isInteger(rangeArr[0]) || !Number.isInteger(rangeArr[1])) {
culpritsIndex = indx;
return false;
}

return true;
})) {
if (Array.isArray(arrOfRanges) && typeof arrOfRanges[0] === "number" && typeof arrOfRanges[1] === "number") {
throw new TypeError("ranges-crop: [THROW_ID_03] The first argument should be AN ARRAY OF RANGES, not a single range! Currently arrOfRanges = ".concat(JSON.stringify(arrOfRanges, null, 0), "!"));
throw new TypeError("ranges-crop: [THROW_ID_03] The first argument should be AN ARRAY OF RANGES, not a single range! Currently arrOfRanges = " + JSON.stringify(arrOfRanges, null, 0) + "!");
}
throw new TypeError("ranges-crop: [THROW_ID_04] The first argument should be AN ARRAY OF ARRAYS! Each sub-array means string slice indexes. In our case, here ".concat(culpritsIndex + 1, "th range (").concat(JSON.stringify(arrOfRanges[culpritsIndex], null, 0), ") does not consist of only natural numbers!"));
}

throw new TypeError("ranges-crop: [THROW_ID_04] The first argument should be AN ARRAY OF ARRAYS! Each sub-array means string slice indexes. In our case, here " + culpritsIndex + "th range (" + JSON.stringify(arrOfRanges[culpritsIndex], null, 0) + ") does not consist of only natural numbers!");
} // validate that any third argument values (if any) are of a string-type


if (!arrOfRanges.filter(function (range) {
return range;
}).every(function (rangeArr, indx) {
if (rangeArr[2] != null && !isStr(rangeArr[2])) {
if (rangeArr[2] != null && typeof rangeArr[2] !== "string") {
culpritsIndex = indx;
return false;
}

return true;
})) {
throw new TypeError("ranges-crop: [THROW_ID_05] The third argument, if present at all, should be of a string-type or null. Currently the ".concat(culpritsIndex, "th range ").concat(JSON.stringify(arrOfRanges[culpritsIndex], null, 0), " has a argument in the range of a type ").concat(_typeof(arrOfRanges[culpritsIndex][2])));
}
var res = mergeRanges__default['default'](arrOfRanges).filter(function (singleRangeArr) {
return singleRangeArr[0] <= strLen && (singleRangeArr[2] !== undefined || singleRangeArr[0] < strLen);
throw new TypeError("ranges-crop: [THROW_ID_05] The third argument, if present at all, should be of a string-type or null. Currently the " + culpritsIndex + "th range " + JSON.stringify(arrOfRanges[culpritsIndex], null, 0) + " has a argument in the range of a type " + typeof arrOfRanges[culpritsIndex][2]);
} // finally, the real action
// ---------------------------------------------------------------------------
var res = (rangesMerge.rMerge(arrOfRanges) || []).filter(function (singleRangeArr) {
return singleRangeArr[0] <= strLen && (singleRangeArr[2] != undefined || singleRangeArr[0] < strLen);
}).map(function (singleRangeArr) {
if (singleRangeArr[1] > strLen) {
if (singleRangeArr[2] !== undefined) {

if (singleRangeArr[2] != undefined) {
return [singleRangeArr[0], strLen, singleRangeArr[2]];
}
return [singleRangeArr[0], strLen];
}
return singleRangeArr;
});
return res;
return res === [] ? null : res;
}

module.exports = rangesCrop;
exports.rCrop = rCrop;
exports.version = version;

0 comments on commit 1db2eff

Please sign in to comment.