Skip to content

Commit

Permalink
feat: introduce safeToString
Browse files Browse the repository at this point in the history
Safe toString resolution, especially useful in case of error reporting
  • Loading branch information
medikoo committed May 15, 2017
1 parent d289f52 commit 0cc6a7b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .lint
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ predef+ Float32Array
./object/first-key.js
forin

./test
predef+ Symbol

./test/reg-exp/#/index.js
predef+ __dirname
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict';

module.exports = {
global: require('./global'),
global: require('./global'),
safeToString: require('./safe-to-string'),

array: require('./array'),
boolean: require('./boolean'),
date: require('./date'),
error: require('./error'),
function: require('./function'),
iterable: require('./iterable'),
json: require('./json'),
math: require('./math'),
number: require('./number'),
object: require('./object'),
regExp: require('./reg-exp'),
string: require('./string')
array: require('./array'),
boolean: require('./boolean'),
date: require('./date'),
error: require('./error'),
function: require('./function'),
iterable: require('./iterable'),
json: require('./json'),
math: require('./math'),
number: require('./number'),
object: require('./object'),
regExp: require('./reg-exp'),
string: require('./string')
};
9 changes: 9 additions & 0 deletions safe-to-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function (value) {
try {
return String(value);
} catch (e) {
return "<non-stringifiable value>";
}
};
11 changes: 11 additions & 0 deletions test/safe-to-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

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');
if (typeof Symbol === 'function') a(t(Symbol()), 'Symbol()');
a(t(Object.create(null)), '<non-stringifiable value>');
};

0 comments on commit 0cc6a7b

Please sign in to comment.