Skip to content

perf: remove metadata definition class #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions projects/ngx-meta/src/core/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ export {
provideCore as provideNgxMetaCore,
withDefaults as withNgxMetaDefaults,
} from './src/provide-core'
export * from './src/base-scoped-metadata'
export * from './src/composable-meta-property'
export * from './src/global-metadata'
export * from './src/global-metadata-definition'
export * from './src/global-metadata-image'
export * from './src/global-metadata-key'
export * from './src/make-global-metadata'
export * from './src/make-metadata'
export * from './src/meta-content'
export * from './src/meta-property'
export * from './src/meta.service'
export * from './src/metadata'
export * from './src/metadata-definition'
export * from './src/metadata-provider'
export * from './src/metadata-setter'
export * from './src/metadata-values'
export * from './src/metadata.service'
export * from './src/provide-metadata'
export * from './src/provide-metadata-factory'
export * from './src/scoped-metadata-definition'
export * from './src/string-key-of'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MetadataProvider } from '../metadata-provider'
import { makeGlobalMetadata } from '../make-global-metadata'

export function makeMetadataProvider<T, Id extends string>(
opts: {
id?: Id
spyName?: string
} = {},
) {
const id = opts.id ?? 'dummy'
const metadata: MetadataProvider<T> = {
metadata: makeGlobalMetadata(id),
set: jasmine.createSpy(opts.spyName ?? id),
}
return metadata
}
16 changes: 0 additions & 16 deletions projects/ngx-meta/src/core/src/__tests__/make-metadata.ts

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions projects/ngx-meta/src/core/src/base-global-metadata.ts

This file was deleted.

13 changes: 0 additions & 13 deletions projects/ngx-meta/src/core/src/base-metadata.ts

This file was deleted.

18 changes: 0 additions & 18 deletions projects/ngx-meta/src/core/src/base-scoped-metadata.ts

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions projects/ngx-meta/src/core/src/make-global-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Metadata } from './metadata'
import { makeMetadata } from './make-metadata'

export const makeGlobalMetadata = <Global extends string = string>(
global: Global,
): Metadata => makeMetadata([global])
10 changes: 10 additions & 0 deletions projects/ngx-meta/src/core/src/make-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Metadata } from './metadata'

export const makeMetadata = <Global extends string = string>(
jsonPath: ReadonlyArray<string>,
global?: Global,
): Metadata => ({
id: jsonPath.join('.'),
jsonPath,
global,
})
5 changes: 0 additions & 5 deletions projects/ngx-meta/src/core/src/metadata-definition.ts

This file was deleted.

129 changes: 36 additions & 93 deletions projects/ngx-meta/src/core/src/metadata-json-resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TestBed } from '@angular/core/testing'
import { MetadataJsonResolver } from './metadata-json-resolver'
import { MetadataValues } from './metadata-values'
import { MetadataDefinition } from './metadata-definition'
import { makeScopedMetadataDefinition } from './__tests__/make-scoped-metadata-definition'
import { makeGlobalMetadataDefinition } from './__tests__/make-global-metadata-definition'
import { Metadata } from './metadata'
import { makeMetadata } from './make-metadata'
import { makeGlobalMetadata } from './make-global-metadata'

