Compare commits

..

52 Commits

Author SHA1 Message Date
d922c691ed 3.0.7 2020-10-15 18:02:14 +00:00
5aecc44ad7 fix(core): update 2020-10-15 18:02:14 +00:00
f7492c4656 3.0.6 2019-10-01 18:06:29 +02:00
63dd46ed49 fix(core): update 2019-10-01 18:06:29 +02:00
984c2bc9d8 3.0.5 2019-09-11 16:44:21 +02:00
16030d5ee8 fix(core): update 2019-09-11 16:44:21 +02:00
ddce50ac0f 3.0.4 2019-09-11 15:38:37 +02:00
baf956d0ed fix(core): update 2019-09-11 15:38:37 +02:00
3340f1a895 3.0.3 2019-09-11 15:37:18 +02:00
ee336af699 fix(core): update 2019-09-11 15:37:17 +02:00
0526c45ea1 3.0.2 2019-03-26 12:11:25 +01:00
9933d72784 fix(core): update 2019-03-26 12:11:24 +01:00
d1bc689b35 3.0.1 2019-03-26 12:07:13 +01:00
25c0deb3f3 fix(core): update 2019-03-26 12:07:12 +01:00
5138219563 3.0.0 2019-03-26 12:02:02 +01:00
93f426cce0 BREAKING CHANGE(remove util dependency and promisify functionality): update 2019-03-26 12:02:01 +01:00
e42061047a 2.0.5 2018-07-03 08:41:43 +02:00
3b07f99488 fix(README): fix import statement 2018-07-03 08:41:42 +02:00
3d252a5e3e 2.0.4 2018-07-03 08:39:25 +02:00
d82722296b fix(README): update name 2018-07-03 08:39:25 +02:00
22317ef83d 2.0.3 2018-07-02 22:59:16 +02:00
054ec0afa4 fix(core): now has 0 dependencies 2018-07-02 22:59:16 +02:00
19d434533f 2.0.2 2018-07-02 22:54:14 +02:00
9b8440f743 fix(core): update to latest gitzone standards 2018-07-02 22:54:13 +02:00
564953e25f 2.0.1 2018-07-02 22:52:45 +02:00
98f190ea21 fix(test): adjust windows test 2018-07-02 22:52:45 +02:00
2b77c8e019 2.0.0 2018-07-02 22:49:21 +02:00
d8e5fbf3bc BREAKING CHANGE(scope): switch scope to pushrocks 2018-07-02 22:49:21 +02:00
1069d5a38e 1.1.8 2018-03-16 10:24:30 +01:00
eb82d16859 update to latest standards 2018-03-16 10:24:27 +01:00
082f533975 update to latest standards 2018-03-16 10:24:12 +01:00
54b2fb0ae5 1.1.7 2017-07-27 18:17:51 +02:00
31c7e607cc start with promise status implementation 2017-07-27 18:17:37 +02:00
b15cdb48a3 1.1.6 2017-07-06 15:00:16 +02:00
7c1605eccf update README and add docs 2017-07-06 15:00:13 +02:00
cea619e964 1.1.5 2017-07-06 14:55:52 +02:00
fb866b36af add polyfill 2017-07-06 14:55:42 +02:00
a8934950ef 1.1.4 2017-07-06 14:14:16 +02:00
590dc27d20 add npmextra.json 2017-07-06 14:14:11 +02:00
3bcff82d31 1.1.3 2017-07-06 14:12:38 +02:00
bfbea0acde update ci 2017-07-06 14:12:35 +02:00
ba18b31f2f 1.1.2 2017-07-06 14:11:24 +02:00
4fc619970f switch to native promisify 2017-07-06 14:10:18 +02:00
e0cfa6ca29 1.1.1 2017-02-12 12:56:29 +01:00
843d217698 update README 2017-02-12 12:56:22 +01:00
dbd2fe73b8 1.1.0 2017-01-28 16:37:58 +01:00
1eea124cf4 add promisify 2017-01-28 16:37:54 +01:00
6a4ee22f36 1.0.4 2017-01-21 00:45:14 +01:00
e23f946e50 improve upon smartq 2017-01-21 00:45:11 +01:00
8d62fc6ef1 improve README 2017-01-20 22:01:10 +01:00
e39ad3b19b 1.0.3 2017-01-20 21:58:56 +01:00
7220959662 ad better readme and some more functions 2017-01-20 21:58:53 +01:00
13 changed files with 2071 additions and 152 deletions

