1.1.0 • Published 3 years ago

supatable-mock-data v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Supatable Mock Data

Do you need mock data in tabular form? Supatable Mock Data has got you covered.

It is a thin layer around faker.js which helps you to create realistic fake tabular data.

Features

  • No reliance on cloud services. You can use Supatable Mock Data locally within your projects.
  • Creation of tables with 1 to n rows and 1 to n columns. (n is determined by the capabilities of your computer).
  • Creation of different kinds of realistic fake table cell data (from adjective via iban to zip-code). See faker.js for a complete list of the types of fakeable data.
  • Fluent API which is easy to understand and learn.

Installation

You can add this library to your project as an npm-package. Open the command line, go to the root-folder of your project type the following:

npm install supatable-mock-data --save

Should you use another package manager (e.g. yarn, pnpm), the installation command might be different.

Usage

Basic (using default configuration)

Let's say you need a table with with 1000 rows where each row should have seven columns with the following characteristics:

Column NameRequiredUniqueMax. Different Values
idtruetrue1000
forenametruefalse500
surnametruefalse250
streettruefalse500
zipcodetruefalse500
countrytruefalse50
phoneNumberfalsetrue1000

To create such a mock-table with the given structure above you would do the following:

import { SupatableMockData } from "supatable-mock-data";

const mockTableCreator = new SupatableMockData();

mockTableCreator
    .setRowsTotal(1000)
    .addColumn({
        format: "datatype.uuid",
        isRequired: true,
        isUnique: true,
        name: "id"
    })
    .addColumn({
        differentValuesMax: 500,
        format: "name.firstName",
        isRequired: true,
        name: "forename"
    })
    .addColumn({
        differentValuesMax: 250,
        format: "name.lastName",
        isRequired: true,
        name: "surname"
    })
    .addColumn({
        differentValuesMax: 500,
        format: "address.streetAddress",
        isRequired: true,
        name: "street"
    })
    .addColumn({
        differentValuesMax: 500,
        format: "address.zipCode",
        isRequired: true,
        name: "zipcode"
    })
    .addColumn({
        differentValuesMax: 50,
        format: "address.country",
        isRequired: true,
        name: "country"
    })
    .addColumn({
        format: "phone.phoneNumber",
        isRequired: false,
        isUnique: true,
        name: "phoneNumber"
    });

const mockTable = mockTableCreator.create();

The resulting data is an array with 1000 rows assigned to the mockTable variable. It should look something like this:

[
    {
        "id":          [Unique mock value],
        "forename":    [Mock value],
        "surename":    [Mock value],
        "street":      [Mock value],
        "zipcode":     [Mock value],
        "country":     [Mock value],
        "phoneNumber": [Unique mock value | undefined]
    },

    // ... 999 more rows
]

Set a custom value for empty cells

Let's stick with the basic example above.

Instead of null you want to insert a custom value in empty cells.

import { SupatableMockData } from "supatable-mock-data";

const mockTableCreator = new SupatableMockData({
    emptyCellContent: "N/A"
});

// ... same code as in basic example above

The resulting data is again an array with 1000 rows. However, this time empty cells contain the string N/A.

[
    {
        "id":          [Unique mock value],
        "forename":    [Mock value],
        "surename":    [Mock value],
        "street":      [Mock value],
        "zipcode":     [Mock value],
        "country":     [Mock value],
        "phoneNumber": [Unique mock value | "N/A"]
    },

    // ... 999 more rows
]

Remove cells with empty values

Let's use the basic example above again.

This time, however, you want to omit cells with empty values altogether from the resulting mock table.

Please Note: Empty cell values can occur only when the isRequired flag in the column definition is set to false.

import { SupatableMockData } from "supatable-mock-data";

const mockTableCreator = new SupatableMockData({
    includeEmptyFields: false
});

// ... same code as in basic example above

The resulting data is again an array with 1000 rows. However, this time empty cells are removed.

[
    {
        "id":          [Unique mock value],
        "forename":    [Mock value],
        "surename":    [Mock value],
        "street":      [Mock value],
        "zipcode":     [Mock value],
        "country":     [Mock value],
        "phoneNumber": [Unique mock value]
    },

    // In this row the phoneNumber column has been removed
    // since it is not required and thus has an empty cell value
    {
        "id":          [Unique mock value],
        "forename":    [Mock value],
        "surename":    [Mock value],
        "street":      [Mock value],
        "zipcode":     [Mock value],
        "country":     [Mock value],
    },

    // ... 998 more rows
]

API

SupatableMockData

The only class exported by the supatable-mock-data package.

It can be instantiated with or without a SupatableMockDataConfig object

import { SupatableMockData } from "supatable-mock-data";

const mockTableCreatorWithConfigDefault = new SupatableMockData();

const mockTableCreatorWithConfigCustom = new SupatableMockData({
    emptyCellContent: "N/A"
});

and provides the following public methods:

NameParametersReturnsDescription
addColumnSupatableMockDataColumnDefinitionSupatableMockDataAdds a column definition to the SupatableMockData instance.
clearSupatableMockDataClears all previously added column definitions and rowsTotal from the SupatableMockData instance.
createany[]Creates the actual mock table based upon the previously added column definitions and rowsTotal
setRowsTotalnumberSupatableMockDataAdds the total number of rows of the mock table to the SupatableMockData instance

Furtermore, it has the following accessors to private fields of a SupatableMockData instance:

NameReturnsDescription
columnDefinitionsSupatableMockDataColumnDefinition[]Returns all column definitions passed to SupatableMockData instance via addColumn method.
configSupatableMockDataConfigReturns configuration of SupatableMockData instance
columnsTotalnumberReturns total number of columns
rowsTotalnumberReturns total number of rows

SupatableMockDataColumnDefinition interface

Describes the properties of a column of the mock-table.

export interface SupatableMockDataColumnDefinition {
    /**
     * The maximum of different values per column (can be less, but never more)
     */
    differentValuesMax?: number  

    /**
     * The format of the mock data (from adjective via iban to zip-code).
     * See faker.js for a complete list of the types of fakeable 
     */
    format: SupatableMockFormat, 

    /**
     * Any additional parameters needed to create the fakeable data (depends on the data format)
     */
    formatParams?: any[],

    /**
     * If required, the column will have no empty cells.
     * If not required, the column could have empty cells.
     */
    isRequired?: boolean,

    /**
     * If unique, all the values in the column are unique. 
     * If not unique, values in the column can be repeated.
     */
    isUnique?: boolean,

    /**
     * The name/label of the column
     */
    name: string
}

SupatableMockDataConfig interface

Provides the configuration of a SupatableMockData instance.

export interface SupatableMockDataConfig {
    /**
     * The content which should be inserted into empty cells.
     * 
     * Default: null
     */
    emptyCellContent?: undefined | null | string | number;

    /**
     * If true, empty cells are not included.
     * If false, empty cells are included.
     * 
     * Default: true
     */
    includeEmptyFields?: boolean;
}

License

Supatable Mock Data is licensed under the MIT License.