@shapediver/sdk.sdtf-primitives
Version:
Extension containing sdTF primitive types
137 lines (111 loc) • 8.86 kB
Markdown
<p align="center">
<a href="https://www.shapediver.com/">
<img src="https://sduse1-assets.shapediver.com/production/assets/img/navbar_logo.png" alt="ShapeDiver" width="392" />
</a>
</p>
# Structured Data Transfer Format - Integration for primitive types
The _Structured Data Transfer Format_ (sdTF) is an API-neutral exchange and storage format for trees of data items as used by parametric 3D modeling software like Grasshopper®.
This open, efficient, and easily extensible data format enables the exchange of complex data structures, independent of software vendors.
See the [sdTF documentation](https://github.com/shapediver/sdTF/tree/development/specification/1.0) for more details.
The aim of this package is to extend the [sdTF-v1](https://www.npmjs.com/package/@shapediver/sdk.sdtf-v1) module to provide processing and typing functionality for primitive data.
It can be used in [Node.js](https://nodejs.org/en/) and modern Browsers, and exposes the respective TypeScript declarations.
## Installation
```shell
npm i @shapediver/sdk.sdtf-v1 @shapediver/sdk.sdtf-primitives
```
This package is a plugin for the [sdTF-v1](https://www.npmjs.com/package/@shapediver/sdk.sdtf-v1) module and cannot be used by itself.
## Usage
This code initializes the SDK and registers the integration for primitive types.
After this step, the integration is fully included in all reading and writing processes of the SDK instance.
```typescript
// index.ts
import { create, SdtfSdk } from "@shapediver/sdk.sdtf-v1"
import { SdtfPrimitiveTypeIntegration } from "@shapediver/sdk.sdtf-primitives"
(async () => {
const sdk: SdtfSdk = await create({
integrations: [ new SdtfPrimitiveTypeIntegration() ]
})
// Use the sdk here
})()
```
## Supported types
The following table lists all [type hints](https://github.com/shapediver/sdTF/tree/development/specification/1.0#typehintname-white_check_mark) that are supported by this integration, their specific _TypeScript types_, and the provided _type guard functions_ that can be used to infer a data content type.
| Type hint name | TypeScript type | Type guard |
|----------------------------------------------------|------------------------------------|---------------------------------------------------------------------------------|
| `"boolean"` or `SdtfPrimitiveTypeHintName.BOOLEAN` | `boolean` | `SdtfPrimitiveTypeGuard.assertBoolean`<br/>`SdtfPrimitiveTypeGuard.isBoolean` |
| `"char"` or `SdtfPrimitiveTypeHintName.CHAR` | `string` | `SdtfPrimitiveTypeGuard.assertString`<br/>`SdtfPrimitiveTypeGuard.isString` |
| `"color"` or `SdtfPrimitiveTypeHintName.COLOR` | `[number, number, number, number]` | `SdtfPrimitiveTypeGuard.assertColor`<br/>`SdtfPrimitiveTypeGuard.isColor` |
| `"data"` or `SdtfPrimitiveTypeHintName.DATA` | `DataView` | `SdtfPrimitiveTypeGuard.assertDataView`<br/>`SdtfPrimitiveTypeGuard.isDataView` |
| `"decimal"` or `SdtfPrimitiveTypeHintName.DECIMAL` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"double"` or `SdtfPrimitiveTypeHintName.DOUBLE` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"guid"` or `SdtfPrimitiveTypeHintName.GUID` | `string` | `SdtfPrimitiveTypeGuard.assertString`<br/>`SdtfPrimitiveTypeGuard.isString` |
| `"image"` or `SdtfPrimitiveTypeHintName.IMAGE` | `DataView` | `SdtfPrimitiveTypeGuard.assertDataView`<br/>`SdtfPrimitiveTypeGuard.isDataView` |
| `"int8"` or `SdtfPrimitiveTypeHintName.INT8` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"int16"` or `SdtfPrimitiveTypeHintName.INT16` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"int32"` or `SdtfPrimitiveTypeHintName.INT32` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"int64"` or `SdtfPrimitiveTypeHintName.INT64` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"single"` or `SdtfPrimitiveTypeHintName.SINGLE` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"string"` or `SdtfPrimitiveTypeHintName.STRING` | `string` | `SdtfPrimitiveTypeGuard.assertString`<br/>`SdtfPrimitiveTypeGuard.isString` |
| `"uint8"` or `SdtfPrimitiveTypeHintName.UINT8` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"uint16"` or `SdtfPrimitiveTypeHintName.UINT16` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"uint32"` or `SdtfPrimitiveTypeHintName.UINT32` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
| `"uint64"` or `SdtfPrimitiveTypeHintName.UINT64` | `number` | `SdtfPrimitiveTypeGuard.assertNumber`<br/>`SdtfPrimitiveTypeGuard.isNumber` |
Additionally, the type guard `SdtfPrimitiveTypeGuard` provides functions to infer type hints by their respective TypeScript type:
__typeGuard.isBooleanType__:\
Returns `true` when the given type hint name is `SdtfPrimitiveTypeHintName.BOOLEAN`.
__typeGuard.isStringType__:\
Returns `true` when the given type hint name is of
* `SdtfPrimitiveTypeHintName.CHAR`
* `SdtfPrimitiveTypeHintName.GUID`
* `SdtfPrimitiveTypeHintName.STRING`
__typeGuard.isNumberType__:\
Returns `true` when the given type hint name is of
* `SdtfPrimitiveTypeHintName.DECIMAL`
* `SdtfPrimitiveTypeHintName.DOUBLE`
* `SdtfPrimitiveTypeHintName.SINGLE`
* `SdtfPrimitiveTypeHintName.INT8`
* `SdtfPrimitiveTypeHintName.INT16`
* `SdtfPrimitiveTypeHintName.INT32`
* `SdtfPrimitiveTypeHintName.INT64`
* `SdtfPrimitiveTypeHintName.UINT8`
* `SdtfPrimitiveTypeHintName.UINT16`
* `SdtfPrimitiveTypeHintName.UINT32`
* `SdtfPrimitiveTypeHintName.UINT64`
__typeGuard.isColorType__:\
Returns `true` when the given type hint name is `SdtfPrimitiveTypeHintName.COLOR`.
__typeGuard.isDataViewType__:\
Returns `true` when the given type hint name is of
* `SdtfPrimitiveTypeHintName.DATA`
* `SdtfPrimitiveTypeHintName.IMAGE`
## Example
This simple example reads a sdTF file and extracts the content of all [data items](https://github.com/shapediver/sdTF/tree/development/specification/1.0#item) which contain a numeric type hint.
Since the extracted content data is of type `unknown`, a type guard function is used to check and infer the content type to `number`.
The numbers are then summed up and shown on the console.\
If any data item has a numeric type hint but does not contain numeric data, a corresponding error is thrown.
```typescript
// index.ts
import { create, SdtfSdk } from "@shapediver/sdk.sdtf-v1"
import { SdtfPrimitiveTypeIntegration, SdtfPrimitiveTypeGuard } from "@shapediver/sdk.sdtf-primitives"
(async () => {
const sdk: SdtfSdk = await create({
integrations: [ new SdtfPrimitiveTypeIntegration() ]
})
// Reads the example file given in the sdTF specification v1:
// https://github.com/shapediver/sdTF/tree/development/specification/1.0#a-complete-example
const asset = await sdk.createParser().readFromFile("./test_data/sdTF_spec_example.sdtf")
// Filter all data items with a numeric type hint (does not check the content!)
const numericData = asset.items.filter(dataItem => SdtfPrimitiveTypeGuard.isNumberType(dataItem.typeHint?.name))
let sum: number = 0
for (const dataItem of numericData) {
const content = await dataItem.getContent() // load data content of type `unknown`
SdtfPrimitiveTypeGuard.assertNumber(content) // infers content to type `number`; throws if not possible
sum += content // add to sum
}
console.log("The sum of all numeric data content is", sum) // Prints out: "The sum of all numeric data content is 479"
})()
```
## Support
If you have questions, please use the [ShapeDiver Help Center](https://help.shapediver.com/).
You can find out more about ShapeDiver [right here](https://www.shapediver.com/).
## Licensing
This project is released under the [MIT License](https://github.com/shapediver/ShapeDiverSdtfTypeScript/blob/master/LICENSE).