0.0.25 • Published 4 years ago

sambal-jsonld v0.0.25

Weekly downloads
24
License
MIT
Repository
github
Last release
4 years ago

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 []
0.0.25

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.19

4 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago