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

Migrate pretty-format to TypeScript #7809

Merged
merged 5 commits into from Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 20 additions & 12 deletions packages/pretty-format/src/collections.ts
Expand Up @@ -23,9 +23,11 @@ const getKeysOfEnumerableProperties = (object: Object) => {
return keys;
};

// Return entries (for example, of a map)
// with spacing, indentation, and comma
// without surrounding punctuation (for example, braces)
/**
* Return entries (for example, of a map)
* with spacing, indentation, and comma
* without surrounding punctuation (for example, braces)
*/
export function printIteratorEntries(
// Flow 0.51.0: property `@@iterator` of $Iterator not found in Object
// To allow simplistic getRecordIterator in immutable.js
Expand Down Expand Up @@ -82,9 +84,11 @@ export function printIteratorEntries(
return result;
}

// Return values (for example, of a set)
// with spacing, indentation, and comma
// without surrounding punctuation (braces or brackets)
/**
* Return values (for example, of a set)
* with spacing, indentation, and comma
* without surrounding punctuation (braces or brackets)
*/
export function printIteratorValues(
iterator: Iterator<any>,
config: Config,
Expand Down Expand Up @@ -121,9 +125,11 @@ export function printIteratorValues(
return result;
}

// Return items (for example, of an array)
// with spacing, indentation, and comma
// without surrounding punctuation (for example, brackets)
/**
* Return items (for example, of an array)
SimenB marked this conversation as resolved.
Show resolved Hide resolved
* with spacing, indentation, and comma
* without surrounding punctuation (for example, brackets)
**/
export function printListItems(
list: any,
config: Config,
Expand Down Expand Up @@ -157,9 +163,11 @@ export function printListItems(
return result;
}

// Return properties of an object
// with spacing, indentation, and comma
// without surrounding punctuation (for example, braces)
/**
* Return properties of an object
* with spacing, indentation, and comma
* without surrounding punctuation (for example, braces)
*/
export function printObjectProperties(
val: Object,
config: Config,
Expand Down
21 changes: 18 additions & 3 deletions packages/pretty-format/src/index.ts
Expand Up @@ -39,13 +39,15 @@ const errorToString = Error.prototype.toString;
const regExpToString = RegExp.prototype.toString;
const symbolToString = Symbol.prototype.toString;

// Explicitly comparing typeof constructor to function avoids undefined as name
// when mock identity-obj-proxy returns the key as the value for any key.
/**
* Explicitly comparing typeof constructor to function avoids undefined as name
* when mock identity-obj-proxy returns the key as the value for any key.
*/
const getConstructorName = (val: any) =>
SimenB marked this conversation as resolved.
Show resolved Hide resolved
(typeof val.constructor === 'function' && val.constructor.name) || 'Object';

// Is val is equal to global window object? Works even if it does not exist :)
/* global window */
/** Is val is equal to global window object? Works even if it does not exist :) */
const isWindow = (val: any) => typeof window !== 'undefined' && val === window;

const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
Expand Down Expand Up @@ -95,6 +97,10 @@ function printError(val: Error): string {
return '[' + errorToString.call(val) + ']';
}

/**
* The first port of call for printing an object, handles most of the
* data-types in JS.
*/
function printBasicValue(
val: any,
printFunctionName: boolean,
Expand Down Expand Up @@ -167,6 +173,10 @@ function printBasicValue(
return null;
}

/**
* Handles more complex objects ( such as objects with circular references.
* maps and sets etc )
*/
function printComplexValue(
val: any,
config: Config,
Expand Down Expand Up @@ -471,6 +481,11 @@ function createIndent(indent: number): string {
return new Array(indent + 1).join(' ');
}

/**
* Returns a presentation string of your `val` object
* @param val any potential JavaScript object
* @param options Custom settings
*/
function prettyFormat(val: any, options?: OptionsReceived): string {
if (options) {
validateOptions(options);
Expand Down