UNPKG

2.07 kBPlain TextView Raw
1// Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
2// Node module: @loopback/testlab
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6/* eslint-disable @typescript-eslint/unified-signatures */
7
8// Important! Date.prototype.toJSON() returns a string.
9export function toJSON(value: Date): string;
10
11// Important! Functions cannot be encoded in JSON.
12export function toJSON(value: Function): undefined;
13
14// Distinguish arrays from objects (an array is an object too)
15export function toJSON(value: unknown[]): unknown[];
16
17/**
18 * JSON encoding does not preserve properties that are undefined
19 * As a result, deepEqual checks fail because the expected model
20 * value contains these undefined property values, while the actual
21 * result returned by REST API does not.
22 * Use this function to convert a model instance into a data object
23 * as returned by REST API
24 */
25export function toJSON(value: object): object;
26
27// The following overloads are provided for convenience.
28// In practice, they should not be necessary, as they simply return the input
29// value without any modifications.
30
31export function toJSON(value: undefined): undefined;
32export function toJSON(value: null): null;
33export function toJSON(value: number): number;
34export function toJSON(value: boolean): boolean;
35export function toJSON(value: string): string;
36
37// The following overloads are required to allow TypesScript handle
38// commonly used union types.
39
40export function toJSON(value: unknown[] | null): unknown[] | null;
41export function toJSON(value: unknown[] | undefined): unknown[] | undefined;
42export function toJSON(
43 value: unknown[] | null | undefined,
44): unknown[] | null | undefined;
45
46export function toJSON(value: object | null): object | null;
47export function toJSON(value: object | undefined): object | undefined;
48export function toJSON(
49 value: object | null | undefined,
50): object | null | undefined;
51
52export function toJSON<T>(value: T) {
53 return JSON.parse(JSON.stringify({value})).value;
54}