Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 24ae997

Browse files
committed
refactor: introduce prettier
1 parent 1e178ad commit 24ae997

25 files changed

+2185
-1951
lines changed

.eslintrc.js

+22-40
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
11
module.exports = {
2-
env: {
3-
commonjs: true,
4-
es6: true,
5-
jest: true,
6-
},
7-
extends: 'airbnb-base',
8-
globals: {
9-
Atomics: 'readonly',
10-
SharedArrayBuffer: 'readonly',
11-
},
12-
parserOptions: {
13-
ecmaVersion: 2018,
14-
},
15-
plugins: [
16-
'jest',
17-
],
18-
rules: {
19-
indent: [
20-
'error',
21-
4,
22-
],
23-
'comma-dangle': [ 'error', {
24-
arrays: 'always-multiline',
25-
objects: 'always-multiline',
26-
imports: 'always-multiline',
27-
exports: 'always-multiline',
28-
functions: 'never',
29-
} ],
30-
'no-restricted-syntax': [ 'error', 'ForInStatement', 'LabeledStatement', 'WithStatement' ],
31-
'object-curly-spacing': [ 'error', 'always' ],
32-
'array-bracket-spacing': [ 'error', 'always' ],
33-
'no-underscore-dangle': 0,
34-
'max-len': [ 'error', 120, 2, {
35-
ignoreComments: false,
36-
ignoreUrls: true,
37-
ignoreRegExpLiterals: true,
38-
ignoreStrings: true,
39-
ignoreTemplateLiterals: true,
40-
} ],
41-
},
2+
env: {
3+
commonjs: true,
4+
es6: true,
5+
jest: true,
6+
},
7+
extends: [
8+
'airbnb-base',
9+
'plugin:jest/recommended',
10+
'eslint:recommended',
11+
'plugin:jest/style',
12+
'plugin:prettier/recommended',
13+
],
14+
globals: {
15+
Atomics: 'readonly',
16+
SharedArrayBuffer: 'readonly',
17+
},
18+
parserOptions: {
19+
ecmaVersion: 2018,
20+
},
21+
rules: {
22+
'no-underscore-dangle': 0,
23+
},
4224
};

.prettierrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"printWidth": 120,
4+
"trailingComma": "all"
5+
}

lib/PatternEventEmitter.js

+124-119
Original file line numberDiff line numberDiff line change
@@ -3,128 +3,133 @@ const { EventEmitter } = require('events');
33
const _ = require('lodash');
44

55
class Listener {
6-
static createMatcher(propPattern) {
7-
return new RegExp([ '^',
8-
propPattern.replace('.', '\\.')
9-
.replace(/\*/g, '.+')
10-
.replace(/\{((?:\w+\|?)*)\}/ig, (str, match) => `(?:${match})`), '$' ].join(''));
11-
}
12-
13-
constructor(propPattern, config) {
14-
this.propPattern = Listener.createMatcher(propPattern);
15-
this.config = config;
16-
this.listeners = [];
17-
}
18-
19-
isMatch(otherPropPattern) {
20-
return this.propPattern.test(otherPropPattern);
21-
}
22-
23-
_callIfMatch(prop, value) {
24-
if (this.isMatch(prop)) {
25-
this.listeners.forEach((l) => l(prop, value));
26-
}
27-
}
28-
29-
clearListeners() {
30-
this.listeners = [];
31-
}
32-
33-
removeListener(listenerCb) {
34-
this.listeners = this.listeners.filter((cb) => cb.__cb !== listenerCb);
35-
}
36-
37-
addListenerCallback(listenerCb) {
38-
const wrapped = (name, value) => {
39-
if (this.propPattern === '*') {
40-
return listenerCb(name, _.cloneDeep({}, this.config));
41-
}
42-
return listenerCb(name, value);
43-
};
44-
wrapped.__cb = listenerCb;
45-
this.listeners = [ ...this.listeners, wrapped ];
46-
return null;
47-
}
6+
static createMatcher(propPattern) {
7+
return new RegExp(
8+
[
9+
'^',
10+
propPattern
11+
.replace('.', '\\.')
12+
.replace(/\*/g, '.+')
13+
.replace(/\{((?:\w+\|?)*)\}/gi, (str, match) => `(?:${match})`),
14+
'$',
15+
].join(''),
16+
);
17+
}
18+
19+
constructor(propPattern, config) {
20+
this.propPattern = Listener.createMatcher(propPattern);
21+
this.config = config;
22+
this.listeners = [];
23+
}
24+
25+
isMatch(otherPropPattern) {
26+
return this.propPattern.test(otherPropPattern);
27+
}
28+
29+
_callIfMatch(prop, value) {
30+
if (this.isMatch(prop)) {
31+
this.listeners.forEach((l) => l(prop, value));
32+
}
33+
}
34+
35+
clearListeners() {
36+
this.listeners = [];
37+
}
38+
39+
removeListener(listenerCb) {
40+
this.listeners = this.listeners.filter((cb) => cb.__cb !== listenerCb);
41+
}
42+
43+
addListenerCallback(listenerCb) {
44+
const wrapped = (name, value) => {
45+
if (this.propPattern === '*') {
46+
return listenerCb(name, _.cloneDeep({}, this.config));
47+
}
48+
return listenerCb(name, value);
49+
};
50+
wrapped.__cb = listenerCb;
51+
this.listeners = [...this.listeners, wrapped];
52+
return null;
53+
}
4854
}
4955

