Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ahnlabcloudmatelabs/jsonld-helper-ts
when receive any shape of JSON-LD, we can parse and use same interface
https://github.com/ahnlabcloudmatelabs/jsonld-helper-ts
json-ld jsonld typescript
Last synced: 26 days ago
JSON representation
when receive any shape of JSON-LD, we can parse and use same interface
- Host: GitHub
- URL: https://github.com/ahnlabcloudmatelabs/jsonld-helper-ts
- Owner: ahnlabcloudmatelabs
- License: mit
- Created: 2023-10-04T08:40:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-12T15:01:19.000Z (about 1 year ago)
- Last Synced: 2024-12-07T20:51:37.396Z (about 1 month ago)
- Topics: json-ld, jsonld, typescript
- Language: TypeScript
- Homepage:
- Size: 79.1 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![cloudmate logo](https://avatars.githubusercontent.com/u/69299682?s=200&v=4)
# JSON-LD Helper
by Cloudmate
---
![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
## Why use this library?
When receive JSON-LD,
```json
{
"@context": "https://www.w3.org/ns/activitystreams",
"name": "juunini",
"type": "Person",
"id": "juunini"
}
```is equals
```json
{
"@context": "https://www.w3.org/ns/activitystreams",
"as:name": "juunini",
"type": "Person",
"@id": "juunini"
}
```and it also equals
```json
[
{
"https://www.w3.org/ns/activitystreams#name": [
{
"@value": "juunini"
}
],
"@id": "juunini",
"https://www.w3.org/ns/activitystreams#type": [
{
"@value": "Person"
}
]
}
]
```when receive any shape of JSON-LD, we can parse and use same interface.
## Installation
```sh
# npm
npm install @cloudmatelabs/jsonld-helper# yarn
yarn add @cloudmatelabs/jsonld-helper# pnpm
pnpm add @cloudmatelabs/jsonld-helper# bun
bun add @cloudmatelabs/jsonld-helper
```## Usage
```ts
import { JsonLDReader } from '@cloudmatelabs/jsonld-helper'const jsonld = await JsonLDReader.parse({
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers"
}
],
"@id": "https://mastodon.social/users/juunini",
"as:type": "Person",
"url": "https://mastodon.social/@juunini",
"as:image": {
"@type": "Image",
"as:mediaType": "image/png",
"url": "https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.png"
},
"manuallyApprovesFollowers": "true"
})const imageType = jsonld
.read('image')
.read('mediaType')
.stringOrElse('')
// image/pngconst imageURL = jsonld
.read('image')
.read('url')
.stringOrThrow()
// https://files.mastodon.social/accounts/headers/109/408/471/076/954/889/original/f4158a0d06a05763.pngconst id = jsonld.read('id').get()
const id = jsonld.read('@id').get()
// https://mastodon.social/users/juuniniconst type = jsonld.read('type').get()
const type = jsonld.read('@type').get()
// Personconst manuallyApprovesFollowers = jsonld
.read('manuallyApprovesFollowers')
.booleanOrElse(false)
// true
```## Methods and Properties
```ts
JsonLDReader.parse (value: object | object[], options?: Options.Expand): Promise.value [readonly]: unknown
.length [readonly]: number.read (key: string): JsonLDReader
.read (index: number): JsonLDReader.get (): unknown
.getOrThrow (error?: Error): unknown
.getOrElse (defaultValue: unknown): unknown.stringOrThrow (error?: Error): string
.stringOrElse (defaultValue: string): string.numberOrThrow (error?: Error): number
.numberOrElse (defaultValue: number): number.booleanOrThrow (error?: Error): boolean
.booleanOrElse (defaultValue: boolean): boolean
```## License
[MIT](./LICENSE)
But, this library use [jsonld].
[jsonld] is licensed under the [BSD-3-Clause License](https://github.com/digitalbazaar/jsonld.js/blob/main/LICENSE)[jsonld]: https://github.com/digitalbazaar/jsonld.js