Skip to content

Commit 0339336

Browse files
committed
feature: @cloudcmd/modal: migrate to ESM
1 parent 2b61857 commit 0339336

16 files changed

Lines changed: 167 additions & 275 deletions

‎.eslintrc.json‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎.madrun.js‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
'use strict';
1+
import {run, cutEnv} from 'madrun';
22

3-
const {run} = require('madrun');
3+
const env = {
4+
NODE_OPTIONS: '"--import supertape/css"',
5+
};
46

5-
module.exports = {
7+
export default {
68
'watch': () => 'nodemon --watch lib --exec',
79
'watch:client': () => run('compile:client', '--watch'),
810
'watch:test': () => run('watch', 'npm test'),
911
'watch:lint': async () => await run('watch', `'npm run lint'`),
1012
'watch:lint:js': () => run('watch', '"run lint:js"'),
1113
'watch:coverage': () => run('watch', 'redrun coverage'),
12-
'coverage': () => 'c8 npm test',
13-
'report': () => 'c8 report --reporter=text-lcov | coveralls',
14+
'coverage': async () => [env, `c8 ${await cutEnv('test')}`],
15+
'report': () => 'c8 report --reporter=lcov',
1416
'lint': () => 'putout .',
1517
'fix:lint': async () => await run('lint', '--fix'),
16-
'test': () => `tape 'lib/**/*.spec.js'`,
17-
'test:update': () => 'UPDATE_FIXTURE=1 redrun test',
18+
'test': () => [env, `tape 'lib/**/*.spec.js'`],
19+
'test:update': async () => [
20+
await cutEnv('redrun test'),{
21+
...env,
22+
UPDATE_FIXTURE: 1,
23+
}],
1824
};

‎.nycrc.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"check-coverage": true,
3+
"all": true,
4+
"exclude": [
5+
"**/*.spec.*",
6+
"**/*.config.*",
7+
".*"
8+
],
9+
"branches": 100,
10+
"lines": 100,
11+
"functions": 100,
12+
"statements": 100
13+
}

‎README.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ Using build file:
5555

