Skip to content

Commit

Permalink
feat: rewrite in TS and start using named exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: previously: "import strIndexesOfPlus from ..." - now "import { strIndexesOfPlus }
from ..."
  • Loading branch information
revelt committed Dec 25, 2020
1 parent 0764be7 commit 2bde77a
Show file tree
Hide file tree
Showing 17 changed files with 382 additions and 276 deletions.
3 changes: 2 additions & 1 deletion packages/str-indexes-of-plus/.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
8 changes: 4 additions & 4 deletions packages/str-indexes-of-plus/README.md
Expand Up @@ -34,19 +34,19 @@ npm i str-indexes-of-plus

```js
import { strict as assert } from "assert";
import indx from "str-indexes-of-plus";
import { strIndexesOfPlus } from "str-indexes-of-plus";

// searches for string in a string, returns array:
assert.deepEqual(indx("abc-abc-abc-abc", "abc"), [0, 4, 8, 12]);
assert.deepEqual(strIndexesOfPlus("abc-abc-abc-abc", "abc"), [0, 4, 8, 12]);

// all graphemes are counted as one, emoji too:
assert.deepEqual(
indx("🐴-🦄", "🦄"),
strIndexesOfPlus("🐴-🦄", "🦄"),
[2] // not [3] considering unicorn is 2-characters long
);

// you can offset the start of a search:
assert.deepEqual(indx("abczabc", "abc", 3), [4]);
assert.deepEqual(strIndexesOfPlus("abczabc", "abc", 3), [4]);
```

