asljs-eventful 0.4.0 → 0.4.1

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.
@@ -1,5 +1,5 @@
1
1
  import { isFunction, isObject, } from './guards.js';
2
- export function isEvenfulLike(value) {
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 (isEvenfulLike(value)) {
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, isEvenfulLike, } from './eventful-like.js';
5
- test('isEvenfulLike returns true for eventful instances', () => {
4
+ import { asEventfulLike, isEventfulLike } from './eventful-like.js';
5
+ test('isEventfulLike returns true for eventful instances', () => {
6
6
  const value = eventful();
7
- assert.equal(isEvenfulLike(value), true);
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 { asEventfulLike, isEvenfulLike, } from './eventful-like.js';
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 = typeof trace === 'function'
16
- ? trace
17
- : null;
18
- const errorHook = typeof error === 'function'
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
@@ -1,6 +1,6 @@
1
1
  export {
2
2
  eventful,
3
- isEvenfulLike,
3
+ isEventfulLike,
4
4
  asEventfulLike
5
5
  } from './dist/eventful.js';
6
6
 
package/eventful.js CHANGED
@@ -1,12 +1,12 @@
1
1
  export {
2
- eventful,
3
- isEvenfulLike,
4
- asEventfulLike,
5
- } from './dist/eventful.js';
2
+ eventful,
3
+ isEventfulLike,
4
+ asEventfulLike,
5
+ } from './dist/eventful.js';
6
6
 
7
7
  export {
8
- EventfulBase,
9
- } from './dist/eventful-base.js';
8
+ EventfulBase,
9
+ } from './dist/eventful-base.js';
10
10
 
11
11
  export {
12
12
  ListenerError
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asljs-eventful",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Lightweight event helper adding on/off/emit to any object.",
5
5
  "files": [
6
6
  "dist/**",
@@ -46,5 +46,8 @@
46
46
  "test": "npm run build:test && node --test dist/*.test.js",
47
47
  "test:watch": "npm run build:test && node --watch --test dist/*.test.js",
48
48
  "coverage": "npm run build:test && NODE_V8_COVERAGE=.coverage node --test dist/*.test.js && node -e \"console.log('Coverage in .coverage (use c8/istanbul if you want reports)')\""
49
+ },
50
+ "dependencies": {
51
+ "asljs-eventful": "file:"
49
52
  }
50
53
  }