Skip to content

Commit

Permalink
feat: Use regex to match against entities @type property for Roadiz…
Browse files Browse the repository at this point in the history
… NS and schema.org
  • Loading branch information
ambroisemaupate committed Mar 22, 2024
1 parent 774bc12 commit 87d2ac3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion utils/roadiz/entity.ts
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
}

0 comments on commit 87d2ac3

Please sign in to comment.