Skip to content

Commit

Permalink
feat: rewrite in TS, start using named exports, stop producing UMD bu…
Browse files Browse the repository at this point in the history
…ilds

BREAKING CHANGE: previously: "import parseTap from ..." - now "import { parseTap } from ..."
  • Loading branch information
revelt committed Jan 1, 2021
1 parent 3a3ee88 commit 6d54b6b
Show file tree
Hide file tree
Showing 20 changed files with 505 additions and 12,768 deletions.
3 changes: 2 additions & 1 deletion packages/tap-parse-string-to-object/.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
6 changes: 3 additions & 3 deletions packages/tap-parse-string-to-object/README.md
Expand Up @@ -34,11 +34,11 @@ npm i tap-parse-string-to-object

```js
import { strict as assert } from "assert";
import parseRawTap from "tap-parse-string-to-object";
import { parseTap } from "tap-parse-string-to-object";

// synchronous parsing (there's also async, see in examples)
assert.deepEqual(
parseRawTap(`TAP version 13
parseTap(`TAP version 13
ok 1 - test/test.js # time=22.582ms { # Subtest: 01.01 - string input
ok 1 - 01.01.01
ok 2 - 01.01.02
Expand Down Expand Up @@ -89,6 +89,6 @@ Please [visit codsen.com](https://codsen.com/os/tap-parse-string-to-object/) for

MIT License

Copyright (c) 2010-2020 Roy Revelt and other contributors
Copyright (c) 2010-2021 Roy Revelt and other contributors

<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 +1 @@
{"total":{"lines":{"total":74,"covered":68,"skipped":0,"pct":91.89},"statements":{"total":76,"covered":70,"skipped":0,"pct":92.11},"functions":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":67,"covered":61,"skipped":0,"pct":91.04}}}
{"total":{"lines":{"total":75,"covered":69,"skipped":0,"pct":92},"statements":{"total":77,"covered":71,"skipped":0,"pct":92.21},"functions":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":67,"covered":61,"skipped":0,"pct":91.04}}}
Expand Up @@ -9,107 +9,51 @@

'use strict';

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

var isStream = require('isstream');
var split2 = require('split2');
var through2 = require('through2');
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');

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

var isStream__default = /*#__PURE__*/_interopDefaultLegacy(isStream);
var split2__default = /*#__PURE__*/_interopDefaultLegacy(split2);
var through2__default = /*#__PURE__*/_interopDefaultLegacy(through2);
var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}

function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}

function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}

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

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

return target;
}

// pings each line to the callback cb()
function stringPingLineByLine(str, cb) {
// console.log(
// `004 ${`\u001b[${33}m${`█`}\u001b[${39}m`} stringPingLineByLine() called!`
// );
var start = null;

for (var i = 0, len = str.length; i < len; i++) {
if (["\n", "\r"].includes(str[i])) {
if (start !== null) {
cb(str.slice(start, i));
start = null;
start = null; // console.log(
// `020 ${`\u001b[${33}m${`█`}\u001b[${39}m`} ${`\u001b[${32}m${`SET `}\u001b[${39}m`} start = null`
// );
}
}
} // not a linebreak character
else if (start === null) {
start = i;
}
start = i; // console.log(
// `028 ${`\u001b[${33}m${`█`}\u001b[${39}m`} ${`\u001b[${32}m${`SET `}\u001b[${39}m`} start = ${start}`
// );
} // if an end is reached, ping the remainder


if (start !== null && !str[i + 1]) {
cb(str.slice(start, i + 1));
}
}
}
var Counter = function () {

var Counter = /*#__PURE__*/function () {
function Counter() {
_classCallCheck(this, Counter);
this.canCount = false;
this.doNothing = false;
this.thereWereFailuresInThisSuite = null;
Expand All @@ -123,85 +67,120 @@ var Counter = function () {
suitesFailed: 0
};
}
_createClass(Counter, [{
key: "readLine",
value: function readLine(lineStr) {
if (!this.doNothing && lineStr.trim() === "---") {
this.doNothing = true;
}
if (this.doNothing && lineStr.trim() === "...") {
this.doNothing = false;
}
else if (!this.doNothing && this.canCount) {
if (lineStr.trim().startsWith("ok") || lineStr.trim().startsWith("not ok")) {
if (lineStr.trim().startsWith("ok")) {
this.total.assertsPassed += 1;
} else if (lineStr.trim().startsWith("not ok")) {
this.total.assertsFailed += 1;
if (!this.thereWereFailuresInThisSuite) {
this.thereWereFailuresInThisSuite = true;
}

var _proto = Counter.prototype;

_proto.readLine = function readLine(lineStr) { // catch the --- to ...

if (!this.doNothing && lineStr.trim() === "---") {
this.doNothing = true; // this.canCount = false;
}

if (this.doNothing && lineStr.trim() === "...") {
this.doNothing = false;
} // catch the assertion result lines
else if (!this.doNothing && this.canCount) {
if (lineStr.trim().startsWith("ok") || lineStr.trim().startsWith("not ok")) {
if (lineStr.trim().startsWith("ok")) {
this.total.assertsPassed += 1;
} else if (lineStr.trim().startsWith("not ok")) {
this.total.assertsFailed += 1;

if (!this.thereWereFailuresInThisSuite) {
this.thereWereFailuresInThisSuite = true;
}
this.total.assertsTotal += 1;
} else {
this.canCount = false;
}

this.total.assertsTotal += 1;
} else {
this.canCount = false;
}
if (!this.doNothing && lineStr.trim() === "{") {
this.total.suitesTotal += 1;
if (this.thereWereFailuresInThisSuite !== null) {
if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed += 1;
} else {
this.total.suitesPassed += 1;
}
} // if { is on a separate line, bump the suite count and reset the this.thereWereFailuresInThisSuite


if (!this.doNothing && lineStr.trim() === "{") {
this.total.suitesTotal += 1;

if (this.thereWereFailuresInThisSuite !== null) {
// second suite onwards already has a gathered result
if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed += 1;
} else {
this.total.suitesPassed += 1;
}
this.thereWereFailuresInThisSuite = false;
}
var magicKeyw = "# Subtest";
if (!this.doNothing && !this.canCount && lineStr.includes(magicKeyw)) {
this.canCount = true;
if (lineStr.slice(0, lineStr.indexOf(magicKeyw)).trim().endsWith("{")) {
this.total.suitesTotal += 1;
if (this.thereWereFailuresInThisSuite === null) {
this.thereWereFailuresInThisSuite = false;
} else if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed += 1;
this.thereWereFailuresInThisSuite = false;
} else {
this.total.suitesPassed += 1;
}
} // reset:


this.thereWereFailuresInThisSuite = false;
} // "# Subtest" opens the gates


var magicKeyw = "# Subtest";

if (!this.doNothing && !this.canCount && lineStr.includes(magicKeyw)) {
this.canCount = true; // if suite's opening curlie is on the same line, for example:
//
// ok 1 - test/test.js # time=22.582ms { # Subtest: ...
//
// then bump the suite count

if (lineStr.slice(0, lineStr.indexOf(magicKeyw)).trim().endsWith("{")) {
this.total.suitesTotal += 1; // we must skip the first opening curlies and count suite passing
// for all others

if (this.thereWereFailuresInThisSuite === null) {
// if it's first suite's opening curlie
this.thereWereFailuresInThisSuite = false;
} else if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed += 1;
this.thereWereFailuresInThisSuite = false;
} else {
this.total.suitesPassed += 1;
}
}
}
}, {
key: "getTotal",
value: function getTotal() {
};

_proto.getTotal = function getTotal() {
if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed += 1;
this.thereWereFailuresInThisSuite = false;
} else if (this.total.suitesTotal) {
this.total.suitesPassed += 1;
}

if (!this.total.suitesTotal && this.total.assertsTotal) {
this.total.suitesTotal = 1;

if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed += 1;
this.thereWereFailuresInThisSuite = false;
} else if (this.total.suitesTotal) {
this.total.suitesPassed += 1;
this.total.suitesFailed = 1;
} else {
this.total.suitesPassed = 1;
}
if (!this.total.suitesTotal && this.total.assertsTotal) {
this.total.suitesTotal = 1;
if (this.thereWereFailuresInThisSuite) {
this.total.suitesFailed = 1;
} else {
this.total.suitesPassed = 1;
}
}
return _objectSpread2({}, this.total);
}
}]);
return _objectSpread__default['default']({}, this.total);
};

return Counter;
}();

function externalApi(something) {
var version = "1.3.1";

/* eslint @typescript-eslint/explicit-module-boundary-types: 0 */

function parseTap(something) {

if (isStream__default['default'](something)) {
return new Promise(function (resolve, reject) {
var counter = new Counter();
something.pipe(split2__default['default']()).pipe(through2__default['default'].obj(function (line, encoding, next) {
something.pipe(split2__default['default']()).pipe(through2__default['default'].obj(function (line, _encoding, next) {
// console.log(
// `${`\u001b[${33}m${`line`}\u001b[${39}m`} = ${JSON.stringify(
// line,
// null,
// 4
// )}`
// );
counter.readLine(line);
next();
}));
Expand All @@ -210,8 +189,8 @@ function externalApi(something) {
});
something.on("error", reject);
});
}
if (typeof something === "string") {
} else if (typeof something === "string") {

if (!something.length) {
return {
ok: true,
Expand All @@ -222,14 +201,19 @@ function externalApi(something) {
suitesPassed: 0,
suitesFailed: 0
};
}
} // in which case, synchronously traverse the string and slice and ping line by
// line


var counter = new Counter();
stringPingLineByLine(something, function (line) {
counter.readLine(line);
});
return counter.getTotal();
}

throw new Error("tap-parse-string-to-object: [THROW_ID_01] inputs should be either string or stream");
}

module.exports = externalApi;
exports.parseTap = parseTap;
exports.version = version;

0 comments on commit 6d54b6b

Please sign in to comment.