25
.gitignore vendored
View File

@ -1,5 +1,22 @@
coverage/ .nogit/
pages/
node_modules/
public/
# artifacts
coverage/
public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,59 +1,119 @@
image: hosttoday/ht-docker-node:npmts # gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages: stages:
- security
- test - test
- release - release
- trigger - metadata
- pages
testLEGACY: # ====================
stage: test # security stage
# ====================
mirror:
stage: security
script: script:
- npmci test legacy - npmci git mirror
tags: tags:
- docker - docker
allow_failure: true - notpriv
testLTS: snyk:
stage: test stage: security
script: script:
- npmci test lts - npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags: tags:
- docker - docker
- notpriv
testSTABLE: # ====================
# test stage
# ====================
testStable:
stage: test stage: test
script: script:
- npmci test stable - npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release: release:
stage: release stage: release
script: script:
- npmci publish - npmci node install stable
- npmci npm publish
only: only:
- tags - tags
tags: tags:
- docker - docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
trigger: trigger:
stage: trigger stage: metadata
script: script:
- npmci trigger - npmci trigger
only: only:
- tags - tags
tags: tags:
- docker - docker
- notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmpage image: hosttoday/ht-docker-dbase:npmci
stage: pages services:
- docker:stable-dind
stage: metadata
script: script:
- npmci command npmpage --host gitlab - npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- docker
- notpriv
only: only:
- tags - tags
artifacts: artifacts:
expire_in: 1 week expire_in: 1 week
paths: paths:
- public - public
allow_failure: true

100
README.md
View File