## Documentation
Expand Down
@@ -1 +1 @@
{"total":{"lines":{"total":34,"covered":34,"skipped":0,"pct":100},"statements":{"total":36,"covered":36,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":35,"covered":35,"skipped":0,"pct":100}}}
{"total":{"lines":{"total":29,"covered":29,"skipped":0,"pct":100},"statements":{"total":31,"covered":31,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":30,"covered":30,"skipped":0,"pct":100}}}
65 changes: 28 additions & 37 deletions packages/str-indexes-of-plus/dist/str-indexes-of-plus.cjs.js
Expand Up @@ -9,66 +9,55 @@

'use strict';

function _typeof(obj) {
"@babel/helpers - typeof";
Object.defineProperty(exports, '__esModule', { value: true });

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);
}
var version = "2.11.0";

function existy(x) {
return x != null;
}
function isStr(something) {
return typeof something === "string";
}
function strIndexesOfPlus(str, searchValue, fromIndex) {
if (arguments.length === 0) {
throw new Error("str-indexes-of-plus/strIndexesOfPlus(): inputs missing!");
}
if (!isStr(str)) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: ".concat(_typeof(str)));
if (fromIndex === void 0) {
fromIndex = 0;
}
if (!isStr(searchValue)) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: ".concat(_typeof(searchValue)));

if (typeof str !== "string") {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: " + typeof str);
}
if (arguments.length >= 3 && !Number.isInteger(fromIndex) && !(isStr(fromIndex) && /^\d*$/.test(fromIndex))) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: ".concat(fromIndex));

if (typeof searchValue !== "string") {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: " + typeof searchValue);
}
if (/^\d*$/.test(fromIndex)) {
fromIndex = Number(fromIndex);

if (isNaN(+fromIndex) || typeof fromIndex === "string" && !/^\d*$/.test(fromIndex)) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: " + fromIndex);
}

var strArr = Array.from(str);
var searchValueArr = Array.from(searchValue);
if (strArr.length === 0 || searchValueArr.length === 0 || existy(fromIndex) && fromIndex >= strArr.length) {

if (strArr.length === 0 || searchValueArr.length === 0 || fromIndex != null && +fromIndex >= strArr.length) {
return [];
}
if (!existy(fromIndex)) {

if (!fromIndex) {
// eslint-disable-next-line no-param-reassign
fromIndex = 0;
}

var res = [];
var matchMode = false;
var potentialFinding;

for (var i = fromIndex, len = strArr.length; i < len; i++) {
if (matchMode) {
if (strArr[i] === searchValueArr[i - potentialFinding]) {
if (i - potentialFinding + 1 === searchValueArr.length) {
res.push(potentialFinding);
if (strArr[i] === searchValueArr[i - +potentialFinding]) {
if (i - +potentialFinding + 1 === searchValueArr.length) {
res.push(+potentialFinding);
}
} else {
potentialFinding = null;
matchMode = false;
}
}

if (!matchMode) {
if (strArr[i] === searchValueArr[0]) {
if (searchValueArr.length === 1) {
Expand All @@ -80,7 +69,9 @@ function strIndexesOfPlus(str, searchValue, fromIndex) {
}
}
}

return res;
}

module.exports = strIndexesOfPlus;
exports.strIndexesOfPlus = strIndexesOfPlus;
exports.version = version;
126 changes: 51 additions & 75 deletions packages/str-indexes-of-plus/dist/str-indexes-of-plus.dev.umd.js
Expand Up @@ -8,100 +8,76 @@
*/

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.strIndexesOfPlus = factory());
}(this, (function () { '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;
};
}
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.strIndexesOfPlus = {}));
}(this, (function (exports) { 'use strict';

return _typeof(obj);
}
var version = "2.11.0";

function existy(x) {
return x != null;
function strIndexesOfPlus(str, searchValue, fromIndex) {
if (fromIndex === void 0) {
fromIndex = 0;
}

function isStr(something) {
return typeof something === "string";
if (typeof str !== "string") {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: " + typeof str);
}

function strIndexesOfPlus(str, searchValue, fromIndex) {
if (arguments.length === 0) {
throw new Error("str-indexes-of-plus/strIndexesOfPlus(): inputs missing!");
}

if (!isStr(str)) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: ".concat(_typeof(str)));
}

if (!isStr(searchValue)) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: ".concat(_typeof(searchValue)));
}

if (arguments.length >= 3 && !Number.isInteger(fromIndex) && !(isStr(fromIndex) && /^\d*$/.test(fromIndex))) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: ".concat(fromIndex));
}
if (typeof searchValue !== "string") {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: " + typeof searchValue);
}

if (/^\d*$/.test(fromIndex)) {
// eslint-disable-next-line no-param-reassign
fromIndex = Number(fromIndex);
}
if (isNaN(+fromIndex) || typeof fromIndex === "string" && !/^\d*$/.test(fromIndex)) {
throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: " + fromIndex);
}

var strArr = Array.from(str);
var searchValueArr = Array.from(searchValue);
var strArr = Array.from(str);
var searchValueArr = Array.from(searchValue);

if (strArr.length === 0 || searchValueArr.length === 0 || existy(fromIndex) && fromIndex >= strArr.length) {
return [];
}
if (strArr.length === 0 || searchValueArr.length === 0 || fromIndex != null && +fromIndex >= strArr.length) {
return [];
}

if (!existy(fromIndex)) {
// eslint-disable-next-line no-param-reassign
fromIndex = 0;
}
if (!fromIndex) {
// eslint-disable-next-line no-param-reassign
fromIndex = 0;
}

var res = [];
var matchMode = false;
var potentialFinding;
var res = [];
var matchMode = false;
var potentialFinding;

for (var i = fromIndex, len = strArr.length; i < len; i++) {
if (matchMode) {
if (strArr[i] === searchValueArr[i - potentialFinding]) {
if (i - potentialFinding + 1 === searchValueArr.length) {
res.push(potentialFinding);
}
} else {
potentialFinding = null;
matchMode = false;
for (var i = fromIndex, len = strArr.length; i < len; i++) {
if (matchMode) {
if (strArr[i] === searchValueArr[i - +potentialFinding]) {
if (i - +potentialFinding + 1 === searchValueArr.length) {
res.push(+potentialFinding);
}
} else {
potentialFinding = null;
matchMode = false;
}
}

if (!matchMode) {
if (strArr[i] === searchValueArr[0]) {
if (searchValueArr.length === 1) {
res.push(i);
} else {
matchMode = true;
potentialFinding = i;
}
if (!matchMode) {
if (strArr[i] === searchValueArr[0]) {
if (searchValueArr.length === 1) {
res.push(i);
} else {
matchMode = true;
potentialFinding = i;
}
}
}

return res;
}

return strIndexesOfPlus;
return res;
}

exports.strIndexesOfPlus = strIndexesOfPlus;
exports.version = version;

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

})));
64 changes: 25 additions & 39 deletions packages/str-indexes-of-plus/dist/str-indexes-of-plus.esm.js
Expand Up @@ -7,64 +7,49 @@
* Homepage: https://codsen.com/os/str-indexes-of-plus/
*/

function existy(x) {
return x != null;
}
function isStr(something) {
return typeof something === "string";
}
function strIndexesOfPlus(str, searchValue, fromIndex) {
if (arguments.length === 0) {
throw new Error("str-indexes-of-plus/strIndexesOfPlus(): inputs missing!");
}
if (!isStr(str)) {
throw new TypeError(
`str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: ${typeof str}`
);
}
if (!isStr(searchValue)) {
throw new TypeError(
`str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: ${typeof searchValue}`
);
var version = "2.11.0";

function strIndexesOfPlus(str, searchValue, fromIndex = 0) {
if (typeof str !== "string") {
throw new TypeError(`str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: ${typeof str}`);
}
if (
arguments.length >= 3 &&
!Number.isInteger(fromIndex) &&
!(isStr(fromIndex) && /^\d*$/.test(fromIndex))
) {
throw new TypeError(
`str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: ${fromIndex}`
);

if (typeof searchValue !== "string") {
throw new TypeError(`str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: ${typeof searchValue}`);
}
if (/^\d*$/.test(fromIndex)) {
fromIndex = Number(fromIndex);

if (isNaN(+fromIndex) || typeof fromIndex === "string" && !/^\d*$/.test(fromIndex)) {
throw new TypeError(`str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: ${fromIndex}`);
}

const strArr = Array.from(str);
const searchValueArr = Array.from(searchValue);
if (
strArr.length === 0 ||
searchValueArr.length === 0 ||
(existy(fromIndex) && fromIndex >= strArr.length)
) {

if (strArr.length === 0 || searchValueArr.length === 0 || fromIndex != null && +fromIndex >= strArr.length) {
return [];
}
if (!existy(fromIndex)) {

if (!fromIndex) {
// eslint-disable-next-line no-param-reassign
fromIndex = 0;
}

const res = [];
let matchMode = false;
let potentialFinding;

for (let i = fromIndex, len = strArr.length; i < len; i++) {
if (matchMode) {
if (strArr[i] === searchValueArr[i - potentialFinding]) {
if (i - potentialFinding + 1 === searchValueArr.length) {
res.push(potentialFinding);
if (strArr[i] === searchValueArr[i - +potentialFinding]) {
if (i - +potentialFinding + 1 === searchValueArr.length) {
res.push(+potentialFinding);
}
} else {
potentialFinding = null;
matchMode = false;
}
}

if (!matchMode) {
if (strArr[i] === searchValueArr[0]) {
if (searchValueArr.length === 1) {
Expand All @@ -76,7 +61,8 @@ function strIndexesOfPlus(str, searchValue, fromIndex) {
}
}
}

return res;
}

export default strIndexesOfPlus;
export { strIndexesOfPlus, version };

0 comments on commit 2bde77a

Please sign in to comment.