describe('MetadataJsonResolver', () => {
let sut: MetadataJsonResolver
Expand All @@ -13,41 +13,34 @@ describe('MetadataJsonResolver', () => {
})

describe('get', () => {
const scope = 'scope'
const key = 'key'
const subKey = 'subKey'
const global = 'global'
const name = 'name'
const value = 'value'

function testGlobalMayBeRetrieved(
metadataDefinition: MetadataDefinition,
metadata: Metadata,
values: MetadataValues,
) {
describe('when global is not defined', () => {
it('should return undefined', () => {
expect(sut.get(metadataDefinition, values)).toBeUndefined()
expect(sut.get(metadata, values)).toBeUndefined()
})
})

describe('when global is defined', () => {
const metadataDefinitionWithGlobal = makeScopedMetadataDefinition({
...metadataDefinition,
global,
})
const metadataWithGlobal = makeMetadata(metadata.jsonPath, global)

describe('but global value does not exist', () => {
it('should return undefined', () => {
expect(
sut.get(metadataDefinitionWithGlobal, values),
).toBeUndefined()
expect(sut.get(metadataWithGlobal, values)).toBeUndefined()
})
})
describe('and global value exists', () => {
const valuesWithGlobal = { [global]: value, ...values }

it('should return global value', () => {
expect(
sut.get(metadataDefinitionWithGlobal, valuesWithGlobal),
).toEqual(value)
expect(sut.get(metadataWithGlobal, valuesWithGlobal)).toEqual(value)
})
})
})
Expand All @@ -58,123 +51,73 @@ describe('MetadataJsonResolver', () => {
const values = undefined

it('should return undefined', () => {
expect(
sut.get(makeGlobalMetadataDefinition(), values),
).toBeUndefined()
expect(sut.get(makeGlobalMetadata('dummy'), values)).toBeUndefined()
})
})
describe('like when scope does not exist', () => {
const metadataDefinition = makeScopedMetadataDefinition({
scope,
})
describe('like when key does not exist', () => {
const metadata = makeMetadata([key, subKey])
const values = {}

testGlobalMayBeRetrieved(metadataDefinition, values)
testGlobalMayBeRetrieved(metadata, values)
})

describe('like when scope is defined but property name does not exist', () => {
const metadataDefinition = makeScopedMetadataDefinition({
scope,
name,
})
describe('like when key is defined but sub key does not exist', () => {
const metadata = makeMetadata([key, subKey])
const values = {
[scope]: {},
[key]: {},
}
testGlobalMayBeRetrieved(metadataDefinition, values)
})

describe('like when scope is null', () => {
const metadataDefinition = makeScopedMetadataDefinition({
scope,
})

const values = { [scope]: null }

it('should return null', () => {
expect(sut.get(metadataDefinition, values)).toBeNull()
})
testGlobalMayBeRetrieved(metadata, values)
})

describe('like when scope value is null and there is sub scope', () => {
const metadataDefinition = makeScopedMetadataDefinition({
scope: `${scope}.subScope`,
name,
})

const values = { [scope]: null }
describe('like when key is null', () => {
const metadata = makeMetadata([key, subKey])
const values = { [key]: null }

it('should return null', () => {
expect(sut.get(metadataDefinition, values)).toBeNull()
expect(sut.get(metadata, values)).toBeNull()
})
})

describe('like when scope is not an object', () => {
const metadataDefinition = makeScopedMetadataDefinition({ scope })
describe('like when value in key is not an object', () => {
const metadata = makeMetadata([key, subKey])
const values = {
[scope]: 42,
[key]: 42,
}
testGlobalMayBeRetrieved(metadataDefinition, values)
testGlobalMayBeRetrieved(metadata, values)
})
})

describe('when specific value is defined', () => {
describe('like when scope does not contain sub scopes', () => {
const metadataDefinition = makeScopedMetadataDefinition({
scope,
name,
})
describe('like when there is a key and sub key', () => {
const metadata = makeMetadata([key, subKey])

const values = {
[scope]: {
[name]: value,
[key]: {
[subKey]: value,
},
}

it('should return value using scope and name as keys', () => {
expect(sut.get(metadataDefinition, values)).toEqual(value)
it('should return value using key and sub key as path', () => {
expect(sut.get(metadata, values)).toEqual(value)
})
})

describe('like when scope contains sub scopes', () => {
const subScope = 'subScope'
const metadataDefinition = makeScopedMetadataDefinition({
scope: `${scope}.${subScope}`,
name,
})

const values = {
[scope]: {
[subScope]: {
[name]: value,
},
},
}

it('should return value using sub scope, scope and name as keys', () => {
expect(sut.get(metadataDefinition, values)).toEqual(value)
})
})
describe('and it is and object, and a global object exists too', () => {
const valueObject = { value: 'value', prop: 'value' }
const globalValueObject = {
globalValue: 'globalValue',
prop: 'globalValue',
}
const metadataDefinition = makeScopedMetadataDefinition({
scope,
name,
global,
})

const metadata = makeMetadata([key, subKey], global)
const values = {
[global]: globalValueObject,
[scope]: {
[name]: valueObject,
[key]: {
[subKey]: valueObject,
},
}

it('should merge both objects, with specific value taking priority', () => {
expect(sut.get(metadataDefinition, values)).toEqual({
expect(sut.get(metadata, values)).toEqual({
...globalValueObject,
...valueObject,
})
Expand Down
7 changes: 2 additions & 5 deletions projects/ngx-meta/src/core/src/metadata-json-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { MetadataDefinition } from './metadata-definition'
import { Metadata } from './metadata'
import { MetadataValues } from './metadata-values'
import { MaybeUndefined } from './maybe-undefined'
import { isObject } from './is-object'
import { Injectable } from '@angular/core'

@Injectable({ providedIn: 'root' })
export class MetadataJsonResolver {
get<T>(
definition: MetadataDefinition,
values?: MetadataValues,
): T | undefined {
get<T>(definition: Metadata, values?: MetadataValues): T | undefined {
if (values === undefined) {
return
}
Expand Down
Loading