Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ES modules #23

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module-type defaults to esm as of sanctuary-js/sanctuary-scripts#56.

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"
},
Comment on lines +11 to +14
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://nodejs.org/api/packages.html#package-entry-points

I want to continue to support importing from sanctuary-show/package.json as I consider this part of the public API.

"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