Skip to content

Commit

Permalink
feat!: export esm modules and types, drop Node < 18, drop custom prom…
Browse files Browse the repository at this point in the history
…ise constructor

- exports functions as "deepAplus" without factory that
  provides a promise constructor
- exports type definitions
- drop support for Node < 18
  • Loading branch information
nknapp committed Jul 16, 2024
1 parent da8c543 commit fe76de5
Show file tree
Hide file tree
Showing 21 changed files with 7,475 additions and 480 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: "nknapp"
liberapay: "nils.knappmeier"
custom: ["https://www.paypal.com/donate/?hosted_button_id=GB656ZSAEQEXN"]
51 changes: 51 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Tests

on:
push:
branches:
- master
pull_request: {}

jobs:
lint:
name: Lint
runs-on: 'ubuntu-latest'
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint
test:
name: Test (Node)
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
operating-system: ['ubuntu-latest', 'windows-latest']
node-version: ['18', '20','22']
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Test
run: npm run test

58 changes: 3 additions & 55 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,57 +1,5 @@
# .gitignore <https://github.com/tunnckoCore/dotfiles>
#
# Copyright (c) 2015 Charlike Mike Reagent, contributors.
# Released under the MIT license.
#

# Always-ignore dirs #
# ####################
_gh_pages
node_modules
bower_components
components
vendor
build
dest
.idea
/*.iml
dist
src
lib-cov
coverage
nbproject
cache
temp
tmp


# Packages #
# ##########
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# OS, Logs and databases #
# #########################
*.pid
*.dat
*.log
*.sql
*.sqlite
*~
~*

# Another files #
# ###############
Icon?
.DS_Store*
Thumbs.db
ehthumbs.db
Desktop.ini
npm-debug.log
.directory
._*
*.iml
.eslintcache
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run thought
npx lint-staged
36 changes: 36 additions & 0 deletions .thought/helpers.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-console */
const path = require("path");

module.exports = {
/**
* Merges the example output into the source code.
* In the example, `console.log` must be wrapped so that `\u0001` is
* inserted before each output.
* `console.log` may not be called in loops.
*/
mergeExample: function (cwd, filename, options) {
const helpers = options.customize.engine.helpers;

var code = helpers.example(path.join(cwd, filename), {
...options,
hash: {
snippet: true,
},
});
var result = helpers.exec(`node ${filename}`, {
...options,
hash: {
cwd: cwd,
lang: "raw",
},
});

return Promise.all([code, result]).then(function ([code, result]) {
console.log(result);
const output = result.split("\u0001");
return code.replace(/console\.log-output/g, function () {
return output.shift().trim();
});
});
},
};
35 changes: 0 additions & 35 deletions .thought/helpers.js

This file was deleted.

1 change: 0 additions & 1 deletion .thought/partials/overview.md.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ resolved.
Unlike other libraries like {{npm 'q-deep'}}, {{npm 'resolve-deep'}} and
{{npm 'swear'}}, this library is designed to work without dependencies to any promise library
(and also without any other dependencies).
Just pass the promise constructor (i.e. `Q.Promise` or `Promise`) as first argument.

**Note: There is no cycle check. You have to check for cycles yourself before passing the
structure to the function**
Expand Down
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

112 changes: 41 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
# deep-aplus

