Socket
Book a DemoInstallSign in
Socket

sambal-jsonld

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sambal-jsonld

Json-ld and schema.org utilities

latest
Source
npmnpm
Version
0.0.25
Version published
Weekly downloads
5
400%
Maintainers
1
Weekly downloads
 
Created
Source

Json-ld utilities

Json-ld utility functions, support schema.org vocabulary.

API

isSchemaType

Check if a jsonld object is of schema.org type. By default, all descendant types will return true.

isSchemaType(jsonld: unknown, type: string, descendants = true): boolean

Examples

const obj = {
    "@id": "/blog1",
    "@type": "BlogPosting",
    ... other properties
}

isSchemaType(obj, "CreativeWork")           // return true
isSchemaType(obj, "creativework")           // return true - case insensitive
isSchemaType(obj, "CreativeWork", false)    // return false

getEntityByType or getEntitiesByType

One really annoying thing about working with schema.org is that you can't really be sure whether a property value is an array or a single value. The schema of an object can be pretty flexibe. For example, a blog can have one author

{
    "@type": "BlogPosting",
    author: {
        "@type": "Person",
        name: "John Smith"
    }
}

or multiple authors

{
    "@type": "BlogPosting",
    author: [
        {
            "@type": "Person",
            name: "John Smith"
        },
        {
            "@type": "Person",
            name: "Jane Doe"
        }
    ]
}

Rather than repeatedly having if statements in your code, Sambal provide getEntityByType and getEntitiesByType to help alleviate this annoyance.

Use getEntityByType if you expect a single value or null if the object type doesn't exist

getEntityByType(jsonld: unknown, type: string, descendants = true)

Examples

const obj = {
    "@type": "BlogPosting",
    author: [
        {
            "@type": "Person",
            name: "John Smith"
        },
        {
            "@type": "Person",
            name: "Jane Doe"
        }
    ]
}

getEntityByType(obj.author, "Person")           // return John Smith object
getEntityByType(obj.author, "person")           // return John Smith object - case insensitive
getEntityByType(obj.author, "Organization")     // return null

Use getEntitiesByType if you expect an array

getEntitiesByType(jsonld: unknown, type: string, descendants = true)

Examples

const obj = {
    "@type": "BlogPosting",
    author: [
        {
            "@type": "Person",
            name: "John Smith"
        },
        {
            "@type": "Person",
            name: "Jane Doe"
        }
    ]
}

getEntitiesByType(obj.author, "Person")           // return array of both authors
getEntitiesByType(obj.author, "person")           // return array of both authors - case insensitive
getEntitiesByType(obj.author, "Organization")     // return []

Keywords

json-ld

FAQs

Package last updated on 05 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts