Skip to content

Commit

Permalink
tweaking/improving _inspect() output of several of the monads
Browse files Browse the repository at this point in the history
  • Loading branch information
getify committed Jul 14, 2020
1 parent 67cb5f1 commit 4021e0e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"io",
"promise"
],
"version": "0.2.0",
"version": "0.2.1",
"main": "src/index.js",
"scripts": {
"build": "mkdir -p dist && node_modules/.bin/mz -rube",
Expand Down
11 changes: 4 additions & 7 deletions src/either.js
@@ -1,5 +1,7 @@
"use strict";

var Just = require("./just.js");

var brand = {};

Left.is = LeftIs;
Expand Down Expand Up @@ -66,13 +68,8 @@ function LeftOrRight(val,isRight = true) {
}

function _inspect() {
return `${publicAPI[Symbol.toStringTag]}(${
typeof val == "string" ? JSON.stringify(val) :
typeof val == "undefined" ? "" :
typeof val == "function" ? (val.name || "anonymous function") :
val && typeof val._inspect == "function" ? val._inspect() :
val
})`;
var v = Just(val)._inspect().match(/^Just\((.*)\)$/)[1];
return `${publicAPI[Symbol.toStringTag]}(${ v })`;
}

function _is(br) {
Expand Down
13 changes: 9 additions & 4 deletions src/just.js
Expand Up @@ -37,13 +37,18 @@ function Just(val) {
}

function _inspect() {
return `${publicAPI[Symbol.toStringTag]}(${
typeof val == "string" ? JSON.stringify(val) :
return `${publicAPI[Symbol.toStringTag]}(${ _serialize(val) })`;
}

function _serialize(val) {
return (
typeof val == "string" ? `"${ val }"` :
typeof val == "undefined" ? "" :
typeof val == "function" ? (val.name || "anonymous function") :
val && typeof val._inspect == "function" ? val._inspect() :
val
})`;
Array.isArray(val) ? `[${ val.map(v => v == null ? String(v) : _serialize(v)) }]` :
String(val)
);
}

function _is(br) {
Expand Down
11 changes: 2 additions & 9 deletions src/maybe.js
Expand Up @@ -75,15 +75,8 @@ function Maybe(val) {
}

function _inspect() {
return `${publicAPI[Symbol.toStringTag]}(${
isJust ? (
typeof val == "string" ? JSON.stringify(val) :
typeof val == "undefined" ? "" :
typeof val == "function" ? (val.name || "anonymous function") :
val && typeof val._inspect == "function" ? val._inspect() :
val
) : ""
})`;
var v = isJust ? mn._inspect().match(/^Just\((.*)\)$/)[1] : "";
return `${publicAPI[Symbol.toStringTag]}(${ v })`;
}

function _is(br) {
Expand Down

0 comments on commit 4021e0e

Please sign in to comment.