Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add .d.ts for jsutils #3000

Merged
merged 4 commits into from Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/jsutils/devAssert.d.ts
@@ -0,0 +1 @@
export function devAssert(condition: unknown, message: string): void;
8 changes: 8 additions & 0 deletions src/jsutils/didYouMean.d.ts
@@ -0,0 +1,8 @@
/**
* Given [ A, B, C ] return ' Did you mean A, B, or C?'.
*/
export function didYouMean(suggestions: ReadonlyArray<string>): string;
export function didYouMean(
subMessage: string,
suggestions: ReadonlyArray<string>,
): string;
4 changes: 4 additions & 0 deletions src/jsutils/identityFunc.d.ts
@@ -0,0 +1,4 @@
/**
* Returns the first argument it receives.
*/
export function identityFunc<T>(x: T): T;
4 changes: 4 additions & 0 deletions src/jsutils/inspect.d.ts
@@ -0,0 +1,4 @@
/**
* Used to print values in error messages.
*/
export function inspect(value: unknown): string;
1 change: 1 addition & 0 deletions src/jsutils/instanceOf.d.ts
@@ -0,0 +1 @@
export const instanceOf: (value: unknown, constructor: unknown) => boolean;
4 changes: 4 additions & 0 deletions src/jsutils/invariant.d.ts
@@ -0,0 +1,4 @@
export function invariant(
condition: unknown,
message?: string,
): asserts condition;
7 changes: 7 additions & 0 deletions src/jsutils/isAsyncIterable.d.ts
@@ -0,0 +1,7 @@
/**
* Returns true if the provided object implements the AsyncIterator protocol via
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method.
*/
export function isAsyncIterable(
maybeAsyncIterable: unknown,
): maybeAsyncIterable is AsyncIterable<unknown>;
19 changes: 19 additions & 0 deletions src/jsutils/isIteratableObject.d.ts
@@ -0,0 +1,19 @@
/**
* Returns true if the provided object is an Object (i.e. not a string literal)
* and implements the Iterator protocol.
*
* This may be used in place of [Array.isArray()][isArray] to determine if
* an object should be iterated-over e.g. Array, Map, Set, Int8Array,
* TypedArray, etc. but excludes string literals.
*
* @example
*
* isIteratableObject([ 1, 2, 3 ]) // true
* isIteratableObject(new Map()) // true
* isIteratableObject('ABC') // false
* isIteratableObject({ key: 'value' }) // false
* isIteratableObject({ length: 1, 0: 'Alpha' }) // false
*/
export function isIteratableObject(
maybeIteratable: unknown,
): maybeIteratable is Iterable<unknown>;
5 changes: 5 additions & 0 deletions src/jsutils/isObjectLike.d.ts
@@ -0,0 +1,5 @@
/**
* Return true if `value` is object-like. A value is object-like if it's not
* `null` and has a `typeof` result of "object".
*/
export function isObjectLike(value: unknown): value is Record<string, unknown>;
5 changes: 5 additions & 0 deletions src/jsutils/isPromise.d.ts
@@ -0,0 +1,5 @@
/**
* Returns true if the value acts like a Promise, i.e. has a "then" function,
* otherwise returns false.
*/
export function isPromise(value: unknown): value is Promise<unknown>;
28 changes: 28 additions & 0 deletions src/jsutils/keyMap.d.ts
@@ -0,0 +1,28 @@
import type { ObjMap } from './ObjMap';
/**
* Creates a keyed JS object from an array, given a function to produce the keys
* for each value in the array.
*
* This provides a convenient lookup for the array items if the key function
* produces unique results.
*
* const phoneBook = [
* { name: 'Jon', num: '555-1234' },
* { name: 'Jenny', num: '867-5309' }
* ]
*
* // { Jon: { name: 'Jon', num: '555-1234' },
* // Jenny: { name: 'Jenny', num: '867-5309' } }
* const entriesByName = keyMap(
* phoneBook,
* entry => entry.name
* )
*
* // { name: 'Jenny', num: '857-6309' }
* const jennyEntry = entriesByName['Jenny']
*
*/
export function keyMap<T>(
list: ReadonlyArray<T>,
keyFn: (item: T) => string,
): ObjMap<T>;
23 changes: 23 additions & 0 deletions src/jsutils/keyValMap.d.ts
@@ -0,0 +1,23 @@
import type { ObjMap } from './ObjMap';
/**
* Creates a keyed JS object from an array, given a function to produce the keys
* and a function to produce the values from each item in the array.
*
* const phoneBook = [
* { name: 'Jon', num: '555-1234' },
* { name: 'Jenny', num: '867-5309' }
* ]
*
* // { Jon: '555-1234', Jenny: '867-5309' }
* const phonesByName = keyValMap(
* phoneBook,
* entry => entry.name,
* entry => entry.num
* )
*
*/
export function keyValMap<T, V>(
list: ReadonlyArray<T>,
keyFn: (item: T) => string,
valFn: (item: T) => V,
): ObjMap<V>;
9 changes: 9 additions & 0 deletions src/jsutils/mapValue.d.ts
@@ -0,0 +1,9 @@
import type { ObjMap } from './ObjMap';
/**
* Creates an object map with the same keys as `map` and values generated by
* running each value of `map` thru `fn`.
*/
export function mapValue<T, V>(
map: ObjMap<T>,
fn: (value: T, key: string) => V,
): ObjMap<V>;
10 changes: 10 additions & 0 deletions src/jsutils/memoize3.d.ts
@@ -0,0 +1,10 @@
/* eslint-disable @typescript-eslint/ban-types */
/**
* Memoizes the provided three-argument function.
*/
export function memoize3<
A1 extends object,
A2 extends object,
A3 extends object,
R
>(fn: (a1: A1, a2: A2, a3: A3) => R): (a1: A1, a2: A2, a3: A3) => R;
8 changes: 8 additions & 0 deletions src/jsutils/naturalCompare.d.ts
@@ -0,0 +1,8 @@
/**
* Returns a number indicating whether a reference string comes before, or after,
* or is the same as the given string in natural sort order.
*
* See: https://en.wikipedia.org/wiki/Natural_sort_order
*
*/
export function naturalCompare(aStr: string, bStr: string): number;
4 changes: 4 additions & 0 deletions src/jsutils/printPathArray.d.ts
@@ -0,0 +1,4 @@
/**
* Build a string describing the path.
*/
export function printPathArray(path: ReadonlyArray<string | number>): string;
11 changes: 11 additions & 0 deletions src/jsutils/promiseForObject.d.ts
@@ -0,0 +1,11 @@
import type { ObjMap } from './ObjMap';
/**
* This function transforms a JS object `ObjMap<Promise<T>>` into
* a `Promise<ObjMap<T>>`
*
* This is akin to bluebird's `Promise.props`, but implemented only using
* `Promise.all` so it will work with any implementation of ES6 promises.
*/
export function promiseForObject<T>(
object: ObjMap<Promise<T>>,
): Promise<ObjMap<T>>;
13 changes: 13 additions & 0 deletions src/jsutils/promiseReduce.d.ts
@@ -0,0 +1,13 @@
import type { PromiseOrValue } from './PromiseOrValue';
/**
* Similar to Array.prototype.reduce(), however the reducing callback may return
* a Promise, in which case reduction will continue after each promise resolves.
*
* If the callback does not return a Promise, then this function will also not
* return a Promise.
*/
export function promiseReduce<T, U>(
values: ReadonlyArray<T>,
callback: (U: any, T: any) => PromiseOrValue<U>,
initialValue: PromiseOrValue<U>,
): PromiseOrValue<U>;
8 changes: 8 additions & 0 deletions src/jsutils/suggestionList.d.ts
@@ -0,0 +1,8 @@
/**
* Given an invalid input string and a list of valid options, returns a filtered
* list of valid options sorted based on their similarity with the input.
*/
export function suggestionList(
input: string,
options: ReadonlyArray<string>,
): Array<string>;
9 changes: 9 additions & 0 deletions src/jsutils/toObjMap.d.ts
@@ -0,0 +1,9 @@
import type {
ObjMap,
ObjMapLike,
ReadOnlyObjMap,
ReadOnlyObjMapLike,
} from './ObjMap';

export function toObjMap<T>(obj: ObjMapLike<T>): ObjMap<T>;
export function toObjMap<T>(obj: ReadOnlyObjMapLike<T>): ReadOnlyObjMap<T>;