5656
```js
5757
const el = document.createElement('div');
58-
el.textContent = 'hello';
5958

59+
el.textContent = 'hello';
6060
// simplest possible modal open using existing html element
6161
modal.open(el);
6262
modal.close();
63-
6463
// usage with title and hooks
6564
modal.open(el, {
6665
title: 'hello world',
@@ -101,4 +100,3 @@ modal.close();
101100
# License
102101

103102
MIT
104-

‎eslint.config.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {safeAlign} from 'eslint-plugin-putout';
2+
3+
export default safeAlign;

‎lib/get-class-name.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
module.exports = ({autoSize, isTitle}) => {
1+
export default ({autoSize, isTitle}) => {
42
const classNames = ['modal-main'];
53

64
if (autoSize)

‎lib/get-class-name.spec.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const test = require('supertape');
4-
const getClassName = require('./get-class-name');
1+
import test from 'supertape';
2+
import getClassName from './get-class-name.js';
53

64
test('get-class-name: no', (t) => {
75
const result = getClassName({});

‎lib/modal.js‎

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
'use strict';
2-
3-
const store = require('fullstore');
4-
const currify = require('currify');
5-
const createElement = require('@cloudcmd/create-element');
6-
7-
require('../css/modal.css');
8-
const parseImages = require('./parse-images');
9-
const showTitle = require('./show-title');
10-
const query = require('./query');
11-
const getClassName = require('./get-class-name');
1+
import store from 'fullstore';
2+
import currify from 'currify';
3+
import createElement from '@cloudcmd/create-element';
4+
import '../css/modal.css';
5+
import _parseImages from './parse-images.js';
6+
import _showTitle from './show-title.js';
7+
import _query from './query.js';
8+
import getClassName from './get-class-name.js';
129

1310
const noop = () => {};
1411

@@ -20,17 +17,14 @@ const innerHTML = `
2017
</div>
2118
`;
2219

23-
const addEvent = currify((el, fn, name) => query(el).addEventListener(name, fn));
24-
const addAllEvents = (el, fn, names) => names.map(addEvent(el, fn));
20+
const addEvent = currify((el, fn, query, name) => query(el).addEventListener(name, fn));
21+
const addAllEvents = (el, fn, query, names) => names.map(addEvent(el, fn, query));
2522

2623
const {isArray} = Array;
2724

28-
module.exports.open = (inner, options = {}) => {
25+
export const open = (inner, options = {}) => {
2926
check(inner);
3027

31-
if (query('main'))
32-
return;
33-
3428
const {
3529
beforeShow = noop,
3630
beforeClose = noop,
@@ -41,8 +35,14 @@ module.exports.open = (inner, options = {}) => {
4135
index = 0,
4236
helpers = {},
4337
title = '',
38+
query = _query,
39+
showTitle = _showTitle,
40+
parseImages = _parseImages,
4441
} = options;
4542

43+
if (query('main'))
44+
return;
45+
4646
optionsStore({
4747
beforeClose,
4848
afterClose,
@@ -64,7 +64,7 @@ module.exports.open = (inner, options = {}) => {
6464
dataName: 'modal-main',
6565
});
6666

67-
addAllEvents('main', onMainClick(onOverlayClick), ['click', 'contextmenu']);
67+
addAllEvents('main', onMainClick(onOverlayClick), query, ['click', 'contextmenu']);
6868

6969
query('close').addEventListener('click', close);
7070

@@ -87,15 +87,19 @@ module.exports.open = (inner, options = {}) => {
8787
return el;
8888
};
8989

90-
const onMainClick = currify((overlayClick, event) => {
90+
const onMainClick = (overlayClick, overrides = {}) => (event) => {
91+
const {query = _query} = overrides;
92+
9193
if (event.target !== query('main'))
9294
return;
9395

9496
close(event);
9597
overlayClick(event);
96-
});
98+
};
9799

98-
const close = (event) => {
100+
export const close = (event, overrides = {}) => {
101+
const {query = _query} = overrides;
102+
99103
if (event)
100104
event.stopPropagation();
101105

@@ -116,10 +120,8 @@ const close = (event) => {
116120
afterClose();
117121
};
118122

119-
module.exports.close = close;
120-
121-
module.exports._optionsStore = optionsStore;
122-
module.exports._onMainClick = onMainClick;
123+
export const _optionsStore = optionsStore;
124+
export const _onMainClick = onMainClick;
123125

124126
function check(inner) {
125127
if (!inner)

‎lib/modal.spec.js‎

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
'use strict';
2-
3-
require('css-modules-require-hook/preset');
1+
import {createRequire} from 'node:module';
42

3+
const require = createRequire(import.meta.url);
54
const {tryCatch} = require('try-catch');
65

7-
const mockRequire = require('mock-require');
8-
96
const autoGlobals = require('auto-globals');
10-
const {test: tape, stub}= require('supertape');
7+
const {test: tape, stub} = require('supertape');
118

129
const {
1310
open,
@@ -17,21 +14,15 @@ const {
1714
} = require('./modal');
1815

1916
const {create} = autoGlobals;
20-
21-
const {reRequire, stopAll} = mockRequire;
22-
2317
const test = autoGlobals(tape);
2418

2519
test('modal: open: main exist', (t) => {
2620
const el = {};
2721
const query = stub().returns(el);
2822

29-
mockRequire('./query', query);
30-
31-
const {open} = mockRequire.reRequire('./modal');
32-
33-
open({});
34-
stopAll();
23+
open({}, {
24+
query,
25+
});
3526

3627
t.calledWith(query, ['main'], 'should call querySelector');
3728
t.end();
@@ -49,16 +40,11 @@ test('modal: open: beforeShow', (t) => {
4940
called = true;
5041
};
5142

52-
mockRequire('./query', query);
53-
54-
const {open} = reRequire('./modal');
55-
5643
open({}, {
5744
beforeShow,
45+
query,
5846
});
5947

60-
stopAll();
61-
6248
t.calledWithNoArgs(beforeShow, 'should call querySelector');
6349
t.end();
6450
});
@@ -77,24 +63,16 @@ test('modal: open: showTitle', (t) => {
7763

7864
const showTitle = stub();
7965

80-
mockRequire('./query', query);
81-
mockRequire('./show-title', showTitle);
82-
83-
const {open} = mockRequire.reRequire('./modal');
84-
8566
open({}, {
67+
query,
68+
showTitle,
8669
beforeShow,
8770
title: 'hello',
8871
helpers: {
8972
title: true,
9073
},
9174
});
9275

93-
mockRequire.stop('./query');
94-
mockRequire.stop('./show-title');
95-
96-
stopAll();
97-
9876
t.calledWith(showTitle, ['hello', el], 'should call querySelector');
9977
t.end();
10078
});
@@ -119,10 +97,6 @@ test('modal: open: images', (t) => {
11997

12098
const parseImages = stub();
12199

122-
mockRequire('./query', query);
123-
mockRequire('./parse-images', parseImages);
124-
125-
const {open} = mockRequire.reRequire('./modal');
126100
const images = [{
127101
title: 'hello',
128102
href: 'world',
@@ -131,12 +105,11 @@ test('modal: open: images', (t) => {
131105
const afterShow = stub();
132106

133107
open(images, {
108+
query,
134109
afterShow,
110+
parseImages,
135111
});
136112

137-
mockRequire.stop('./query');
138-
mockRequire.stop('./parse-images');
139-
140113
const inner = [{
141114
title: 'hello',
142115
href: 'world',
@@ -148,8 +121,6 @@ test('modal: open: images', (t) => {
148121
helpers: {},
149122
};
150123

151-
stopAll();
152-
153124
t.calledWith(parseImages, [el, inner, options], 'should call querySelector');
154125
t.end();
155126
});
@@ -160,7 +131,9 @@ test('modal: onMainClick', (t) => {
160131
stopPropagation: stub(),
161132
};
162133

163-
_onMainClick(overlayClick, e);
134+
const click = _onMainClick(overlayClick);
135+
136+
click(e);
164137

165138
t.calledWith(overlayClick, [e], 'should call overlayClick');
166139
t.end();
@@ -172,7 +145,13 @@ test('modal: onMainClick: no main', (t) => {
172145
target: {},
173146
};
174147

175-
_onMainClick(overlayClick, e);
148+
const query = stub();
149+
150+
const click = _onMainClick(overlayClick, {
151+
query,
152+
});
153+
154+
click(e);
176155

177156
t.notCalled(overlayClick, 'should not call overlayClick');
178157
t.end();

0 commit comments

Comments
 (0)