aveazul 0.1.1 → 0.1.3
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/README.md +63 -50
- package/lib/aveazul.js +97 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
# AveAzul
|
|
2
2
|
|
|
3
|
-
AveAzul ("Blue Bird" in Spanish) is a Promise
|
|
4
|
-
|
|
5
|
-
## Requirements
|
|
6
|
-
|
|
7
|
-
- Node.js 16 or higher
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
- Extends native Promise with Bluebird-like utility methods
|
|
12
|
-
- Built on top of the efficient `xaa` library for async operations
|
|
13
|
-
- Zero external runtime dependencies (other than `xaa`)
|
|
14
|
-
- TypeScript-friendly
|
|
15
|
-
- Familiar Bluebird-style API
|
|
3
|
+
AveAzul ("Blue Bird" in Spanish) is a Promise library that extends native Promises with Bluebird-like utility methods. Built on top of native Promises, it provides a familiar API for Node.js developers who are used to working with [Bluebird](https://github.com/petkaantonov/bluebird) ([npm](https://www.npmjs.com/package/bluebird)).
|
|
16
4
|
|
|
17
5
|
## Installation
|
|
18
6
|
|
|
@@ -25,63 +13,88 @@ npm install aveazul
|
|
|
25
13
|
```javascript
|
|
26
14
|
const AveAzul = require('aveazul');
|
|
27
15
|
|
|
28
|
-
//
|
|
29
|
-
const promise = new AveAzul((resolve
|
|
30
|
-
|
|
31
|
-
});
|
|
16
|
+
// Basic Promise usage
|
|
17
|
+
const promise = new AveAzul((resolve) => resolve(42));
|
|
18
|
+
promise.then(value => console.log(value)); // 42
|
|
32
19
|
|
|
33
|
-
//
|
|
34
|
-
promise
|
|
35
|
-
.tap(value => console.log('Got:', value))
|
|
36
|
-
.delay(500)
|
|
37
|
-
.then(value => console.log('After delay:', value));
|
|
38
|
-
|
|
39
|
-
// Static helpers
|
|
40
|
-
AveAzul.delay(1000, 'hello')
|
|
41
|
-
.then(value => console.log(value));
|
|
42
|
-
|
|
43
|
-
// Array operations
|
|
20
|
+
// Utility methods
|
|
44
21
|
AveAzul.resolve([1, 2, 3])
|
|
45
22
|
.map(x => x * 2)
|
|
46
|
-
.filter(x => x >
|
|
47
|
-
.then(result => console.log(result)); // [6]
|
|
23
|
+
.filter(x => x > 2)
|
|
24
|
+
.then(result => console.log(result)); // [4, 6]
|
|
25
|
+
|
|
26
|
+
// Promisify callback-style functions
|
|
27
|
+
const fs = require('fs');
|
|
28
|
+
const readFile = AveAzul.promisify(fs.readFile);
|
|
29
|
+
readFile('file.txt').then(content => console.log(content));
|
|
30
|
+
|
|
31
|
+
// Promisify all methods of an object
|
|
32
|
+
const obj = {
|
|
33
|
+
method(cb) { cb(null, 'result'); }
|
|
34
|
+
};
|
|
35
|
+
AveAzul.promisifyAll(obj);
|
|
36
|
+
obj.methodAsync().then(result => console.log(result)); // 'result'
|
|
48
37
|
```
|
|
49
38
|
|
|
50
39
|
## API
|
|
51
40
|
|
|
52
41
|
### Instance Methods
|
|
53
42
|
|
|
54
|
-
- `tap(fn)` - Execute side effects
|
|
43
|
+
- `tap(fn)` - Execute side effects and return original value
|
|
55
44
|
- `filter(fn)` - Filter array elements
|
|
56
|
-
- `map(fn)` -
|
|
57
|
-
- `return(value)` - Inject a value
|
|
45
|
+
- `map(fn)` - Transform array elements
|
|
46
|
+
- `return(value)` - Inject a new value
|
|
58
47
|
- `each(fn)` - Iterate over array elements
|
|
59
|
-
- `delay(ms)` - Delay
|
|
60
|
-
- `timeout(ms, message?)` -
|
|
48
|
+
- `delay(ms)` - Delay resolution
|
|
49
|
+
- `timeout(ms, message?)` - Reject after specified time
|
|
61
50
|
- `try(fn)` - Wrap sync/async functions
|
|
62
|
-
- `props(obj)` -
|
|
63
|
-
- `
|
|
51
|
+
- `props(obj)` - Resolve object properties
|
|
52
|
+
- `catchIf(predicate, fn)` - Catch specific errors
|
|
53
|
+
- `tapCatch(fn)` - Execute side effects on rejection
|
|
54
|
+
- `reduce(fn, initialValue?)` - Reduce array elements
|
|
64
55
|
- `throw(reason)` - Return rejected promise
|
|
65
56
|
- `catchThrow(reason)` - Catch and throw new error
|
|
66
57
|
- `catchReturn(value)` - Catch and return value
|
|
67
|
-
- `get(propertyPath)` -
|
|
58
|
+
- `get(propertyPath)` - Retrieve property value
|
|
68
59
|
|
|
69
60
|
### Static Methods
|
|
70
61
|
|
|
71
|
-
- `
|
|
72
|
-
- `
|
|
73
|
-
- `
|
|
74
|
-
- `
|
|
75
|
-
- `
|
|
76
|
-
- `
|
|
77
|
-
- `
|
|
78
|
-
- `
|
|
79
|
-
- `
|
|
62
|
+
- `delay(ms, value?)` - Resolve after specified time
|
|
63
|
+
- `map(value, fn)` - Transform array elements
|
|
64
|
+
- `try(fn)` - Wrap sync/async functions
|
|
65
|
+
- `props(obj)` - Resolve object properties
|
|
66
|
+
- `defer()` - Create a deferred promise
|
|
67
|
+
- `promisify(fn, options?)` - Convert callback-style functions to promises
|
|
68
|
+
- `each(items, fn)` - Iterate over array elements
|
|
69
|
+
- `reduce(array, fn, initialValue?)` - Reduce array elements
|
|
70
|
+
- `throw(reason)` - Return rejected promise
|
|
71
|
+
- `promisifyAll(target, options?)` - Convert all methods of an object/class to promises
|
|
72
|
+
|
|
73
|
+
### PromisifyAll Options
|
|
74
|
+
|
|
75
|
+
- `suffix` (default: 'Async') - Suffix to append to promisified method names
|
|
76
|
+
- `filter` - Filter function to determine which methods to promisify
|
|
77
|
+
- `promisifier` - Custom function to handle promisification
|
|
78
|
+
- `multiArgs` (default: false) - Whether to support multiple callback arguments
|
|
79
|
+
- `excludeMain` (default: false) - Whether to exclude promisifying the main object/class
|
|
80
|
+
- `context` - The context (this) to use when calling methods
|
|
81
|
+
|
|
82
|
+
## Development
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Install dependencies
|
|
86
|
+
npm install
|
|
87
|
+
|
|
88
|
+
# Run tests
|
|
89
|
+
npm test
|
|
90
|
+
npm run test:watch
|
|
91
|
+
npm run test:coverage
|
|
92
|
+
```
|
|
80
93
|
|
|
81
94
|
## License
|
|
82
95
|
|
|
83
|
-
Apache
|
|
96
|
+
Apache-2.0
|
|
84
97
|
|
|
85
|
-
##
|
|
98
|
+
## Author
|
|
86
99
|
|
|
87
|
-
|
|
100
|
+
Joel Chen
|
package/lib/aveazul.js
CHANGED
|
@@ -353,4 +353,101 @@ AveAzul.reduce = (array, fn, initialValue) => {
|
|
|
353
353
|
*/
|
|
354
354
|
AveAzul.throw = reason => AveAzul.reject(reason);
|
|
355
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Bluebird-style promisifyAll() for converting all methods of an object or class to promise-based versions
|
|
358
|
+
* Similar to Bluebird's Promise.promisifyAll()
|
|
359
|
+
*
|
|
360
|
+
* @param {Object|Function} target - The object or class to promisify
|
|
361
|
+
* @param {Object} [options] - Configuration options
|
|
362
|
+
* @param {string} [options.suffix='Async'] - Suffix to append to promisified method names
|
|
363
|
+
* @param {Function} [options.filter] - Filter function to determine which methods to promisify
|
|
364
|
+
* @param {Function} [options.promisifier] - Custom function to handle promisification
|
|
365
|
+
* @param {boolean} [options.multiArgs=false] - Whether to support multiple callback arguments
|
|
366
|
+
* @param {boolean} [options.excludeMain=false] - Whether to exclude promisifying the main object/class
|
|
367
|
+
* @param {Object} [options.context] - The context (this) to use when calling methods
|
|
368
|
+
* @returns {Object|Function} The promisified object or class
|
|
369
|
+
* @throws {TypeError} If target is null, undefined, or not an object/function
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* // Promisify an object
|
|
373
|
+
* const obj = {
|
|
374
|
+
* method(cb) { cb(null, 'result'); }
|
|
375
|
+
* };
|
|
376
|
+
* AveAzul.promisifyAll(obj);
|
|
377
|
+
* const result = await obj.methodAsync();
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* // Promisify a class
|
|
381
|
+
* class MyClass {
|
|
382
|
+
* method(cb) { cb(null, 'result'); }
|
|
383
|
+
* }
|
|
384
|
+
* AveAzul.promisifyAll(MyClass);
|
|
385
|
+
* const instance = new MyClass();
|
|
386
|
+
* const result = await instance.methodAsync();
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* // With custom options
|
|
390
|
+
* const obj = {
|
|
391
|
+
* method(cb) { cb(null, 'result1', 'result2'); }
|
|
392
|
+
* };
|
|
393
|
+
* AveAzul.promisifyAll(obj, {
|
|
394
|
+
* suffix: 'Promise',
|
|
395
|
+
* multiArgs: true,
|
|
396
|
+
* filter: (name) => name === 'method'
|
|
397
|
+
* });
|
|
398
|
+
* const [result1, result2] = await obj.methodPromise();
|
|
399
|
+
*/
|
|
400
|
+
AveAzul.promisifyAll = (target, options = {}) => {
|
|
401
|
+
const {
|
|
402
|
+
suffix = 'Async',
|
|
403
|
+
filter = (name, func, targetObj, passedOptions) => {
|
|
404
|
+
return (
|
|
405
|
+
typeof func === 'function' &&
|
|
406
|
+
!func.name.startsWith('_') &&
|
|
407
|
+
!func.name.startsWith('promisify') &&
|
|
408
|
+
!func.name.startsWith('promisifyAll')
|
|
409
|
+
);
|
|
410
|
+
},
|
|
411
|
+
promisifier = (fn, context, multiArgs) => {
|
|
412
|
+
if (multiArgs) {
|
|
413
|
+
return (...args) => {
|
|
414
|
+
return new AveAzul((resolve, reject) => {
|
|
415
|
+
args.push((err, ...results) => {
|
|
416
|
+
if (err) reject(err);
|
|
417
|
+
else resolve(results);
|
|
418
|
+
});
|
|
419
|
+
fn.apply(context, args);
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
return AveAzul.promisify(fn, { context });
|
|
424
|
+
},
|
|
425
|
+
multiArgs = false,
|
|
426
|
+
excludeMain = false,
|
|
427
|
+
context = target
|
|
428
|
+
} = options;
|
|
429
|
+
|
|
430
|
+
if (target == null || (typeof target !== 'object' && typeof target !== 'function')) {
|
|
431
|
+
throw new TypeError('target must be an object');
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const targetObj = target.prototype || target;
|
|
435
|
+
const keys = Object.getOwnPropertyNames(targetObj);
|
|
436
|
+
|
|
437
|
+
for (const key of keys) {
|
|
438
|
+
const func = targetObj[key];
|
|
439
|
+
if (filter(key, func, targetObj, options)) {
|
|
440
|
+
const promisifiedKey = key + suffix;
|
|
441
|
+
targetObj[promisifiedKey] = promisifier(func, context, multiArgs);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (!excludeMain && typeof target === 'function') {
|
|
446
|
+
target.promisify = AveAzul.promisify;
|
|
447
|
+
target.promisifyAll = AveAzul.promisifyAll;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return target;
|
|
451
|
+
};
|
|
452
|
+
|
|
356
453
|
module.exports = AveAzul;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aveazul",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Bluebird-like APIs in extended native Promise",
|
|
5
5
|
"main": "lib/aveazul.js",
|
|
6
6
|
"homepage": "https://github.com/jchip/aveazul",
|
|
7
7
|
"license": "Apache-2.0",
|