1
0
Fork 0

docs and tests

This commit is contained in:
ssube 2021-08-08 14:58:43 -05:00
parent 50b029ff8c
commit b4343ba05b
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
10 changed files with 75 additions and 15 deletions

View File

@ -4,12 +4,12 @@
## deferUntil() function
Reject after a set number of attempts if the given predicate does not return true.
Resolve if `cb` returns true within `max` tries, otherwise reject with a `TimeoutError`<!-- -->.
<b>Signature:</b>
```typescript
export declare function deferUntil(cb: PredicateC0, step: number, tries: number): Promise<void>;
export declare function deferUntil(cb: PredicateC0, step: number, max: number): Promise<void>;
```
## Parameters
@ -18,7 +18,7 @@ export declare function deferUntil(cb: PredicateC0, step: number, tries: number)
| --- | --- | --- |
| cb | PredicateC0 | |
| step | number | |
| tries | number | |
| max | number | |
<b>Returns:</b>

View File

@ -4,6 +4,8 @@
## deferValue() function
Resolve with the given value, after a set amount of time.
<b>Signature:</b>
```typescript

View File

@ -4,6 +4,8 @@
## filterZip() function
Filter and zip some arrays. The `cb` is called for each slice, which is kept if `cb` returns true.
<b>Signature:</b>
```typescript

View File

@ -32,14 +32,14 @@
| [constructorName(val)](./js-utils.constructorname.md) | Get the constructor name from an instance. |
| [defaultWhen(condition, items)](./js-utils.defaultwhen.md) | Return the first element when <code>condition</code> is true and the second element when <code>condition</code> is false. |
| [defer(ms)](./js-utils.defer.md) | Resolve after a set amount of time. |
| [deferUntil(cb, step, tries)](./js-utils.deferuntil.md) | Reject after a set number of attempts if the given predicate does not return true. |
| [deferValue(ms, val)](./js-utils.defervalue.md) | |
| [deferUntil(cb, step, max)](./js-utils.deferuntil.md) | Resolve if <code>cb</code> returns true within <code>max</code> tries, otherwise reject with a <code>TimeoutError</code>. |
| [deferValue(ms, val)](./js-utils.defervalue.md) | Resolve with the given value, after a set amount of time. |
| [doesExist(val)](./js-utils.doesexist.md) | Check if a variable is some <code>T</code>. |
| [encode(chunks, encoding)](./js-utils.encode.md) | Concatenate then encode a list of buffers. |
| [ensureArray(val)](./js-utils.ensurearray.md) | |
| [ensureArray(val)](./js-utils.ensurearray_1.md) | |
| [entriesOf(map)](./js-utils.entriesof.md) | Get entries of a map-like. |
| [filterZip(cb, l1)](./js-utils.filterzip.md) | |
| [filterZip(cb, l1)](./js-utils.filterzip.md) | Filter and zip some arrays. The <code>cb</code> is called for each slice, which is kept if <code>cb</code> returns true. |
| [filterZip(cb, l1, l2)](./js-utils.filterzip_1.md) | |
| [filterZip(cb, l1, l2, l3)](./js-utils.filterzip_2.md) | |
| [filterZip(cb, l1, l2, l3, l4)](./js-utils.filterzip_3.md) | |
@ -82,7 +82,7 @@
| [spyLogger(spies)](./js-utils.spylogger.md) | Create a spy logger using the provided methods, which returns itself as a child. ensure all methods are present by extending null logger |
| [sum(a, b)](./js-utils.sum.md) | Add numbers. PredicateR2<!-- -->&lt;<!-- -->number, number<!-- -->&gt; |
| [timeout(ms, inner)](./js-utils.timeout.md) | Reject after a set amount of time if the original promise has not yet resolved. |
| [toArray(val)](./js-utils.toarray.md) | |
| [toArray(val)](./js-utils.toarray.md) | Copy an existing array-like or convert a single value to an array. |
| [toArray(val)](./js-utils.toarray_1.md) | |
| [trim(val, max, tail)](./js-utils.trim.md) | |
| [waitFor(cb, step, tries)](./js-utils.waitfor.md) | |

View File

