Skip to content

Commit

Permalink
add ability to extend multiple parents
Browse files Browse the repository at this point in the history
  • Loading branch information
napalmpapalam committed Jul 31, 2023
1 parent a4e4a41 commit f1047f2
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 30 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [1.0.0-rc.3] - 2023-07-31
### Added
- `@distributedlab/reactity` - `extend` ability to extend multiple parents

### Changed
- `@distributedlab/reactity` - `extend` arguments order

## [1.0.0-rc.2] - 2023-07-17
### Fixed
- `@distributedlab/tools` - `Time` `TimeDate` data when the value is in timestamp format
Expand Down Expand Up @@ -304,7 +311,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[old repo]: https://github.com/distributed-lab/web-kit-old

[Unreleased]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.2...HEAD
[Unreleased]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.3...HEAD
[1.0.0-rc.3]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.2...1.0.0-rc.3
[1.0.0-rc.2]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.1...1.0.0-rc.2
[1.0.0-rc.1]: https://github.com/distributed-lab/web-kit/compare/1.0.0-rc.0...1.0.0-rc.1
[1.0.0-rc.0]: https://github.com/distributed-lab/web-kit/compare/0.2.0...1.0.0-rc.0
Expand Down
2 changes: 1 addition & 1 deletion packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/fetcher",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "Fetch API wrapper with the extended functionality and simple interface",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/jac/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/jac",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "A library for constructing JSON-API compliant requests and responses",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/reactivity",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "Implementation of the reactivity connections to propagate changes between objects",
"repository": {
"type": "git",
Expand Down
72 changes: 60 additions & 12 deletions packages/reactivity/src/hooks/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ref } from './ref'
const mockedAge = 10
const changedAge = 20
const mockedName = 'John'
const mockedSex = 'male'

describe('performs extend hook unit test', () => {
test('should basically extend child with parent', () => {
Expand All @@ -20,9 +21,12 @@ describe('performs extend hook unit test', () => {
const name = computed(() => mockedName)

return toRaw(
extend(parent, {
name,
}),
extend(
{
name,
},
parent,
),
)
}

Expand Down Expand Up @@ -53,9 +57,12 @@ describe('performs extend hook unit test', () => {
const name = computed(() => mockedName)

return toRaw(
extend(parent, {
name,
}),
extend(
{
name,
},
parent,
),
)
}

Expand Down Expand Up @@ -84,9 +91,12 @@ describe('performs extend hook unit test', () => {
const name = computed(() => mockedName)

return toRaw(
extend(parent, {
name,
}),
extend(
{
name,
},
parent,
),
)
}

Expand All @@ -111,14 +121,52 @@ describe('performs extend hook unit test', () => {
const age = ref(changedAge)

return toRaw(
extend(parent, {
age,
}),
extend(
{
age,
},
parent,
),
)
}

const child = createChild()

expect(child.age).toBe(changedAge)
})

test('should extend multiple parents', () => {
const createParent1 = () => {
const age = ref(mockedAge)

return toRaw({
age,
})
}

const createParent2 = () => {
const name = ref(mockedName)

return toRaw({
name,
})
}

const createChild = () => {
const parent1 = createParent1()
const parent2 = createParent2()
const sex = ref(mockedSex)

return toRaw(extend({ sex }, parent1, parent2))
}

const child = createChild()

expect(child).toHaveProperty('age')
expect(child).toHaveProperty('name')
expect(child).toHaveProperty('sex')
expect(child.age).toBe(mockedAge)
expect(child.name).toBe(mockedName)
expect(child.sex).toBe(mockedSex)
})
})
15 changes: 9 additions & 6 deletions packages/reactivity/src/hooks/extend.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type { Extended, Raw } from '@/types'
import type { Extended, Parent } from '@/types'

import { computed } from './computed'
import { unref } from './ref'

export const extend = <P extends object, C extends object>(
parent: Raw<P>,
export const extend = <C extends object, P extends Parent[]>(
child: C,
): Extended<P, C> => {
const obj = {} as Extended<P, C>
...parents: [...P]
): Extended<C, P> => {
const obj = {} as Extended<C, P>

parents.forEach(p => {
Object.defineProperties(obj, createDescriptors(p))
})

Object.defineProperties(obj, createDescriptors(parent))
Object.defineProperties(obj, createDescriptors(child))

return obj
Expand Down
16 changes: 10 additions & 6 deletions packages/reactivity/src/types/extend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { Raw } from './raw'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Parent<T = any> = T

export type Extended<P extends object, C extends object> = Omit<
Raw<P>,
keyof C
> &
C
export type UnionToIntersection<U> = (
U extends object ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never

export type Extended<C extends object, P extends readonly Parent[]> = C &
Omit<UnionToIntersection<P[number]>, keyof C>
2 changes: 1 addition & 1 deletion packages/tools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/tools",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "Collection of common utility functions and classes",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/w3p/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@distributedlab/w3p",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "Wrapper for Web3 Providers",
"repository": {
"type": "git",
Expand Down

0 comments on commit f1047f2

Please sign in to comment.