# Whistle MDE functions

This page contains a list of the custom functions that have been built by the
Manufacturing Data Engine (MDE) team and available in the Whistle runtime shipped
with MDE.

## Import

The functions in the MDE package must be prefixed with `mde::`.

## Functions

This section shares the list of the custom functions built by the
MDE team.

### lookupByKey

`lookupByKey(bucketName: String, naturalKey: string, bucketVersion: integer )`
returns `JSON || null`

#### Arguments

**bucketName** : `String` - The name of the bucket in which to look for the
metadata instance by natural key.

**naturalKey** : `String` - The natural key of the metadata instance.

**bucketVersion** : `String` - The version of the metadata bucket.

#### Description

Looks for metadata instances with the provided natural key in a bucket version,
and returns the latest instance from the list. If no instance was found, returns
`null`.

For example, given the following two metadata instances:


    [
      {
        "instance_id": "a614e25d-fded-41c3-9a56-3222cd30070d",
        "bucket_number": "1",
        "bucket_name": "device",
        "bucket_version": "1",
        "natural_key": "stamping-f34452",
        "instance": { "deviceName": "metal-machine-device" },
        "created_timestamp": "2023-06-27 20:00:29.603000 UTC"
      },
      {
        "instance_id": "6cfdf894-2fb6-4951-82c6-c4eada587529",
        "bucket_number": "1",
        "bucket_name": "device",
        "bucket_version": "1",
        "natural_key": "stamping-f34452",
        "instance": { "deviceName": "metal-stamping-machine-new-name" },
        "created_timestamp": "2023-06-29 21:00:30.603000 UTC"
      }
    ]

Running:

```python
instance: lookupByKey("device", "stamping-f34452", 1)
```

It will return:

    instance == {
    "instance_id": "6cfdf894-2fb6-4951-82c6-c4eada587529",
    "bucket_number": "1",
    "bucket_name": "device",
    "bucket_version": "1",
    "natural_key": "stamping-f34452",
    "instance": { "deviceName": "metal-stamping-machine-new-name" },
    "created_timestamp": "2023-06-29 21:00:30.603000 UTC"
    }

### removeWhitespace

`removeWhitespace(input: String)` returns `Primitive (String)`

#### Arguments

**input** : `String` - The string from which to remove white spaces

#### Description

Removes white spaces from a string.

An empty string or null input will return null.

For example:

```python
simple = removeWhitespace("This is an example")
```

It will return:

    simple == "Thisisanexample"

### sanitizeTagName

`sanitizeTagName(tagName: String)` returns `Primitive (String)`

#### Arguments

**string** : `String` - The string to sanitize

#### Description

Removes characters not compliant with the
[MDE naming restrictions](https://docs.cloud.google.com/manufacturing-data-engine/docs/concepts/tag.md.txt#naming_restrictions_for_tags).

An empty string or null input will return null.

For example:

```python
simple = sanitizeTagName("This is-an_example!#")
```

It will return:

    simple == "Thisis-an_example"

### sanitizeTagName

`sanitizeTagName(tagName: String, replacementString : String)` returns
`Primitive (String)`

#### Arguments

**tagName** : `String` - The string to sanitize

**replacementString** : `String` - The replacement string to use

#### Description

Removes characters not compliant with the
[MDE naming restrictions](https://docs.cloud.google.com/manufacturing-data-engine/docs/concepts/tag.md.txt#naming_restrictions_for_tags)
and replaces them with the specified `replacementString`.

An empty string or null input will return null.

For example:

```python
simple = sanitizeTagName("This is-an_example!#", "-")
```

It will return:
`none
simple == "This-is-an_example--"\`