@ -1,24 +1,90 @@
# smartq # @pushrocks/smartpromise
dropin replacement for q
## Availabililty simple promises and Deferred constructs
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartq)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartq) ## Availabililty and Links
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartq)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartq/) - [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
- [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
- [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
## Status for master ## Status for master
[![build status](https://GitLab.com/pushrocks/smartq/badges/master/build.svg)](https://GitLab.com/pushrocks/smartq/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartq/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartq/commits/master) [![build status](https://gitlab.com/pushrocks/smartpromise/badges/master/build.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartq.svg)](https://www.npmjs.com/package/smartq) [![coverage report](https://gitlab.com/pushrocks/smartpromise/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartpromise/commits/master)
[![Dependency Status](https://david-dm.org/pushrocks/smartq.svg)](https://david-dm.org/pushrocks/smartq) [![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartpromise.svg)](https://www.npmjs.com/package/@pushrocks/smartpromise)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartq/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm) [![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartpromise/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartpromise)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartq/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartq) [![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage ## Usage
Use TypeScript for best in class instellisense. Use TypeScript for best in class instellisense.
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks) > Note: smartq uses native ES6 promises
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
```typescript
import * as q from '@pushrocks/smartpromise'
// Deferred
// -----------------------------------------------
let myAsyncFunction = (): Promise<string> => {
let done = q.defer<string>() // returns your typical Deferred object
setTimeout(() => {
done.resolve('hi') // will throw type error for other types than string as argument ;)
},6000);
console.log(done.status) // logs "pending";
done.promise.then(() => {
console.log(done.status) // logs "fullfilled"
console.log(done.duration) // logs the milliseconds between instantiation and fullfillment
})
return done.promise
}
let myAsyncFunction2 = async () => {
let aString = await myAsyncFunction()
console.log(aString) // will log 'hi' to console
}
myAsyncFunction2();
// Resolved and Rejected promises
// ------------------------------------------------
q.resolvedPromise(`I'll get logged to console soon`)
.then(x => {
console.log(x)
})
q.rejectedPromise(`what a lovely error message`)
.then(() => {
console.log('This never makes it to console')
}/*, alternatively put a reject function here */)
.catch(err => {
console.log(err)
})
// Promisify (typed)
// ------------------------------------------------
let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
cb(null, someString)
}
let myPromisedFunction = q.promisify(myCallbackFunction)
myPromisedFunction('helloThere', 2).then(x => {
console.log(x) // will log 'helloThere' to console
})
```
For further information read the linked docs at the top of this readme.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

15
dist/index.d.ts vendored
View File

@ -1,15 +0,0 @@
import 'typings-global';
export interface IResolve<T> {
(value?: T | Promise<T>): void;
}
export interface IReject {
(reason?: any): void;
}
export declare class Deferred<T> {
promise: Promise<T>;
resolve: IResolve<T>;
reject: IReject;
constructor();
}
export declare let defer: <T>() => Deferred<T>;
export declare let all: () => void;

17
dist/index.js vendored
View File

@ -1,17 +0,0 @@
"use strict";
require("typings-global");
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
exports.Deferred = Deferred;
exports.defer = () => {
return new Deferred();
};
exports.all = () => {
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBR1UsUUFBQSxHQUFHLEdBQUc7QUFFakIsQ0FBQyxDQUFBIn0=

16
npmextra.json Normal file
View File

@ -0,0 +1,16 @@
{
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartpromise",
"shortDescription": "simple promises and Deferred constructs",
"npmPackagename": "@pushrocks/smartpromise",
"license": "MIT"
}
}
}

1718
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,13 @@
{ {
"name": "smartq", "name": "@pushrocks/smartpromise",
"version": "1.0.2", "private": false,
"description": "dropin replacement for q", "version": "3.0.7",
"description": "simple promises and Deferred constructs",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
"scripts": { "scripts": {
"test": "(npmts)" "test": "(tstest test/)",
"build": "(tsbuild)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -17,12 +19,24 @@
"url": "https://gitlab.com/pushrocks/smartq/issues" "url": "https://gitlab.com/pushrocks/smartq/issues"
}, },
"homepage": "https://gitlab.com/pushrocks/smartq#README", "homepage": "https://gitlab.com/pushrocks/smartq#README",
"dependencies": { "dependencies": {},
"typings-global": "^1.0.14"
},
"devDependencies": { "devDependencies": {
"@types/should": "^8.1.30", "@gitzone/tsbuild": "^2.1.17",
"should": "^11.1.2", "@gitzone/tstest": "^1.0.24",
"typings-test": "^1.0.3" "@pushrocks/tapbundle": "^3.0.13",
} "@types/node": "^12.7.5",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

1
test/test.d.ts vendored
View File

@ -1 +0,0 @@
import 'typings-test';

View File

@ -1,23 +0,0 @@
"use strict";
require("typings-test");
const should = require("should");
const q = require("../dist/index");
describe('smartq', function () {
it('should return a Deferred for .defer()', function (done) {
let myDeferred = q.defer();
myDeferred.promise.then(() => {
done();
});
myDeferred.resolve();
});
it('should let types flow through the Promise', function (done) {
let myString = 'someString';
let myDeferred = q.defer();
myDeferred.promise.then(x => {
should(x).equal('someString');
done();
});
myDeferred.resolve(myString);
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQixpQ0FBZ0M7QUFDaEMsbUNBQWtDO0FBRWxDLFFBQVEsQ0FBQyxRQUFRLEVBQUU7SUFDZixFQUFFLENBQUMsdUNBQXVDLEVBQUUsVUFBUyxJQUFJO1FBQ3JELElBQUksVUFBVSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUMxQixVQUFVLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQztZQUNwQixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDJDQUEyQyxFQUFFLFVBQVMsSUFBSTtRQUN6RCxJQUFJLFFBQVEsR0FBRyxZQUFZLENBQUE7UUFDM0IsSUFBSSxVQUFVLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBVSxDQUFBO1FBQ2xDLFVBQVUsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDckIsTUFBTSxDQUFDLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxZQUFZLENBQUMsQ0FBQTtZQUM3QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQTtJQUNoQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=

View File

@ -1,24 +1,30 @@
import 'typings-test' import { expect, tap } from '@pushrocks/tapbundle';
import * as smartpromise from '../ts/index';
import * as should from 'should' tap.test('should return a Deferred for .defer()', async () => {
import * as q from '../dist/index' const myDeferred = smartpromise.defer();
const expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
myDeferred.resolve();
return expectPromise;
});
describe('smartq', function() { tap.test('should let types flow through the Promise', async () => {
it('should return a Deferred for .defer()', function(done) { const myString = 'someString';
let myDeferred = q.defer() const myDeferred = smartpromise.defer<string>();
myDeferred.promise.then(() => { const expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
done() myDeferred.resolve(myString);
}) return expectPromise;
myDeferred.resolve() });
})
it('should let types flow through the Promise', function(done) { tap.test('should map callbacks', async () => {
let myString = 'someString' const inputArray = ['hi', 'awesome'];
let myDeferred = q.defer<string>() const myPromisified = async myInput => {
myDeferred.promise.then(x => { return myInput;
should(x).equal('someString') };
done() const expectPromise = expect(smartpromise.map(inputArray, myPromisified)).to.eventually.deep.equal(
}) inputArray
myDeferred.resolve(myString) );
}) return expectPromise;
}) });
tap.start();

View File

@ -1,30 +1,94 @@
import 'typings-global'
export interface IResolve<T> { export interface IResolve<T> {
(value?: T | Promise<T>): void (value?: T | PromiseLike<T>): void;
} }
export interface IReject { export interface IReject {
(reason?: any): void (reason?: any): void;
} }
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
export class Deferred<T> { export class Deferred<T> {
promise: Promise<T> public promise: Promise<T>;
resolve: IResolve<T> public resolve: IResolve<T>;
reject: IReject public reject: IReject;
public status: TDeferredStatus;
public startedAt: number;
public stoppedAt: number;
public get duration(): number {
if (this.stoppedAt) {
return this.stoppedAt - this.startedAt;
} else {
return Date.now() - this.startedAt;
}
}
constructor() { constructor() {
this.promise = new Promise<T>((resolve, reject) => { this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve this.resolve = (valueArg: T | PromiseLike<T>) => {
this.reject = reject this.status = 'fulfilled';
}) this.stoppedAt = Date.now();
resolve(valueArg);
};
this.reject = (reason: any) => {
this.status = 'rejected';
this.stoppedAt = Date.now();
reject(reason);
};
this.startedAt = Date.now();
this.status = 'pending';
});
} }
} }
export let defer = <T>() => { export const defer = <T>() => {
return new Deferred<T>() return new Deferred<T>();
};
/**
* Creates a new resolved promise for the provided value.
*/
export const resolvedPromise = <T>(value?: T): Promise<T> => {
return Promise.resolve(value);
};
/**
* Creates a new rejected promise for the provided reason.
*/
export const rejectedPromise = (err) => {
return Promise.reject(err);
};
interface IAsyncFunction<T> {
(someArg: T): Promise<T>;
} }
/**
* accepts an array of inputs and a function that accepts the input.
* runs all items with the function and returns the result array when all items have run
* @param inputArg
* @param functionArg
*/
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
const promiseArray: Promise<any>[] = [];
const resultArray = [];
for (const item of inputArg) {
const promise: Promise<any> = functionArg(item);
promiseArray.push(promise);
promise.then((x) => {
resultArray.push(x);
});
}
await Promise.all(promiseArray);
return resultArray;
};
export let all = () => { export const timeoutWrap = (ms, promise) => {
return new Promise((resolve, reject) => {
} setTimeout(() => {
reject(new Error('timeout'));
}, ms);
promise.then(resolve, reject);
});
};

View File

@ -1,3 +1,17 @@
{ {
"extends": "tslint-config-standard" "extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
} }