@ -4,6 +4,8 @@
## toArray() function
Copy an existing array-like or convert a single value to an array.
<b>Signature:</b>
```typescript

View File

@ -53,6 +53,9 @@ export function ensureArray<T>(val: Maybe<ReadonlyArray<T>>): ReadonlyArray<T> {
return toArray(val);
}
/**
* Copy an existing array-like or convert a single value to an array.
*/
export function toArray<TVal>(val: Maybe<TVal | Array<TVal>>): Array<TVal>;
export function toArray<TVal>(val: Maybe<TVal | ReadonlyArray<TVal>>): ReadonlyArray<TVal>;
export function toArray<TVal>(val: Maybe<TVal | ReadonlyArray<TVal>>): ReadonlyArray<TVal> {
@ -80,6 +83,9 @@ export function isEmpty(val: Maybe<Array<unknown> | ReadonlyArray<unknown>>): bo
return isNone(val) || lengthOf(val) === 0;
}
/**
* Filter and zip some arrays. The `cb` is called for each slice, which is kept if `cb` returns true.
*/
export function filterZip<T1>(cb: (a: T1) => boolean, l1: Array<T1>): Array<T1>;
export function filterZip<T1, T2>(cb: (a: T1, b: T2) => boolean, l1: Array<T1>, l2: Array<T2>): [Array<T1>, Array<T2>];
export function filterZip<T1, T2, T3>(cb: (a: T1, b: T2) => boolean, l1: Array<T1>, l2: Array<T2>, l3: Array<T3>): [Array<T1>, Array<T2>, Array<T3>];

View File

@ -3,6 +3,7 @@ import { PredicateC0 } from './Predicate';
/**
* Resolve after a set amount of time.
*
* @public
*/
export function defer(ms: number): Promise<void> {
@ -13,6 +14,11 @@ export function defer(ms: number): Promise<void> {
});
}
/**
* Resolve with the given value, after a set amount of time.
*
* @public
*/
export function deferValue<T>(ms: number, val: T): Promise<T> {
return new Promise((res, _rej) => {
setTimeout(() => {
@ -23,6 +29,7 @@ export function deferValue<T>(ms: number, val: T): Promise<T> {
/**
* Reject after a set amount of time if the original promise has not yet resolved.
*
* @public
*/
export function timeout<T>(ms: number, inner: Promise<T>): Promise<T> {
@ -36,13 +43,14 @@ export function timeout<T>(ms: number, inner: Promise<T>): Promise<T> {
}
/**
* Reject after a set number of attempts if the given predicate does not return true.
* Resolve if `cb` returns true within `max` tries, otherwise reject with a `TimeoutError`.
*
* @public
* @throws TimeoutError
*/
export async function deferUntil(cb: PredicateC0, step: number, tries: number): Promise<void> {
export async function deferUntil(cb: PredicateC0, step: number, max: number): Promise<void> {
let count = 0;
while (count < tries) {
while (count < max) {
await defer(step);
if (cb()) {
return;

View File

@ -1,14 +1,17 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { ChildProcessError } from '../../src';
import { ChildStreams, childResult } from '../../src/Child';
import { Maybe, mustExist } from '../../src/Maybe';
const { match, stub } = sinon;
type Closer = (status: number) => Promise<void>;
function createChild(): ChildStreams & { closer: Maybe<Closer> } {
return {
closer /* Maybe<Closer> */ : undefined,
closer /* Maybe<Closer> */: undefined,
on(event: string, fn: Closer) {
if (event === 'close') {
this.closer = fn;
@ -30,7 +33,7 @@ function createChild(): ChildStreams & { closer: Maybe<Closer> } {
describe('child process utils', async () => {
describe('wait for child helper', async () => {
it('should read stdout data', async () => {
it('should read exit status', async () => {
const child = createChild();
const resultPromise = childResult(child);
@ -40,9 +43,6 @@ describe('child process utils', async () => {
expect(result.status).to.equal(0);
});
it('should read stderr data');
it('should resolve on success status');
it('should reject on failure status', async () => {
const child = createChild();
@ -51,5 +51,29 @@ describe('child process utils', async () => {
return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
});
it('should read output', async () => {
const child = createChild();
stub(child.stdout, 'on').withArgs('data', match.func).yields(Buffer.from('hello'));
const resultPromise = childResult(child);
await mustExist(child.closer)(0);
return expect(resultPromise).to.eventually.deep.equal({
stderr: '',
stdout: 'hello',
status: 0,
});
});
it('should reject on error output', async () => {
const child = createChild();
stub(child.stderr, 'on').withArgs('data', match.func).yields(Buffer.from('nope'));
const resultPromise = childResult(child);
await mustExist(child.closer)(0);
return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
});
});
});

View File

@ -43,6 +43,13 @@ describe('map utils', async () => {
const input = {};
expect(makeDict(input)).to.equal(input);
});
over('maps of strings', dictionary(string(), string()), (it) => {
it('should convert map to dict', async (data) => {
const map = new Map(Object.entries(data));
expect(makeDict(map)).to.deep.equal(data);
});
});
});
describe('make map', async () => {

View File

@ -5,6 +5,7 @@ import { over } from 'mocha-foam';
import { NotFoundError } from '../../src/error/NotFoundError';
import { defaultWhen, doesExist, isNone, isSome, mustCoalesce, mustDefault, mustFind, removeNone } from '../../src/Maybe';
import { isNil } from '../../src/Optional';
describe('maybe utils', () => {
describe('remove none', () => {
@ -75,6 +76,14 @@ describe('maybe utils', () => {
});
});
describe('is nil guard', () => {
over('any value', anything(), (it) => {
it('should be an alias for is none', async (x) => {
expect(isNone(x)).to.equal(isNil(x));
});
});
});
describe('is some guard', () => {
over('any value', anything(), (it) => {
it('should be a synonym for does exist', (val) => {