Skip to content

Commit

Permalink
feat: toShortStringRepresentation util
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 18, 2017
1 parent c0c69a6 commit 6842d06
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
global: require("./global"),
optionalChaining: require("./optional-chaining"),
safeToString: require("./safe-to-string"),
toShortStringRepresentation: require("./to-short-string-representation"),

array: require("./array"),
boolean: require("./boolean"),
Expand Down
23 changes: 23 additions & 0 deletions test/to-short-string-representation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

var repeat = require("../string/#/repeat");

module.exports = function (t, a) {
a(t(), "undefined");
a(t(null), "null");
a(t(10), "10");
a(t("str"), "str");
a(
t({
toString: function () {
return "miszka";
}
}),
"miszka"
);
// eslint-disable-next-line symbol-description
if (typeof Symbol === "function") a(t(Symbol()), "Symbol()");
a(t(Object.create(null)), "<non-stringifiable value>");
a(t(repeat.call("a", 300)), repeat.call("a", 99) + "…");
a(t("mar\ntoo\nfar"), "mar too far");
};
14 changes: 14 additions & 0 deletions to-short-string-representation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

var safeToString = require("./safe-to-string");

var reNewLine = /[\n\r\u2028\u2029]/g;

module.exports = function (value) {
var string = safeToString(value);
// Trim if too long
if (string.length > 100) string = string.slice(0, 99) + "…";
// Replace eventual new lines
string = string.replace(reNewLine, " ");
return string;
};

0 comments on commit 6842d06

Please sign in to comment.