5056
class PatternEventEmitter extends EventEmitter {
51-
constructor(config) {
52-
super();
53-
this.__config = config;
54-
this.__listeners = new Map();
55-
}
56-
57-
get config() {
58-
return this.__config;
59-
}
60-
61-
set config(config) {
62-
this.__config = config;
63-
[ ...this.__listeners.values() ].forEach((l) => {
64-
Object.assign(l, { config });
65-
});
66-
return config;
67-
}
68-
69-
_findListeners(propPattern) {
70-
return [ ...this.__listeners.values() ].filter((l) => l.isMatch(propPattern));
71-
}
72-
73-
removeListener(propPath, cb) {
74-
let propPattern = propPath;
75-
let listenerCb = cb;
76-
if (_.isFunction(propPattern)) {
77-
listenerCb = propPattern;
78-
propPattern = '';
79-
}
80-
this._findListeners(propPattern).forEach((l) => l.removeListener(listenerCb));
81-
return this;
82-
}
83-
84-
removeAllListeners(prop) {
85-
this._findListeners(prop).forEach((l) => l.clearListeners());
86-
return this;
87-
}
88-
89-
addListener(prop, cb) {
90-
let propPattern = prop;
91-
let listenerCb = cb;
92-
if (_.isFunction(propPattern)) {
93-
listenerCb = propPattern;
94-
propPattern = '*';
95-
}
96-
let listener = this.__listeners.get(propPattern);
97-
if (!listener) {
98-
listener = new Listener(propPattern, this.config);
99-
this.__listeners.set(propPattern, listener);
100-
}
101-
listener.addListenerCallback(listenerCb);
102-
return this;
103-
}
104-
105-
on(prop, ...rest) {
106-
return this.addListener(prop, ...rest);
107-
}
108-
109-
once(prop, cb) {
110-
let propPattern = prop;
111-
let listenerCb = cb;
112-
if (_.isFunction(propPattern)) {
113-
listenerCb = propPattern;
114-
propPattern = '*';
115-
}
116-
// have to keep reference to self to maintain length;
117-
const wrapped = (a, b, c) => {
118-
listenerCb(a, b, c);
119-
this.removeListener(prop, wrapped);
120-
};
121-
return this.addListener(propPattern, wrapped);
122-
}
123-
124-
emit(name, value) {
125-
[ ...this.__listeners.values() ].forEach((l) => l._callIfMatch(name, value));
126-
return true;
127-
}
57+
constructor(config) {
58+
super();
59+
this.__config = config;
60+
this.__listeners = new Map();
61+
}
62+
63+
get config() {
64+
return this.__config;
65+
}
66+
67+
set config(config) {
68+
this.__config = config;
69+
[...this.__listeners.values()].forEach((l) => {
70+
Object.assign(l, { config });
71+
});
72+
}
73+
74+
_findListeners(propPattern) {
75+
return [...this.__listeners.values()].filter((l) => l.isMatch(propPattern));
76+
}
77+
78+
removeListener(propPath, cb) {
79+
let propPattern = propPath;
80+
let listenerCb = cb;
81+
if (_.isFunction(propPattern)) {
82+
listenerCb = propPattern;
83+
propPattern = '';
84+
}
85+
this._findListeners(propPattern).forEach((l) => l.removeListener(listenerCb));
86+
return this;
87+
}
88+
89+
removeAllListeners(prop) {
90+
this._findListeners(prop).forEach((l) => l.clearListeners());
91+
return this;
92+
}
93+
94+
addListener(prop, cb) {
95+
let propPattern = prop;
96+
let listenerCb = cb;
97+
if (_.isFunction(propPattern)) {
98+
listenerCb = propPattern;
99+
propPattern = '*';
100+
}
101+
let listener = this.__listeners.get(propPattern);
102+
if (!listener) {
103+
listener = new Listener(propPattern, this.config);
104+
this.__listeners.set(propPattern, listener);
105+
}
106+
listener.addListenerCallback(listenerCb);
107+
return this;
108+
}
109+
110+
on(prop, ...rest) {
111+
return this.addListener(prop, ...rest);
112+
}
113+
114+
once(prop, cb) {
115+
let propPattern = prop;
116+
let listenerCb = cb;
117+
if (_.isFunction(propPattern)) {
118+
listenerCb = propPattern;
119+
propPattern = '*';
120+
}
121+
// have to keep reference to self to maintain length;
122+
const wrapped = (a, b, c) => {
123+
listenerCb(a, b, c);
124+
this.removeListener(prop, wrapped);
125+
};
126+
return this.addListener(propPattern, wrapped);
127+
}
128+
129+
emit(name, value) {
130+
[...this.__listeners.values()].forEach((l) => l._callIfMatch(name, value));
131+
return true;
132+
}
128133
}
129134

130135
module.exports = PatternEventEmitter;

lib/helpers.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
function denodeify(fn, scope) {
2-
function _denodeify(...args) {
3-
return new Promise((resolve, reject) => {
4-
fn.apply(scope || this, [ ...args, (err, result) => {
5-
if (err) {
6-
return reject(err);
7-
}
8-
return resolve(result);
9-
} ]);
10-
});
11-
}
12-
return _denodeify;
2+
function _denodeify(...args) {
3+
return new Promise((resolve, reject) => {
4+
fn.apply(scope || this, [
5+
...args,
6+
(err, result) => {
7+
if (err) {
8+
return reject(err);
9+
}
10+
return resolve(result);
11+
},
12+
]);
13+
});
14+
}
15+
return _denodeify;
1316
}
1417

1518
module.exports = {
16-
denodeify,
19+
denodeify,
1720
};

0 commit comments

Comments
 (0)