asljs-eventful 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/eventful-like.js +2 -2
- package/dist/eventful-like.test.js +3 -13
- package/dist/eventful.js +6 -10
- package/dist/guards.js +6 -0
- package/eventful.d.ts +1 -1
- package/eventful.js +6 -6
- package/package.json +1 -1
package/dist/eventful-like.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isFunction, isObject, } from './guards.js';
|
|
2
|
-
export function
|
|
2
|
+
export function isEventfulLike(value) {
|
|
3
3
|
if (!isObject(value)
|
|
4
4
|
&& !isFunction(value)) {
|
|
5
5
|
return false;
|
|
@@ -7,7 +7,7 @@ export function isEvenfulLike(value) {
|
|
|
7
7
|
return typeof value.on === 'function';
|
|
8
8
|
}
|
|
9
9
|
export function asEventfulLike(value) {
|
|
10
|
-
if (
|
|
10
|
+
if (isEventfulLike(value)) {
|
|
11
11
|
return value;
|
|
12
12
|
}
|
|
13
13
|
return undefined;
|
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
import test from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import { eventful, } from './eventful.js';
|
|
4
|
-
import { asEventfulLike,
|
|
5
|
-
test('
|
|
4
|
+
import { asEventfulLike, isEventfulLike } from './eventful-like.js';
|
|
5
|
+
test('isEventfulLike returns true for eventful instances', () => {
|
|
6
6
|
const value = eventful();
|
|
7
|
-
assert.equal(
|
|
8
|
-
});
|
|
9
|
-
test('isEvenfulLike returns true for plain object with on method', () => {
|
|
10
|
-
const value = { on: () => () => true };
|
|
11
|
-
assert.equal(isEvenfulLike(value), true);
|
|
12
|
-
});
|
|
13
|
-
test('isEvenfulLike returns false for non-like values', () => {
|
|
14
|
-
assert.equal(isEvenfulLike(undefined), false);
|
|
15
|
-
assert.equal(isEvenfulLike(null), false);
|
|
16
|
-
assert.equal(isEvenfulLike({}), false);
|
|
17
|
-
assert.equal(isEvenfulLike({ on: 1 }), false);
|
|
7
|
+
assert.equal(isEventfulLike(value), true);
|
|
18
8
|
});
|
|
19
9
|
test('asEventfulLike returns value when compatible', () => {
|
|
20
10
|
const value = eventful();
|
package/dist/eventful.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ListenerError, } from './types.js';
|
|
2
|
-
import {
|
|
3
|
-
import { eventNameTypeGuard, functionTypeGuard, isFunction, isObject, } from './guards.js';
|
|
2
|
+
import { asFunction, eventNameTypeGuard, functionTypeGuard, isFunction, isObject, } from './guards.js';
|
|
4
3
|
const eventfulImpl = (object = Object.create(null), options = {}) => {
|
|
5
4
|
if (!isObject(object)
|
|
6
5
|
&& !isFunction(object)) {
|
|
@@ -11,13 +10,11 @@ const eventfulImpl = (object = Object.create(null), options = {}) => {
|
|
|
11
10
|
throw new Error(`Method "${method}" already exists.`);
|
|
12
11
|
}
|
|
13
12
|
}
|
|
14
|
-
const { strict = false, trace = null, error = null } = options;
|
|
15
|
-
const traceHook =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
? error
|
|
20
|
-
: null;
|
|
13
|
+
const { strict = false, trace = null, error = null, } = options;
|
|
14
|
+
const traceHook = asFunction(trace)
|
|
15
|
+
?? null;
|
|
16
|
+
const errorHook = asFunction(error)
|
|
17
|
+
?? null;
|
|
21
18
|
const enhanced = object !== eventful;
|
|
22
19
|
const traceFn = (action, payload) => {
|
|
23
20
|
traceHook?.(action, payload);
|
|
@@ -146,5 +143,4 @@ const eventfulImpl = (object = Object.create(null), options = {}) => {
|
|
|
146
143
|
}
|
|
147
144
|
};
|
|
148
145
|
export const eventful = eventfulImpl;
|
|
149
|
-
export { isEvenfulLike, asEventfulLike, };
|
|
150
146
|
eventful(eventful);
|
package/dist/guards.js
CHANGED
|
@@ -7,6 +7,12 @@ export function eventNameTypeGuard(value) {
|
|
|
7
7
|
export function isFunction(value) {
|
|
8
8
|
return typeof value === 'function';
|
|
9
9
|
}
|
|
10
|
+
export function asFunction(value) {
|
|
11
|
+
if (isFunction(value)) {
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
10
16
|
export function isObject(value) {
|
|
11
17
|
return typeof value === 'object'
|
|
12
18
|
&& value !== null;
|
package/eventful.d.ts
CHANGED
package/eventful.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from './dist/eventful.js';
|
|
2
|
+
eventful,
|
|
3
|
+
isEventfulLike,
|
|
4
|
+
asEventfulLike,
|
|
5
|
+
} from './dist/eventful.js';
|
|
6
6
|
|
|
7
7
|
export {
|
|
8
|
-
|
|
9
|
-
} from './dist/eventful-base.js';
|
|
8
|
+
EventfulBase,
|
|
9
|
+
} from './dist/eventful-base.js';
|
|
10
10
|
|
|
11
11
|
export {
|
|
12
12
|
ListenerError
|