-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use regex to match against entities
@type
property for Roadiz…
… NS and schema.org
- Loading branch information
1 parent
774bc12
commit 87d2ac3
Showing
1 changed file
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
import type { JsonLdObject } from '@roadiz/types' | ||
|
||
/* | ||
* Matches a JsonLdObject with a specific @type name. | ||
* This method supports both NS and non-NS prefixed types. | ||
* | ||
* @see https://regex101.com/r/jPbnxj/1 | ||
*/ | ||
export function isEntityType(entity: JsonLdObject, type: string): boolean { | ||
return entity['@type'] === type | ||
const regex = new RegExp('^(?:NS)?' + type + '$', 'gi') | ||
const matches = entity['@type']?.match(regex) | ||
return matches !== null && matches.length > 0 | ||
} | ||
|
||
/* | ||
* Matches a JsonLdObject with a schema.org namespace @type name. | ||
* This method supports http://schema.org, https://schema.org and none namespaced types. | ||
* | ||
* @see https://regex101.com/r/5way2I/1 | ||
*/ | ||
export function isSchemaOrgType(entity: JsonLdObject, type: string): boolean { | ||
const regex = new RegExp('^(?:https?:\\/\\/schema\\.org\\/)?' + type + '$', 'gi') | ||
const matches = entity['@type']?.match(regex) | ||
return matches !== null && matches.length > 0 | ||
} |