[![NPM version](https://badge.fury.io/js/deep-aplus.svg)](http://badge.fury.io/js/deep-aplus)
[![Travis Build Status](https://travis-ci.org/nknapp/deep-aplus.svg?branch=master)](https://travis-ci.org/nknapp/deep-aplus)
[![Coverage Status](https://img.shields.io/coveralls/nknapp/deep-aplus.svg)](https://coveralls.io/r/nknapp/deep-aplus)


> Resolve a whole structure of promises, library agnostic
# deep-aplus

[![NPM version](https://img.shields.io/npm/v/deep-aplus.svg)](https://npmjs.com/package/deep-aplus)
[![Github Actions Status](https://github.com/nknapp/deep-aplus/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/nknapp/deep-aplus/actions/workflows/ci.yml)

> Resolve a whole structure of promises
This small library is a promise-library agnostic function that resolves a whole structure
or objects, arrays, promises and values to a single promise in which the whole structure is
or objects, arrays, promises and values to a single promise in which the whole structure is
resolved.

Unlike other libraries like [q-deep](https://npmjs.com/package/q-deep), [resolve-deep](https://npmjs.com/package/resolve-deep) and
Unlike other libraries like [q-deep](https://npmjs.com/package/q-deep), [resolve-deep](https://npmjs.com/package/resolve-deep) and
[swear](https://npmjs.com/package/swear), this library is designed to work without dependencies to any promise library
(and also without any other dependencies).
Just pass the promise constructor (i.e. `Q.Promise` or `Promise`) as first argument.

**Note: There is no cycle check. You have to check for cycles yourself before passing the
structure to the function**

structure to the function**

# Installation

Expand All @@ -33,81 +27,57 @@ npm install deep-aplus
The following example demonstrates how to use this module:

```js
var Q = require('q')
var deep = require('deep-aplus')(Q.Promise)
import { deepAplus } from "../dist/index.mjs";

// Create a promise that returns a value (for demonstration purposes)
function P(value) {
return Q.delay(1).then(function () {
return value
})
return new Promise((resolve) => setTimeout(() => resolve(value), 1));
}

deep(2).then(console.log) // 2
.then(() => deep(P(2)))
.then(console.log) // 2

.then(() => deep({a: 1, b: P(2)}))
.then(console.log) // { a: 1, b: 2 }

.then(() => deep({a: 1, b: [2, P(3)]}))
.then(console.log) // { a: 1, b: [ 2, 3 ] }

.then(() => deep({a: 1, b: {c: 2, d: P(3)}}))
.then(console.log) // { a: 1, b: { c: 2, d: 3 } }

// Nesting promises
.then(() => deep({a: 1, b: P([2, P(3)])}))
.then(console.log) // { a: 1, b: [ 2, 3 ] }
console.log(await deepAplus(2));
// 2
console.log(await deepAplus(P(2)));
// 2

.then(() => deep({a: 1, b: P([2, P(3)])}))
.then(console.log) // { a: 1, b: [ 2, 3 ] }
console.log(await deepAplus({ a: 1, b: P(2) }));
// { a: 1, b: 2 }

.then(() => deep({a: 1, b: P({c: 2, d: P(3)})}))
.then(console.log) // { a: 1, b: { c: 2, d: 3 } }
console.log(await deepAplus({ a: 1, b: [2, P(3)] }));
// { a: 1, b: [ 2, 3 ] }

// does not dive into classes in order to preserve their functionality
.then(() => {
function A() {
this.a = 2;
this.b = P(3)
}
return deep(new A())
})
.then(console.log) // A { a: 2, b: { state: 'pending' } })
```


## API-reference

<a name="module_index"></a>
console.log(await deepAplus({ a: 1, b: { c: 2, d: P(3) } }));
// { a: 1, b: { c: 2, d: 3 } }

### index ⇒ <code>function</code>
Creates a `deep(value)`-function using the provided constructor to
create the resulting promise and promises for intermediate steps.
The `deep` function returns a promise for the resolution of an arbitrary
structure passed as parameter
// Nesting promises
console.log(await deepAplus({ a: 1, b: P([2, P(3)]) }));
// { a: 1, b: [ 2, 3 ] }

**Returns**: <code>function</code> - a function that returns a promise (of the provided class)
for a whole object structure
**Access:** public
console.log(await deepAplus({ a: 1, b: P([2, P(3)]) }));
// { a: 1, b: [ 2, 3 ] }

| Param | Type | Description |
| --- | --- | --- |
| Promise | <code>function</code> | class in which promises are created |
console.log(await deepAplus({ a: 1, b: P({ c: 2, d: P(3) }) }));
// { a: 1, b: { c: 2, d: 3 } }

// does not dive into classes in order to preserve their functionality
class A {
a = 2;
b = P(3);
}

console.log(await deepAplus(new A()));
// A { a: 2, b: Promise { <pending> } })
```

# License

## License
`deep-aplus` is published under the MIT-license.

`deep-aplus` is published under the MIT-license.
See [LICENSE.md](LICENSE.md) for details.

## Release-Notes
# Release-Notes

For release notes, see [CHANGELOG.md](CHANGELOG.md)

## Contributing guidelines

See [CONTRIBUTING.md](CONTRIBUTING.md).
# Contributing guidelines

See [CONTRIBUTING.md](CONTRIBUTING.md).
Loading

0 comments on commit fe76de5

Please sign in to comment.