Skip to content

Commit

Permalink
Merge pull request #23 from sanctuary-js/davidchambers/esm
Browse files Browse the repository at this point in the history
use ES modules
  • Loading branch information
davidchambers authored Dec 12, 2023
2 parents 4cc1515 + e736c23 commit cbae402
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 180 deletions.
1 change: 0 additions & 1 deletion .config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ repo-owner = sanctuary-js
repo-name = sanctuary-show
contributing-file = .github/CONTRIBUTING.md
heading-level = 3
module-type = commonjs
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"root": true,
"extends": ["./node_modules/sanctuary-style/eslint.json"],
"parserOptions": {"sourceType": "module"},
"overrides": [
{
"files": ["*.md"],
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/.nyc_output/
/coverage/
/node_modules/
311 changes: 146 additions & 165 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,171 +30,152 @@
//. };
//. ```

(f => {
export {show as default};

'use strict';
// $$show :: String
const $$show = '@@show';

/* istanbul ignore else */
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = f ();
} else if (typeof define === 'function' && define.amd != null) {
define ([], f);
} else {
self.sanctuaryShow = f ();
}
// seen :: Array Any
const seen = [];

// entry :: Object -> String -> String
const entry = o => k => show (k) + ': ' + show (o[k]);

// sortedKeys :: Object -> Array String
const sortedKeys = o => (Object.keys (o)).sort ();

//# show :: Showable a => a -> String
//.
//. Returns a useful string representation of the given value.
//.
//. Dispatches to the value's `@@show` method if present.
//.
//. Where practical, `show (eval ('(' + show (x) + ')')) = show (x)`.
//.
//. ```javascript
//. > show (null)
//. 'null'
//.
//. > show (undefined)
//. 'undefined'
//.
//. > show (true)
//. 'true'
//.
//. > show (new Boolean (false))
//. 'new Boolean (false)'
//.
//. > show (-0)
//. '-0'
//.
//. > show (NaN)
//. 'NaN'
//.
//. > show (new Number (Infinity))
//. 'new Number (Infinity)'
//.
//. > show ('foo\n"bar"\nbaz\n')
//. '"foo\\n\\"bar\\"\\nbaz\\n"'
//.
//. > show (new String (''))
//. 'new String ("")'
//.
//. > show (['foo', 'bar', 'baz'])
//. '["foo", "bar", "baz"]'
//.
//. > show ([[[[[0]]]]])
//. '[[[[[0]]]]]'
//.
//. > show ({x: [1, 2], y: [3, 4], z: [5, 6]})
//. '{"x": [1, 2], "y": [3, 4], "z": [5, 6]}'
//. ```
const show = x => {
if (seen.indexOf (x) >= 0) return '<Circular>';

const repr = Object.prototype.toString.call (x);

switch (repr) {

case '[object Null]':
return 'null';

case '[object Undefined]':
return 'undefined';

case '[object Boolean]':
return typeof x === 'object' ?
'new Boolean (' + show (x.valueOf ()) + ')' :
x.toString ();

case '[object Number]':
return typeof x === 'object' ?
'new Number (' + show (x.valueOf ()) + ')' :
1 / x === -Infinity ? '-0' : x.toString (10);

case '[object String]':
return typeof x === 'object' ?
'new String (' + show (x.valueOf ()) + ')' :
JSON.stringify (x);

case '[object RegExp]':
return x.toString ();

case '[object Date]':
return 'new Date (' +
show (isNaN (x.valueOf ()) ? NaN : x.toISOString ()) +
')';

case '[object Error]':
return 'new ' + x.name + ' (' + show (x.message) + ')';

case '[object Arguments]':
return 'function () { return arguments; } (' +
(Array.prototype.map.call (x, show)).join (', ') +
')';

case '[object Array]':
seen.push (x);
try {
return '[' + ((x.map (show)).concat (
sortedKeys (x)
.filter (k => !(/^\d+$/.test (k)))
.map (entry (x))
)).join (', ') + ']';
} finally {
seen.pop ();
}

case '[object Object]':
seen.push (x);
try {
return (
$$show in x &&
(x.constructor == null || x.constructor.prototype !== x) ?
x[$$show] () :
'{' + ((sortedKeys (x)).map (entry (x))).join (', ') + '}'
);
} finally {
seen.pop ();
}

case '[object Set]':
seen.push (x);
try {
return 'new Set (' + show (Array.from (x.values ())) + ')';
} finally {
seen.pop ();
}

case '[object Map]':
seen.push (x);
try {
return 'new Map (' + show (Array.from (x.entries ())) + ')';
} finally {
seen.pop ();
}

default:
return repr.replace (/^\[(.*)\]$/, '<$1>');

}) (() => {

'use strict';

// $$show :: String
const $$show = '@@show';

// seen :: Array Any
const seen = [];

// entry :: Object -> String -> String
const entry = o => k => show (k) + ': ' + show (o[k]);

// sortedKeys :: Object -> Array String
const sortedKeys = o => (Object.keys (o)).sort ();

//# show :: Showable a => a -> String
//.
//. Returns a useful string representation of the given value.
//.
//. Dispatches to the value's `@@show` method if present.
//.
//. Where practical, `show (eval ('(' + show (x) + ')')) = show (x)`.
//.
//. ```javascript
//. > show (null)
//. 'null'
//.
//. > show (undefined)
//. 'undefined'
//.
//. > show (true)
//. 'true'
//.
//. > show (new Boolean (false))
//. 'new Boolean (false)'
//.
//. > show (-0)
//. '-0'
//.
//. > show (NaN)
//. 'NaN'
//.
//. > show (new Number (Infinity))
//. 'new Number (Infinity)'
//.
//. > show ('foo\n"bar"\nbaz\n')
//. '"foo\\n\\"bar\\"\\nbaz\\n"'
//.
//. > show (new String (''))
//. 'new String ("")'
//.
//. > show (['foo', 'bar', 'baz'])
//. '["foo", "bar", "baz"]'
//.
//. > show ([[[[[0]]]]])
//. '[[[[[0]]]]]'
//.
//. > show ({x: [1, 2], y: [3, 4], z: [5, 6]})
//. '{"x": [1, 2], "y": [3, 4], "z": [5, 6]}'
//. ```
const show = x => {
if (seen.indexOf (x) >= 0) return '<Circular>';

const repr = Object.prototype.toString.call (x);

switch (repr) {

case '[object Null]':
return 'null';

case '[object Undefined]':
return 'undefined';

case '[object Boolean]':
return typeof x === 'object' ?
'new Boolean (' + show (x.valueOf ()) + ')' :
x.toString ();

case '[object Number]':
return typeof x === 'object' ?
'new Number (' + show (x.valueOf ()) + ')' :
1 / x === -Infinity ? '-0' : x.toString (10);

case '[object String]':
return typeof x === 'object' ?
'new String (' + show (x.valueOf ()) + ')' :
JSON.stringify (x);

case '[object RegExp]':
return x.toString ();

case '[object Date]':
return 'new Date (' +
show (isNaN (x.valueOf ()) ? NaN : x.toISOString ()) +
')';

case '[object Error]':
return 'new ' + x.name + ' (' + show (x.message) + ')';

case '[object Arguments]':
return 'function () { return arguments; } (' +
(Array.prototype.map.call (x, show)).join (', ') +
')';

case '[object Array]':
seen.push (x);
try {
return '[' + ((x.map (show)).concat (
sortedKeys (x)
.filter (k => !(/^\d+$/.test (k)))
.map (entry (x))
)).join (', ') + ']';
} finally {
seen.pop ();
}

case '[object Object]':
seen.push (x);
try {
return (
$$show in x &&
(x.constructor == null || x.constructor.prototype !== x) ?
x[$$show] () :
'{' + ((sortedKeys (x)).map (entry (x))).join (', ') + '}'
);
} finally {
seen.pop ();
}

case '[object Set]':
seen.push (x);
try {
return 'new Set (' + show (Array.from (x.values ())) + ')';
} finally {
seen.pop ();
}

case '[object Map]':
seen.push (x);
try {
return 'new Map (' + show (Array.from (x.entries ())) + ')';
} finally {
seen.pop ();
}

default:
return repr.replace (/^\[(.*)\]$/, '<$1>');

}
};

return show;

});
}
};
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"type": "git",
"url": "git://github.com/sanctuary-js/sanctuary-show.git"
},
"type": "module",
"exports": {
".": "./index.js",
"./package.json": "./package.json"
},
"files": [
"/LICENSE",
"/README.md",
Expand All @@ -15,15 +20,14 @@
],
"dependencies": {},
"devDependencies": {
"c8": "8.0.x",
"oletus": "4.0.x",
"sanctuary-scripts": "6.0.x"
},
"scripts": {
"doctest": "sanctuary-doctest",
"lint": "sanctuary-lint",
"release": "sanctuary-release",
"test": "npm run lint && sanctuary-test && npm run doctest"
},
"mocha": {
"ui": "tdd"
}
}
10 changes: 10 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euf -o pipefail

node_modules/.bin/c8 \
--check-coverage \
--100 \
--reporter text \
--reporter html \
--include index.js \
node_modules/.bin/oletus -- test/index.js
Loading

0 comments on commit cbae402

Please sign in to comment.