supatable-mock-data
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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 Name Required Unique Max. Different Values
id true true 1000
forename true false 500
surname true false 250
street true false 500
zipcode true false 500
country true false 50
phoneNumber false true 1000

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:

Name Parameters Returns Description
addColumn SupatableMockDataColumnDefinition SupatableMockData Adds a column definition to
the SupatableMockData instance.
clear SupatableMockData Clears all previously added
column definitions and rowsTotal
from the SupatableMockData instance.
create any[] Creates the actual mock table
based upon the previously added
column definitions and rowsTotal
setRowsTotal number SupatableMockData Adds 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:

Name Returns Description
columnDefinitions SupatableMockDataColumnDefinition[] Returns all column definitions
passed to SupatableMockData instance
via addColumn method.
config SupatableMockDataConfig Returns configuration of SupatableMockData instance
columnsTotal number Returns total number of columns
rowsTotal number Returns 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.

Package Sidebar

Install

npm i supatable-mock-data

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

5.24 MB

Total Files

23

Last publish

Collaborators

  • mbrt-yeah