Skip to content

Commit

Permalink
fix: optionally dedupe with meta props
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 16, 2022
1 parent 88ec4cd commit 3277488
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/zhead/src/tagDedupeKey.ts
Expand Up @@ -9,7 +9,11 @@ const ArrayMetaProperties = [
'music:album', 'music:musician',
]

export function tagDedupeKey<T extends HeadTag>(tag: T): string | false {
export function tagDedupeKeyWithMetaProps<T extends HeadTag>(tag: T): string | false {
return tagDedupeKey(tag, (val) => ArrayMetaProperties.findIndex(p => val.startsWith(p)) === -1)
}

export function tagDedupeKey<T extends HeadTag>(tag: T, fn?: (key: string) => boolean): string | false {
const { props, tag: tagName } = tag
// must only be a single base so we always dedupe
if (UniqueTags.includes(tagName))
Expand All @@ -30,7 +34,7 @@ export function tagDedupeKey<T extends HeadTag>(tag: T): string | false {
// open graph props can have multiple tags with the same property
if (typeof props[n] !== 'undefined') {
const val = String(props[n])
if (ArrayMetaProperties.findIndex(p => val.startsWith(p)) !== -1)
if (fn && !fn(val))
return false
// for example: meta-name-description
return `${tagName}:${n}:${val}`
Expand Down
4 changes: 2 additions & 2 deletions test/zhead/tagDedupeKey.test.ts
@@ -1,4 +1,4 @@
import { tagDedupeKey } from 'zhead'
import {tagDedupeKey, tagDedupeKeyWithMetaProps} from 'zhead'

describe('tagDedupeKey', () => {
test('title key', async () => {
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('tagDedupeKey', () => {
})

test('og exception', async () => {
const key = tagDedupeKey({
const key = tagDedupeKeyWithMetaProps({
tag: 'meta',
props: {
property: 'og:image',
Expand Down

0 comments on commit 3277488

Please sign in to comment.